Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
Change-Id: I1a66e5ea99a32f6cead79b80317dc7d7b3a77fa7
  • Loading branch information
zhoney committed May 27, 2021
1 parent 5b3b47c commit bd8e9b3
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public abstract class AbstractRecords implements Records {
private final ObjectIntMapping<Id> idMapping;
private final RecordType type;
private final boolean concurrent;
private Record currentRecord;

public AbstractRecords(RecordType type, boolean concurrent) {
this.type = type;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@

public abstract class DoubleWayMultiPathsRecords extends AbstractRecords {

protected final Stack<Record> sourceRecords;
protected final Stack<Record> targetRecords;
private final Stack<Record> sourceRecords;
private final Stack<Record> 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,
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -193,6 +193,22 @@ private PathSet linkPath(Stack<Record> 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<Record> sourceRecords() {
return this.sourceRecords;
}

protected Stack<Record> targetRecords() {
return this.targetRecords;
}

protected boolean forward() {
return this.forward;
}

protected int current() {
return this.current;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,8 +46,9 @@ public int size() {

public Set<Id> ids(long limit) {
Set<Id> ids = CollectionFactory.newIdSet(CollectionType.EC);
for (int i = 1; i < this.records.size(); i++) {
IntIterator iterator = this.records.get(i).keys();
Stack<Record> 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--;
Expand All @@ -56,8 +59,9 @@ public Set<Id> 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<Record> 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--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,11 +41,11 @@ public KoutRecords(RecordType type, boolean concurrent,

@Override
public int size() {
return this.currentRecord.size();
return this.currentRecord().size();
}

public Set<Id> ids(long limit) {
IntIterator iterator = this.records.peek().keys();
IntIterator iterator = this.records().peek().keys();
Set<Id> ids = CollectionFactory.newIdSet(CollectionType.EC);
while ((limit == NO_LIMIT || limit-- > 0L) && iterator.hasNext()) {
ids.add(this.id(iterator.next()));
Expand All @@ -53,9 +55,10 @@ public Set<Id> ids(long limit) {

public PathSet paths(long limit) {
PathSet paths = new PathSet();
IntIterator iterator = this.records.peek().keys();
Stack<Record> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public PathSet findPath(Id target, Function<Id, Boolean> 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;
Expand All @@ -73,13 +74,13 @@ public PathSet findPath(Id target, Function<Id, Boolean> 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);
}

Expand All @@ -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<Record> all, int node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,13 @@

public abstract class SingleWayMultiPathsRecords extends AbstractRecords {

protected final Stack<Record> records;
private final Stack<Record> 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) {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -173,4 +169,8 @@ public Path getPath(int layerIndex, int target) {
Collections.reverse(ids);
return new Path(ids);
}

public Stack<Record> records() {
return this.records;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,72 +220,17 @@ public static <V> MutableIntObjectMap<V> newIntObjectMap(
return new IntObjectHashMap<>(map);
}

public static <V> MutableIntObjectMap<V> newIntObjectMap(
int key1, V value1) {
return IntObjectHashMap.newWithKeysValues(key1, value1);
}

public static <V> MutableIntObjectMap<V> newIntObjectMap(
int key1, V value1,
int key2, V value2) {
return IntObjectHashMap.newWithKeysValues(key1, value1, key2, value2);
}

public static <V> MutableIntObjectMap<V> 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 <V> MutableIntObjectMap<V> newIntObjectMap(
int key1, V value1,
int key2, V value2,
int key3, V value3,
Object... objects) {
IntObjectHashMap<V> 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 <V> MutableIntObjectMap<V> newIntObjectMap(Id key1,
V value1) {
return IntObjectHashMap.newWithKeysValues((int) key1.asLong(), value1);
}

public static <V> MutableIntObjectMap<V> newIntObjectMap(
Id key1, V value1,
Id key2, V value2) {
return IntObjectHashMap.newWithKeysValues((int) key1.asLong(), value1,
(int) key2.asLong(), value2);
}

public static <V> MutableIntObjectMap<V> 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 <V> MutableIntObjectMap<V> newIntObjectMap(
Object... objects) {
IntObjectHashMap<V> map = IntObjectHashMap.newMap();
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,7 +109,7 @@
PageStateTest.class,
Int2IntsMapTest.class,
ObjectIntMappingTest.class,
IdSet.class,
IdSetTest.class,

/* serializer */
BytesBufferTest.class,
Expand Down

0 comments on commit bd8e9b3

Please sign in to comment.