-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket_api.go
370 lines (316 loc) · 10.2 KB
/
bucket_api.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright 2023-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package rosmar
import (
"context"
"database/sql"
"errors"
"fmt"
sgbucket "github.com/couchbase/sg-bucket"
)
func (bucket *Bucket) String() string {
return fmt.Sprintf("B#%d", bucket.serial)
}
//////// Interface BucketStore
// The URL used to open the bucket.
func (bucket *Bucket) GetURL() string { return bucket.url }
// The bucket's name. This defaults to the last path component of the URL.
func (bucket *Bucket) GetName() string { return bucket.name }
// Renames the bucket. This doesn't affect its URL, only the value returned by GetName.
func (bucket *Bucket) setName(name string) error {
info("Bucket %s is now named %q", bucket, name)
_, err := bucket.db().Exec(`UPDATE bucket SET name=?1`, name)
if err == nil {
bucket.name = name
}
return err
}
// The universally unique ID given the bucket when it was created.
func (bucket *Bucket) UUID() (string, error) {
var uuid string
row := bucket.db().QueryRow(`SELECT uuid FROM bucket;`)
err := scan(row, &uuid)
return uuid, err
}
// Closes a bucket.
func (bucket *Bucket) Close(_ context.Context) {
traceEnter("Bucket.Close", "%s", bucket)
unregisterBucket(bucket)
bucket.mutex.Lock()
defer bucket.mutex.Unlock()
bucket.closed = true
}
// _closeSqliteDB closes the underlying sqlite database and shuts down dcpFeeds. Must have a lock to call this function.
func (bucket *Bucket) _closeSqliteDB() {
bucket.expManager.stop()
for _, c := range bucket.collections {
c.close()
}
if bucket.sqliteDB != nil {
bucket.sqliteDB.Close()
bucket.collections = nil
}
}
// Closes a bucket and deletes its directory and files (unless it's in-memory.)
func (bucket *Bucket) CloseAndDelete(ctx context.Context) (err error) {
bucket.mutex.Lock()
defer bucket.mutex.Unlock()
bucket._closeSqliteDB()
return deleteBucket(ctx, bucket)
}
func (bucket *Bucket) IsSupported(feature sgbucket.BucketStoreFeature) bool {
switch feature {
case sgbucket.BucketStoreFeatureCollections:
return true
case sgbucket.BucketStoreFeatureSubdocOperations:
return true
case sgbucket.BucketStoreFeatureXattrs:
return true
case sgbucket.BucketStoreFeatureCrc32cMacroExpansion:
return true
case sgbucket.BucketStoreFeaturePreserveExpiry:
return true
case sgbucket.BucketStoreFeatureN1ql:
return false
case sgbucket.BucketStoreFeatureMultiXattrSubdocOperations:
return true
default:
return false
}
}
func (bucket *Bucket) GetMaxVbno() (uint16, error) {
return kNumVbuckets, nil
}
//////// DATA STORES:
var defaultDataStoreName = sgbucket.DataStoreNameImpl{
Scope: sgbucket.DefaultScope,
Collection: sgbucket.DefaultCollection,
}
func validateName(name sgbucket.DataStoreName) (sgbucket.DataStoreNameImpl, error) {
return sgbucket.NewValidDataStoreName(name.ScopeName(), name.CollectionName())
}
func (bucket *Bucket) DefaultDataStore() sgbucket.DataStore {
traceEnter("DefaultDataStore", "%s", bucket)
collection, err := bucket.getOrCreateCollection(defaultDataStoreName, true)
if err != nil {
warn("DefaultDataStore() -> %v", err)
return nil
}
return collection
}
func (bucket *Bucket) NamedDataStore(name sgbucket.DataStoreName) (sgbucket.DataStore, error) {
traceEnter("NamedDataStore", "%s.%s", bucket, name)
sc, err := validateName(name)
if err != nil {
warn("NamedDataStore(%q) -> %v", name, err)
return nil, err
}
collection, err := bucket.getOrCreateCollection(sc, true)
if err != nil {
err = fmt.Errorf("unable to retrieve NamedDataStore for rosmar Bucket: %v", err)
warn("NamedDataStore(%q) -> %v", name, err)
return nil, err
}
return collection, nil
}
func (bucket *Bucket) CreateDataStore(_ context.Context, name sgbucket.DataStoreName) error {
traceEnter("CreateDataStore", "%s.%s", bucket, name)
sc, err := validateName(name)
if err != nil {
return err
}
_, err = bucket.createCollection(sc)
return err
}
func (bucket *Bucket) DropDataStore(name sgbucket.DataStoreName) error {
traceEnter("DropDataStore", "%s.%s", bucket, name)
sc, err := validateName(name)
if err != nil {
return err
}
return bucket.dropCollection(sc)
}
// ListDataStores returns a list of the names of all data stores in the bucket.
func (bucket *Bucket) ListDataStores() (result []sgbucket.DataStoreName, err error) {
traceEnter("ListDataStores", "%s", bucket)
defer func() { traceExit("ListDataStores", err, "%v", result) }()
rows, err := bucket.db().Query(`SELECT id, scope, name FROM collections ORDER BY id`)
if err != nil {
return nil, err
}
for rows.Next() {
var id CollectionID
var scope, name string
if err := rows.Scan(&id, &scope, &name); err != nil {
return nil, err
}
result = append(result, sgbucket.DataStoreNameImpl{Scope: scope, Collection: name})
}
err = rows.Close()
return
}
//////// COLLECTION INTERNALS:
func (bucket *Bucket) _getCollectionID(scope, collection string) (id CollectionID, err error) {
row := bucket._db().QueryRow(`SELECT id FROM collections WHERE scope=?1 AND name=?2`, scope, collection)
err = scan(row, &id)
return
}
func (bucket *Bucket) createCollection(name sgbucket.DataStoreNameImpl) (*Collection, error) {
bucket.mutex.Lock()
defer bucket.mutex.Unlock()
return bucket._createCollection(name)
}
// caller must hold bucket mutex
func (bucket *Bucket) _createCollection(name sgbucket.DataStoreNameImpl) (*Collection, error) {
result, err := bucket._db().Exec(`INSERT INTO collections (scope, name) VALUES (?, ?)`, name.Scope, name.Collection)
if err != nil {
return nil, err
}
collectionID, err := result.LastInsertId()
if err != nil {
return nil, err
}
return bucket._initCollection(name, CollectionID(collectionID)), nil
}
func (bucket *Bucket) _initCollection(name sgbucket.DataStoreNameImpl, id CollectionID) *Collection {
collection := newCollection(bucket, name, id)
bucket.collections[name] = collection
return collection
}
func (bucket *Bucket) getCollection(name sgbucket.DataStoreNameImpl) (*Collection, error) {
return bucket.getOrCreateCollection(name, false)
}
func (bucket *Bucket) getOrCreateCollection(name sgbucket.DataStoreNameImpl, orCreate bool) (*Collection, error) {
bucket.mutex.Lock()
defer bucket.mutex.Unlock()
if collection, ok := bucket.collections[name]; ok {
return collection, nil
}
id, err := bucket._getCollectionID(name.Scope, name.Collection)
if err == nil {
return bucket._initCollection(name, id), nil
} else if err == sql.ErrNoRows {
if orCreate {
return bucket._createCollection(name)
} else {
return nil, sgbucket.MissingError{Key: name.String()}
}
} else {
return nil, err
}
}
func (bucket *Bucket) getOpenCollectionByID(id CollectionID) *Collection {
bucket.mutex.Lock()
defer bucket.mutex.Unlock()
for _, coll := range bucket.collections {
if coll.id == id {
return coll
}
}
return nil
}
/* unused (so far)
func (bucket *Bucket) getCollectionByID(id CollectionID) (*Collection, error) {
bucket.mutex.Lock()
defer bucket.mutex.Unlock()
for _, coll := range bucket.collections {
if coll.id == id {
return coll, nil
}
}
row := bucket.db().QueryRow(`SELECT scope,name FROM collections WHERE id=?1`, id)
var scope, name string
err := scan(row, &scope, &name)
if err != nil {
return nil, err
}
dsName := sgbucket.DataStoreNameImpl{Scope: scope, Collection: name}
return bucket._initCollection(dsName, id), nil
}
*/
func (bucket *Bucket) dropCollection(name sgbucket.DataStoreNameImpl) error {
if name.IsDefault() {
return errors.New("default collection cannot be dropped")
}
bucket.mutex.Lock()
defer bucket.mutex.Unlock()
if c := bucket.collections[name]; c != nil {
c.close()
delete(bucket.collections, name)
}
_, err := bucket._db().Exec(`DELETE FROM collections WHERE scope=? AND name=?`, name.ScopeName(), name.CollectionName())
if err != nil {
return err
}
return nil
}
//////// EXPIRATION (CUSTOM API):
// nextExpiration returns the earliest expiration time of any document, or 0 if none.
func (bucket *Bucket) nextExpiration() (exp Exp, err error) {
var expVal sql.NullInt64
row := bucket.db().QueryRow(`SELECT min(exp) FROM documents WHERE exp > 0`)
err = scan(row, &expVal)
if expVal.Valid {
exp = Exp(expVal.Int64)
}
return
}
// expireDocuments immediately deletes all expired documents in this bucket.
func (bucket *Bucket) expireDocuments() (int64, error) {
names, err := bucket.ListDataStores()
if err != nil {
return 0, err
}
var count int64
for _, name := range names {
if coll, err := bucket.getCollection(name.(sgbucket.DataStoreNameImpl)); err != nil {
return 0, err
} else if n, err := coll.expireDocuments(); err != nil {
return 0, err
} else {
count += n
}
}
return count, nil
}
// scheduleExpiration schedules the next expiration of documents to occur, from the minimum expiration value in the bucket. This requires locking expiration manager.
func (bucket *Bucket) _scheduleExpiration() {
if nextExp, err := bucket.nextExpiration(); err == nil && nextExp > 0 {
bucket.expManager._scheduleExpirationAtOrBefore(nextExp)
}
}
func (bucket *Bucket) doExpiration() {
bucket.expManager._clearNext()
debug("EXP: Running scheduled expiration...")
if n, err := bucket.expireDocuments(); err != nil {
// If there's an error expiring docs, it means there is a programming error of a leaked expiration goroutine.
panic("Error expiring docs: " + err.Error())
} else if n > 0 {
info("Bucket %s expired %d docs", bucket, n)
}
bucket._scheduleExpiration()
}
// Completely removes all deleted documents (tombstones).
func (bucket *Bucket) PurgeTombstones() (count int64, err error) {
traceEnter("PurgeTombstones", "")
err = bucket.inTransaction(func(txn *sql.Tx) error {
result, err := txn.Exec(`DELETE FROM documents WHERE value IS NULL`)
if err == nil {
count, err = result.RowsAffected()
}
return err
})
traceExit("PurgeTombstones", err, "%d", count)
return
}
var (
// Enforce interface conformance:
_ sgbucket.BucketStore = &Bucket{}
_ sgbucket.DynamicDataStoreBucket = &Bucket{}
_ sgbucket.DeleteableStore = &Bucket{}
)