-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.go
174 lines (152 loc) · 3.58 KB
/
memory.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
package bolt // import "github.com/robertgzr/joe-bolt-memory"
import (
"os"
"path"
"github.com/go-joe/joe"
"github.com/pkg/errors"
bolt "go.etcd.io/bbolt"
"go.uber.org/zap"
)
type memory struct {
path string
mode os.FileMode
options *bolt.Options
logger *zap.Logger
db *bolt.DB
}
func Memory(path string) joe.Module {
return joe.ModuleFunc(func(conf *joe.Config) error {
mem, err := NewMemory(path, WithLogger(conf.Logger("memory")))
if err != nil {
return err
}
conf.SetMemory(mem)
return nil
})
}
func NewMemory(path string, opts ...Option) (joe.Memory, error) {
m := &memory{
path: path,
mode: 0666,
options: nil,
}
for _, opt := range opts {
err := opt(m)
if err != nil {
return nil, err
}
}
if m.logger == nil {
m.logger = zap.NewNop()
}
m.logger.Debug("Opening database", zap.String("path", m.path))
db, err := bolt.Open(m.path, m.mode, m.options)
if err != nil {
return nil, errors.Wrap(err, "failed to open database")
}
m.db = db
m.logger.Info("Memory initialized successfully",
zap.String("path", m.path),
)
return m, nil
}
// pathkey is the structure used to encode/decode a bolt bucket and key.
// It will extract the `bucket` name from the root of the path and use the
// remaining components as the key.
type pathkey struct {
bucket, key []byte
}
func (bk *pathkey) String() string {
return path.Join(string(bk.bucket), string(bk.key))
}
func pathkeyFromString(in string) *pathkey {
bucket, key := path.Split(in)
if bucket == "" {
bucket = "_joe"
} else {
bucket = bucket[:len(bucket)-1] // trim trailing slash
}
return &pathkey{bucket: []byte(bucket), key: []byte(key)}
}
func (m *memory) Set(key string, value []byte) error {
pk := pathkeyFromString(key)
m.logger.Debug("Database access: Put",
zap.ByteString("bucket", pk.bucket),
zap.ByteString("key", pk.key),
)
err := m.db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists(pk.bucket)
if err != nil {
return err
}
return b.Put(pk.key, value)
})
if err != nil {
return err
}
return nil
}
func (m *memory) Get(key string) ([]byte, bool, error) {
var value []byte
pk := pathkeyFromString(key)
m.logger.Debug("Database access: Get",
zap.ByteString("bucket", pk.bucket),
zap.ByteString("key", pk.key),
)
err := m.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(pk.bucket)
if b == nil {
return bolt.ErrBucketNotFound
}
value = b.Get(pk.key)
return nil
})
if err != nil {
if err == bolt.ErrBucketNotFound || os.IsNotExist(err) {
return nil, false, nil
}
return nil, true, err
}
return value, true, nil
}
func (m *memory) Delete(key string) (bool, error) {
pk := pathkeyFromString(key)
m.logger.Debug("Database access: Delete",
zap.ByteString("bucket", pk.bucket),
zap.ByteString("key", pk.key),
)
err := m.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(pk.bucket)
if b == nil {
return bolt.ErrBucketNotFound
}
return b.Delete(pk.key)
})
if err != nil {
if err == bolt.ErrBucketNotFound || os.IsNotExist(err) {
return false, nil
}
return true, err
}
return true, nil
}
func (m *memory) Keys() ([]string, error) {
var keys []string
err := m.db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(bucket []byte, b *bolt.Bucket) error {
return b.ForEach(func(key, _ []byte) error {
pk := &pathkey{bucket: bucket, key: key}
keys = append(keys, pk.String())
return nil
})
})
})
if err != nil {
return keys, err
}
return keys, nil
}
func (m *memory) Close() error {
m.logger.Debug("Closing database", zap.String("path", m.path))
return m.db.Close()
}