forked from manishrjain/gocrud
/
datastore.go
113 lines (99 loc) · 2.99 KB
/
datastore.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package datastore
import (
"github.com/aslanides/gocrud/store"
"github.com/aslanides/gocrud/x"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/cloud"
"google.golang.org/cloud/datastore"
)
var log = x.Log("store")
type Datastore struct {
tablePrefix string
ctx context.Context
projectId string
}
// Init takes 2 arguments: tablePrefix and projectId.
func (ds *Datastore) Init(args ...string) {
if len(args) != 2 {
log.WithField("args", args).Fatal("Invalid arguments")
return
}
ds.tablePrefix = args[0]
project := args[1]
client, err := google.DefaultClient(oauth2.NoContext,
"https://www.googleapis.com/auth/devstorage.full_control")
if err != nil {
x.LogErr(log, err).Fatal("Unable to get client")
}
ds.ctx = cloud.NewContext(project, client)
if ds.ctx == nil {
log.Fatal("Failed to get context. context is nil")
}
ds.projectId = project
log.Info("Connection to Google datastore established")
}
func (ds *Datastore) getIKey(i x.Instruction) *datastore.Key {
skey := datastore.NewKey(ds.ctx, ds.tablePrefix+"Entity", i.SubjectId, 0, nil)
return datastore.NewIncompleteKey(ds.ctx, ds.tablePrefix+"Instruction", skey)
}
func (ds *Datastore) Commit(its []*x.Instruction) error {
var keys []*datastore.Key
for _, i := range its {
dkey := ds.getIKey(*i)
keys = append(keys, dkey)
}
client, err := datastore.NewClient(ds.ctx, ds.projectId)
if err != nil {
x.LogErr(log, err).Error("While creating new client")
return err
}
if _, err := client.PutMulti(ds.ctx, keys, its); err != nil {
x.LogErr(log, err).Error("While committing instructions")
return err
}
log.Debugf("%d Instructions committed", len(its))
return nil
}
func (ds *Datastore) IsNew(id string) bool {
dkey := datastore.NewKey(ds.ctx, ds.tablePrefix+"Entity", id, 0, nil)
client, err := datastore.NewClient(ds.ctx, ds.projectId)
if err != nil {
x.LogErr(log, err).Error("While creating client")
return false
}
q := datastore.NewQuery(ds.tablePrefix + "Instruction").Ancestor(dkey).
Limit(1).KeysOnly()
keys, err := client.GetAll(ds.ctx, q, nil)
if err != nil {
x.LogErr(log, err).Error("While GetAll")
return false
}
if len(keys) > 0 {
return false
}
return true
}
func (ds *Datastore) GetEntity(subject string) (reply []x.Instruction, rerr error) {
skey := datastore.NewKey(ds.ctx, ds.tablePrefix+"Entity", subject, 0, nil)
log.Infof("skey: %+v", skey)
client, err := datastore.NewClient(ds.ctx, ds.projectId)
if err != nil {
x.LogErr(log, err).Error("While creating client")
return reply, err
}
var dkeys []*datastore.Key
q := datastore.NewQuery(ds.tablePrefix + "Instruction").Ancestor(skey)
dkeys, rerr = client.GetAll(ds.ctx, q, &reply)
log.Debugf("Got num keys: %+v", len(dkeys))
return
}
func (ds *Datastore) Iterate(fromId string, num int, ch chan x.Entity) (found int, last x.Entity, err error) {
log.Fatal("Not implemented")
return
}
func init() {
log.Info("Initing datastore")
store.Register("datastore", new(Datastore))
}