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 dd45208d66..e75c94bea6 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 @@ -32,6 +32,7 @@ public abstract class AbstractRecords implements Records { private final ObjectIntMapping idMapping; private final RecordType type; private final boolean concurrent; + private Record currentRecord; public AbstractRecords(RecordType type, boolean concurrent) { this.type = type; @@ -52,4 +53,12 @@ protected Id id(int code) { protected Record newRecord() { return RecordFactory.newRecord(this.type, this.concurrent); } + + protected Record currentRecord() { + return this.currentRecord; + } + + protected void currentRecord(Record record) { + this.currentRecord = record; + } } 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 0757430cb3..2dfb195e8e 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 @@ -38,13 +38,12 @@ public abstract class DoubleWayMultiPathsRecords extends AbstractRecords { - protected final Stack sourceRecords; - protected final Stack targetRecords; + private final Stack sourceRecords; + private final Stack targetRecords; - protected Record currentRecord; private IntIterator lastRecordKeys; - protected int current; - protected boolean forward; + private int current; + private boolean forward; private int accessed; public DoubleWayMultiPathsRecords(RecordType type, boolean concurrent, @@ -67,19 +66,20 @@ public DoubleWayMultiPathsRecords(RecordType type, boolean concurrent, @Override public void startOneLayer(boolean forward) { this.forward = forward; - this.currentRecord = this.newRecord(); + this.currentRecord(this.newRecord()); this.lastRecordKeys = this.forward ? this.sourceRecords.peek().keys() : this.targetRecords.peek().keys(); } @Override public void finishOneLayer() { + Record record = this.currentRecord(); if (this.forward) { - this.sourceRecords.push(this.currentRecord); + this.sourceRecords.push(record); } else { - this.targetRecords.push(this.currentRecord); + this.targetRecords.push(record); } - this.accessed += this.currentRecord.size(); + this.accessed += record.size(); } @Watched @@ -92,7 +92,7 @@ public boolean hasNextKey() { @Override public Id nextKey() { this.current = this.lastRecordKeys.next(); - return this.id(current); + return this.id(this.current); } @Watched @@ -193,6 +193,22 @@ private PathSet linkPath(Stack all, int id, int layerIndex) { @Watched protected void addPath(int current, int parent) { - this.currentRecord.addPath(current, parent); + this.currentRecord().addPath(current, parent); + } + + protected Stack sourceRecords() { + return this.sourceRecords; + } + + protected Stack targetRecords() { + return this.targetRecords; + } + + protected boolean forward() { + return this.forward; + } + + protected int current() { + return this.current; } } 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 24762006b6..98c530bbd7 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 @@ -22,10 +22,12 @@ import static com.baidu.hugegraph.backend.query.Query.NO_LIMIT; import java.util.Set; +import java.util.Stack; import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.traversal.algorithm.HugeTraverser.PathSet; import com.baidu.hugegraph.traversal.algorithm.records.record.IntIterator; +import com.baidu.hugegraph.traversal.algorithm.records.record.Record; import com.baidu.hugegraph.traversal.algorithm.records.record.RecordType; import com.baidu.hugegraph.type.define.CollectionType; import com.baidu.hugegraph.util.collection.CollectionFactory; @@ -44,8 +46,9 @@ public int size() { public Set ids(long limit) { Set ids = CollectionFactory.newIdSet(CollectionType.EC); - for (int i = 1; i < this.records.size(); i++) { - IntIterator iterator = this.records.get(i).keys(); + Stack records = this.records(); + for (int i = 1; i < records.size(); i++) { + IntIterator iterator = records.get(i).keys(); while ((limit == NO_LIMIT || limit > 0L) && iterator.hasNext()) { ids.add(this.id(iterator.next())); limit--; @@ -56,8 +59,9 @@ public Set ids(long limit) { public PathSet paths(long limit) { PathSet paths = new PathSet(); - for (int i = 1; i < this.records.size(); i++) { - IntIterator iterator = this.records.get(i).keys(); + Stack records = this.records(); + for (int i = 1; i < records.size(); i++) { + IntIterator iterator = records.get(i).keys(); while ((limit == NO_LIMIT || limit > 0L) && iterator.hasNext()) { paths.add(this.getPath(i, iterator.next())); limit--; 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 1302e9612a..da58b6cd3b 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 @@ -22,10 +22,12 @@ import static com.baidu.hugegraph.backend.query.Query.NO_LIMIT; import java.util.Set; +import java.util.Stack; import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.traversal.algorithm.HugeTraverser.PathSet; import com.baidu.hugegraph.traversal.algorithm.records.record.IntIterator; +import com.baidu.hugegraph.traversal.algorithm.records.record.Record; import com.baidu.hugegraph.traversal.algorithm.records.record.RecordType; import com.baidu.hugegraph.type.define.CollectionType; import com.baidu.hugegraph.util.collection.CollectionFactory; @@ -39,11 +41,11 @@ public KoutRecords(RecordType type, boolean concurrent, @Override public int size() { - return this.currentRecord.size(); + return this.currentRecord().size(); } public Set ids(long limit) { - IntIterator iterator = this.records.peek().keys(); + IntIterator iterator = this.records().peek().keys(); Set ids = CollectionFactory.newIdSet(CollectionType.EC); while ((limit == NO_LIMIT || limit-- > 0L) && iterator.hasNext()) { ids.add(this.id(iterator.next())); @@ -53,9 +55,10 @@ public Set ids(long limit) { public PathSet paths(long limit) { PathSet paths = new PathSet(); - IntIterator iterator = this.records.peek().keys(); + Stack records = this.records(); + IntIterator iterator = records.peek().keys(); while ((limit == NO_LIMIT || limit-- > 0L) && iterator.hasNext()) { - paths.add(this.getPath(this.records.size() - 1, iterator.next())); + paths.add(this.getPath(records.size() - 1, iterator.next())); } return paths; } 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 23d3513f16..b8b69c06c6 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 @@ -59,8 +59,9 @@ public PathSet findPath(Id target, Function filter, if (!filter.apply(target)) { return paths; } - paths.add(this.forward ? this.linkPath(this.current, targetCode) : - this.linkPath(targetCode, this.current)); + paths.add(this.forward() ? + this.linkPath(this.current(), targetCode) : + this.linkPath(targetCode, this.current())); this.pathFound = true; if (!all) { return paths; @@ -73,13 +74,13 @@ public PathSet findPath(Id target, Function filter, * 2. path of node doesn't have loop */ if (!this.pathFound && this.isNew(targetCode)) { - this.addPath(targetCode, this.current); + this.addPath(targetCode, this.current()); } return paths; } private boolean isNew(int node) { - return !this.currentRecord.containsKey(node) && + return !this.currentRecord().containsKey(node) && !this.accessedVertices.contains(node); } @@ -93,11 +94,11 @@ private Path linkPath(int source, int target) { } private Path linkSourcePath(int source) { - return this.linkPath(this.sourceRecords, source); + return this.linkPath(this.sourceRecords(), source); } private Path linkTargetPath(int target) { - return this.linkPath(this.targetRecords, target); + return this.linkPath(this.targetRecords(), target); } private Path linkPath(Stack all, int node) { 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 48b56eaa3e..2e21dc1cd5 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 @@ -43,16 +43,13 @@ public abstract class SingleWayMultiPathsRecords extends AbstractRecords { - protected final Stack records; + private final Stack records; private final int sourceCode; private final boolean nearest; private final MutableIntSet accessedVertices; - protected Record currentRecord; - protected IntIterator lastRecordKeys; - protected int current; - protected boolean forward; + private IntIterator lastRecordKeys; public SingleWayMultiPathsRecords(RecordType type, boolean concurrent, Id source, boolean nearest) { @@ -72,13 +69,13 @@ public SingleWayMultiPathsRecords(RecordType type, boolean concurrent, @Override public void startOneLayer(boolean forward) { - this.currentRecord = this.newRecord(); + this.currentRecord(this.newRecord()); this.lastRecordKeys = this.records.peek().keys(); } @Override public void finishOneLayer() { - this.records.push(this.currentRecord); + this.records.push(this.currentRecord()); } @Override @@ -88,8 +85,7 @@ public boolean hasNextKey() { @Override public Id nextKey() { - this.current = this.lastRecordKeys.next(); - return this.id(current); + return this.id(this.lastRecordKeys.next()); } @Override @@ -119,11 +115,11 @@ public void addPath(Id source, Id target) { int sourceCode = this.code(source); int targetCode = this.code(target); if (this.nearest && this.accessedVertices.contains(targetCode) || - !this.nearest && this.currentRecord.containsKey(targetCode) || + !this.nearest && this.currentRecord().containsKey(targetCode) || targetCode == this.sourceCode) { return; } - this.currentRecord.addPath(targetCode, sourceCode); + this.currentRecord().addPath(targetCode, sourceCode); this.accessedVertices.add(targetCode); } @@ -173,4 +169,8 @@ public Path getPath(int layerIndex, int target) { Collections.reverse(ids); return new Path(ids); } + + public Stack records() { + return this.records; + } } 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 b3be5be893..a7a021bc62 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 @@ -220,63 +220,6 @@ 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 static MutableIntObjectMap newIntObjectMap(Id key1, - V value1) { - return IntObjectHashMap.newWithKeysValues((int) key1.asLong(), value1); - } - - public static MutableIntObjectMap newIntObjectMap( - Id key1, V value1, - Id key2, V value2) { - return IntObjectHashMap.newWithKeysValues((int) key1.asLong(), value1, - (int) key2.asLong(), value2); - } - - public static MutableIntObjectMap newIntObjectMap( - Id key1, V value1, - Id key2, V value2, - Id key3, V value3) { - return IntObjectHashMap.newWithKeysValues((int) key1.asLong(), value1, - (int) key2.asLong(), value2, - (int) key3.asLong(), value3); - } - @SuppressWarnings("unchecked") public static MutableIntObjectMap newIntObjectMap( Object... objects) { @@ -284,8 +227,10 @@ public static MutableIntObjectMap newIntObjectMap( 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) ((Id) objects[i]).asLong(), (V) objects[i + 1]); + for (int i = 0; i < objects.length; i += 2) { + int key = objects[i] instanceof Id ? + (int) ((Id) objects[i]).asLong() : (int) objects[i]; + map.put(key, (V) objects[i + 1]); } return map; } 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 73a7fea018..94a6f35506 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 @@ -36,6 +36,7 @@ import com.baidu.hugegraph.unit.core.DataTypeTest; import com.baidu.hugegraph.unit.core.DirectionsTest; import com.baidu.hugegraph.unit.core.ExceptionTest; +import com.baidu.hugegraph.unit.core.IdSetTest; import com.baidu.hugegraph.unit.core.Int2IntsMapTest; import com.baidu.hugegraph.unit.core.LocksTableTest; import com.baidu.hugegraph.unit.core.PageStateTest; @@ -108,7 +109,7 @@ PageStateTest.class, Int2IntsMapTest.class, ObjectIntMappingTest.class, - IdSet.class, + IdSetTest.class, /* serializer */ BytesBufferTest.class,