-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
69 lines (55 loc) · 1.55 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package internal
import (
"fmt"
"github.com/101loops/hrd/internal/trafo"
"github.com/101loops/hrd/internal/types"
"github.com/qedus/nds"
ae "appengine"
ds "appengine/datastore"
)
var (
dsGet = func(ctx ae.Context, keys []*ds.Key, dst interface{}) error {
return ds.GetMulti(ctx, keys, dst)
}
ndsGet = func(ctx ae.Context, keys []*ds.Key, dst interface{}) error {
return nds.GetMulti(ctx, keys, dst)
}
)
// Get loads entities for the given keys.
func Get(kind *types.Kind, keys []*types.Key, dst interface{}, useGlobalCache bool, multi bool) ([]*types.Key, error) {
if err := validateGetKeys(kind, keys); err != nil {
return nil, err
}
ctx := kind.Context
ctx.Infof(LogDatastoreAction("getting", "from", keys, kind.Name))
docList, err := trafo.NewWriteableDocList(dst, keys, multi)
if err != nil {
return nil, err
}
docsPipe := docList.Pipe(ctx)
var dsErr error
dsKeys := toDSKeys(ctx, keys)
if useGlobalCache {
dsErr = ndsGet(ctx, dsKeys, docsPipe.Properties())
}
dsErr = dsGet(ctx, dsKeys, docsPipe.Properties())
return docList.ApplyResult(dsKeys, dsErr)
}
func validateGetKeys(kind *types.Kind, keys []*types.Key) error {
if keys == nil || len(keys) == 0 {
return fmt.Errorf("no keys provided")
}
for i, key := range keys {
if key.Incomplete() {
return fmt.Errorf("'%v' is incomplete (%dth index)", key, i)
}
}
for _, k := range keys {
keyKind := k.Kind
if keyKind != kind.Name {
err := fmt.Errorf("invalid key kind '%v' for kind '%v'", keyKind, kind.Name)
return logErr(kind.Context, err)
}
}
return nil
}