-
Notifications
You must be signed in to change notification settings - Fork 51
/
delete.go
66 lines (53 loc) · 1.67 KB
/
delete.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
// 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 (
"github.com/dgraph-io/badger/v4"
)
// Delete deletes a record from the badgerhold, datatype just needs to be an example of the type stored so that
// the proper bucket and indexes are updated
func (s *Store) Delete(key, dataType interface{}) error {
return s.Badger().Update(func(tx *badger.Txn) error {
return s.TxDelete(tx, key, dataType)
})
}
// TxDelete is the same as Delete except it allows you to specify your own transaction
func (s *Store) TxDelete(tx *badger.Txn, key, dataType interface{}) error {
storer := s.newStorer(dataType)
gk, err := s.encodeKey(key, storer.Type())
if err != nil {
return err
}
value := newElemType(dataType)
item, err := tx.Get(gk)
if err == badger.ErrKeyNotFound {
return ErrNotFound
}
if err != nil {
return err
}
err = item.Value(func(bVal []byte) error {
return s.decode(bVal, value)
})
if err != nil {
return err
}
// delete data
err = tx.Delete(gk)
if err != nil {
return err
}
// remove any indexes
return s.indexDelete(storer, tx, gk, value)
}
// DeleteMatching deletes all the records that match the passed in query
func (s *Store) DeleteMatching(dataType interface{}, query *Query) error {
return s.Badger().Update(func(tx *badger.Txn) error {
return s.TxDeleteMatching(tx, dataType, query)
})
}
// TxDeleteMatching does the same as DeleteMatching, but allows you to specify your own transaction
func (s *Store) TxDeleteMatching(tx *badger.Txn, dataType interface{}, query *Query) error {
return s.deleteQuery(tx, dataType, query)
}