Skip to content

Commit

Permalink
remove alloc id feat
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Mar 13, 2017
1 parent dfbc247 commit 90c6a5f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 62 deletions.
1 change: 0 additions & 1 deletion ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
// Client type
type Client struct {
*datastore.Client
AllocateIncompleteID bool
}

// NewClient creates new ds client which wrap datastore client
Expand Down
64 changes: 3 additions & 61 deletions save.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package ds

import (
"context"

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

func beforeSave(kind string, src interface{}) {
Expand All @@ -23,18 +21,10 @@ 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)
if key := x.GetKey(); key.Incomplete() && client.AllocateIncompleteID {
keys, err := client.AllocateIDs(ctx, []*datastore.Key{key})
if err != nil {
return err
}
x.SetKey(keys[0])
}
key, err := client.Put(ctx, x.GetKey(), x)
x.SetKey(key)
if err != nil {
Expand All @@ -47,61 +37,13 @@ func (client *Client) SaveModel(ctx context.Context, kind string, src interface{
// see more in SaveModel
func (client *Client) SaveModels(ctx context.Context, kind string, src interface{}) error {
xs := valueOf(src)
if client.AllocateIncompleteID {
keys := make([]*datastore.Key, 0, xs.Len())
mapIndex := make(map[int]int)
for i := 0; i < xs.Len(); i++ {
inf := xs.Index(i).Interface()
beforeSave(kind, inf)
if x, ok := inf.(KeyGetSetter); ok {
if k := x.GetKey(); k.Incomplete() {
keys = append(keys, datastore.IncompleteKey(kind, nil))
mapIndex[len(keys)-1] = i
}
}
}
keys, err := client.AllocateIDs(ctx, keys)
if err != nil {
return err
}
for dst, src := range mapIndex {
xs.Index(src).Interface().(KeyGetSetter).SetKey(keys[dst])
}
} else {
for i := 0; i < xs.Len(); i++ {
x := xs.Index(i).Interface()
beforeSave(kind, x)
}
for i := 0; i < xs.Len(); i++ {
x := xs.Index(i).Interface()
beforeSave(kind, x)
}
err := client.PutModels(ctx, src)
if err != nil {
return err
}
return nil
}

// AllocateModel calls AllocateIDModel and SaveModel
func (client *Client) AllocateModel(ctx context.Context, kind string, src interface{}) error {
err := client.AllocateIDModel(ctx, kind, src)
if err != nil {
return err
}
err = client.SaveModel(ctx, kind, src)
if err != nil {
return err
}
return nil
}

// AllocateModels calls AllocateIDModels and SaveModels
func (client *Client) AllocateModels(ctx context.Context, kind string, src interface{}) error {
err := client.AllocateIDModels(ctx, kind, src)
if err != nil {
return err
}
err = client.SaveModels(ctx, kind, src)
if err != nil {
return err
}
return nil
}

0 comments on commit 90c6a5f

Please sign in to comment.