From cc37f3f7819e8a72c2e259449da35cab22b028b6 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Wed, 28 Nov 2018 22:35:43 +0800 Subject: [PATCH] task indexlabel has conflicts with label index or schema name index fixed: #239 Change-Id: I0b429c00032c0cdf80090b8350be90f6f2b3f74b --- .../baidu/hugegraph/core/GraphManager.java | 4 +- .../java/com/baidu/hugegraph/HugeGraph.java | 10 ++-- .../hugegraph/backend/cache/RamCache.java | 2 +- ...eInfo.java => BackendStoreSystemInfo.java} | 10 +++- .../baidu/hugegraph/schema/IndexLabel.java | 54 +++++++++++-------- .../baidu/hugegraph/schema/SchemaElement.java | 3 ++ .../baidu/hugegraph/task/TaskScheduler.java | 8 ++- .../com/baidu/hugegraph/cmd/InitStore.java | 4 +- 8 files changed, 59 insertions(+), 36 deletions(-) rename hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/{BackendStoreInfo.java => BackendStoreSystemInfo.java} (89%) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index 8eaec51295..0bc3f8a33c 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -41,7 +41,7 @@ import com.baidu.hugegraph.auth.HugeGraphAuthProxy; import com.baidu.hugegraph.backend.cache.Cache; import com.baidu.hugegraph.backend.cache.CacheManager; -import com.baidu.hugegraph.backend.store.BackendStoreInfo; +import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo; import com.baidu.hugegraph.backend.store.memory.InMemoryDBStoreProvider; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; @@ -193,7 +193,7 @@ private void checkBackendVersionOrExit() { if (InMemoryDBStoreProvider.matchType(hugegraph.backend())) { hugegraph.initBackend(); } - BackendStoreInfo info = new BackendStoreInfo(hugegraph); + BackendStoreSystemInfo info = new BackendStoreSystemInfo(hugegraph); if (!info.exist()) { LOG.error("The backend store of '{}' has not been initialized", hugegraph.name()); 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 c60522e56f..c225431b2b 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java @@ -48,7 +48,7 @@ import com.baidu.hugegraph.backend.serializer.SerializerFactory; import com.baidu.hugegraph.backend.store.BackendProviderFactory; import com.baidu.hugegraph.backend.store.BackendStore; -import com.baidu.hugegraph.backend.store.BackendStoreInfo; +import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo; import com.baidu.hugegraph.backend.store.BackendStoreProvider; import com.baidu.hugegraph.backend.tx.GraphTransaction; import com.baidu.hugegraph.backend.tx.SchemaTransaction; @@ -196,7 +196,7 @@ public void initBackend() { this.loadGraphStore().open(this.configuration); try { this.storeProvider.init(); - this.initBackendStoreInfo(); + this.initBackendStoreSystemInfo(); } finally { this.loadGraphStore().close(); this.loadSystemStore().close(); @@ -225,7 +225,7 @@ public void truncateBackend() { this.waitUntilAllTasksCompleted(); this.storeProvider.truncate(); - this.initBackendStoreInfo(); + this.initBackendStoreSystemInfo(); } private void waitUntilAllTasksCompleted() { @@ -237,8 +237,8 @@ private void waitUntilAllTasksCompleted() { } } - private void initBackendStoreInfo() { - new BackendStoreInfo(this).init(); + private void initBackendStoreSystemInfo() { + new BackendStoreSystemInfo(this).init(); } private SchemaTransaction openSchemaTransaction() throws HugeException { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/RamCache.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/RamCache.java index 95e89714d4..213b9c838a 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/RamCache.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/RamCache.java @@ -314,7 +314,7 @@ public void tick() { if (expireItems > 0) { LOG.info("Cache expired {} items cost {}ms (size {}, expire {}ms)", - expireItems, now() - current, this.size(), expireTime); + expireItems, now() - current, this.size(), expireTime); } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreInfo.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreSystemInfo.java similarity index 89% rename from hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreInfo.java rename to hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreSystemInfo.java index dcf563dd06..a6ce83303d 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreInfo.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreSystemInfo.java @@ -28,10 +28,12 @@ import com.baidu.hugegraph.backend.BackendException; import com.baidu.hugegraph.backend.tx.SchemaTransaction; import com.baidu.hugegraph.schema.PropertyKey; +import com.baidu.hugegraph.schema.SchemaElement; +import com.baidu.hugegraph.type.HugeType; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.Log; -public class BackendStoreInfo { +public class BackendStoreSystemInfo { private static final Logger LOG = Log.logger(HugeGraph.class); @@ -39,18 +41,22 @@ public class BackendStoreInfo { private final HugeGraph graph; - public BackendStoreInfo(HugeGraph graph) { + public BackendStoreSystemInfo(HugeGraph graph) { this.graph = graph; } public void init() { SchemaTransaction schema = this.graph.schemaTransaction(); + // Use property key to store backend version String backendVersion = this.graph.backendVersion(); PropertyKey backendInfo = this.graph.schema() .propertyKey(PK_BACKEND_INFO) .userdata("version", backendVersion) .build(); schema.addPropertyKey(backendInfo); + + // Set schema counter if needed + schema.setNextIdLowest(HugeType.SYS_SCHEMA, SchemaElement.MAX_SYS_ID); } private Map info() { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/IndexLabel.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/IndexLabel.java index 517903d28e..ef928706ce 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/IndexLabel.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/IndexLabel.java @@ -128,13 +128,23 @@ public void userdata(String key, Object value) { throw new NotSupportException("user data for index label"); } - public static final IndexLabel VL_IL = new IndexLabel(-1, "~vli"); - public static final IndexLabel EL_IL = new IndexLabel(-2, "~eli"); - - public static final IndexLabel PK_NAME_IL = new IndexLabel(-3, "~pkni"); - public static final IndexLabel VL_NAME_IL = new IndexLabel(-4, "~vlni"); - public static final IndexLabel EL_NAME_IL = new IndexLabel(-5, "~elni"); - public static final IndexLabel IL_NAME_IL = new IndexLabel(-6, "~ilni"); + // ABS of System index id must be below SchemaElement.MAX_SYS_ID(32) + private static final int VL_IL_ID = -1; + private static final int EL_IL_ID = -2; + private static final int PKN_IL_ID = -3; + private static final int VLN_IL_ID = -4; + private static final int ELN_IL_ID = -5; + private static final int ILN_IL_ID = -6; + + // Label index + static final IndexLabel VL_IL = new IndexLabel(VL_IL_ID, "~vli"); + static final IndexLabel EL_IL = new IndexLabel(EL_IL_ID, "~eli"); + + // Schema name index + static final IndexLabel PKN_IL = new IndexLabel(PKN_IL_ID, "~pkni"); + static final IndexLabel VLN_IL = new IndexLabel(VLN_IL_ID, "~vlni"); + static final IndexLabel ELN_IL = new IndexLabel(ELN_IL_ID, "~elni"); + static final IndexLabel ILN_IL = new IndexLabel(ILN_IL_ID, "~ilni"); public static IndexLabel label(HugeType type) { switch (type) { @@ -145,13 +155,13 @@ public static IndexLabel label(HugeType type) { case EDGE_IN: return EL_IL; case PROPERTY_KEY: - return PK_NAME_IL; + return PKN_IL; case VERTEX_LABEL: - return VL_NAME_IL; + return VLN_IL; case EDGE_LABEL: - return EL_NAME_IL; + return ELN_IL; case INDEX_LABEL: - return IL_NAME_IL; + return ILN_IL; default: throw new AssertionError(String.format( "No primitive index label for '%s'", type)); @@ -160,20 +170,20 @@ public static IndexLabel label(HugeType type) { public static IndexLabel label(HugeGraph graph, Id id) { // Primitive IndexLabel first - if (id.asLong() < 0) { + if (id.asLong() < 0 && id.asLong() > -SchemaElement.MIN_SYS_ID) { switch ((int) id.asLong()) { - case -1: + case VL_IL_ID: return VL_IL; - case -2: + case EL_IL_ID: return EL_IL; - case -3: - return PK_NAME_IL; - case -4: - return VL_NAME_IL; - case -5: - return EL_NAME_IL; - case -6: - return IL_NAME_IL; + case PKN_IL_ID: + return PKN_IL; + case VLN_IL_ID: + return VLN_IL; + case ELN_IL_ID: + return ELN_IL; + case ILN_IL_ID: + return ILN_IL; default: throw new AssertionError(String.format( "No primitive index label for '%s'", id)); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaElement.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaElement.java index c35d6b0fc5..3662f36ef0 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaElement.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaElement.java @@ -37,6 +37,9 @@ public abstract class SchemaElement implements Namifiable, Typifiable { + public static final int MAX_SYS_ID = 32; + public static final int MIN_SYS_ID = 7; + protected final HugeGraph graph; private final Id id; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/task/TaskScheduler.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/task/TaskScheduler.java index 6f0452a10d..687c52bdfd 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/task/TaskScheduler.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/task/TaskScheduler.java @@ -31,6 +31,7 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeoutException; +import org.apache.commons.collections.IteratorUtils; import org.apache.tinkerpop.gremlin.structure.Graph.Hidden; import org.apache.tinkerpop.gremlin.structure.Vertex; @@ -375,8 +376,11 @@ private Iterator> queryTask(Map conditions, query.limit(limit); } Iterator vertices = this.tx().queryVertices(query); - return new MapperIterator<>(vertices, HugeTask::fromVertex); - }); + @SuppressWarnings("unchecked") + List> tasks = IteratorUtils.toList(new MapperIterator<>( + vertices, HugeTask::fromVertex)); + return tasks; + }).iterator(); } private V call(Runnable runnable) { diff --git a/hugegraph-dist/src/main/java/com/baidu/hugegraph/cmd/InitStore.java b/hugegraph-dist/src/main/java/com/baidu/hugegraph/cmd/InitStore.java index c2e45faea4..23da927b15 100644 --- a/hugegraph-dist/src/main/java/com/baidu/hugegraph/cmd/InitStore.java +++ b/hugegraph-dist/src/main/java/com/baidu/hugegraph/cmd/InitStore.java @@ -30,7 +30,7 @@ import com.baidu.hugegraph.HugeFactory; import com.baidu.hugegraph.HugeGraph; -import com.baidu.hugegraph.backend.store.BackendStoreInfo; +import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo; import com.baidu.hugegraph.config.ServerOptions; import com.baidu.hugegraph.dist.RegisterUtil; import com.baidu.hugegraph.util.E; @@ -93,7 +93,7 @@ private static void initGraph(String config) throws InterruptedException { LOG.info("Init graph with config file: {}", config); HugeGraph graph = HugeFactory.open(config); - BackendStoreInfo backendStoreInfo = new BackendStoreInfo(graph); + BackendStoreSystemInfo backendStoreInfo = new BackendStoreSystemInfo(graph); try { if (backendStoreInfo.exist()) { LOG.info("Skip init-store due to the backend store of '{}' " +