Skip to content

Commit

Permalink
Allow deletion of specific language. (dgraph-io#3242)
Browse files Browse the repository at this point in the history
This change introduces the syntax "<s> <p@lang> * ." to allow users to
delete a specific language tagged value without having to specify the
current value.
  • Loading branch information
martinmr authored and dna2github committed Jul 19, 2019
1 parent f1afc6d commit 8966a2c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
20 changes: 20 additions & 0 deletions chunker/rdf/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ var testNQuads = []struct {
ObjectValue: &api.Value{Val: &api.Value_DefaultVal{DefaultVal: "Alice In Wonderland"}},
},
},
{
input: `_:alice <name@en> "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 <name@en> * .`,
nq: api.NQuad{
Subject: "_:alice",
Predicate: "name",
ObjectId: "",
Lang: "en",
ObjectValue: &api.Value{Val: &api.Value_DefaultVal{DefaultVal: "_STAR_ALL"}},
},
},
{
input: `_:alice <name> "Alice In Wonderland"^^<xs:string> .`,
nq: api.NQuad{
Expand Down
44 changes: 44 additions & 0 deletions dgraph/cmd/alpha/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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> <name> "Mark"@en .
<0x12345> <name> "Marco"@es .
<0x12345> <name> "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> <name@fr> * .
}
}
`
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 = `
{
Expand Down
2 changes: 1 addition & 1 deletion posting/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions wiki/content/mutations/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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> <name@es> * .
}
}
```

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`.
Expand Down

0 comments on commit 8966a2c

Please sign in to comment.