Skip to content

Commit

Permalink
docstore: fix batch Creates (#2726)
Browse files Browse the repository at this point in the history
  • Loading branch information
vangent committed Jan 28, 2020
1 parent 29950f8 commit 6b3f548
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 6 additions & 2 deletions docstore/driver/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ func GroupActions(actions []*Action) (beforeGets, getList, writeList, afterGets
agets := map[interface{}]*Action{}
cgets := map[interface{}]*Action{}
writes := map[interface{}]*Action{}
var nilkeys []*Action
for _, a := range actions {
if a.Kind == Get {
if a.Key == nil {
// Probably a Create.
nilkeys = append(nilkeys, a)
} else if a.Kind == Get {
// If there was a prior write with this key, make sure this get
// happens after the writes.
if _, ok := writes[a.Key]; ok {
Expand Down Expand Up @@ -91,7 +95,7 @@ func GroupActions(actions []*Action) (beforeGets, getList, writeList, afterGets
return as
}

return vals(bgets), vals(cgets), vals(writes), vals(agets)
return vals(bgets), vals(cgets), append(vals(writes), nilkeys...), vals(agets)
}

// AsFunc creates and returns an "as function" that behaves as follows:
Expand Down
9 changes: 7 additions & 2 deletions docstore/driver/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func TestGroupActions(t *testing.T) {
},
want: [][]int{{0}, {1}, {2, 3}, {4}},
},
{
in: []*Action{{Kind: Create}, {Kind: Create}, {Kind: Create}},
want: [][]int{nil, nil, {0, 1, 2}, nil},
},
} {
got := make([][]*Action, 4)
got[0], got[1], got[2], got[3] = GroupActions(test.in)
Expand All @@ -106,10 +110,11 @@ func TestGroupActions(t *testing.T) {
if a1.Kind != a2.Kind {
return a1.Kind < a2.Kind
}
return a1.Key.(int) < a2.Key.(int)
a1k, _ := a1.Key.(int)
a2k, _ := a2.Key.(int)
return a1k < a2k
}))
if diff != "" {

t.Errorf("%v: %s", test.in, diff)
}
}
Expand Down

0 comments on commit 6b3f548

Please sign in to comment.