From 9d5439f3b2ce44bd3516a97c3101e9997634a714 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Fri, 16 Apr 2021 20:33:33 +0800 Subject: [PATCH] adapt primitive collection for kout/kneighbor Change-Id: Iffad898d0ff04f8b8937eb774352c853bae87aef --- .../api/traversers/AllShortestPathsAPI.java | 3 +- .../api/traversers/CrosspointsAPI.java | 3 +- .../hugegraph/api/traversers/PathsAPI.java | 3 +- .../hugegraph/api/traversers/RaysAPI.java | 2 +- .../hugegraph/api/traversers/RingsAPI.java | 3 +- .../baidu/hugegraph/structure/HugeEdge.java | 4 +- .../baidu/hugegraph/structure/HugeVertex.java | 5 --- .../traversal/algorithm/HugeTraverser.java | 30 +++++---------- .../traversal/algorithm/KoutTraverser.java | 12 +++--- .../algorithm/records/IntArrayRecord.java | 4 +- .../algorithm/records/IntIntRecord.java | 2 +- .../algorithm/records/IntSetRecord.java | 2 +- .../algorithm/records/MultiPathsRecords.java | 4 +- .../algorithm/records/RecordType.java | 4 +- .../records/ShortestPathRecords.java | 1 + .../type/define/CollectionImplType.java | 2 + .../util/collection/CollectionFactory.java | 37 +++++++++++++++++++ .../util/collection/ObjectIntMapping.java | 10 +++-- .../hugegraph/unit/util/JsonUtilTest.java | 6 +-- 19 files changed, 80 insertions(+), 57 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AllShortestPathsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AllShortestPathsAPI.java index f05424d348..9407f53d54 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AllShortestPathsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AllShortestPathsAPI.java @@ -89,7 +89,6 @@ public String get(@Context GraphManager manager, HugeTraverser.PathSet paths = traverser.allShortestPaths( sourceId, targetId, dir, edgeLabels, depth, degree, skipDegree, capacity); - return manager.serializer(g).writePaths("paths", paths.paths(), - false); + return manager.serializer(g).writePaths("paths", paths, false); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CrosspointsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CrosspointsAPI.java index f7fa516f5f..cc4689f615 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CrosspointsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CrosspointsAPI.java @@ -84,7 +84,6 @@ public String get(@Context GraphManager manager, HugeTraverser.PathSet paths = traverser.paths(sourceId, dir, targetId, dir, edgeLabel, depth, degree, capacity, limit); - return manager.serializer(g).writePaths("crosspoints", - paths.paths(), true); + return manager.serializer(g).writePaths("crosspoints", paths, true); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PathsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PathsAPI.java index a4bf4e241a..5e5efc6571 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PathsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PathsAPI.java @@ -97,8 +97,7 @@ public String get(@Context GraphManager manager, dir.opposite(), edgeLabel, depth, degree, capacity, limit); - return manager.serializer(g).writePaths("paths", paths.paths(), - false); + return manager.serializer(g).writePaths("paths", paths, false); } @POST diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/RaysAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/RaysAPI.java index a3f163f99e..7b9a2a570f 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/RaysAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/RaysAPI.java @@ -83,6 +83,6 @@ public String get(@Context GraphManager manager, HugeTraverser.PathSet paths = traverser.rays(source, dir, edgeLabel, depth, degree, capacity, limit); - return manager.serializer(g).writePaths("rays", paths.paths(), false); + return manager.serializer(g).writePaths("rays", paths, false); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/RingsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/RingsAPI.java index 23e3bfb654..3960a627be 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/RingsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/RingsAPI.java @@ -86,7 +86,6 @@ public String get(@Context GraphManager manager, HugeTraverser.PathSet paths = traverser.rings(source, dir, edgeLabel, depth, sourceInRing, degree, capacity, limit); - return manager.serializer(g).writePaths("rings", paths.paths(), - false); + return manager.serializer(g).writePaths("rings", paths, false); } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeEdge.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeEdge.java index f579fc9b13..0a90b14502 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeEdge.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeEdge.java @@ -501,10 +501,10 @@ public static HugeEdge constructEdge(HugeVertex ownerVertex, VertexLabel otherVertexLabel; if (isOutEdge) { - ownerVertex.correctVertexLabel(srcLabel); +// ownerVertex.correctVertexLabel(srcLabel); otherVertexLabel = tgtLabel; } else { - ownerVertex.correctVertexLabel(tgtLabel); +// ownerVertex.correctVertexLabel(tgtLabel); otherVertexLabel = srcLabel; } HugeVertex otherVertex = new HugeVertex(graph, otherVertexId, diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java index 60692f41d1..c707983d8d 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java @@ -35,7 +35,6 @@ import org.apache.tinkerpop.gremlin.structure.VertexProperty; import org.apache.tinkerpop.gremlin.structure.util.ElementHelper; import org.apache.tinkerpop.gremlin.structure.util.StringFactory; -import org.eclipse.collections.impl.list.mutable.FastList; import com.baidu.hugegraph.HugeException; import com.baidu.hugegraph.HugeGraph; @@ -659,10 +658,6 @@ public void resetEdges() { this.edges = newSet(); } - public void removeEdge(HugeEdge edge) { - this.edges.remove(edge); - } - public void addEdge(HugeEdge edge) { if (this.edges == EMPTY_LIST) { this.edges = newSet(); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java index 307a709a64..69e1aff15b 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java @@ -70,7 +70,6 @@ public class HugeTraverser { private HugeGraph graph; private static CollectionFactory collectionFactory; - protected ObjectIntMapping idMapping; public static final String DEFAULT_CAPACITY = "10000000"; public static final String DEFAULT_ELEMENTS_LIMIT = "10000000"; @@ -91,7 +90,9 @@ public class HugeTraverser { public HugeTraverser(HugeGraph graph) { this.graph = graph; - collectionFactory = new CollectionFactory(this.collectionImplType()); + if (collectionFactory == null) { + collectionFactory = new CollectionFactory(this.collectionImplType()); + } } public HugeGraph graph() { @@ -491,16 +492,6 @@ protected static List joinPath(Node prev, Node back, boolean ring) { return path; } - @Watched - protected Id id(int code) { - return (Id) this.idMapping.code2Object(code); - } - - @Watched - protected int code(Id id) { - return this.idMapping.object2Code(id); - } - public static class Node { private Id id; @@ -700,10 +691,7 @@ public void clear() { this.paths.clear(); } - public boolean addAll(PathSet paths) { - return this.paths.addAll(paths.paths); - } - + @Override public boolean isEmpty() { return this.paths.isEmpty(); } @@ -713,14 +701,12 @@ public boolean contains(Object o) { return this.paths.contains(o); } + @Override public int size() { return this.paths.size(); } - public Set paths() { - return this.paths; - } - + @Override public Iterator iterator() { return this.paths.iterator(); } @@ -735,6 +721,10 @@ public T[] toArray(T[] a) { return this.paths.toArray(a); } + public boolean addAll(PathSet paths) { + return this.paths.addAll(paths.paths); + } + public Set vertices() { Set vertices = newIdSet(); for (Path path : this.paths) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KoutTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KoutTraverser.java index 2c3845e28c..dc24266536 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KoutTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KoutTraverser.java @@ -92,9 +92,9 @@ public Set kout(Id sourceV, Directions dir, String label, return latest; } - public Set customizedKout(Id source, EdgeStep step, int maxDepth, - boolean nearest, long capacity, - long limit) { + public Set customizedKout(Id source, EdgeStep step, + int maxDepth, boolean nearest, + long capacity, long limit) { E.checkNotNull(source, "source vertex id"); this.checkVertexExist(source, "source vertex"); checkPositive(maxDepth, "k-out max_depth"); @@ -114,9 +114,9 @@ public Set customizedKout(Id source, EdgeStep step, int maxDepth, return results; } - public Set customizedKout(Id source, EdgeStep step, int maxDepth, - boolean nearest, long capacity, - boolean single) { + public Set customizedKout(Id source, EdgeStep step, + int maxDepth, boolean nearest, + long capacity, boolean single) { Set latest = newSet(single); Set all = newSet(single); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/IntArrayRecord.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/IntArrayRecord.java index a67e4d344b..2365f0f786 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/IntArrayRecord.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/IntArrayRecord.java @@ -25,7 +25,7 @@ public class IntArrayRecord implements Record { - private Int2IntsMap layer; + private final Int2IntsMap layer; public IntArrayRecord() { this.layer = new Int2IntsMap(); @@ -58,7 +58,7 @@ public int size() { public class IntArrayIterator implements IntIterator { - private int[] array; + private final int[] array; private int index; public IntArrayIterator(int[] array) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/IntIntRecord.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/IntIntRecord.java index 5f9c06951c..fc12a5a395 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/IntIntRecord.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/IntIntRecord.java @@ -24,7 +24,7 @@ public class IntIntRecord implements Record { - private IntIntHashMap layer; + private final IntIntHashMap layer; public IntIntRecord() { this.layer = new IntIntHashMap(); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/IntSetRecord.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/IntSetRecord.java index 9c0cc561a9..c4c025df28 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/IntSetRecord.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/IntSetRecord.java @@ -25,7 +25,7 @@ public class IntSetRecord implements Record { - private IntObjectHashMap layer; + private final IntObjectHashMap layer; public IntSetRecord() { this.layer = new IntObjectHashMap<>(); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/MultiPathsRecords.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/MultiPathsRecords.java index 6763a80181..6564db944e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/MultiPathsRecords.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/MultiPathsRecords.java @@ -37,7 +37,7 @@ public class MultiPathsRecords implements Records { - private final ObjectIntMapping idMapping; + private final ObjectIntMapping idMapping; private final RecordType type; protected final Stack sourceRecords; @@ -50,7 +50,7 @@ public class MultiPathsRecords implements Records { private int accessed; public MultiPathsRecords(Id sourceV, Id targetV, RecordType type) { - this.idMapping = new ObjectIntMapping(); + this.idMapping = new ObjectIntMapping<>(); this.type = type; int sourceCode = this.code(sourceV); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/RecordType.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/RecordType.java index 9a1adbd2f1..1f08cda9d5 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/RecordType.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/RecordType.java @@ -21,7 +21,7 @@ import com.baidu.hugegraph.type.define.SerialEnum; -public enum RecordType implements SerialEnum { +public enum RecordType implements SerialEnum { // Java Collection Framework ARRAY(1, "array"), @@ -62,4 +62,4 @@ public static RecordType fromCode(byte code) { throw new AssertionError("Unsupported record code: " + code); } } -} \ No newline at end of file +} diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/ShortestPathRecords.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/ShortestPathRecords.java index 06a51167a7..eb5be3dfe8 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/ShortestPathRecords.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/ShortestPathRecords.java @@ -42,6 +42,7 @@ public ShortestPathRecords(Id sourceV, Id targetV) { this.accessedVertices = new IntHashSet(); this.accessedVertices.add(this.code(sourceV)); this.accessedVertices.add(this.code(targetV)); + this.pathFound = false; } @Override diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/CollectionImplType.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/CollectionImplType.java index eb23855cd8..db2bcfc27e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/CollectionImplType.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/CollectionImplType.java @@ -57,6 +57,8 @@ public static CollectionImplType fromCode(byte code) { return JCF; case 2: return EC; + case 3: + return FU; default: throw new AssertionError( "Unsupported collection code: " + code); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/CollectionFactory.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/CollectionFactory.java index f0f89707b1..54b928af69 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/CollectionFactory.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/CollectionFactory.java @@ -36,6 +36,7 @@ import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.type.define.CollectionImplType; +import com.baidu.hugegraph.util.E; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -215,6 +216,42 @@ public static MutableIntObjectMap newIntObjectMap( return new IntObjectHashMap<>(map); } + public static MutableIntObjectMap newIntObjectMap( + int key1, V value1) { + return IntObjectHashMap.newWithKeysValues(key1, value1); + } + + public static MutableIntObjectMap newIntObjectMap( + int key1, V value1, + int key2, V value2) { + return IntObjectHashMap.newWithKeysValues(key1, value1, key2, value2); + } + + public static MutableIntObjectMap newIntObjectMap( + int key1, V value1, + int key2, V value2, + int key3, V value3) { + return IntObjectHashMap.newWithKeysValues(key1, value1, key2, value2, + key3, value3); + } + + @SuppressWarnings("unchecked") + public static MutableIntObjectMap newIntObjectMap( + int key1, V value1, + int key2, V value2, + int key3, V value3, + Object... objects) { + IntObjectHashMap map = IntObjectHashMap.newWithKeysValues( + key1, value1, key2, value2, key3, value3); + E.checkArgument(objects.length % 2 == 0, + "Must provide even arguments for " + + "CollectionFactory.newIntObjectMap"); + for (int i = 0; i < objects.length; i+=2) { + map.put((int) objects[i], (V) objects[i + 1]); + } + return map; + } + public Set newIdSet() { return newIdSet(this.type); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/ObjectIntMapping.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/ObjectIntMapping.java index d09431d12a..733b11baa4 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/ObjectIntMapping.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/ObjectIntMapping.java @@ -25,23 +25,25 @@ import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.perf.PerfUtil.Watched; -public final class ObjectIntMapping { +public final class ObjectIntMapping { private static final int MAGIC = 1 << 16; - private final IntObjectHashMap int2IdMap; + private final IntObjectHashMap int2IdMap; public ObjectIntMapping() { - this.int2IdMap = new IntObjectHashMap(1000000); + this.int2IdMap = new IntObjectHashMap<>(1000000); } @Watched + @SuppressWarnings("unchecked") public int object2Code(Object object) { int key = object.hashCode(); + // TODO: improve hash algorithm for (int i = 1; i > 0; i <<= 1) { for (int j = 0; i >= MAGIC && j < 10; j++) { Id existed = (Id) this.int2IdMap.get(key); if (existed == null) { - this.int2IdMap.put(key, object); + this.int2IdMap.put(key, (V) object); return key; } if (existed.equals(object)) { diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/util/JsonUtilTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/util/JsonUtilTest.java index a42d55082b..3214aa7909 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/util/JsonUtilTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/util/JsonUtilTest.java @@ -25,7 +25,6 @@ import org.apache.tinkerpop.shaded.jackson.core.type.TypeReference; import org.eclipse.collections.api.map.primitive.MutableIntObjectMap; -import org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap; import org.junit.Test; import org.mockito.Mockito; @@ -54,6 +53,7 @@ import com.baidu.hugegraph.unit.BaseUnitTest; import com.baidu.hugegraph.unit.FakeObjects; import com.baidu.hugegraph.util.JsonUtil; +import com.baidu.hugegraph.util.collection.CollectionFactory; import com.google.common.collect.ImmutableList; public class JsonUtilTest extends BaseUnitTest { @@ -235,7 +235,7 @@ public void testSerializeVertexWithNumberId() { HugeVertex vertex = new HugeVertex(fakeObject.graph(), id, vl); MutableIntObjectMap> properties = - IntObjectHashMap.newWithKeysValues( + CollectionFactory.newIntObjectMap( (int) name.id().asLong(), new HugeVertexProperty<>(vertex, name, "marko"), (int) age.id().asLong(), @@ -287,7 +287,7 @@ public void testSerializeEdge() { Whitebox.setInternalState(edge, "targetVertex", target); MutableIntObjectMap> properties = - IntObjectHashMap.newWithKeysValues( + CollectionFactory.newIntObjectMap( (int) date.id().asLong(), new HugeEdgeProperty<>(edge, date, Utils.date("2019-03-12")), (int) weight.id().asLong(),