Skip to content

Commit

Permalink
Bugfix: Set found to false to avoid deleting index edge when value do…
Browse files Browse the repository at this point in the history
…esn't match. (#3843)

Return found as false to avoid deleting index corresponding to a uid when the value doesn't match. For scalar values, after fingerprinting found is always true as values get fingerprinted to math.MaxUint64. Hence we return found as false from this second check. Fixes #3803.
  • Loading branch information
pawanrawal authored Aug 21, 2019
1 parent 1dcfbf1 commit 2f18d10
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
21 changes: 19 additions & 2 deletions dgraph/cmd/alpha/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1294,14 +1294,15 @@ func TestDeleteAllSP2(t *testing.T) {
}

func TestDeleteScalarValue(t *testing.T) {
var s = `name: string .`
var s = `name: string @index(exact) .`
require.NoError(t, schema.ParseBytes([]byte(""), 1))
require.NoError(t, alterSchemaWithRetry(s))

var m = `
{
set {
<0x12345> <name> "xxx" .
<0x12345> <name> "xxx" .
<0x12346> <name> "xxx" .
}
}
`
Expand Down Expand Up @@ -1342,6 +1343,17 @@ func TestDeleteScalarValue(t *testing.T) {
require.NoError(t, err)
require.JSONEq(t, `{"data": {"me":[{"name":"xxx"}]}}`, output)

indexQuery := `
{
me(func: eq(name, "xxx")) {
name
}
}
`
output, err = runGraphqlQuery(indexQuery)
require.NoError(t, err)
require.JSONEq(t, `{"data": {"me":[{"name":"xxx"}, {"name":"xxx"}]}}`, output)

var d2 = `
{
delete {
Expand All @@ -1356,6 +1368,11 @@ func TestDeleteScalarValue(t *testing.T) {
output, err = runGraphqlQuery(q)
require.NoError(t, err)
require.JSONEq(t, `{"data": {"me":[]}}`, output)

// Verify index was also updated this time and one of the triples got deleted.
output, err = runGraphqlQuery(indexQuery)
require.NoError(t, err)
require.JSONEq(t, `{"data": {"me":[{"name": "xxx"}]}}`, output)
}

func TestDeleteValueLang(t *testing.T) {
Expand Down
8 changes: 7 additions & 1 deletion posting/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,15 @@ func (txn *Txn) addMutationHelper(ctx context.Context, l *List, doUpdateIndex bo
return val, found, emptyCountParams, err
}

// This is a scalar value of non-list type and a delete edge mutation, so if the value
// given by the user doesn't match the value we have, we return found to be false, to avoid
// deleting the uid from index posting list.
// This second check is required because we fingerprint the scalar values as math.MaxUint64,
// so even though they might be different the check in the doUpdateIndex block above would
// return found to be true.
if pFound && !(bytes.Equal(currPost.Value, newPost.Value) &&
types.TypeID(currPost.ValType) == types.TypeID(newPost.ValType)) {
return val, found, emptyCountParams, err
return val, false, emptyCountParams, nil
}
}

Expand Down

0 comments on commit 2f18d10

Please sign in to comment.