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/store/BackendStoreInfo.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreSystemInfo.java similarity index 88% 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..816e28315a 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,23 @@ 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 to reserve primitive system id + schema.setNextIdLowest(HugeType.SYS_SCHEMA, + SchemaElement.MAX_PRIMITIVE_SYS_ID); } private Map info() { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java index eed0a583fa..5012038999 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java @@ -1223,6 +1223,7 @@ private Iterator joinTxRecords( Map addedTxRecords, Map removedTxRecords, Map updatedTxRecords) { + this.checkOwnerThread(); // Return the origin results if there is no change in tx if (addedTxRecords.isEmpty() && removedTxRecords.isEmpty() && 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..cc30819870 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_PRIMITIVE_SYS_ID + 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,21 @@ 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.NEXT_PRIMITIVE_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..2ebeeb89a7 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_PRIMITIVE_SYS_ID = 32; + public static final int NEXT_PRIMITIVE_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..b438b87e0b 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 @@ -33,6 +33,7 @@ import org.apache.tinkerpop.gremlin.structure.Graph.Hidden; import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils; import com.baidu.hugegraph.HugeException; import com.baidu.hugegraph.HugeGraph; @@ -263,11 +264,7 @@ public HugeTask findTask(Id id) { } public Iterator> findTasks(List ids) { - Object[] idArray = ids.toArray(new Id[ids.size()]); - return this.call(() -> { - Iterator vertices = this.tx().queryVertices(idArray); - return new MapperIterator<>(vertices, HugeTask::fromVertex); - }); + return this.queryTask(ids); } public Iterator> findAllTask(long limit) { @@ -375,8 +372,22 @@ private Iterator> queryTask(Map conditions, query.limit(limit); } Iterator vertices = this.tx().queryVertices(query); - return new MapperIterator<>(vertices, HugeTask::fromVertex); - }); + Iterator> tasks = + new MapperIterator<>(vertices, HugeTask::fromVertex); + // Convert iterator to list to avoid across thread tx accessed + return IteratorUtils.list(tasks); + }).iterator(); + } + + private Iterator> queryTask(List ids) { + return this.call(() -> { + Object[] idArray = ids.toArray(new Id[ids.size()]); + Iterator vertices = this.tx().queryVertices(idArray); + Iterator> tasks = + new MapperIterator<>(vertices, HugeTask::fromVertex); + // Convert iterator to list to avoid across thread tx accessed + return IteratorUtils.list(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 '{}' " +