Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for upsert block #88

Merged
merged 2 commits into from
Jun 26, 2019
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
15 changes: 14 additions & 1 deletion src/main/proto/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ message Mutation {
bytes delete_json = 2;
bytes set_nquads = 3;
bytes del_nquads = 4;
string query = 5;

repeated NQuad set = 10;
repeated NQuad del = 11;
Expand All @@ -79,11 +80,23 @@ message Mutation {
bool ignore_index_conflict = 15; // this field is not parsed and used by the server anymore.
}


message Operation {
string schema = 1;
string drop_attr = 2;
bool drop_all = 3;

enum DropOp {
NONE = 0;
ALL = 1;
DATA = 2;
ATTR = 3;
TYPE = 4;
}
DropOp drop_op = 4;

// If drop_op is ATTR or TYPE, drop_value holds the name of the predicate or
// type to delete.
string drop_value = 5;
}

// Worker services.
Expand Down
97 changes: 97 additions & 0 deletions src/test/java/io/dgraph/UpsertBlockTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package io.dgraph;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.protobuf.ByteString;
import org.testng.annotations.Test;
import io.dgraph.DgraphProto.*;

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

public class UpsertBlockTest extends DgraphIntegrationTest {
@Test
public void upsertBlockTest() throws Exception {
DgraphProto.Operation op =
DgraphProto.Operation.newBuilder()
.setSchema("email: string @index(exact) @upsert .").build();
dgraphClient.alter(op);

String query = "" +
"{\n" +
" me(func: eq(email, \"[email protected]\")) {\n" +
" v as uid\n" +
" }\n" +
"}\n";

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

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

Mutation mu = Mutation.newBuilder()
.setQuery(query)
.setSetJson(ByteString.copyFromUtf8(jA.toString()))
.build();

Transaction txn = dgraphClient.newTransaction();
txn.mutate(mu);
txn.commit();

String query2 = "" +
"{\n" +
" me(func: eq(email, \"[email protected]\")) {\n" +
" name\n" +
" email\n" +
" }\n" +
"}\n";

txn = dgraphClient.newTransaction();
Response response = txn.query(query2);
String res = response.getJson().toStringUtf8();
String exp1 = "{\"me\":[{\"name\":\"wrong\",\"email\":\"[email protected]\"}]}";
assertEquals(res, exp1);

jA = new JsonArray();
p1 = new JsonObject();
p1.addProperty("uid", "uid(v)");
p1.addProperty("name", "ashish");
jA.add(p1);

mu = Mutation.newBuilder()
.setQuery(query)
.setSetJson(ByteString.copyFromUtf8(jA.toString()))
.build();

txn = dgraphClient.newTransaction();
txn.mutate(mu);
txn.commit();

txn = dgraphClient.newTransaction();
response = txn.query(query2);
res = response.getJson().toStringUtf8();
String exp2 = "{\"me\":[{\"name\":\"ashish\",\"email\":\"[email protected]\"}]}";
assertEquals(res, exp2);

mu = Mutation.newBuilder()
.setQuery(query)
.setDelNquads(ByteString.copyFromUtf8("uid(v) <name> * .\nuid(v) <email> * ."))
.build();

txn = dgraphClient.newTransaction();
txn.mutate(mu);
txn.commit();

txn = dgraphClient.newTransaction();
response = txn.query(query2);
res = response.getJson().toStringUtf8();
String exp3 = "{\"me\":[]}";
assertEquals(res, exp3);
}
}