From 3a51a427978155aebeb2bb0d3e7bfb88defb2e9a Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Tue, 11 May 2021 22:21:49 +0800 Subject: [PATCH] add objectintmapping unit test Change-Id: Ib653623d5cc79696f58ec50ccadca8ac7039c2f1 --- .../mapping/ConcurrentObjectIntMapping.java | 10 +- .../collection/mapping/ObjectIntMapping.java | 1 + .../mapping/SingleObjectIntMapping.java | 10 +- .../unit/core/ObjectIntMappingTest.java | 142 ++++++++++++++++++ 4 files changed, 157 insertions(+), 6 deletions(-) create mode 100644 hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/ObjectIntMappingTest.java diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/mapping/ConcurrentObjectIntMapping.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/mapping/ConcurrentObjectIntMapping.java index 92d0837d50..fb2b202b12 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/mapping/ConcurrentObjectIntMapping.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/mapping/ConcurrentObjectIntMapping.java @@ -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 implements ObjectIntMapping { @@ -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(); + } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/mapping/ObjectIntMapping.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/mapping/ObjectIntMapping.java index 2bf66a4673..45d63b4747 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/mapping/ObjectIntMapping.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/mapping/ObjectIntMapping.java @@ -23,4 +23,5 @@ public interface ObjectIntMapping { public int object2Code(Object object); public Object code2Object(int code); + public void clear(); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/mapping/SingleObjectIntMapping.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/mapping/SingleObjectIntMapping.java index 9bea0178e9..281a0ed736 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/mapping/SingleObjectIntMapping.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/mapping/SingleObjectIntMapping.java @@ -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 implements ObjectIntMapping { @@ -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(); @@ -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(); + } } diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/ObjectIntMappingTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/ObjectIntMappingTest.java new file mode 100644 index 0000000000..1f20e7e262 --- /dev/null +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/ObjectIntMappingTest.java @@ -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 mapping = + MappingFactory.newObjectIntMapping(); + + @After + public void clear() { + mapping.clear(); + } + + @Test + public void testNumberIdMapping() { + Set 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 strings = new LinkedHashSet<>(); + Set 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 strIter = strings.iterator(); + Iterator 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 uuids = new LinkedHashSet<>(); + Set 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 uuidIter = uuids.iterator(); + Iterator 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 codes = new LinkedHashSet<>(); + Set 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 objectIter = objects.iterator(); + Iterator codeIter = codes.iterator(); + while (objectIter.hasNext() && codeIter.hasNext()) { + Assert.assertEquals(objectIter.next(), + mapping.code2Object(codeIter.next())); + } + + Assert.assertFalse(objectIter.hasNext()); + Assert.assertFalse(codeIter.hasNext()); + } +}