From ad8927f9587a03f1942fea89090ab89eb968693f Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Wed, 3 Apr 2019 17:56:13 -0700 Subject: [PATCH] Allow deletion of specific language. (#3242) This change introduces the syntax " * ." to allow users to delete a specific language tagged value without having to specify the current value. --- chunker/rdf/parse_test.go | 20 +++++++++++++++ dgraph/cmd/alpha/run_test.go | 44 +++++++++++++++++++++++++++++++++ posting/list.go | 2 +- wiki/content/mutations/index.md | 13 ++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/chunker/rdf/parse_test.go b/chunker/rdf/parse_test.go index f5f2c4f834c..101fe390238 100644 --- a/chunker/rdf/parse_test.go +++ b/chunker/rdf/parse_test.go @@ -114,6 +114,26 @@ var testNQuads = []struct { ObjectValue: &api.Value{Val: &api.Value_DefaultVal{DefaultVal: "Alice In Wonderland"}}, }, }, + { + input: `_:alice "Alice In Wonderland" .`, + nq: api.NQuad{ + Subject: "_:alice", + Predicate: "name", + ObjectId: "", + Lang: "en", + ObjectValue: &api.Value{Val: &api.Value_DefaultVal{DefaultVal: "Alice In Wonderland"}}, + }, + }, + { + input: `_:alice * .`, + nq: api.NQuad{ + Subject: "_:alice", + Predicate: "name", + ObjectId: "", + Lang: "en", + ObjectValue: &api.Value{Val: &api.Value_DefaultVal{DefaultVal: "_STAR_ALL"}}, + }, + }, { input: `_:alice "Alice In Wonderland"^^ .`, nq: api.NQuad{ diff --git a/dgraph/cmd/alpha/run_test.go b/dgraph/cmd/alpha/run_test.go index f12615ebd1c..27f8682bdbf 100644 --- a/dgraph/cmd/alpha/run_test.go +++ b/dgraph/cmd/alpha/run_test.go @@ -1469,6 +1469,50 @@ func TestDeleteScalarValue(t *testing.T) { require.JSONEq(t, `{"data": {"me":[]}}`, output) } +func TestDeleteValueLang(t *testing.T) { + var s = `name: string @lang .` + require.NoError(t, schema.ParseBytes([]byte(""), 1)) + require.NoError(t, alterSchemaWithRetry(s)) + + var m = ` + { + set { + <0x12345> "Mark"@en . + <0x12345> "Marco"@es . + <0x12345> "Marc"@fr . + } + } + ` + err := runMutation(m) + require.NoError(t, err) + + q := ` + { + me(func: uid(0x12345)) { + name@* + } + }` + output, err := runQuery(q) + require.NoError(t, err) + require.JSONEq(t, `{"data": {"me":[ + {"name@en":"Mark", "name@es":"Marco", "name@fr":"Marc"}]}}`, output) + + var d1 = ` + { + delete { + <0x12345> * . + } + } + ` + err = runMutation(d1) + require.NoError(t, err) + + // Verify only the specific tagged value was deleted. + output, err = runQuery(q) + require.NoError(t, err) + require.JSONEq(t, output, `{"data": {"me":[{"name@en":"Mark", "name@es":"Marco"}]}}`) +} + func TestDropAll(t *testing.T) { var m1 = ` { diff --git a/posting/list.go b/posting/list.go index aade0d7405d..44862af583c 100644 --- a/posting/list.go +++ b/posting/list.go @@ -190,7 +190,7 @@ func (l *List) SetForDeletion() bool { } func hasDeleteAll(mpost *pb.Posting) bool { - return mpost.Op == Del && bytes.Equal(mpost.Value, []byte(x.Star)) + return mpost.Op == Del && bytes.Equal(mpost.Value, []byte(x.Star)) && len(mpost.LangTag) == 0 } // Ensure that you either abort the uncommitted postings or commit them before calling me. diff --git a/wiki/content/mutations/index.md b/wiki/content/mutations/index.md index 39c6aff3df8..3b2bda2dcc2 100644 --- a/wiki/content/mutations/index.md +++ b/wiki/content/mutations/index.md @@ -268,6 +268,19 @@ Deleting the value of a non-list predicate (i.e a 1-to-1 relation) can be done i 1. Using the star notation mentioned in the last section. 1. Setting the object to a specific value. If the value passed is not the current value, the mutation will succeed but will have no effect. If the value passed is the current value, the mutation will succeed and will delete the triple. +For language-tagged values, the following special syntax is supported: + +``` +{ + delete { + <0x12345> * . + } +} +``` + +In this example, the value of name tagged with language tag `es` will be deleted. +Other tagged values are left untouched. + ## Mutations using cURL Mutations can be done over HTTP by making a `POST` request to an Alpha's `/mutate` endpoint. On the command line this can be done with curl. To commit the mutation, pass the HTTP header `X-DgraphCommitNow: true`.