forked from asdine/storm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget.go
More file actions
48 lines (37 loc) · 925 Bytes
/
get.go
File metadata and controls
48 lines (37 loc) · 925 Bytes
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
package storm
import (
"reflect"
"github.com/boltdb/bolt"
)
// Get a value from a bucket
func (n *Node) Get(bucketName string, key interface{}, to interface{}) error {
ref := reflect.ValueOf(to)
if !ref.IsValid() || ref.Kind() != reflect.Ptr {
return ErrPtrNeeded
}
id, err := toBytes(key, n.s.Codec)
if err != nil {
return err
}
if n.tx != nil {
return n.get(n.tx, bucketName, id, to)
}
return n.s.Bolt.View(func(tx *bolt.Tx) error {
return n.get(tx, bucketName, id, to)
})
}
func (n *Node) get(tx *bolt.Tx, bucketName string, id []byte, to interface{}) error {
bucket := n.GetBucket(tx, bucketName)
if bucket == nil {
return ErrNotFound
}
raw := bucket.Get(id)
if raw == nil {
return ErrNotFound
}
return n.s.Codec.Decode(raw, to)
}
// Get a value from a bucket
func (s *DB) Get(bucketName string, key interface{}, to interface{}) error {
return s.root.Get(bucketName, key, to)
}