Skip to content

Commit

Permalink
extract Record and Records for paths and shortest path
Browse files Browse the repository at this point in the history
Change-Id: I4b79d0987f1eebcfb952bc4627a2f4fc8d2dcaf3
  • Loading branch information
zhoney committed Apr 13, 2021
1 parent 2fd2f80 commit 7456243
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.baidu.hugegraph.perf.PerfUtil.Watched;
import com.baidu.hugegraph.structure.HugeEdge;
import com.baidu.hugegraph.traversal.algorithm.records.MultiPathsRecords;
import com.baidu.hugegraph.traversal.algorithm.records.RecordType;
import com.baidu.hugegraph.type.define.Directions;
import com.baidu.hugegraph.util.E;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -93,8 +94,8 @@ private class Traverser {

public Traverser(Id sourceV, Id targetV, Id label,
long degree, long capacity, long limit) {
this.record = new MultiPathsRecords(sourceV, targetV);

this.record = new MultiPathsRecords(sourceV, targetV,
RecordType.ARRAY);
this.label = label;
this.degree = degree;
this.capacity = capacity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.perf.PerfUtil.Watched;
import com.baidu.hugegraph.structure.HugeEdge;
import com.baidu.hugegraph.traversal.algorithm.records.ShortestPathRecord;
import com.baidu.hugegraph.traversal.algorithm.records.ShortestPathRecords;
import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep;
import com.baidu.hugegraph.type.define.Directions;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.collection.ObjectIntMapping;
import com.google.common.collect.ImmutableList;

public class ShortestPathTraverser extends HugeTraverser {

public ShortestPathTraverser(HugeGraph graph) {
super(graph);
this.idMapping = new ObjectIntMapping();
}

@Watched
Expand Down Expand Up @@ -142,7 +140,7 @@ public PathSet allShortestPaths(Id sourceV, Id targetV, Directions dir,

private class Traverser {

private ShortestPathRecord record;
private ShortestPathRecords record;
private final Directions direction;
private final Map<Id, String> labels;
private final long degree;
Expand All @@ -152,7 +150,7 @@ private class Traverser {
public Traverser(Id sourceV, Id targetV, Directions dir,
Map<Id, String> labels, long degree,
long skipDegree, long capacity) {
this.record = new ShortestPathRecord(sourceV, targetV);
this.record = new ShortestPathRecords(sourceV, targetV);
this.direction = dir;
this.labels = labels;
this.degree = degree;
Expand Down Expand Up @@ -221,7 +219,7 @@ public PathSet backward(boolean all) {
Id target = edge.id().otherVertexId();

PathSet paths = this.record.findPath(target,
t -> this.superNode(t, opposite),
t -> !this.superNode(t, opposite),
all, false);

if (paths.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

import com.baidu.hugegraph.util.collection.Int2IntsMap;

public class ArrayRecord implements Record {
public class IntArrayRecord implements Record {

private Int2IntsMap layer;

public ArrayRecord() {
public IntArrayRecord() {
this.layer = new Int2IntsMap();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.traversal.algorithm.records;

import org.eclipse.collections.api.iterator.IntIterator;
import org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap;

public class IntIntRecord implements Record {

private IntIntHashMap layer;

public IntIntRecord() {
this.layer = new IntIntHashMap();
}

@Override
public IntIterator keys() {
return this.layer.keySet().intIterator();
}

@Override
public boolean containsKey(int key) {
return this.layer.containsKey(key);
}

@Override
public IntIterator get(int key) {
return null;
}

@Override
public void addPath(int node, int parent) {
this.layer.put(node, parent);
}

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

public IntIntHashMap layer() {
return this.layer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,58 +38,59 @@
public class MultiPathsRecords implements Records {

private final ObjectIntMapping idMapping;
private final RecordType type;

private final Stack<Record> sourceLayers;
private final Stack<Record> targetLayers;

private Record currentLayer;
private IntIterator lastLayerKeys;
private int current;
private boolean forward;
protected final Stack<Record> sourceRecords;
protected final Stack<Record> targetRecords;

protected Record currentRecord;
private IntIterator lastRecordKeys;
protected int current;
protected boolean forward;
private int accessed;

public MultiPathsRecords(Id sourceV, Id targetV) {
public MultiPathsRecords(Id sourceV, Id targetV, RecordType type) {
this.idMapping = new ObjectIntMapping();
this.type = type;

int sourceCode = this.code(sourceV);
int targetCode = this.code(targetV);
Record firstSourceLayer = this.newLayer();
Record firstTargetLayer = this.newLayer();
firstSourceLayer.addPath(sourceCode, 0);
firstTargetLayer.addPath(targetCode, 0);
this.sourceLayers = new Stack<>();
this.targetLayers = new Stack<>();
this.sourceLayers.push(firstSourceLayer);
this.targetLayers.push(firstTargetLayer);
Record firstSourceRecord = this.newRecord();
Record firstTargetRecord = this.newRecord();
firstSourceRecord.addPath(sourceCode, 0);
firstTargetRecord.addPath(targetCode, 0);
this.sourceRecords = new Stack<>();
this.targetRecords = new Stack<>();
this.sourceRecords.push(firstSourceRecord);
this.targetRecords.push(firstTargetRecord);

this.accessed = 2;
}

public void startOneLayer(boolean forward) {
this.forward = forward;
this.currentLayer = newLayer();
this.lastLayerKeys = this.forward ? this.sourceLayers.peek().keys() :
this.targetLayers.peek().keys();
this.currentRecord = this.newRecord();
this.lastRecordKeys = this.forward ? this.sourceRecords.peek().keys() :
this.targetRecords.peek().keys();
}

public void finishOneLayer() {
if (this.forward) {
this.sourceLayers.push(this.currentLayer);
this.sourceRecords.push(this.currentRecord);
} else {
this.targetLayers.push(this.currentLayer);
this.targetRecords.push(this.currentRecord);
}
this.accessed += this.currentLayer.size();
this.accessed += this.currentRecord.size();
}

@Watched
public boolean hasNextKey() {
return this.lastLayerKeys.hasNext();
return this.lastRecordKeys.hasNext();
}

@Watched
public Id nextKey() {
this.current = this.lastLayerKeys.next();
this.current = this.lastRecordKeys.next();
return this.id(current);
}

Expand All @@ -113,22 +114,21 @@ public long accessed() {
return this.accessed;
}

private boolean contains(int node) {
protected boolean contains(int node) {
return this.forward ? this.targetContains(node) :
this.sourceContains(node);
}

private boolean sourceContains(int node) {
return this.sourceLayers.peek().containsKey(node);
return this.sourceRecords.peek().containsKey(node);
}

private boolean targetContains(int node) {
return this.targetLayers.peek().containsKey(node);
return this.targetRecords.peek().containsKey(node);
}

@Watched
private PathSet linkPath(int source, int target, boolean ring) {

PathSet results = new PathSet();
PathSet sources = this.linkSourcePath(source);
PathSet targets = this.linkTargetPath(target);
Expand All @@ -151,13 +151,13 @@ private PathSet linkPath(int source, int target, boolean ring) {
}

private PathSet linkSourcePath(int source) {
return this.linkPath(this.sourceLayers, source,
this.sourceLayers.size() - 1);
return this.linkPath(this.sourceRecords, source,
this.sourceRecords.size() - 1);
}

private PathSet linkTargetPath(int target) {
return this.linkPath(this.targetLayers, target,
this.targetLayers.size() - 1);
return this.linkPath(this.targetRecords, target,
this.targetRecords.size() - 1);
}

private PathSet linkPath(Stack<Record> all, int id, int layerIndex) {
Expand Down Expand Up @@ -189,21 +189,21 @@ private PathSet linkPath(Stack<Record> all, int id, int layerIndex) {
}

@Watched
private void addPath(int current, int parent) {
this.currentLayer.addPath(current, parent);
protected void addPath(int current, int parent) {
this.currentRecord.addPath(current, parent);
}

@Watched
private int code(Id id) {
protected int code(Id id) {
return this.idMapping.object2Code(id);
}

@Watched
private Id id(int code) {
protected Id id(int code) {
return (Id) this.idMapping.code2Object(code);
}

public Record newLayer() {
return RecordFactory.newRecord();
private Record newRecord() {
return RecordFactory.newRecord(this.type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@

public class RecordFactory {

public static Record newRecord() {
return new ArrayRecord();
}

public static Record newRecord(RecordType type) {
switch (type) {
case ARRAY:
return new ArrayRecord();
return new IntArrayRecord();
case SET:
return new IntSetRecord();
case INT:
return new IntIntRecord();
default:
throw new AssertionError("Unsupported record type: " + type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public enum RecordType implements SerialEnum {
ARRAY(1, "array"),

// Eclipse Collection
SET(2, "set");
SET(2, "set"),

INT(3, "int");

private final byte code;
private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

public interface Records {

static final int INIT_CAPACITY = 16;

public void startOneLayer(boolean forward);

public void finishOneLayer();
Expand Down
Loading

0 comments on commit 7456243

Please sign in to comment.