Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
Change-Id: Icb18c665b27dced23415bdcfc582fa372d1fe2ba
  • Loading branch information
zhoney committed May 21, 2021
1 parent b5f5121 commit b3969e4
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import com.baidu.hugegraph.backend.store.raft.RaftGroupManager;
import com.baidu.hugegraph.config.ConfigOption;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.config.TypedOption;
import com.baidu.hugegraph.exception.NotSupportException;
import com.baidu.hugegraph.iterator.FilterIterator;
import com.baidu.hugegraph.rpc.RpcServiceConfig4Client;
Expand Down Expand Up @@ -552,7 +553,7 @@ public long now() {
}

@Override
public <V> V option(ConfigOption<V> option) {
public <K, V> V option(TypedOption<K, V> option) {
this.verifyAnyPermission();
return this.hugegraph.option(option);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo;
import com.baidu.hugegraph.backend.store.raft.RaftGroupManager;
import com.baidu.hugegraph.config.ConfigOption;
import com.baidu.hugegraph.config.TypedOption;
import com.baidu.hugegraph.rpc.RpcServiceConfig4Client;
import com.baidu.hugegraph.rpc.RpcServiceConfig4Server;
import com.baidu.hugegraph.schema.EdgeLabel;
Expand Down Expand Up @@ -170,7 +171,7 @@ public interface HugeGraph extends Graph {

public long now();

public <V> V option(ConfigOption<V> option);
public <K, V> V option(TypedOption<K, V> option);

public void registerRpcServices(RpcServiceConfig4Server serverConfig,
RpcServiceConfig4Client clientConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import com.baidu.hugegraph.config.ConfigOption;
import com.baidu.hugegraph.config.CoreOptions;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.config.TypedOption;
import com.baidu.hugegraph.event.EventHub;
import com.baidu.hugegraph.event.EventListener;
import com.baidu.hugegraph.exception.NotAllowException;
Expand Down Expand Up @@ -116,7 +117,7 @@ public class StandardHugeGraph implements HugeGraph {
StandardHugeGraph.SysTransaction.class
};

public static final Set<ConfigOption<?>> ALLOWED_CONFIGS = ImmutableSet.of(
public static final Set<TypedOption<?, ?>> ALLOWED_CONFIGS = ImmutableSet.of(
CoreOptions.TASK_WAIT_TIMEOUT,
CoreOptions.TASK_SYNC_DELETION,
CoreOptions.TASK_TTL_DELETE_BATCH,
Expand Down Expand Up @@ -978,7 +979,7 @@ public long now() {
}

@Override
public <V> V option(ConfigOption<V> option) {
public <K, V> V option(TypedOption<K, V> option) {
HugeConfig config = this.configuration();
if (!ALLOWED_CONFIGS.contains(option)) {
throw new NotAllowException("Not allowed to access config: %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static com.baidu.hugegraph.config.OptionChecker.rangeInt;

import com.baidu.hugegraph.backend.query.Query;
import com.baidu.hugegraph.type.define.CollectionType;
import com.baidu.hugegraph.util.Bytes;

public class CoreOptions extends OptionHolder {
Expand Down Expand Up @@ -624,12 +625,13 @@ public static synchronized CoreOptions instance() {
10
);

public static final ConfigOption<String> OLTP_COLLECTION_IMPL_TYPE =
new ConfigOption<>(
public static final ConfigConvOption<String, CollectionType> OLTP_COLLECTION_IMPL_TYPE =
new ConfigConvOption<>(
"oltp.collection_impl_type",
"The implementation type of collections " +
"used in oltp algorithm.",
allowValues("jcf", "ec", "fu"),
CollectionType::valueOf,
"ec"
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import com.baidu.hugegraph.schema.VertexLabel;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.Cardinality;
import com.baidu.hugegraph.type.define.CollectionImplType;
import com.baidu.hugegraph.type.define.CollectionType;
import com.baidu.hugegraph.type.define.Directions;
import com.baidu.hugegraph.type.define.HugeKeys;
import com.baidu.hugegraph.type.define.IdStrategy;
Expand Down Expand Up @@ -223,7 +223,11 @@ public Collection<HugeEdge> getEdges() {
}

public void resetEdges() {
this.edges = CollectionFactory.newList(CollectionImplType.EC);
/*
* Use list to hold edges for vertices to reduce memory usage and
* add operation time.
*/
this.edges = newList();
}

public void removeEdge(HugeEdge edge) {
Expand All @@ -232,7 +236,7 @@ public void removeEdge(HugeEdge edge) {

public void addEdge(HugeEdge edge) {
if (this.edges == EMPTY_LIST) {
this.edges = CollectionFactory.newList(CollectionImplType.EC);
this.edges = CollectionFactory.newList(CollectionType.EC);
}
this.edges.add(edge);
}
Expand Down Expand Up @@ -642,13 +646,25 @@ public static HugeVertex create(final GraphTransaction tx,
return new HugeVertex4Insert(tx, id, label);
}

private static <V> Set<V> newSet() {
return CollectionFactory.newSet(CollectionType.EC);
}

private static <V> List<V> newList() {
return CollectionFactory.newList(CollectionType.EC);
}

private static final class HugeVertex4Insert extends HugeVertex {

private GraphTransaction tx;

public HugeVertex4Insert(final GraphTransaction tx,
Id id, VertexLabel label) {
super(tx.graph(), id, label);
/*
* Use set to hold edges for inserted vertex
* to avoid duplicated edges
*/
this.edges = newSet();
this.tx = tx;
this.fresh(true);
Expand Down Expand Up @@ -679,9 +695,5 @@ protected GraphTransaction tx() {
}
return null;
}

private static <V> Set<V> newSet() {
return CollectionFactory.newSet(CollectionImplType.EC);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep;
import com.baidu.hugegraph.traversal.optimize.TraversalUtil;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.CollectionImplType;
import com.baidu.hugegraph.type.define.CollectionType;
import com.baidu.hugegraph.type.define.Directions;
import com.baidu.hugegraph.type.define.HugeKeys;
import com.baidu.hugegraph.util.CollectionUtil;
Expand Down Expand Up @@ -103,9 +103,8 @@ protected int concurrentDepth() {
return this.graph.option(CoreOptions.OLTP_CONCURRENT_DEPTH);
}

private CollectionImplType collectionImplType() {
return CollectionImplType.valueOf(this.graph.option(
CoreOptions.OLTP_COLLECTION_IMPL_TYPE).toUpperCase());
private CollectionType collectionImplType() {
return this.graph.option(CoreOptions.OLTP_COLLECTION_IMPL_TYPE);
}

protected Set<Id> adjacentVertices(Set<Id> vertices, Directions dir,
Expand Down Expand Up @@ -549,22 +548,6 @@ public String toString() {
}
}

public static class KNode extends Node {

public KNode(Id id, KNode parent) {
super(id, parent);
}

@Override
public boolean equals(Object object) {
if (!(object instanceof KNode)) {
return false;
}
KNode other = (KNode) object;
return Objects.equals(this.id(), other.id());
}
}

public static class Path {

public static final Path EMPTY_PATH = new Path(ImmutableList.of());
Expand All @@ -585,7 +568,7 @@ public Id crosspoint() {
return this.crosspoint;
}

public void addToEnd(Id id) {
public void addToLast(Id id) {
this.vertices.add(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@

public class KneighborTraverser extends OltpTraverser {

private volatile boolean stop;

public KneighborTraverser(HugeGraph graph) {
super(graph);
}
Expand Down Expand Up @@ -88,14 +86,13 @@ public KneighborRecords customizedKneighbor(Id source, EdgeStep step,
source, true);

Consumer<Id> consumer = v -> {
if (this.stop) {
if (this.reachLimit(limit, records.size())) {
return;
}
Iterator<Edge> edges = edgesOfVertex(v, step);
while (!this.stop && edges.hasNext()) {
while (!this.reachLimit(limit, records.size()) && edges.hasNext()) {
Id target = ((HugeEdge) edges.next()).id().otherVertexId();
records.addPath(v, target);
this.reachLimit(limit, records.size());
}
};

Expand All @@ -108,10 +105,6 @@ public KneighborRecords customizedKneighbor(Id source, EdgeStep step,
}

private boolean reachLimit(long limit, int size) {
if (limit == NO_LIMIT || size < limit) {
return false;
}
this.stop = true;
return true;
return limit != NO_LIMIT && size >= limit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
public class KoutTraverser extends OltpTraverser {

private int depth;
private volatile boolean stop = false;

public KoutTraverser(HugeGraph graph) {
super(graph);
Expand Down Expand Up @@ -117,16 +116,15 @@ public KoutRecords customizedKout(Id source, EdgeStep step,
source, nearest);

Consumer<Id> consumer = v -> {
if (this.stop) {
if (this.reachLimit(limit, this.depth, records.size())) {
return;
}
Iterator<Edge> edges = edgesOfVertex(v, step);
while (!this.stop && edges.hasNext()) {
while (!this.reachLimit(limit, this.depth, records.size()) &&
edges.hasNext()) {
Id target = ((HugeEdge) edges.next()).id().otherVertexId();
records.addPath(v, target);

this.checkCapacity(capacity, records.accessed(), this.depth);
this.checkLimit(limit, this.depth, records.size());
}
};

Expand All @@ -149,12 +147,7 @@ private void checkCapacity(long capacity, long accessed, int depth) {
}
}

private void checkLimit(long limit, long depth, int size) {
if (limit == NO_LIMIT || depth > 0) {
return;
}
if (size >= limit) {
this.stop = true;
}
private boolean reachLimit(long limit, long depth, int size) {
return limit != NO_LIMIT && depth <= 0 && size >= limit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.apache.tinkerpop.gremlin.structure.Property;
import org.apache.tinkerpop.gremlin.structure.Vertex;
Expand All @@ -38,8 +35,6 @@
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.config.CoreOptions;
import com.baidu.hugegraph.iterator.FilterIterator;
import com.baidu.hugegraph.structure.HugeEdge;
import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep;
import com.baidu.hugegraph.util.Consumers;

import jersey.repackaged.com.google.common.base.Objects;
Expand Down Expand Up @@ -81,63 +76,6 @@ public static void destroy() {
}
}

protected Set<Node> adjacentVertices(Set<Node> latest, EdgeStep step,
Set<Node> all, long remaining,
boolean single) {
if (single) {
return this.adjacentVertices(latest, step, all, remaining);
} else {
AtomicLong remain = new AtomicLong(remaining);
return this.adjacentVertices(latest, step, all, remain);
}
}

protected Set<Node> adjacentVertices(Set<Node> vertices, EdgeStep step,
Set<Node> excluded, long remaining) {
Set<Node> neighbors = newSet();
for (Node source : vertices) {
Iterator<Edge> edges = this.edgesOfVertex(source.id(), step);
while (edges.hasNext()) {
Id target = ((HugeEdge) edges.next()).id().otherVertexId();
KNode kNode = new KNode(target, (KNode) source);
if (excluded != null && excluded.contains(kNode)) {
continue;
}
neighbors.add(kNode);
if (remaining != NO_LIMIT && --remaining <= 0L) {
return neighbors;
}
}
}
return neighbors;
}

protected Set<Node> adjacentVertices(Set<Node> vertices, EdgeStep step,
Set<Node> excluded,
AtomicLong remaining) {
Set<Node> neighbors = ConcurrentHashMap.newKeySet();
this.traverseNodes(vertices.iterator(), v -> {
Iterator<Edge> edges = this.edgesOfVertex(v.id(), step);
while (edges.hasNext()) {
Id target = ((HugeEdge) edges.next()).id().otherVertexId();
KNode kNode = new KNode(target, (KNode) v);
if (excluded != null && excluded.contains(kNode)) {
continue;
}
neighbors.add(kNode);
if (remaining.decrementAndGet() <= 0L) {
return;
}
}
});
return neighbors;
}

protected long traverseNodes(Iterator<Node> vertices,
Consumer<Node> consumer) {
return this.traverse(vertices, consumer, "traverse-nodes");
}

protected long traversePairs(Iterator<Pair<Id, Id>> pairs,
Consumer<Pair<Id, Id>> consumer) {
return this.traverse(pairs, consumer, "traverse-pairs");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private PathSet linkPath(Stack<Record> all, int id, int layerIndex) {
iter.remove();
continue;
}
path.addToEnd(sid);
path.addToLast(sid);
}

results.addAll(paths);
Expand Down
Loading

0 comments on commit b3969e4

Please sign in to comment.