forked from timshannon/badgerhold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
put.go
250 lines (205 loc) · 6.14 KB
/
put.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2019 Tim Shannon. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package badgerhold
import (
"errors"
"reflect"
"github.com/dgraph-io/badger/v3"
)
// ErrKeyExists is the error returned when data is being Inserted for a Key that already exists
var ErrKeyExists = errors.New("This Key already exists in badgerhold for this type")
// ErrUniqueExists is the error thrown when data is being inserted for a unique constraint value that already exists
var ErrUniqueExists = errors.New("This value cannot be written due to the unique constraint on the field")
// sequence tells badgerhold to insert the key as the next sequence in the bucket
type sequence struct{}
// NextSequence is used to create a sequential key for inserts
// Inserts a uint64 as the key
// store.Insert(badgerhold.NextSequence(), data)
func NextSequence() interface{} {
return sequence{}
}
// Insert inserts the passed in data into the badgerhold
//
// If the key already exists in the badgerhold, then an ErrKeyExists is returned
// If the data struct has a field tagged as `badgerholdKey` and it is the same type
// as the Insert key, AND the data struct is passed by reference, AND the key field
// is currently set to the zero-value for that type, then that field will be set to
// the value of the insert key.
//
// To use this with badgerhold.NextSequence() use a type of `uint64` for the key field.
func (s *Store) Insert(key, data interface{}) error {
err := s.Badger().Update(func(tx *badger.Txn) error {
return s.TxInsert(tx, key, data)
})
if err == badger.ErrConflict {
return s.Insert(key, data)
}
return err
}
// TxInsert is the same as Insert except it allows you to specify your own transaction
func (s *Store) TxInsert(tx *badger.Txn, key, data interface{}) error {
storer := s.newStorer(data)
var err error
if _, ok := key.(sequence); ok {
key, err = s.getSequence(storer.Type())
if err != nil {
return err
}
}
gk, err := s.encodeKey(key, storer.Type())
if err != nil {
return err
}
_, err = tx.Get(gk)
if err != badger.ErrKeyNotFound {
return ErrKeyExists
}
value, err := s.encode(data)
if err != nil {
return err
}
// insert data
err = tx.Set(gk, value)
if err != nil {
return err
}
// insert any indexes
err = s.indexAdd(storer, tx, gk, data)
if err != nil {
return err
}
dataVal := reflect.Indirect(reflect.ValueOf(data))
if !dataVal.CanSet() {
return nil
}
if keyField, ok := getKeyField(dataVal.Type()); ok {
fieldValue := dataVal.FieldByName(keyField.Name)
keyValue := reflect.ValueOf(key)
if keyValue.Type() != keyField.Type {
return nil
}
if !fieldValue.CanSet() {
return nil
}
if !reflect.DeepEqual(fieldValue.Interface(), reflect.Zero(keyField.Type).Interface()) {
return nil
}
fieldValue.Set(keyValue)
}
return nil
}
// Update updates an existing record in the badgerhold
// if the Key doesn't already exist in the store, then it fails with ErrNotFound
func (s *Store) Update(key interface{}, data interface{}) error {
err := s.Badger().Update(func(tx *badger.Txn) error {
return s.TxUpdate(tx, key, data)
})
if err == badger.ErrConflict {
return s.Update(key, data)
}
return err
}
// TxUpdate is the same as Update except it allows you to specify your own transaction
func (s *Store) TxUpdate(tx *badger.Txn, key interface{}, data interface{}) error {
storer := s.newStorer(data)
gk, err := s.encodeKey(key, storer.Type())
if err != nil {
return err
}
existingItem, err := tx.Get(gk)
if err == badger.ErrKeyNotFound {
return ErrNotFound
}
if err != nil {
return err
}
// delete any existing indexes
existingVal := newElemType(data)
err = existingItem.Value(func(existing []byte) error {
return s.decode(existing, existingVal)
})
if err != nil {
return err
}
err = s.indexDelete(storer, tx, gk, existingVal)
if err != nil {
return err
}
value, err := s.encode(data)
if err != nil {
return err
}
// put data
err = tx.Set(gk, value)
if err != nil {
return err
}
// insert any new indexes
return s.indexAdd(storer, tx, gk, data)
}
// Upsert inserts the record into the badgerhold if it doesn't exist. If it does already exist, then it updates
// the existing record
func (s *Store) Upsert(key interface{}, data interface{}) error {
err := s.Badger().Update(func(tx *badger.Txn) error {
return s.TxUpsert(tx, key, data)
})
if err == badger.ErrConflict {
return s.Upsert(key, data)
}
return err
}
// TxUpsert is the same as Upsert except it allows you to specify your own transaction
func (s *Store) TxUpsert(tx *badger.Txn, key interface{}, data interface{}) error {
storer := s.newStorer(data)
gk, err := s.encodeKey(key, storer.Type())
if err != nil {
return err
}
existingItem, err := tx.Get(gk)
if err == nil {
// existing entry found
// delete any existing indexes
existingVal := newElemType(data)
err = existingItem.Value(func(existing []byte) error {
return s.decode(existing, existingVal)
})
if err != nil {
return err
}
err = s.indexDelete(storer, tx, gk, existingVal)
if err != nil {
return err
}
} else if err != badger.ErrKeyNotFound {
return err
}
// existing entry not found
value, err := s.encode(data)
if err != nil {
return err
}
// put data
err = tx.Set(gk, value)
if err != nil {
return err
}
// insert any new indexes
return s.indexAdd(storer, tx, gk, data)
}
// UpdateMatching runs the update function for every record that match the passed in query
// Note that the type of record in the update func always has to be a pointer
func (s *Store) UpdateMatching(dataType interface{}, query *Query, update func(record interface{}) error) error {
err := s.Badger().Update(func(tx *badger.Txn) error {
return s.TxUpdateMatching(tx, dataType, query, update)
})
if err == badger.ErrConflict {
return s.UpdateMatching(dataType, query, update)
}
return err
}
// TxUpdateMatching does the same as UpdateMatching, but allows you to specify your own transaction
func (s *Store) TxUpdateMatching(tx *badger.Txn, dataType interface{}, query *Query,
update func(record interface{}) error) error {
return s.updateQuery(tx, dataType, query, update)
}