Skip to content

Commit

Permalink
add AllocateIncompleteID and remove batch 500 save models
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Feb 14, 2017
1 parent 404faf1 commit 2cf666c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
3 changes: 2 additions & 1 deletion ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
// Client type
type Client struct {
*datastore.Client
AllocateIncompleteID bool
}

// NewClient creates new ds client which wrap datastore client
Expand All @@ -19,5 +20,5 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
if err != nil {
return nil, err
}
return &Client{client}, nil
return &Client{Client: client}, nil
}
34 changes: 9 additions & 25 deletions save.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ds

import (
"context"

"cloud.google.com/go/datastore"
)

func beforeSave(kind string, src interface{}) {
Expand All @@ -21,38 +23,20 @@ func beforeSave(kind string, src interface{}) {
// SaveModel saves model to datastore
// kind is optional, if key already set
// if key was not set in model, will call NewKey with given kind
// if key is incomplete key and AllocateIncompleteID is true, will call allocate ids before put model to datastore
func (client *Client) SaveModel(ctx context.Context, kind string, src interface{}) error {
beforeSave(kind, src)

x := src.(KeyGetSetter)
key, err := client.Put(ctx, x.GetKey(), x)
x.SetKey(key)
if err != nil {
return err
}
return nil
}

const maxPutBatchSize = 500

func (client *Client) saveModels(ctx context.Context, src interface{}) error {
xs := valueOf(src)
if xs.Len() > maxPutBatchSize {
// TODO: refactor error
errChan := make(chan error)
go func() { errChan <- client.saveModels(ctx, xs.Slice(0, maxPutBatchSize).Interface()) }()
go func() { errChan <- client.saveModels(ctx, xs.Slice(maxPutBatchSize, xs.Len()).Interface()) }()
err := <-errChan
if err != nil {
return err
}
err = <-errChan
if key := x.GetKey(); key.Incomplete() && client.AllocateIncompleteID {
keys, err := client.AllocateIDs(ctx, []*datastore.Key{key})
if err != nil {
return err
}
return nil
x.SetKey(keys[0])
}
err := client.PutModels(ctx, src)
key, err := client.Put(ctx, x.GetKey(), x)
x.SetKey(key)
if err != nil {
return err
}
Expand All @@ -67,7 +51,7 @@ func (client *Client) SaveModels(ctx context.Context, kind string, src interface
x := xs.Index(i).Interface()
beforeSave(kind, x)
}
err := client.saveModels(ctx, src)
err := client.PutModels(ctx, src)
if err != nil {
return err
}
Expand Down

0 comments on commit 2cf666c

Please sign in to comment.