Skip to content

Commit

Permalink
fix: left index of system type can't be deleted (#675)
Browse files Browse the repository at this point in the history
Change-Id: I654b2e4c69f8aebfc7ebed3225b2dd09da1720a0
  • Loading branch information
javeme authored Sep 11, 2019
1 parent d47f309 commit 64f9409
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,6 @@ public void rollbackTx() {
}
}

@Override
public String toString() {
return this.store;
}

protected Cluster cluster() {
return this.sessions.cluster();
}
Expand Down
56 changes: 43 additions & 13 deletions hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,17 @@ private SchemaTransaction openSchemaTransaction() throws HugeException {
}
}

private GraphTransaction openSystemTransaction() throws HugeException {
this.checkGraphNotClosed();
try {
return new CachedGraphTransaction(this, this.loadSystemStore());
} catch (BackendException e) {
String message = "Failed to open system transaction";
LOG.error("{}", message, e);
throw new HugeException(message);
}
}

private GraphTransaction openGraphTransaction() throws HugeException {
this.checkGraphNotClosed();
try {
Expand Down Expand Up @@ -309,6 +320,16 @@ public SchemaTransaction schemaTransaction() {
return this.tx.schemaTransaction();
}

public GraphTransaction systemTransaction() {
this.checkGraphNotClosed();
/*
* NOTE: system operations must be committed manually,
* Maybe users need to auto open tinkerpop tx by readWrite().
*/
this.tx.readWrite();
return this.tx.systemTransaction();
}

public GraphTransaction graphTransaction() {
this.checkGraphNotClosed();
/*
Expand Down Expand Up @@ -726,6 +747,10 @@ private SchemaTransaction schemaTransaction() {
return this.getOrNewTransaction().schemaTx;
}

private GraphTransaction systemTransaction() {
return this.getOrNewTransaction().systemTx;
}

private GraphTransaction graphTransaction() {
return this.getOrNewTransaction().graphTx;
}
Expand All @@ -740,7 +765,8 @@ private Txs getOrNewTransaction() {
Txs txs = this.transactions.get();
if (txs == null) {
// TODO: close SchemaTransaction if GraphTransaction is error
txs = new Txs(openSchemaTransaction(), openGraphTransaction());
txs = new Txs(openSchemaTransaction(), openSystemTransaction(),
openGraphTransaction());
this.transactions.set(txs);
}
return txs;
Expand All @@ -763,26 +789,24 @@ private void destroyTransaction() {

private static final class Txs {

public final SchemaTransaction schemaTx;
public final GraphTransaction graphTx;
private final SchemaTransaction schemaTx;
private final GraphTransaction systemTx;
private final GraphTransaction graphTx;

public Txs(SchemaTransaction schemaTx, GraphTransaction graphTx) {
assert schemaTx != null && graphTx != null;
public Txs(SchemaTransaction schemaTx, GraphTransaction systemTx,
GraphTransaction graphTx) {
assert schemaTx != null && systemTx != null && graphTx != null;
this.schemaTx = schemaTx;
this.systemTx = systemTx;
this.graphTx = graphTx;
}

public void commit() {
this.schemaTx.commit();
this.graphTx.commit();
}

public void rollback() {
try {
this.schemaTx.rollback();
} finally {
this.graphTx.rollback();
}
this.graphTx.rollback();
}

public void close() {
Expand All @@ -792,6 +816,12 @@ public void close() {
LOG.error("Failed to close GraphTransaction", e);
}

try {
this.systemTx.close();
} catch (Exception e) {
LOG.error("Failed to close SystemTransaction", e);
}

try {
this.schemaTx.close();
} catch (Exception e) {
Expand All @@ -801,8 +831,8 @@ public void close() {

@Override
public String toString() {
return String.format("{schemaTx=%s,graphTx=%s}",
this.schemaTx, this.graphTx);
return String.format("{schemaTx=%s,systemTx=%s,graphTx=%s}",
this.schemaTx, this.systemTx, this.graphTx);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public <R> R metadata(HugeType type, String meta, Object[] args) {
return dispatcher.dispatchMetaHandler(session, meta, args);
}

@Override
public String toString() {
return String.format("%s/%s", this.database(), this.store());
}

protected abstract BackendTable<Session, ?> table(HugeType type);

// NOTE: Need to support passing null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,6 @@ public BackendFeatures features() {
return FEATURES;
}

@Override
public String toString() {
return this.store;
}

/***************************** Store defines *****************************/

public static class InMemorySchemaStore extends InMemoryDBStore {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public GraphIndexTransaction(HugeGraph graph, BackendStore store) {

protected Id asyncRemoveIndexLeft(ConditionQuery query,
HugeElement element) {
RemoveLeftIndexJob callable = new RemoveLeftIndexJob(query, element);
RemoveLeftIndexJob job = new RemoveLeftIndexJob(query, element);
HugeTask<?> task = EphemeralJobBuilder.of(this.graph())
.name(element.id().asString())
.job(callable)
.job(job)
.schedule();
return task.id();
}
Expand Down Expand Up @@ -1291,16 +1291,16 @@ public static class RemoveLeftIndexJob extends EphemeralJob<Object> {

private static final String REMOVE_LEFT_INDEX = "remove_left_index";

private ConditionQuery query;
private HugeElement element;
private final ConditionQuery query;
private final HugeElement element;
private GraphIndexTransaction tx;

private RemoveLeftIndexJob(ConditionQuery query,
HugeElement element) {
private RemoveLeftIndexJob(ConditionQuery query, HugeElement element) {
E.checkArgumentNotNull(query, "query");
E.checkArgumentNotNull(element, "element");
this.query = query;
this.element = element;
this.tx = null;
}

@Override
Expand All @@ -1310,7 +1310,9 @@ public String type() {

@Override
public Object execute() {
this.tx = this.graph().graphTransaction().indexTransaction();
this.tx = this.element.schemaLabel().system() ?
this.graph().systemTransaction().indexTransaction() :
this.graph().graphTransaction().indexTransaction();
return this.removeIndexLeft(this.query, this.element);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ public void status(SchemaStatus status) {
this.status = status;
}

public boolean system() {
return this.longId() < 0L;
}

public boolean primitive() {
return false;
long id = this.longId();
return -MAX_PRIMITIVE_SYS_ID <= id && id < 0L;
}

public boolean hidden() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,6 @@ public BackendFeatures features() {
return FEATURES;
}

@Override
public String toString() {
return this.store;
}

protected void initTables() {
Session session = this.sessions.session();
for (MysqlTable table : this.tables()) {
Expand Down

0 comments on commit 64f9409

Please sign in to comment.