forked from paquesid/badgerhold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
206 lines (169 loc) · 5.3 KB
/
store.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
// 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 (
"reflect"
"strings"
"sync"
"github.com/dgraph-io/badger"
)
const (
// BadgerHoldIndexTag is the struct tag used to define an a field as indexable for a badgerhold
BadgerHoldIndexTag = "badgerholdIndex"
// BadgerholdKeyTag is the struct tag used to define an a field as a key for use in a Find query
BadgerholdKeyTag = "badgerholdKey"
// badgerholdPrefixTag is the prefix for an alternate (more standard) version of a struct tag
badgerholdPrefixTag = "badgerhold"
badgerholdPrefixIndexValue = "index"
badgerholdPrefixKeyValue = "key"
badgerholdPrefixUniqueValue = "unique"
)
// Store is a badgerhold wrapper around a badger DB
type Store struct {
db *badger.DB
sequenceBandwith uint64
sequences *sync.Map
}
// Options allows you set different options from the defaults
// For example the encoding and decoding funcs which default to Gob
type Options struct {
Encoder EncodeFunc
Decoder DecodeFunc
SequenceBandwith uint64
badger.Options
}
// DefaultOptions are a default set of options for opening a BadgerHold database
// Includes badgers own default options
var DefaultOptions = Options{
Options: badger.DefaultOptions,
Encoder: DefaultEncode,
Decoder: DefaultDecode,
SequenceBandwith: 100,
}
// Open opens or creates a badgerhold file.
func Open(options Options) (*Store, error) {
encode = options.Encoder
decode = options.Decoder
db, err := badger.Open(options.Options)
if err != nil {
return nil, err
}
return &Store{
db: db,
sequenceBandwith: options.SequenceBandwith,
sequences: &sync.Map{},
}, nil
}
// Badger returns the underlying Badger DB the badgerhold is based on
func (s *Store) Badger() *badger.DB {
return s.db
}
// Close closes the badger db
func (s *Store) Close() error {
var err error
s.sequences.Range(func(key, value interface{}) bool {
err = value.(*badger.Sequence).Release()
if err != nil {
return false
}
return true
})
if err != nil {
return err
}
return s.db.Close()
}
/*
NOTE: Not going to implement ReIndex and Remove index
I had originally created these to make the transition from a plain bolt or badger DB easier
but there is too much chance for lost data, and it's probably better that any conversion be
done by the developer so they can directly manage how they want data to be migrated.
If you disagree, feel free to open an issue and we can revisit this.
*/
// Storer is the Interface to implement to skip reflect calls on all data passed into the badgerhold
type Storer interface {
Type() string // used as the badgerdb index prefix
Indexes() map[string]Index //[indexname]indexFunc
}
// anonType is created from a reflection of an unknown interface
type anonStorer struct {
rType reflect.Type
indexes map[string]Index
}
// Type returns the name of the type as determined from the reflect package
func (t *anonStorer) Type() string {
return t.rType.Name()
}
// Indexes returns the Indexes determined by the reflect package on this type
func (t *anonStorer) Indexes() map[string]Index {
return t.indexes
}
// newStorer creates a type which satisfies the Storer interface based on reflection of the passed in dataType
// if the Type doesn't meet the requirements of a Storer (i.e. doesn't have a name) it panics
// You can avoid any reflection costs, by implementing the Storer interface on a type
func newStorer(dataType interface{}) Storer {
s, ok := dataType.(Storer)
if ok {
return s
}
tp := reflect.TypeOf(dataType)
for tp.Kind() == reflect.Ptr {
tp = tp.Elem()
}
storer := &anonStorer{
rType: tp,
indexes: make(map[string]Index),
}
if storer.rType.Name() == "" {
panic("Invalid Type for Storer. Type is unnamed")
}
if storer.rType.Kind() != reflect.Struct {
panic("Invalid Type for Storer. BadgerHold only works with structs")
}
for i := 0; i < storer.rType.NumField(); i++ {
indexName := ""
unique := false
if strings.Contains(string(storer.rType.Field(i).Tag), BadgerHoldIndexTag) {
indexName = storer.rType.Field(i).Tag.Get(BadgerHoldIndexTag)
if indexName != "" {
indexName = storer.rType.Field(i).Name
}
} else if tag := storer.rType.Field(i).Tag.Get(badgerholdPrefixTag); tag != "" {
if tag == badgerholdPrefixIndexValue {
indexName = storer.rType.Field(i).Name
} else if tag == badgerholdPrefixUniqueValue {
indexName = storer.rType.Field(i).Name
unique = true
}
}
if indexName != "" {
storer.indexes[indexName] = Index{
IndexFunc: func(name string, value interface{}) ([]byte, error) {
tp := reflect.ValueOf(value)
for tp.Kind() == reflect.Ptr {
tp = tp.Elem()
}
return encode(tp.FieldByName(name).Interface())
},
Unique: unique,
}
}
}
return storer
}
func (s *Store) getSequence(typeName string) (uint64, error) {
seq, ok := s.sequences.Load(typeName)
if !ok {
newSeq, err := s.Badger().GetSequence([]byte(typeName), s.sequenceBandwith)
if err != nil {
return 0, err
}
s.sequences.Store(typeName, newSeq)
seq = newSeq
}
return seq.(*badger.Sequence).Next()
}
func typePrefix(typeName string) []byte {
return []byte("bh_" + typeName)
}