Skip to content

Commit

Permalink
Merge pull request #12 from acoshift/batch-delete
Browse files Browse the repository at this point in the history
delete: DeleteByKeys support batch delete > 500 keys, resolved #10
  • Loading branch information
acoshift authored Apr 8, 2017
2 parents 56a7740 + 1bec522 commit 23ab192
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,41 @@ func (client *Client) DeleteByKey(ctx context.Context, key *datastore.Key) error

// DeleteByKeys deletes data from datastore by keys
func (client *Client) DeleteByKeys(ctx context.Context, keys []*datastore.Key) error {
err := client.DeleteMulti(ctx, keys)
var err error
l := len(keys)
p := 500
if l > p {
for i := 0; i < l/p+1; i++ {
m := (i + 1) * p
if m > l {
m = l
}
if i*p == m {
break
}
e := client.DeleteMulti(ctx, keys[i*p:m])
if e != nil {
if err == nil {
err = e
} else {
if errs, ok := err.(datastore.MultiError); ok {
err = append(errs, e)
} else {
err = datastore.MultiError{err, e}
}
}
}
}
} else {
err = client.DeleteMulti(ctx, keys)
}
if client.Cache != nil {
client.Cache.DelMulti(keys)
}
return err
if err != nil {
return err
}
return nil
}

// DeleteByID deletes data from datastore by IDKey
Expand Down

0 comments on commit 23ab192

Please sign in to comment.