This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
badger.go
190 lines (164 loc) · 4.7 KB
/
badger.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
/*
* Author: Markus Stenberg <fingon@iki.fi>
*
* Copyright (c) 2017 Markus Stenberg
*
* Created: Sat Dec 23 15:10:01 2017 mstenber
* Last modified: Wed Feb 6 11:34:03 2019 mstenber
* Edit time: 181 min
*
*/
package badger
import (
"log"
"github.com/dgraph-io/badger"
"github.com/fingon/go-tfhfs/mlog"
"github.com/fingon/go-tfhfs/storage"
)
// badgerBackend provides on-disk storage.
//
// - key prefix 1 + block id -> metadata
// - key prefix 2 + block id -> data (essentially immutable)
// - key prefix 3 + name -> block id
type badgerBackend struct {
storage.DirectoryBackendBase
db *badger.DB
}
var _ storage.Backend = &badgerBackend{}
// Init makes the instance actually useful
func NewBadgerBackend() storage.Backend {
self := &badgerBackend{}
return self
}
func (self *badgerBackend) Init(config storage.BackendConfiguration) {
dir := config.Directory
(&self.DirectoryBackendBase).Init(config)
opts := badger.DefaultOptions
opts.Dir = dir
opts.ValueDir = dir
// Default ValueLogFileSize is 1GB (1<<30), which is bit much
// if I want to rsync files across at some point. So use
// 1<<27. Even 128MB files should not be THAT common for my
// typical use cases..
opts.ValueLogFileSize = 1 << 27
if config.Unsafe {
opts.SyncWrites = false
}
db, err := badger.Open(opts)
if err != nil {
log.Panic("badger.Open", err)
}
self.db = db
}
func (self *badgerBackend) Flush() {
mlog.Printf2("storage/badger/badger", "bad.Flush start")
// 0.5 = 2x write amplification (but 50% storage efficiency)
// 0.2 = 5x write amplification (but 80% storage efficiency)
// TBD: This should be a parameter..
err := self.db.RunValueLogGC(0.5)
// 5x write amplification(!)
if err != nil && err != badger.ErrNoRewrite {
log.Panic(err)
}
mlog.Printf2("storage/badger/badger", " gc done %v", err)
}
func (self *badgerBackend) Close() {
mlog.Printf2("storage/badger/badger", "bad.Close")
self.db.Close()
}
func (self *badgerBackend) DeleteBlock(b *storage.Block) {
mlog.Printf2("storage/badger/badger", "bad.DeleteBlock %x", b.Id)
self.db.Update(func(txn *badger.Txn) error {
k := append([]byte("1"), []byte(b.Id)...)
if err := self.delete(k); err != nil {
log.Panic("txn.Delete", err)
}
k = append([]byte("2"), []byte(b.Id)...)
if err := self.delete(k); err != nil {
log.Panic("txn.Delete 2", err)
}
return nil
})
}
func (self *badgerBackend) getKKValue(prefix, suffix []byte) (v []byte, err error) {
err = self.db.View(func(txn *badger.Txn) error {
k := append(prefix, suffix...)
i, err := txn.Get(k)
if err == nil {
v, err = i.ValueCopy(nil)
}
return err
})
return
}
func (self *badgerBackend) GetBlockData(b *storage.Block) []byte {
bv, _ := self.getKKValue([]byte("2"), []byte(b.Id))
return bv
}
func (self *badgerBackend) GetBlockById(id string) *storage.Block {
bv, err := self.getKKValue([]byte("1"), []byte(id))
if err == badger.ErrKeyNotFound {
return nil
}
if err != nil {
log.Panic("get error:", err)
}
b := &storage.Block{Id: id, Backend: self}
_, err = b.BlockMetadata.UnmarshalMsg(bv)
if err != nil {
log.Panic(err)
}
mlog.Printf2("storage/badger/badger", "b.GetBlockById %x", id)
return b
}
func (self *badgerBackend) GetBlockIdByName(name string) string {
bv, err := self.getKKValue([]byte("3"), []byte(name))
if err == badger.ErrKeyNotFound {
return ""
}
if err != nil {
log.Panic("get error:", err)
}
return string(bv)
}
func (self *badgerBackend) setKKValue(prefix, suffix, value []byte) {
k := append(prefix, suffix...)
if err := self.set(k, value); err != nil {
log.Panic("set", err)
}
}
func (self *badgerBackend) delete(k []byte) error {
return self.db.Update(func(txn *badger.Txn) error {
return txn.Delete(k)
})
}
func (self *badgerBackend) set(k, v []byte) error {
return self.db.Update(func(txn *badger.Txn) error {
return txn.Set(k, v)
})
}
func (self *badgerBackend) SetNameToBlockId(name, block_id string) {
mlog.Printf2("storage/badger/badger", "bad.SetNameToBlockId %s = %x", name, block_id)
self.setKKValue([]byte("3"), []byte(name), []byte(block_id))
}
func (self *badgerBackend) StoreBlock(b *storage.Block) {
data := *b.Data.Get()
mlog.Printf2("storage/badger/badger", "bad.StoreBlock %x (%d b)", b.Id, len(data))
self.updateBlock(b)
self.setKKValue([]byte("2"), []byte(b.Id), data)
}
func (self *badgerBackend) updateBlock(b *storage.Block) {
buf, err := b.BlockMetadata.MarshalMsg(nil)
if err != nil {
log.Panic(err)
}
self.setKKValue([]byte("1"), []byte(b.Id), buf)
}
func (self *badgerBackend) UpdateBlock(b *storage.Block) int {
mlog.Printf2("storage/badger/badger", "bad.UpdateBlock %x", b.Id)
self.updateBlock(b)
return 1
}
func (self *badgerBackend) Supports(feature storage.BackendFeature) bool {
return false
}