-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
341 lines (282 loc) · 7 KB
/
database.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package musly
import (
"encoding/binary"
"github.com/boltdb/bolt"
)
const binNumTracks = 1000
var (
// DatabaseVersion indicates the internal version of the boltdb
DatabaseVersion = []byte{0, 0, 1}
// static bucket names and keys
metadataBucket = []byte("metadata")
// stores jukebox.method used
metadataMethod = []byte("method")
// stores jukebox.decoder used
metadataDecoder = []byte("decoder")
// stores DatabaseVersion
metadataVersion = []byte("database_version")
// stores amount of tracks per entry in jukeboxBucket
metadataBinNumTracks = []byte("bin_num_tracks")
jukeboxMusicStyle = []byte("jukebox.music_style_set")
jukeboxBucket = []byte("jukebox")
jukeboxTrackBucket = []byte("tracks")
)
var putInt = binary.BigEndian.PutUint64
func (b *Box) storeJukebox() error {
// jukebox is stored in segments as to not require the full thing to be in
// memory twice while loading it. Bucket layout uses sequential keys
// starting from 0;
//
// 0: jukebox header
// 1: tracks segment
// 2: tracks segment
// 3: ...
var intBuf = make([]byte, 8)
// lock for the whole function, so we can have a consistent read state from
// musly internals
b.jukeboxMu.RLock()
defer b.jukeboxMu.RUnlock()
err := b.DB.Update(func(tx *bolt.Tx) error {
tx.DeleteBucket(jukeboxBucket)
bkt, err := tx.CreateBucket(jukeboxBucket)
if err != nil {
return err
}
// write out the jukebox header first
var buf = make([]byte, b.jukebox.binSize(1, 0))
n, err := b.jukebox.toBin(buf, 1, 0, 0)
if err != nil {
return err
}
// write header
err = bkt.Put(intBuf, buf[:n])
if err != nil {
return err
}
// store the size of each segment
bkt = tx.Bucket(metadataBucket)
if bkt == nil {
return ErrInvalidDatabase
}
if b.musicStyleSet.IsSet() {
err = bkt.Put(jukeboxMusicStyle, []byte("true"))
if err != nil {
return err
}
}
putInt(intBuf, binNumTracks)
return bkt.Put(metadataBinNumTracks, intBuf)
})
if err != nil {
return err
}
var max = b.jukebox.trackCount()
var pos = 0
var buf []byte
fn := func(tx *bolt.Tx) error {
putInt(intBuf, uint64(pos)/binNumTracks+1)
bkt := tx.Bucket(jukeboxBucket)
size := b.jukebox.binSize(0, binNumTracks)
if len(buf) < size {
buf = make([]byte, size)
}
n, err := b.jukebox.toBin(buf, 0, binNumTracks, pos)
if err != nil {
return err
}
// write track segment
return bkt.Put(intBuf, buf[:n])
}
for pos = 0; pos < max; pos += binNumTracks {
err = b.DB.Update(fn)
if err != nil {
return err
}
}
return nil
}
func (b *Box) loadJukebox() error {
return b.DB.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket(jukeboxBucket)
if bkt == nil {
// nothing to load
return &Error{
Err: ErrInvalidDatabase,
Info: "missing jukebox",
}
}
intBuf := make([]byte, 8)
header := bkt.Get(intBuf)
if header == nil {
return &Error{
Err: ErrInvalidDatabase,
Info: "missing jukebox header",
}
}
b.jukeboxMu.Lock()
defer b.jukeboxMu.Unlock()
expected, err := b.jukebox.fromBin(header, 1, 0)
if err != nil {
return err
}
for pos := 0; pos < expected; pos += int(b.binNumTracks) {
putInt(intBuf, uint64(pos)/b.binNumTracks+1)
segment := bkt.Get(intBuf)
if segment == nil {
return &Error{
Err: ErrInvalidDatabase,
Info: "missing jukebox segment",
}
}
var amount = int(b.binNumTracks)
if pos+amount > expected {
amount = expected - pos
}
_, err = b.jukebox.fromBin(segment, 0, amount)
if err != nil {
return err
}
}
return nil
})
}
// storeTrack stores a track for later use with the id given
func (b *Box) storeTrack(t track, id TrackID) error {
return b.DB.Batch(func(tx *bolt.Tx) error {
bkt, err := tx.CreateBucketIfNotExists(jukeboxTrackBucket)
if err != nil {
return err
}
if b.musicStyleSet.IsSet() {
b.jukeboxMu.RLock()
err = b.jukebox.addTrack(t, id)
b.jukeboxMu.RUnlock()
if err != nil {
return err
}
}
key := make([]byte, 8)
putInt(key, uint64(id))
return bkt.Put(key, b.trackToBytes(t))
})
}
// loadTrack returns a Track previously stored by a call to storeTrack
func (b *Box) loadTrack(id TrackID) (track, error) {
t := b.newTrackBytes()
err := b.DB.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket(jukeboxTrackBucket)
if bkt == nil {
return nil
}
key := make([]byte, 8)
putInt(key, uint64(id))
tt := bkt.Get(key)
if tt == nil {
return &Error{
Err: ErrMissingTrack,
IDs: []TrackID{id},
}
}
copy(t, tt)
return nil
})
if err != nil {
b.freeTrack(b.bytesToTrack(t))
return nil, err
}
return b.bytesToTrack(t), nil
}
// loadTracks returns multiple tracks previously stored by calls to storeTrack
func (b *Box) loadTracks(ids []TrackID) ([]track, error) {
var tracks = make([]track, len(ids))
err := b.DB.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket(jukeboxTrackBucket)
if bkt == nil {
return nil
}
key := make([]byte, 8)
var err *Error
b.jukeboxMu.RLock() // hold lock for newTrack calls
for i, id := range ids {
t := b.newTrack()
putInt(key, uint64(id))
v := bkt.Get(key)
copy(b.trackToBytes(t), v)
tracks[i] = t
if v == nil {
if err != nil {
err.IDs = append(err.IDs, id)
} else {
err = &Error{
Err: ErrMissingTrack,
IDs: []TrackID{id},
}
}
}
}
b.jukeboxMu.RUnlock()
return err
})
if err != nil {
b.freeTracks(tracks)
return nil, err
}
return tracks, nil
}
// TrackCount returns the amount of tracks in the box
func (b *Box) TrackCount() int {
var n int
// ignore an unlikely database error here, we assume the user will be using
// the track count with some other function, so they will catch a persistent
// database error at that point. This approach makes us able to not have to
// return an error in this function
b.DB.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket(jukeboxTrackBucket)
if bkt == nil {
return nil
}
n = bkt.Stats().KeyN
return nil
})
return n
}
// AllTrackIDs returns all TrackIDs stored in this box
func (b *Box) AllTrackIDs() ([]TrackID, error) {
var ids = make([]TrackID, 0, 1024)
err := b.DB.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket(jukeboxTrackBucket)
if bkt == nil {
return nil
}
c := bkt.Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
ids = append(ids, TrackID(binary.BigEndian.Uint64(k)))
}
return nil
})
return ids, err
}
// RemoveTrack removes a single track, see RemoveTracks
func (b *Box) RemoveTrack(id TrackID) error {
return b.RemoveTracks([]TrackID{id})
}
// RemoveTracks removes all IDs given from both the jukebox aswell as the
// internal track database. IDs that don't exist are ignored.
func (b *Box) RemoveTracks(ids []TrackID) error {
b.modified.Set()
return b.DB.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket(jukeboxTrackBucket)
if bkt == nil {
return nil
}
key := make([]byte, 8)
for _, id := range ids {
putInt(key, uint64(id))
bkt.Delete(key)
}
b.jukeboxMu.Lock()
b.jukebox.removeTracks(ids)
b.jukeboxMu.Unlock()
return nil
})
}