forked from paquesid/badgerhold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
82 lines (66 loc) · 2.75 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
70
71
72
73
74
75
76
77
78
79
80
81
82
// 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"
"github.com/dgraph-io/badger"
)
// ErrNotFound is returned when no data is found for the given key
var ErrNotFound = errors.New("No data found for this key")
// Get retrieves a value from badgerhold and puts it into result. Result must be a pointer
func (s *Store) Get(key, result interface{}) error {
return s.Badger().View(func(tx *badger.Txn) error {
return s.TxGet(tx, key, result)
})
}
// TxGet allows you to pass in your own badger transaction to retrieve a value from the badgerhold and puts it
// into result
func (s *Store) TxGet(tx *badger.Txn, key, result interface{}) error {
storer := newStorer(result)
gk, err := encodeKey(key, storer.Type())
if err != nil {
return err
}
item, err := tx.Get(gk)
if err == badger.ErrKeyNotFound {
return ErrNotFound
}
return item.Value(func(value []byte) error {
return decode(value, result)
})
}
// Find retrieves a set of values from the badgerhold that matches the passed in query
// result must be a pointer to a slice.
// The result of the query will be appended to the passed in result slice, rather than the passed in slice being
// emptied.
func (s *Store) Find(result interface{}, query *Query) error {
return s.Badger().View(func(tx *badger.Txn) error {
return s.TxFind(tx, result, query)
})
}
// FindPRS retrieves a set of values from the badgerhold that matches the passed in query
// result must be a pointer to a slice.
// The result of the query will be appended to the passed in result slice, rather than the passed in slice being
// emptied.
func (s *Store) FindPRS(result interface{}, query *Query, kuncian string) error {
return s.Badger().View(func(tx *badger.Txn) error {
return s.TxFindPRS(tx, result, query, kuncian)
})
}
// TxFind allows you to pass in your own badger transaction to retrieve a set of values from the badgerhold
func (s *Store) TxFind(tx *badger.Txn, result interface{}, query *Query) error {
return findQuery(tx, result, query)
}
// TxFindPRS allows you to pass in your own badger transaction to retrieve a set of values from the badgerhold
func (s *Store) TxFindPRS(tx *badger.Txn, result interface{}, query *Query, kuncian string) error {
return findQueryPRS(tx, result, query, kuncian)
}
func (s *Store) GetSourceCount(result interface{}, query *Query, kuncian string, count *int) error {
return s.Badger().View(func(tx *badger.Txn) error {
return s.TxGetSourceCount(tx, result, query, kuncian, count)
})
}
func (s *Store) TxGetSourceCount(tx *badger.Txn, result interface{}, query *Query, kuncian string, count *int) error {
return countSource(tx, result, query, kuncian, count)
}