From be0eaeae60ca59634e2e5e1cedc7619aadf40f1a Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Wed, 12 May 2021 15:07:53 +0800 Subject: [PATCH] improve Change-Id: I742ce92cea71f556d0c7b5a52229e46006f759e7 --- .../traversal/algorithm/KneighborTraverser.java | 5 +++-- .../traversal/algorithm/KoutTraverser.java | 4 ++-- .../traversal/algorithm/PathsTraverser.java | 6 +++--- .../algorithm/records/AbstractRecords.java | 6 ------ .../records/DoubleWayMultiPathsRecords.java | 6 +++--- .../algorithm/records/KneighborRecords.java | 6 +++--- .../traversal/algorithm/records/KoutRecords.java | 7 +++---- .../algorithm/records/ShortestPathRecords.java | 2 +- .../records/SingleWayMultiPathsRecords.java | 13 +++++-------- .../mapping/ConcurrentObjectIntMapping.java | 1 + .../collection/mapping/SingleObjectIntMapping.java | 1 + .../com/baidu/hugegraph/unit/UnitTestSuite.java | 2 ++ 12 files changed, 27 insertions(+), 32 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KneighborTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KneighborTraverser.java index 307ef3fb5e..6846ae8f7a 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KneighborTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KneighborTraverser.java @@ -83,8 +83,9 @@ public KneighborRecords customizedKneighbor(Id source, EdgeStep step, boolean concurrent = maxDepth >= this.concurrentDepth() && step.direction() == Directions.BOTH; - KneighborRecords records = new KneighborRecords(source, RecordType.INT, - true, concurrent); + KneighborRecords records = new KneighborRecords(RecordType.INT, + concurrent, + source, true); Consumer consumer = v -> { if (this.stop) { 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 6019d4942c..edc0399de0 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 @@ -113,8 +113,8 @@ public KoutRecords customizedKout(Id source, EdgeStep step, this.depth = maxDepth; boolean concurrent = maxDepth >= this.concurrentDepth() && step.direction() == Directions.BOTH; - KoutRecords records = new KoutRecords(source, RecordType.INT, - nearest, concurrent); + KoutRecords records = new KoutRecords(RecordType.INT, concurrent, + source, nearest); Consumer consumer = v -> { if (this.stop) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java index 4f5e839b0b..aa07ba2d5c 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java @@ -31,7 +31,6 @@ import com.baidu.hugegraph.traversal.algorithm.records.record.RecordType; import com.baidu.hugegraph.type.define.Directions; import com.baidu.hugegraph.util.E; -import com.google.common.collect.ImmutableList; public class PathsTraverser extends HugeTraverser { @@ -94,8 +93,9 @@ private class Traverser { public Traverser(Id sourceV, Id targetV, Id label, long degree, long capacity, long limit) { - this.record = new DoubleWayMultiPathsRecords(sourceV, targetV, - RecordType.ARRAY); + this.record = new DoubleWayMultiPathsRecords(RecordType.ARRAY, + false, + sourceV, targetV); this.label = label; this.degree = degree; this.capacity = capacity; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/AbstractRecords.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/AbstractRecords.java index fb92e854f9..9a5431b09a 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/AbstractRecords.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/AbstractRecords.java @@ -39,12 +39,6 @@ public AbstractRecords(RecordType type, boolean concurrent) { this.idMapping = MappingFactory.newObjectIntMapping(this.concurrent); } - public AbstractRecords(RecordType type) { - this.type = type; - this.concurrent = false; - this.idMapping = MappingFactory.newObjectIntMapping(); - } - @Watched protected int code(Id id) { return this.idMapping.object2Code(id); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/DoubleWayMultiPathsRecords.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/DoubleWayMultiPathsRecords.java index 433e7338cc..a6bc4b7a73 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/DoubleWayMultiPathsRecords.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/DoubleWayMultiPathsRecords.java @@ -47,9 +47,9 @@ public class DoubleWayMultiPathsRecords extends AbstractRecords { protected boolean forward; private int accessed; - public DoubleWayMultiPathsRecords(Id sourceV, Id targetV, RecordType type) { - super(type); - + public DoubleWayMultiPathsRecords(RecordType type, boolean concurrent, + Id sourceV, Id targetV) { + super(type, concurrent); int sourceCode = this.code(sourceV); int targetCode = this.code(targetV); Record firstSourceRecord = this.newRecord(); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KneighborRecords.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KneighborRecords.java index 723d000e83..5302719b09 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KneighborRecords.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KneighborRecords.java @@ -36,9 +36,9 @@ public class KneighborRecords extends SingleWayMultiPathsRecords { private final Id source; - public KneighborRecords(Id source, RecordType type, - boolean nearest, boolean concurrent) { - super(source, type, nearest, concurrent); + public KneighborRecords(RecordType type, boolean concurrent, + Id source, boolean nearest) { + super(type, concurrent, source, nearest); this.source = source; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KoutRecords.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KoutRecords.java index e48707a221..41d6a44086 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KoutRecords.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/KoutRecords.java @@ -32,10 +32,9 @@ public class KoutRecords extends SingleWayMultiPathsRecords { - public KoutRecords(Id source, - RecordType type, - boolean nearest, boolean concurrent) { - super(source, type, nearest, concurrent); + public KoutRecords(RecordType type, boolean concurrent, + Id source, boolean nearest) { + super(type, concurrent, source, nearest); } @Override 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 dcb51b292a..336b76a3bf 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 @@ -40,7 +40,7 @@ public class ShortestPathRecords extends DoubleWayMultiPathsRecords { private boolean pathFound; public ShortestPathRecords(Id sourceV, Id targetV) { - super(sourceV, targetV, RecordType.INT); + super(RecordType.INT, false, sourceV, targetV); this.accessedVertices = new IntHashSet(); this.accessedVertices.add(this.code(sourceV)); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/SingleWayMultiPathsRecords.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/SingleWayMultiPathsRecords.java index 55e7b46c83..e9185d874f 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/SingleWayMultiPathsRecords.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/SingleWayMultiPathsRecords.java @@ -21,12 +21,9 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Set; import java.util.Stack; -import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import org.eclipse.collections.api.set.primitive.MutableIntSet; @@ -49,15 +46,15 @@ public class SingleWayMultiPathsRecords extends AbstractRecords { protected final Stack records; private final boolean nearest; - private final Set accessedVertices; + private final MutableIntSet accessedVertices; protected Record currentRecord; protected IntIterator lastRecordKeys; protected int current; protected boolean forward; - public SingleWayMultiPathsRecords(Id source, RecordType type, - boolean nearest, boolean concurrent) { + public SingleWayMultiPathsRecords(RecordType type, boolean concurrent, + Id source, boolean nearest) { super(type, concurrent); this.nearest = nearest; @@ -68,8 +65,8 @@ public SingleWayMultiPathsRecords(Id source, RecordType type, this.records = new Stack<>(); this.records.push(firstRecord); - this.accessedVertices = concurrent ? ConcurrentHashMap.newKeySet() : - new HashSet(); + this.accessedVertices = concurrent ? new IntHashSet().asSynchronized() : + new IntHashSet(); this.accessedVertices.add(sourceCode); } 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 fb2b202b12..301fe5ffd7 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 @@ -40,6 +40,7 @@ public synchronized Object code2Object(int code) { return this.singleObjectIntMapping.code2Object(code); } + @Override public synchronized void clear() { this.singleObjectIntMapping.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 281a0ed736..2e6bcb2a26 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 @@ -60,6 +60,7 @@ public synchronized Object code2Object(int code) { return this.int2IdMap.get(code); } + @Override public void clear() { this.int2IdMap.clear(); } diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java index e2db7a7780..63c4d5d087 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java @@ -38,6 +38,7 @@ import com.baidu.hugegraph.unit.core.ExceptionTest; import com.baidu.hugegraph.unit.core.Int2IntsMapTest; import com.baidu.hugegraph.unit.core.LocksTableTest; +import com.baidu.hugegraph.unit.core.ObjectIntMappingTest; import com.baidu.hugegraph.unit.core.QueryTest; import com.baidu.hugegraph.unit.core.RangeTest; import com.baidu.hugegraph.unit.core.RolePermissionTest; @@ -101,6 +102,7 @@ BackendStoreSystemInfoTest.class, TraversalUtilTest.class, Int2IntsMapTest.class, + ObjectIntMappingTest.class, /* serializer */ BytesBufferTest.class,