diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java index 34fcc6d495..807d3c4f71 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java @@ -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; @@ -552,7 +553,7 @@ public long now() { } @Override - public V option(ConfigOption option) { + public V option(TypedOption option) { this.verifyAnyPermission(); return this.hugegraph.option(option); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java index ed26bbb5d0..e9b93f4d86 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java @@ -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; @@ -170,7 +171,7 @@ public interface HugeGraph extends Graph { public long now(); - public V option(ConfigOption option); + public V option(TypedOption option); public void registerRpcServices(RpcServiceConfig4Server serverConfig, RpcServiceConfig4Client clientConfig); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java index 1a3e87547c..a45aeb7664 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java @@ -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; @@ -116,7 +117,7 @@ public class StandardHugeGraph implements HugeGraph { StandardHugeGraph.SysTransaction.class }; - public static final Set> ALLOWED_CONFIGS = ImmutableSet.of( + public static final Set> ALLOWED_CONFIGS = ImmutableSet.of( CoreOptions.TASK_WAIT_TIMEOUT, CoreOptions.TASK_SYNC_DELETION, CoreOptions.TASK_TTL_DELETE_BATCH, @@ -978,7 +979,7 @@ public long now() { } @Override - public V option(ConfigOption option) { + public V option(TypedOption option) { HugeConfig config = this.configuration(); if (!ALLOWED_CONFIGS.contains(option)) { throw new NotAllowException("Not allowed to access config: %s", diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java index 60eede09a8..4c90d4db0d 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java @@ -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 { @@ -624,12 +625,13 @@ public static synchronized CoreOptions instance() { 10 ); - public static final ConfigOption OLTP_COLLECTION_IMPL_TYPE = - new ConfigOption<>( + public static final ConfigConvOption 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" ); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java index c707983d8d..63ae52b5e3 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeVertex.java @@ -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; @@ -223,7 +223,11 @@ public Collection 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) { @@ -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); } @@ -642,6 +646,14 @@ public static HugeVertex create(final GraphTransaction tx, return new HugeVertex4Insert(tx, id, label); } + private static Set newSet() { + return CollectionFactory.newSet(CollectionType.EC); + } + + private static List newList() { + return CollectionFactory.newList(CollectionType.EC); + } + private static final class HugeVertex4Insert extends HugeVertex { private GraphTransaction tx; @@ -649,6 +661,10 @@ private static final class HugeVertex4Insert extends HugeVertex { 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); @@ -679,9 +695,5 @@ protected GraphTransaction tx() { } return null; } - - private static Set newSet() { - return CollectionFactory.newSet(CollectionImplType.EC); - } } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java index a9129e8df8..797e97cf49 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java @@ -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; @@ -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 adjacentVertices(Set vertices, Directions dir, @@ -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()); @@ -585,7 +568,7 @@ public Id crosspoint() { return this.crosspoint; } - public void addToEnd(Id id) { + public void addToLast(Id id) { this.vertices.add(id); } 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 6846ae8f7a..543070f7cd 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 @@ -36,8 +36,6 @@ public class KneighborTraverser extends OltpTraverser { - private volatile boolean stop; - public KneighborTraverser(HugeGraph graph) { super(graph); } @@ -88,14 +86,13 @@ public KneighborRecords customizedKneighbor(Id source, EdgeStep step, source, true); Consumer consumer = v -> { - if (this.stop) { + if (this.reachLimit(limit, records.size())) { return; } Iterator 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()); } }; @@ -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; } } 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 edc0399de0..afcf9dc32d 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 @@ -38,7 +38,6 @@ public class KoutTraverser extends OltpTraverser { private int depth; - private volatile boolean stop = false; public KoutTraverser(HugeGraph graph) { super(graph); @@ -117,16 +116,15 @@ public KoutRecords customizedKout(Id source, EdgeStep step, source, nearest); Consumer consumer = v -> { - if (this.stop) { + if (this.reachLimit(limit, this.depth, records.size())) { return; } Iterator 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()); } }; @@ -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; } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/OltpTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/OltpTraverser.java index 0837ff5604..af4a73cc39 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/OltpTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/OltpTraverser.java @@ -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; @@ -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; @@ -81,63 +76,6 @@ public static void destroy() { } } - protected Set adjacentVertices(Set latest, EdgeStep step, - Set 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 adjacentVertices(Set vertices, EdgeStep step, - Set excluded, long remaining) { - Set neighbors = newSet(); - for (Node source : vertices) { - Iterator 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 adjacentVertices(Set vertices, EdgeStep step, - Set excluded, - AtomicLong remaining) { - Set neighbors = ConcurrentHashMap.newKeySet(); - this.traverseNodes(vertices.iterator(), v -> { - Iterator 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 vertices, - Consumer consumer) { - return this.traverse(vertices, consumer, "traverse-nodes"); - } - protected long traversePairs(Iterator> pairs, Consumer> consumer) { return this.traverse(pairs, consumer, "traverse-pairs"); 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 a6bc4b7a73..bae50817fe 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 @@ -178,7 +178,7 @@ private PathSet linkPath(Stack all, int id, int layerIndex) { iter.remove(); continue; } - path.addToEnd(sid); + path.addToLast(sid); } results.addAll(paths); 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 5302719b09..24762006b6 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 @@ -24,34 +24,29 @@ import java.util.Set; import com.baidu.hugegraph.backend.id.Id; -import com.baidu.hugegraph.traversal.algorithm.HugeTraverser; 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.RecordType; -import com.baidu.hugegraph.type.define.CollectionImplType; +import com.baidu.hugegraph.type.define.CollectionType; import com.baidu.hugegraph.util.collection.CollectionFactory; -import com.google.common.collect.ImmutableList; public class KneighborRecords extends SingleWayMultiPathsRecords { - private final Id source; - public KneighborRecords(RecordType type, boolean concurrent, Id source, boolean nearest) { super(type, concurrent, source, nearest); - this.source = source; } + @Override public int size() { return (int) this.accessed(); } public Set ids(long limit) { - Set ids = CollectionFactory.newIdSet(CollectionImplType.EC); - ids.add(this.source); + Set ids = CollectionFactory.newIdSet(CollectionType.EC); for (int i = 1; i < this.records.size(); i++) { IntIterator iterator = this.records.get(i).keys(); - while ((limit == NO_LIMIT || limit > 1L) && iterator.hasNext()) { + while ((limit == NO_LIMIT || limit > 0L) && iterator.hasNext()) { ids.add(this.id(iterator.next())); limit--; } @@ -61,10 +56,9 @@ public Set ids(long limit) { public PathSet paths(long limit) { PathSet paths = new PathSet(); - paths.add(new HugeTraverser.Path(ImmutableList.of(this.source))); for (int i = 1; i < this.records.size(); i++) { IntIterator iterator = this.records.get(i).keys(); - while ((limit == NO_LIMIT || limit > 1L) && iterator.hasNext()) { + 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 41d6a44086..1302e9612a 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 @@ -27,7 +27,7 @@ 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.RecordType; -import com.baidu.hugegraph.type.define.CollectionImplType; +import com.baidu.hugegraph.type.define.CollectionType; import com.baidu.hugegraph.util.collection.CollectionFactory; public class KoutRecords extends SingleWayMultiPathsRecords { @@ -44,7 +44,7 @@ public int size() { public Set ids(long limit) { IntIterator iterator = this.records.peek().keys(); - Set ids = CollectionFactory.newIdSet(CollectionImplType.EC); + Set ids = CollectionFactory.newIdSet(CollectionType.EC); while ((limit == NO_LIMIT || limit-- > 0L) && iterator.hasNext()) { ids.add(this.id(iterator.next())); } 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 e9185d874f..2b53d9ed0c 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 @@ -41,7 +41,7 @@ import com.baidu.hugegraph.traversal.algorithm.records.record.Record; import com.baidu.hugegraph.traversal.algorithm.records.record.RecordType; -public class SingleWayMultiPathsRecords extends AbstractRecords { +public abstract class SingleWayMultiPathsRecords extends AbstractRecords { protected final Stack records; @@ -127,9 +127,7 @@ public void addPath(Id source, Id target) { this.accessedVertices.add(targetCode); } - public int size() { - return 0; - } + public abstract int size(); public Path getPath(int target) { List ids = new ArrayList<>(); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/CollectionImplType.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/CollectionType.java similarity index 88% rename from hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/CollectionImplType.java rename to hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/CollectionType.java index db2bcfc27e..0d02fbb517 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/CollectionImplType.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/CollectionType.java @@ -19,7 +19,7 @@ package com.baidu.hugegraph.type.define; -public enum CollectionImplType implements SerialEnum { +public enum CollectionType implements SerialEnum { // Java Collection Framework JCF(1, "jcf"), @@ -34,10 +34,10 @@ public enum CollectionImplType implements SerialEnum { private final String name; static { - SerialEnum.register(CollectionImplType.class); + SerialEnum.register(CollectionType.class); } - CollectionImplType(int code, String name) { + CollectionType(int code, String name) { assert code < 256; this.code = (byte) code; this.name = name; @@ -51,7 +51,7 @@ public String string() { return this.name; } - public static CollectionImplType fromCode(byte code) { + public static CollectionType fromCode(byte code) { switch (code) { case 1: return JCF; 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 54b928af69..f3aaaf1a62 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 @@ -35,7 +35,7 @@ import org.eclipse.collections.impl.set.mutable.UnifiedSet; import com.baidu.hugegraph.backend.id.Id; -import com.baidu.hugegraph.type.define.CollectionImplType; +import com.baidu.hugegraph.type.define.CollectionType; import com.baidu.hugegraph.util.E; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; @@ -44,13 +44,13 @@ public class CollectionFactory { - private CollectionImplType type; + private CollectionType type; public CollectionFactory() { - this.type = CollectionImplType.EC; + this.type = CollectionType.EC; } - public CollectionFactory(CollectionImplType type) { + public CollectionFactory(CollectionType type) { this.type = type; } @@ -66,7 +66,7 @@ public List newList(Collection collection) { return newList(this.type, collection); } - public static List newList(CollectionImplType type) { + public static List newList(CollectionType type) { switch (type) { case EC: return new FastList<>(); @@ -80,7 +80,7 @@ public static List newList(CollectionImplType type) { } } - public static List newList(CollectionImplType type, + public static List newList(CollectionType type, int initialCapacity) { switch (type) { case EC: @@ -95,7 +95,7 @@ public static List newList(CollectionImplType type, } } - public static List newList(CollectionImplType type, + public static List newList(CollectionType type, Collection collection) { switch (type) { case EC: @@ -122,7 +122,7 @@ public Set newSet(Collection collection) { return newSet(this.type, collection); } - public static Set newSet(CollectionImplType type) { + public static Set newSet(CollectionType type) { switch (type) { case EC: return new UnifiedSet<>(); @@ -136,7 +136,7 @@ public static Set newSet(CollectionImplType type) { } } - public static Set newSet(CollectionImplType type, + public static Set newSet(CollectionType type, int initialCapacity) { switch (type) { case EC: @@ -151,7 +151,7 @@ public static Set newSet(CollectionImplType type, } } - public static Set newSet(CollectionImplType type, + public static Set newSet(CollectionType type, Collection collection) { switch (type) { case EC: @@ -174,7 +174,11 @@ public Map newMap(int initialCapacity) { return newMap(this.type, initialCapacity); } - public static Map newMap(CollectionImplType type) { + public static Map newMap(CollectionType type) { + /* + * EC is faster 10%-20% than JCF, and it's more stable & less + * memory cost(size is bigger, EC is better). + */ switch (type) { case EC: return new UnifiedMap<>(); @@ -188,7 +192,7 @@ public static Map newMap(CollectionImplType type) { } } - public static Map newMap(CollectionImplType type, + public static Map newMap(CollectionType type, int initialCapacity) { switch (type) { case EC: @@ -256,7 +260,7 @@ public Set newIdSet() { return newIdSet(this.type); } - public static Set newIdSet(CollectionImplType type) { + public static Set newIdSet(CollectionType type) { return new IdSet(type); } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/IdSet.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/IdSet.java index d1c38f429a..ea1860691c 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/IdSet.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/IdSet.java @@ -31,7 +31,7 @@ import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.backend.id.IdGenerator; import com.baidu.hugegraph.iterator.ExtendableIterator; -import com.baidu.hugegraph.type.define.CollectionImplType; +import com.baidu.hugegraph.type.define.CollectionType; import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; @@ -40,7 +40,7 @@ public class IdSet extends AbstractSet { private LongHashSet numberIds; private Set nonNumberIds; - public IdSet(CollectionImplType type) { + public IdSet(CollectionType type) { this.numberIds = new LongHashSet(); switch (type) { case JCF: