@@ -41,6 +41,8 @@ type Bucket struct {
41
41
//
42
42
// This is non-persisted across transactions so it must be set in every Tx.
43
43
FillPercent float64
44
+
45
+ slave * Bucket
44
46
}
45
47
46
48
// newBucket returns a new bucket associated with a transaction.
@@ -82,10 +84,20 @@ func (b *Bucket) Cursor() *Cursor {
82
84
}
83
85
}
84
86
85
- // Bucket retrieves a nested bucket by name.
87
+ // Bucket retrieves a nested bucket by name with slave.
88
+ func (b * Bucket ) Bucket (name []byte ) * Bucket {
89
+ rb := b .bucket (name )
90
+ if rb != nil && b .slave != nil {
91
+ rb .slave = b .slave .bucket (name )
92
+ }
93
+
94
+ return rb
95
+ }
96
+
97
+ // bucket retrieves a nested bucket by name.
86
98
// Returns nil if the bucket does not exist.
87
99
// The bucket instance is only valid for the lifetime of the transaction.
88
- func (b * Bucket ) Bucket (name []byte ) * Bucket {
100
+ func (b * Bucket ) bucket (name []byte ) * Bucket {
89
101
if b .buckets != nil {
90
102
if child := b .buckets [string (name )]; child != nil {
91
103
return child
@@ -142,10 +154,20 @@ func (b *Bucket) openBucket(value []byte) *Bucket {
142
154
return & child
143
155
}
144
156
145
- // CreateBucket creates a new bucket at the given key and returns the new bucket.
157
+ // CreateBucket creates a new bucket at the given key and returns the new bucket with slave.
158
+ func (b * Bucket ) CreateBucket (key []byte ) (rb * Bucket , err error ) {
159
+ rb , err = b .createBucket (key )
160
+ if err == nil && b .slave != nil {
161
+ rb .slave , err = b .slave .createBucket (key )
162
+ }
163
+
164
+ return
165
+ }
166
+
167
+ // createBucket creates a new bucket at the given key and returns the new bucket.
146
168
// Returns an error if the key already exists, if the bucket name is blank, or if the bucket name is too long.
147
169
// The bucket instance is only valid for the lifetime of the transaction.
148
- func (b * Bucket ) CreateBucket (key []byte ) (rb * Bucket , err error ) {
170
+ func (b * Bucket ) createBucket (key []byte ) (rb * Bucket , err error ) {
149
171
if lg := b .tx .db .Logger (); lg != discardLogger {
150
172
lg .Debugf ("Creating bucket %q" , key )
151
173
defer func () {
@@ -199,10 +221,20 @@ func (b *Bucket) CreateBucket(key []byte) (rb *Bucket, err error) {
199
221
return b .Bucket (newKey ), nil
200
222
}
201
223
202
- // CreateBucketIfNotExists creates a new bucket if it doesn't already exist and returns a reference to it.
224
+ // CreateBucketIfNotExists creates a new bucket with slave if it doesn't already exist and returns a reference to it.
225
+ func (b * Bucket ) CreateBucketIfNotExists (key []byte ) (rb * Bucket , err error ) {
226
+ rb , err = b .createBucketIfNotExists (key )
227
+ if err == nil && b .slave != nil {
228
+ rb .slave , err = b .slave .createBucketIfNotExists (key )
229
+ }
230
+
231
+ return
232
+ }
233
+
234
+ // createBucketIfNotExists creates a new bucket if it doesn't already exist and returns a reference to it.
203
235
// Returns an error if the bucket name is blank, or if the bucket name is too long.
204
236
// The bucket instance is only valid for the lifetime of the transaction.
205
- func (b * Bucket ) CreateBucketIfNotExists (key []byte ) (rb * Bucket , err error ) {
237
+ func (b * Bucket ) createBucketIfNotExists (key []byte ) (rb * Bucket , err error ) {
206
238
if lg := b .tx .db .Logger (); lg != discardLogger {
207
239
lg .Debugf ("Creating bucket if not exist %q" , key )
208
240
defer func () {
@@ -269,8 +301,18 @@ func (b *Bucket) CreateBucketIfNotExists(key []byte) (rb *Bucket, err error) {
269
301
}
270
302
271
303
// DeleteBucket deletes a bucket at the given key.
272
- // Returns an error if the bucket does not exist, or if the key represents a non-bucket value.
273
304
func (b * Bucket ) DeleteBucket (key []byte ) (err error ) {
305
+ err = b .deleteBucket (key )
306
+ if err == nil && b .slave != nil {
307
+ err = b .slave .deleteBucket (key )
308
+ }
309
+
310
+ return
311
+ }
312
+
313
+ // deleteBucket deletes a bucket at the given key.
314
+ // Returns an error if the bucket does not exist, or if the key represents a non-bucket value.
315
+ func (b * Bucket ) deleteBucket (key []byte ) (err error ) {
274
316
if lg := b .tx .db .Logger (); lg != discardLogger {
275
317
lg .Debugf ("Deleting bucket %q" , key )
276
318
defer func () {
@@ -327,13 +369,23 @@ func (b *Bucket) DeleteBucket(key []byte) (err error) {
327
369
return nil
328
370
}
329
371
372
+ // MoveBucket moves a sub-bucket from the source bucket to the destination bucket with slave.
373
+ func (b * Bucket ) MoveBucket (key []byte , dstBucket * Bucket ) (err error ) {
374
+ err = b .moveBucket (key , dstBucket )
375
+ if err == nil && b .slave != nil && dstBucket .slave != nil {
376
+ err = b .slave .moveBucket (key , dstBucket .slave )
377
+ }
378
+
379
+ return
380
+ }
381
+
330
382
// MoveBucket moves a sub-bucket from the source bucket to the destination bucket.
331
383
// Returns an error if
332
384
// 1. the sub-bucket cannot be found in the source bucket;
333
385
// 2. or the key already exists in the destination bucket;
334
386
// 3. or the key represents a non-bucket value;
335
387
// 4. the source and destination buckets are the same.
336
- func (b * Bucket ) MoveBucket (key []byte , dstBucket * Bucket ) (err error ) {
388
+ func (b * Bucket ) moveBucket (key []byte , dstBucket * Bucket ) (err error ) {
337
389
lg := b .tx .db .Logger ()
338
390
if lg != discardLogger {
339
391
lg .Debugf ("Moving bucket %q" , key )
@@ -445,11 +497,21 @@ func (b *Bucket) Get(key []byte) []byte {
445
497
return v
446
498
}
447
499
448
- // Put sets the value for a key in the bucket.
500
+ // Put sets the value for a key in the bucket with slave.
501
+ func (b * Bucket ) Put (key []byte , value []byte ) (err error ) {
502
+ err = b .put (key , value )
503
+ if err == nil && b .slave != nil {
504
+ err = b .slave .put (key , value )
505
+ }
506
+
507
+ return
508
+ }
509
+
510
+ // put sets the value for a key in the bucket.
449
511
// If the key exist then its previous value will be overwritten.
450
512
// Supplied value must remain valid for the life of the transaction.
451
513
// Returns an error if the bucket was created from a read-only transaction, if the key is blank, if the key is too large, or if the value is too large.
452
- func (b * Bucket ) Put (key []byte , value []byte ) (err error ) {
514
+ func (b * Bucket ) put (key []byte , value []byte ) (err error ) {
453
515
if lg := b .tx .db .Logger (); lg != discardLogger {
454
516
lg .Debugf ("Putting key %q" , key )
455
517
defer func () {
@@ -493,10 +555,20 @@ func (b *Bucket) Put(key []byte, value []byte) (err error) {
493
555
return nil
494
556
}
495
557
558
+ // Delete removes a key from the bucket with slave.
559
+ func (b * Bucket ) Delete (key []byte ) (err error ) {
560
+ err = b .delete (key )
561
+ if err == nil && b .slave != nil {
562
+ err = b .slave .delete (key )
563
+ }
564
+
565
+ return
566
+ }
567
+
496
568
// Delete removes a key from the bucket.
497
569
// If the key does not exist then nothing is done and a nil error is returned.
498
570
// Returns an error if the bucket was created from a read-only transaction.
499
- func (b * Bucket ) Delete (key []byte ) (err error ) {
571
+ func (b * Bucket ) delete (key []byte ) (err error ) {
500
572
if lg := b .tx .db .Logger (); lg != discardLogger {
501
573
lg .Debugf ("Deleting key %q" , key )
502
574
defer func () {
@@ -539,8 +611,18 @@ func (b *Bucket) Sequence() uint64 {
539
611
return b .InSequence ()
540
612
}
541
613
542
- // SetSequence updates the sequence number for the bucket.
614
+ // SetSequence updates the sequence number for the bucket with slave .
543
615
func (b * Bucket ) SetSequence (v uint64 ) error {
616
+ err := b .setSequence (v )
617
+ if err == nil && b .slave != nil {
618
+ err = b .slave .setSequence (v )
619
+ }
620
+
621
+ return err
622
+ }
623
+
624
+ // SetSequence updates the sequence number for the bucket.
625
+ func (b * Bucket ) setSequence (v uint64 ) error {
544
626
if b .tx .db == nil {
545
627
return errors .ErrTxClosed
546
628
} else if ! b .Writable () {
@@ -558,8 +640,18 @@ func (b *Bucket) SetSequence(v uint64) error {
558
640
return nil
559
641
}
560
642
561
- // NextSequence returns an autoincrementing integer for the bucket.
643
+ // NextSequence returns an autoincrementing integer for the bucket with slave .
562
644
func (b * Bucket ) NextSequence () (uint64 , error ) {
645
+ r , err := b .nextSequence ()
646
+ if err == nil && b .slave != nil {
647
+ _ , err = b .slave .nextSequence ()
648
+ }
649
+
650
+ return r , err
651
+ }
652
+
653
+ // nextSequence returns an autoincrementing integer for the bucket.
654
+ func (b * Bucket ) nextSequence () (uint64 , error ) {
563
655
if b .tx .db == nil {
564
656
return 0 , errors .ErrTxClosed
565
657
} else if ! b .Writable () {
0 commit comments