-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.go
169 lines (153 loc) · 3.88 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
// Package badgerstorage implements certmagic.Storage on top of a Badger database.
//
// It's an alternative to the default file-system storage used by CertMagic.
package badgerstorage
import (
"bytes"
"fmt"
"github.com/caddyserver/certmagic"
"github.com/dgraph-io/badger/v2"
"oya.to/namedlocker"
)
var (
_ certmagic.Storage = (*Storage)(nil)
)
// Storage implements certmagic.Storage
type Storage struct {
// DB is the underlying badger database
DB *badger.DB
ls namedlocker.Store
}
// Lock implements certmagic.Storage.Lock
func (sto *Storage) Lock(key string) error {
sto.ls.Lock(key)
return nil
}
// Unlock implements certmagic.Storage.Unlock
func (sto *Storage) Unlock(key string) error {
return sto.ls.TryUnlock(key)
}
// Store implements certmagic.Storage.Store
func (sto *Storage) Store(key string, value []byte) error {
err := sto.DB.Update(func(txn *badger.Txn) error {
return txn.Set([]byte(key), value)
})
if err != nil {
return fmt.Errorf("Storage.Store: %w", err)
}
return nil
}
// Load implements certmagic.Storage.Load
func (sto *Storage) Load(key string) ([]byte, error) {
var val []byte
err := sto.DB.View(func(txn *badger.Txn) error {
itm, err := txn.Get([]byte(key))
if err != nil {
return err
}
return itm.Value(func(v []byte) error {
val = append([]byte(nil), v...)
return nil
})
})
if err != nil {
return nil, fmt.Errorf("Storage.Load: %w", err)
}
return val, nil
}
// Delete implements certmagic.Storage.Delete
func (sto *Storage) Delete(key string) error {
err := sto.DB.Update(func(txn *badger.Txn) error {
k := []byte(key)
return txn.Delete(k)
})
if err != nil {
return fmt.Errorf("Storage.Delete: %w", err)
}
return nil
}
// Exists implements certmagic.Storage.Exists
func (sto *Storage) Exists(key string) bool {
err := sto.DB.View(func(txn *badger.Txn) error {
_, err := txn.Get([]byte(key))
return err
})
return err == nil
}
// List implements certmagic.Storage.List
func (sto *Storage) List(prefix string, recursive bool) ([]string, error) {
seen := map[string]bool{}
var keys []string
err := sto.DB.View(func(txn *badger.Txn) error {
pfx := make([]byte, 0, len(prefix)+1)
pfx = append(pfx, prefix...)
pfx = append(pfx, '/')
it := txn.NewIterator(badger.IteratorOptions{Prefix: pfx})
defer it.Close()
it.Rewind()
if !it.Valid() {
return badger.ErrKeyNotFound
}
for ; it.Valid(); it.Next() {
walkKey(it.Item().Key(), len(pfx), recursive, func(k []byte) {
if seen[string(k)] {
return
}
seen[string(k)] = true
keys = append(keys, string(k))
})
}
return nil
})
if err != nil {
return nil, fmt.Errorf("Storage.List: %w", err)
}
return keys, nil
}
// Stat implements certmagic.Storage.Stat
func (sto *Storage) Stat(key string) (certmagic.KeyInfo, error) {
inf := certmagic.KeyInfo{Key: key}
err := sto.DB.View(func(txn *badger.Txn) error {
fn := make([]byte, 0, len(key)+1)
fn = append(fn, key...)
// if it exists, it must be a "file"
itm, err := txn.Get(fn)
if err == nil {
inf.IsTerminal = true
// itm.ValueSize() is only an "estimate",
// but certmagic - at this time - seems to only use it for sorting, so it's ok.
inf.Size = itm.ValueSize()
return nil
}
// otherwise, look for a "directory"
dir := append(fn, '/')
it := txn.NewIterator(badger.IteratorOptions{Prefix: dir})
defer it.Close()
if it.Rewind(); !it.ValidForPrefix(dir) {
return badger.ErrKeyNotFound
}
return nil
})
if err != nil {
return certmagic.KeyInfo{}, fmt.Errorf("Storage.Stat: %w", err)
}
return inf, nil
}
// New return a new Storage using db to store persisted data.
func New(db *badger.DB) *Storage {
return &Storage{DB: db}
}
func walkKey(k []byte, sp int, recursive bool, f func([]byte)) {
if sp >= len(k) {
return
}
if i := bytes.IndexByte(k[sp:], '/'); i >= 0 {
sp += i
} else {
sp = len(k)
}
f(k[:sp])
if recursive {
walkKey(k, sp+1, recursive, f)
}
}