Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
10 changes: 5 additions & 5 deletions hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -225,7 +225,7 @@ public void truncateBackend() {
this.waitUntilAllTasksCompleted();

this.storeProvider.truncate();
this.initBackendStoreInfo();
this.initBackendStoreSystemInfo();
}

private void waitUntilAllTasksCompleted() {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,36 @@
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);

private static final String PK_BACKEND_INFO = Hidden.hide("backend_info");

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<String, Object> info() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,7 @@ private <V extends HugeElement> Iterator<V> joinTxRecords(
Map<Id, V> addedTxRecords,
Map<Id, V> removedTxRecords,
Map<Id, V> updatedTxRecords) {
this.checkOwnerThread();
// Return the origin results if there is no change in tx
if (addedTxRecords.isEmpty() &&
removedTxRecords.isEmpty() &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -263,11 +264,7 @@ public <V> HugeTask<V> findTask(Id id) {
}

public <V> Iterator<HugeTask<V>> findTasks(List<Id> ids) {
Object[] idArray = ids.toArray(new Id[ids.size()]);
return this.call(() -> {
Iterator<Vertex> vertices = this.tx().queryVertices(idArray);
return new MapperIterator<>(vertices, HugeTask::fromVertex);
});
return this.queryTask(ids);
}

public <V> Iterator<HugeTask<V>> findAllTask(long limit) {
Expand Down Expand Up @@ -375,8 +372,22 @@ private <V> Iterator<HugeTask<V>> queryTask(Map<String, Object> conditions,
query.limit(limit);
}
Iterator<Vertex> vertices = this.tx().queryVertices(query);
return new MapperIterator<>(vertices, HugeTask::fromVertex);
});
Iterator<HugeTask<V>> tasks =
new MapperIterator<>(vertices, HugeTask::fromVertex);
// Convert iterator to list to avoid across thread tx accessed
return IteratorUtils.list(tasks);
}).iterator();
}

private <V> Iterator<HugeTask<V>> queryTask(List<Id> ids) {
return this.call(() -> {
Object[] idArray = ids.toArray(new Id[ids.size()]);
Iterator<Vertex> vertices = this.tx().queryVertices(idArray);
Iterator<HugeTask<V>> tasks =
new MapperIterator<>(vertices, HugeTask::fromVertex);
// Convert iterator to list to avoid across thread tx accessed
return IteratorUtils.list(tasks);
}).iterator();
}

private <V> V call(Runnable runnable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 '{}' " +
Expand Down