Skip to content

Commit

Permalink
Fixed running multiple upserts within one transaction (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grandys authored Mar 30, 2020
1 parent 1324663 commit 2b82ed4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main/java/io/dgraph/AsyncTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,21 @@ public CompletableFuture<Response> doRequest(Request request) {
mutated = true;
}

Request requestStartTs = Request.newBuilder(request).setStartTs(context.getStartTs()).build();

return client
.runWithRetries(
"doRequest",
() -> {
StreamObserverBridge<Response> bridge = new StreamObserverBridge<>();
DgraphStub localStub = client.getStubWithJwt(stub);
localStub.query(request, bridge);
localStub.query(requestStartTs, bridge);

return bridge
.getDelegate()
.thenApply(
(response) -> {
if (request.getCommitNow()) {
if (requestStartTs.getCommitNow()) {
finished = true;
}
mergeContext(response.getTxn());
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/io/dgraph/UpsertBlockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
Expand Down Expand Up @@ -307,6 +308,47 @@ public void upsertZeroMutations() {
}
}

@Test
public void upsertMultipleWithinSingleTransactionTest() {
DgraphProto.Operation op =
DgraphProto.Operation.newBuilder()
.setSchema("email: string @index(exact) @upsert .")
.build();
dgraphClient.alter(op);

JsonArray jsonData = new JsonArray();
JsonObject person = new JsonObject();
person.addProperty("uid", "uid(v)");
person.addProperty("name", "wrong");
jsonData.add(person);

JsonObject person2 = new JsonObject();
person2.addProperty("email", "[email protected]");
person2.addProperty("uid", "uid(v)");
jsonData.add(person2);

String query =
"{\n"
+ " me(func: eq(email, \"[email protected]\")) {\n"
+ " v as uid\n"
+ " }\n"
+ "}\n";
Mutation mu =
Mutation.newBuilder().setSetJson(ByteString.copyFromUtf8(jsonData.toString())).build();
Request request = Request.newBuilder().addMutations(mu).setQuery(query).build();

Transaction transaction = dgraphClient.newTransaction();
transaction.doRequest(request);

try {
transaction.doRequest(request);
} catch (RuntimeException e) {
fail(e.getMessage());
}

transaction.discard();
}

static class User {
String branch;
}
Expand Down

0 comments on commit 2b82ed4

Please sign in to comment.