Skip to content

Commit

Permalink
add objectintmapping unit test
Browse files Browse the repository at this point in the history
Change-Id: Ib653623d5cc79696f58ec50ccadca8ac7039c2f1
  • Loading branch information
zhoney committed May 11, 2021
1 parent 29046da commit 3a51a42
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package com.baidu.hugegraph.util.collection.mapping;

import com.baidu.hugegraph.perf.PerfUtil;
import com.baidu.hugegraph.perf.PerfUtil.Watched;

public class ConcurrentObjectIntMapping<V> implements ObjectIntMapping<V> {

Expand All @@ -29,14 +29,18 @@ public ConcurrentObjectIntMapping() {
this.singleObjectIntMapping = new SingleObjectIntMapping<>();
}

@PerfUtil.Watched
@Watched
@SuppressWarnings("unchecked")
public synchronized int object2Code(Object object) {
return this.singleObjectIntMapping.object2Code(object);
}

@PerfUtil.Watched
@Watched
public synchronized Object code2Object(int code) {
return this.singleObjectIntMapping.code2Object(code);
}

public synchronized void clear() {
this.singleObjectIntMapping.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public interface ObjectIntMapping<V> {

public int object2Code(Object object);
public Object code2Object(int code);
public void clear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.perf.PerfUtil;
import com.baidu.hugegraph.perf.PerfUtil.Watched;

public class SingleObjectIntMapping<V> implements ObjectIntMapping<V> {

Expand All @@ -34,7 +34,7 @@ public SingleObjectIntMapping() {
this.int2IdMap = new IntObjectHashMap<>();
}

@PerfUtil.Watched
@Watched
@SuppressWarnings("unchecked")
public synchronized int object2Code(Object object) {
int key = object.hashCode();
Expand All @@ -55,8 +55,12 @@ public synchronized int object2Code(Object object) {
throw new HugeException("Failed to get code for id: %s", object);
}

@PerfUtil.Watched
@Watched
public synchronized Object code2Object(int code) {
return this.int2IdMap.get(code);
}

public void clear() {
this.int2IdMap.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.unit.core;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.lang.RandomStringUtils;
import org.junit.After;
import org.junit.Test;

import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.id.IdGenerator;
import com.baidu.hugegraph.testutil.Assert;
import com.baidu.hugegraph.util.collection.mapping.MappingFactory;
import com.baidu.hugegraph.util.collection.mapping.ObjectIntMapping;

public class ObjectIntMappingTest {

private static ObjectIntMapping<Id> mapping =
MappingFactory.newObjectIntMapping();

@After
public void clear() {
mapping.clear();
}

@Test
public void testNumberIdMapping() {
Set<Integer> codes = new LinkedHashSet<>();
for (int i = 0; i < 1000000; i++) {
codes.add(mapping.object2Code(IdGenerator.of(i)));
}
Assert.assertEquals(1000000, codes.size());

int j = 0;
for (Integer code : codes) {
Assert.assertEquals(IdGenerator.of(j++), mapping.code2Object(code));
}
}

@Test
public void testStringIdMapping() {
Set<String> strings = new LinkedHashSet<>();
Set<Integer> codes = new LinkedHashSet<>();
for (int i = 0; i < 1000000; i++) {
String string = RandomStringUtils.randomAlphanumeric(10);
strings.add(string);
codes.add(mapping.object2Code(IdGenerator.of(string)));
}
Assert.assertEquals(strings.size(), codes.size());

Iterator<String> strIter = strings.iterator();
Iterator<Integer> codeIter = codes.iterator();
while (strIter.hasNext() && codeIter.hasNext()) {
Assert.assertEquals(IdGenerator.of(strIter.next()),
mapping.code2Object(codeIter.next()));
}

Assert.assertFalse(strIter.hasNext());
Assert.assertFalse(codeIter.hasNext());
}

@Test
public void testUUIDIdMapping() {
Set<UUID> uuids = new LinkedHashSet<>();
Set<Integer> codes = new LinkedHashSet<>();
for (int i = 0; i < 1000000; i++) {
UUID uuid = UUID.randomUUID();
uuids.add(uuid);
codes.add(mapping.object2Code(IdGenerator.of(uuid)));
}
Assert.assertEquals(uuids.size(), codes.size());

Iterator<UUID> uuidIter = uuids.iterator();
Iterator<Integer> codeIter = codes.iterator();
while (uuidIter.hasNext() && codeIter.hasNext()) {
Assert.assertEquals(IdGenerator.of(uuidIter.next()),
mapping.code2Object(codeIter.next()));
}

Assert.assertFalse(uuidIter.hasNext());
Assert.assertFalse(codeIter.hasNext());
}

@Test
public void testMixedIdMapping() {
Set<Integer> codes = new LinkedHashSet<>();
Set<Object> objects = new LinkedHashSet<>();
Object object;

for (int i = 0; i < 1000000; i++) {
object = IdGenerator.of(i);
objects.add(object);
codes.add(mapping.object2Code(object));
}

for (int i = 0; i < 1000000; i++) {
String string = RandomStringUtils.randomAlphanumeric(10);
object = IdGenerator.of(string);
objects.add(object);
codes.add(mapping.object2Code(object));
}

for (int i = 0; i < 1000000; i++) {
UUID uuid = UUID.randomUUID();
object = IdGenerator.of(uuid);
objects.add(object);
codes.add(mapping.object2Code(object));
}
Assert.assertEquals(objects.size(), codes.size());

Iterator<Object> objectIter = objects.iterator();
Iterator<Integer> codeIter = codes.iterator();
while (objectIter.hasNext() && codeIter.hasNext()) {
Assert.assertEquals(objectIter.next(),
mapping.code2Object(codeIter.next()));
}

Assert.assertFalse(objectIter.hasNext());
Assert.assertFalse(codeIter.hasNext());
}
}

0 comments on commit 3a51a42

Please sign in to comment.