forked from brahma-adshonor/gorr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
210 lines (169 loc) · 3.87 KB
/
storage.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
207
208
209
210
package gorr
import (
"errors"
"flag"
"fmt"
"github.com/boltdb/bolt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
)
var (
bolt_db_big_value_thresh = flag.Int("bolt_db_big_value_thresh", 1024*1024, "value size than threshold will be store to a single file")
gorr_bolt_bucket_name = flag.String("gorr_bolt_bucket_name", "global_bucket", "bucket name used by gorr in bolt db")
)
type MapStorage struct {
mu sync.Mutex
m map[string][]byte
}
func NewMapStorage(capacity int) *MapStorage {
s := MapStorage{m: make(map[string][]byte, 10000)}
return &s
}
func (s *MapStorage) Put(key string, value []byte) error {
s.mu.Lock()
defer s.mu.Unlock()
s.m[key] = value
return nil
}
func (s *MapStorage) Get(key string) ([]byte, error) {
s.mu.Lock()
defer s.mu.Unlock()
v, exist := s.m[key]
if !exist {
return nil, errors.New("key not exists")
}
return v, nil
}
func (s *MapStorage) Clear() {
s.mu.Lock()
defer s.mu.Unlock()
s.m = make(map[string][]byte)
}
func (s *MapStorage) Close() {
}
func (s *MapStorage) AllFiles() []string {
return nil
}
type BoltStorage struct {
db *bolt.DB
mu sync.Mutex
file map[string]int
}
// bolt key/value db
func NewBoltStorage(path string) (*BoltStorage, error) {
db, err := bolt.Open(path, 0600, &bolt.Options{Timeout: 2 * time.Second})
if err != nil {
return nil, err
}
_ = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucket([]byte(*gorr_bolt_bucket_name))
if err == bolt.ErrBucketExists {
return nil
}
return err
})
s := &BoltStorage{db: db, file: make(map[string]int)}
return s, nil
}
func genBigValueFileName() string {
prefix := "gorr.file.db." + time.Now().Format("20060102150405")
path := prefix
for i := 0; i < 32; i++ {
if _, err := os.Stat(path); os.IsNotExist(err) {
return path
}
path = fmt.Sprintf("%s.%d", prefix, i)
}
return ""
}
func (s *BoltStorage) Put(key string, value []byte) error {
s.mu.Lock()
defer s.mu.Unlock()
err := s.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(*gorr_bolt_bucket_name))
value = append(value, byte('m'))
if len(value) > *bolt_db_big_value_thresh {
file, err := s.GetBigValueFile(key)
if err != nil {
file = genBigValueFileName()
}
path := filepath.Dir(s.db.Path()) + "/" + file
err = ioutil.WriteFile(path, value, 0644)
if err == nil {
err = b.Put([]byte(key), []byte(file+"p"))
}
s.file[path] = 1
return err
} else {
err := b.Put([]byte(key), value)
return err
}
})
return err
}
func (s *BoltStorage) GetBigValueFile(key string) (string, error) {
var ret []byte
err := s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(*gorr_bolt_bucket_name))
ret = b.Get([]byte(key))
return nil
})
if err != nil {
return "", err
}
sz := len(ret)
if sz == 0 {
return "", fmt.Errorf("invalid value from db")
}
if ret[sz-1] == 'p' {
file := string(ret[:sz-1])
return file, nil
}
return "", fmt.Errorf("big value not exist")
}
func (s *BoltStorage) Get(key string) ([]byte, error) {
s.mu.Lock()
defer s.mu.Unlock()
var ret []byte
err := s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(*gorr_bolt_bucket_name))
ret = b.Get([]byte(key))
return nil
})
if err != nil {
return nil, err
}
sz := len(ret)
if sz == 0 {
return nil, fmt.Errorf("invalid value from db")
}
if ret[sz-1] == 'p' {
file := string(ret[:sz-1])
path := filepath.Dir(s.db.Path()) + "/" + file
ret, err = ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read file by path from db failed, path:%s, err:%s", path, err.Error())
}
}
return ret[:len(ret)-1], nil
}
func (s *BoltStorage) Clear() {
}
func (s *BoltStorage) Close() {
s.mu.Lock()
defer s.mu.Unlock()
s.db.Close()
s.db = nil
}
func (s *BoltStorage) AllFiles() []string {
s.mu.Lock()
defer s.mu.Unlock()
all := []string{s.db.Path()}
for k := range s.file {
all = append(all, k)
}
return all
}