-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastore.go
197 lines (168 loc) · 5.1 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package kv
import (
"context"
"net/url"
"cloud.google.com/go/datastore"
"google.golang.org/api/iterator"
)
const DataStoreKind = "keyvalue"
type datastoreKeyValue struct {
Key *datastore.Key `datastore:"__key__"`
Val []byte `datastore:"val,noindex"`
}
type DatastoreDB struct {
*datastore.Client
cancel func()
}
type datastoreTransaction struct {
*datastore.Transaction
*datastore.Client
}
type datastoreIterator struct {
*datastore.Iterator
}
func NewDatastoreDbFromUrl(u *url.URL) (*DatastoreDB, error) {
ctx, cancel := context.WithCancel(context.Background())
// Create a datastore client. In a typical application, you would create
// a single client which is reused for every datastore operation.
dsClient, err := datastore.NewClient(ctx, u.Host)
if err != nil {
cancel()
return nil, err
}
return &DatastoreDB{dsClient, cancel}, nil
}
// datastore db
// Get gets the value of a key within a single query transaction
func (dsDb *DatastoreDB) Close() error {
defer dsDb.cancel()
return dsDb.Client.Close()
}
// Get gets the value of a key within a single query transaction
func (dsDb *DatastoreDB) Get(ctx context.Context, key []byte) (res []byte, err error) {
k := datastore.NameKey(DataStoreKind, string(key), nil)
e := &datastoreKeyValue{}
if err := dsDb.Client.Get(ctx, k, e); err != nil {
if err == datastore.ErrNoSuchEntity {
err = ErrNotFound
}
return nil, err
}
return []byte(e.Val), err
}
// Put sets the value of a key within a single query transaction
func (dsDb *DatastoreDB) Put(ctx context.Context, key, value []byte) error {
k := datastore.NameKey(DataStoreKind, string(key), nil)
e := &datastoreKeyValue{
Key: k,
Val: value,
}
if _, err := dsDb.Client.Put(ctx, k, e); err != nil {
if err == datastore.ErrNoSuchEntity {
err = ErrNotFound
}
return err
}
return nil
}
// Delete removes a key within a single transaction
func (dsDb *DatastoreDB) Delete(ctx context.Context, key []byte) error {
k := datastore.NameKey(DataStoreKind, string(key), nil)
if err := dsDb.Client.Delete(ctx, k); err != nil {
if err == datastore.ErrNoSuchEntity {
err = ErrNotFound
}
return err
}
return nil
}
// NewTransaction for batching multiple values inside a transaction
func (dsDb *DatastoreDB) NewTransaction(ctx context.Context, readOnly bool) (OrderedTransaction, error) {
tx, err := dsDb.Client.NewTransaction(ctx)
if err != nil {
return nil, err
}
return &datastoreTransaction{
tx,
dsDb.Client, // save for iterators later on
}, nil
}
// datastoreTransaction
// Seeks initializes an iterator at the given key (inclusive)
func (dsDb *datastoreTransaction) Close() error {
return dsDb.Discard(context.Background())
}
// Get gets the value of a key within a single query transaction
func (dsDb *datastoreTransaction) Get(ctx context.Context, key []byte) (res []byte, err error) {
k := datastore.NameKey(DataStoreKind, string(key), nil)
e := &datastoreKeyValue{}
if err := dsDb.Transaction.Get(k, e); err != nil {
if err == datastore.ErrNoSuchEntity {
err = ErrNotFound
}
return nil, err
}
return []byte(e.Val), err
}
// Put sets the value of a key within a single query transaction
func (dsDb *datastoreTransaction) Put(ctx context.Context, key, value []byte) error {
k := datastore.NameKey(DataStoreKind, string(key), nil)
e := &datastoreKeyValue{
Key: k,
Val: value,
}
if _, err := dsDb.Transaction.Put(k, e); err != nil {
if err == datastore.ErrNoSuchEntity {
err = ErrNotFound
}
return err
}
return nil
}
// Delete removes a key within a single transaction
func (dsDb *datastoreTransaction) Delete(ctx context.Context, key []byte) error {
k := datastore.NameKey(DataStoreKind, string(key), nil)
if err := dsDb.Transaction.Delete(k); err != nil {
if err == datastore.ErrNoSuchEntity {
err = ErrNotFound
}
return err
}
return nil
}
func (dsDb *datastoreTransaction) Seek(ctx context.Context, StartKey []byte) (Iterator, error) {
k := datastore.NameKey(DataStoreKind, string(StartKey), nil)
query := datastore.NewQuery(DataStoreKind).
Filter("__key__ >=", k).
Order("__key__") //.Transaction(dsDb.Transaction)
it := dsDb.Client.Run(ctx, query)
return &datastoreIterator{
it,
}, nil
}
// Discard removes all sides effects of the transaction
func (dsDb *datastoreTransaction) Discard(ctx context.Context) error {
return dsDb.Transaction.Rollback()
}
// Commit persists all side effects of the transaction and returns an error if there are any conflics
func (dsDb *datastoreTransaction) Commit(ctx context.Context) error {
_, err := dsDb.Transaction.Commit()
return err
}
// datastoreIterator
// Next yeilds the next key-value in iterator. Key-values can not be re-used between iterations. Make sure top copy the values if you must.
func (it *datastoreIterator) Next(ctx context.Context) (key, value []byte, err error) {
kv := &datastoreKeyValue{}
_, err = it.Iterator.Next(kv)
if err == iterator.Done {
return nil, nil, ErrNotFound
}
if err != nil {
return nil, nil, err
}
return []byte(kv.Key.String()), kv.Val, nil
}
// Close must always be called to clean up iterators.
func (dsDb *datastoreIterator) Close() error {
return nil
}