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

Allow overwriting values of predicates of type uid #4411

Merged
merged 5 commits into from
Dec 19, 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
36 changes: 1 addition & 35 deletions dgraph/cmd/alpha/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,41 +388,7 @@ func TestMutationSingleUid(t *testing.T) {
}
}
`
require.Error(t, runMutation(m))
}

// Verify multiple uids are allowed after mutation.
func TestSchemaMutationUid(t *testing.T) {
// reset Schema
require.NoError(t, schema.ParseBytes([]byte(""), 1))

var s1 = `
friend: uid .
`
require.NoError(t, alterSchema(s1))
var m1 = `
{
set {
<0x1> <friend> <0x2> .
<0x1> <friend> <0x3> .
}
}
`
require.Error(t, runMutation(m1))

var s2 = `
friend: [uid] .
`
require.NoError(t, alterSchema(s2))
var m2 = `
{
set {
<0x1> <friend> <0x2> .
<0x1> <friend> <0x3> .
}
}
`
require.NoError(t, runMutation(m2))
require.NoError(t, runMutation(m))
}

// Verify a list uid predicate cannot be converted to a single-element predicate.
Expand Down
4 changes: 0 additions & 4 deletions posting/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,6 @@ func (txn *Txn) addMutationHelper(ctx context.Context, l *List, doUpdateIndex bo
"Acquired lock %v %v %v", dur, t.Attr, t.Entity)
}

if err := l.canMutateUid(txn, t); err != nil {
return val, found, emptyCountParams, err
}

if doUpdateIndex {
// Check original value BEFORE any mutation actually happens.
val, found, err = l.findValue(txn.StartTs, fingerprintEdge(t))
Expand Down
33 changes: 0 additions & 33 deletions posting/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,39 +365,6 @@ func fingerprintEdge(t *pb.DirectedEdge) uint64 {
return id
}

// canMutateUid returns an error if all the following conditions are met.
// * Predicate is of type UidID.
// * Predicate is not set to a list of UIDs in the schema.
// * The existing posting list has an entry that does not match the proposed
// mutation's UID.
// In this case, the user should delete the existing predicate and retry, or mutate
// the schema to allow for multiple UIDs. This method is necessary to support UID
// predicates with single values because previously all UID predicates were
// considered lists.
// This functions returns a nil error in all other cases.
func (l *List) canMutateUid(txn *Txn, edge *pb.DirectedEdge) error {
l.AssertRLock()

if types.TypeID(edge.ValueType) != types.UidID {
return nil
}

if schema.State().IsList(edge.Attr) {
return nil
}

return l.iterate(txn.StartTs, 0, func(obj *pb.Posting) error {
if obj.Uid != edge.GetValueId() {
return errors.Errorf(
"cannot add value with uid %x to predicate %s because one of the existing "+
"values does not match this uid, either delete the existing values first or "+
"modify the schema to '%s: [uid]'",
edge.GetValueId(), edge.Attr, edge.Attr)
}
return nil
})
}

func (l *List) addMutation(ctx context.Context, txn *Txn, t *pb.DirectedEdge) error {
l.Lock()
defer l.Unlock()
Expand Down