Skip to content
This repository has been archived by the owner on Apr 5, 2023. It is now read-only.

Commit

Permalink
src: simplified firestore implementation, added limit arg
Browse files Browse the repository at this point in the history
  • Loading branch information
remorses committed Jun 11, 2020
1 parent df196be commit 1954852
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/database_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type UpdateParams struct {
type DeleteManyParams struct {
Collection string
Where WhereTree // `mapstructure:"where"`
Limit int
}

type Pagination struct {
Expand Down
6 changes: 5 additions & 1 deletion src/fakedata/fakedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fakedata
import (
"context"
"fmt"
"math"
"time"

"github.com/256dpi/lungo"
Expand Down Expand Up @@ -187,6 +188,9 @@ func (self *FakeDatabaseFunctions) UpdateMany(ctx context.Context, p goke.Update
}

func (self *FakeDatabaseFunctions) DeleteMany(ctx context.Context, p goke.DeleteManyParams, hook goke.TransformDocument) (goke.NodesMutationPayload, error) {
if p.Limit == 0 {
p.Limit = math.MaxInt16
}
db, err := self.Init(ctx)
payload := goke.NodesMutationPayload{}
if err != nil {
Expand All @@ -196,7 +200,7 @@ func (self *FakeDatabaseFunctions) DeleteMany(ctx context.Context, p goke.Delete
testutil.PrettyPrint(p)

// find accessible nodes
nodes, err := self.FindMany(ctx, goke.FindManyParams{Collection: p.Collection, Where: p.Where}, hook)
nodes, err := self.FindMany(ctx, goke.FindManyParams{Collection: p.Collection, Where: p.Where, Limit: p.Limit}, hook)
if err != nil {
return payload, err
}
Expand Down
9 changes: 4 additions & 5 deletions src/firestore/firestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,9 @@ func (self *FirestoreDatabaseFunctions) updateMany(ctx context.Context, p goke.U
}

func (self *FirestoreDatabaseFunctions) DeleteMany(ctx context.Context, p goke.DeleteManyParams, hook goke.TransformDocument) (goke.NodesMutationPayload, error) {
return self.deleteMany(ctx, p, hook, math.MaxInt32)
}

func (self *FirestoreDatabaseFunctions) deleteMany(ctx context.Context, p goke.DeleteManyParams, hook goke.TransformDocument, count int) (goke.NodesMutationPayload, error) {
if p.Limit == 0 {
p.Limit = math.MaxInt16
}
db, err := self.Init(ctx)
payload := goke.NodesMutationPayload{}
if err != nil {
Expand All @@ -190,7 +189,7 @@ func (self *FirestoreDatabaseFunctions) deleteMany(ctx context.Context, p goke.D
iter := query.Documents(ctx)
defer iter.Stop()
// TODO use batching for firestore's updateMany
for payload.AffectedCount < count {
for payload.AffectedCount < p.Limit {
doc, err := iter.Next()
if err == iterator.Done {
break
Expand Down
6 changes: 5 additions & 1 deletion src/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math"
"time"

goke "github.com/remorses/goke/src"
Expand Down Expand Up @@ -187,6 +188,9 @@ func (self *MongodbDatabaseFunctions) UpdateMany(ctx context.Context, p goke.Upd
}

func (self *MongodbDatabaseFunctions) DeleteMany(ctx context.Context, p goke.DeleteManyParams, hook goke.TransformDocument) (goke.NodesMutationPayload, error) {
if p.Limit == 0 {
p.Limit = math.MaxInt16
}
db, err := self.Init(ctx)
payload := goke.NodesMutationPayload{}
if err != nil {
Expand All @@ -196,7 +200,7 @@ func (self *MongodbDatabaseFunctions) DeleteMany(ctx context.Context, p goke.Del
testutil.PrettyPrint(p)

// find accessible nodes
nodes, err := self.FindMany(ctx, goke.FindManyParams{Collection: p.Collection, Where: p.Where}, hook)
nodes, err := self.FindMany(ctx, goke.FindManyParams{Collection: p.Collection, Where: p.Where, Limit: p.Limit}, hook)
if err != nil {
return payload, err
}
Expand Down

0 comments on commit 1954852

Please sign in to comment.