@@ -285,19 +285,21 @@ func (b *Bucket) DeleteBucket(key []byte) (err error) {
285
285
return errors .ErrTxNotWritable
286
286
}
287
287
288
+ newKey := cloneBytes (key )
289
+
288
290
// Move cursor to correct position.
289
291
c := b .Cursor ()
290
- k , _ , flags := c .seek (key )
292
+ k , _ , flags := c .seek (newKey )
291
293
292
294
// Return an error if bucket doesn't exist or is not a bucket.
293
- if ! bytes .Equal (key , k ) {
295
+ if ! bytes .Equal (newKey , k ) {
294
296
return errors .ErrBucketNotFound
295
297
} else if (flags & common .BucketLeafFlag ) == 0 {
296
298
return errors .ErrIncompatibleValue
297
299
}
298
300
299
301
// Recursively delete all child buckets.
300
- child := b .Bucket (key )
302
+ child := b .Bucket (newKey )
301
303
err = child .ForEachBucket (func (k []byte ) error {
302
304
if err := child .DeleteBucket (k ); err != nil {
303
305
return fmt .Errorf ("delete bucket: %s" , err )
@@ -309,15 +311,15 @@ func (b *Bucket) DeleteBucket(key []byte) (err error) {
309
311
}
310
312
311
313
// Remove cached copy.
312
- delete (b .buckets , string (key ))
314
+ delete (b .buckets , string (newKey ))
313
315
314
316
// Release all bucket pages to freelist.
315
317
child .nodes = nil
316
318
child .rootNode = nil
317
319
child .free ()
318
320
319
321
// Delete the node if we have a matching key.
320
- c .node ().del (key )
322
+ c .node ().del (newKey )
321
323
322
324
return nil
323
325
}
@@ -328,48 +330,62 @@ func (b *Bucket) DeleteBucket(key []byte) (err error) {
328
330
// 2. or the key already exists in the destination bucket;
329
331
// 3. or the key represents a non-bucket value;
330
332
// 4. the source and destination buckets are the same.
331
- func (b * Bucket ) MoveBucket (key []byte , dstBucket * Bucket ) error {
333
+ func (b * Bucket ) MoveBucket (key []byte , dstBucket * Bucket ) (err error ) {
334
+ lg := b .tx .db .Logger ()
335
+ lg .Debugf ("Moving bucket %q" , string (key ))
336
+ defer func () {
337
+ if err != nil {
338
+ lg .Errorf ("Moving bucket %q failed: %v" , string (key ), err )
339
+ } else {
340
+ lg .Debugf ("Moving bucket %q successfully" , string (key ))
341
+ }
342
+ }()
343
+
332
344
if b .tx .db == nil || dstBucket .tx .db == nil {
333
345
return errors .ErrTxClosed
334
346
} else if ! dstBucket .Writable () {
335
347
return errors .ErrTxNotWritable
336
348
}
337
349
350
+ newKey := cloneBytes (key )
351
+
338
352
// Move cursor to correct position.
339
353
c := b .Cursor ()
340
- k , v , flags := c .seek (key )
354
+ k , v , flags := c .seek (newKey )
341
355
342
356
// Return an error if bucket doesn't exist or is not a bucket.
343
- if ! bytes .Equal (key , k ) {
357
+ if ! bytes .Equal (newKey , k ) {
344
358
return errors .ErrBucketNotFound
345
359
} else if (flags & common .BucketLeafFlag ) == 0 {
346
- return fmt .Errorf ("key %q isn't a bucket in the source bucket: %w" , key , errors .ErrIncompatibleValue )
360
+ lg .Errorf ("An incompatible key %s exists in the source bucket" , string (newKey ))
361
+ return errors .ErrIncompatibleValue
347
362
}
348
363
349
364
// Do nothing (return true directly) if the source bucket and the
350
365
// destination bucket are actually the same bucket.
351
366
if b == dstBucket || (b .RootPage () == dstBucket .RootPage () && b .RootPage () != 0 ) {
352
- return fmt .Errorf ("source bucket %s and target bucket %s are the same: %w" , b .String (), dstBucket .String (), errors .ErrSameBuckets )
367
+ lg .Errorf ("The source bucket (%s) and the target bucket (%s) are the same bucket" , b .String (), dstBucket .String ())
368
+ return errors .ErrSameBuckets
353
369
}
354
370
355
371
// check whether the key already exists in the destination bucket
356
372
curDst := dstBucket .Cursor ()
357
- k , _ , flags = curDst .seek (key )
373
+ k , _ , flags = curDst .seek (newKey )
358
374
359
375
// Return an error if there is an existing key in the destination bucket.
360
- if bytes .Equal (key , k ) {
376
+ if bytes .Equal (newKey , k ) {
361
377
if (flags & common .BucketLeafFlag ) != 0 {
362
378
return errors .ErrBucketExists
363
379
}
364
- return fmt .Errorf ("key %q already exists in the target bucket: %w" , key , errors .ErrIncompatibleValue )
380
+ lg .Errorf ("An incompatible key %s exists in the target bucket" , string (newKey ))
381
+ return errors .ErrIncompatibleValue
365
382
}
366
383
367
384
// remove the sub-bucket from the source bucket
368
- delete (b .buckets , string (key ))
369
- c .node ().del (key )
385
+ delete (b .buckets , string (newKey ))
386
+ c .node ().del (newKey )
370
387
371
388
// add te sub-bucket to the destination bucket
372
- newKey := cloneBytes (key )
373
389
newValue := cloneBytes (v )
374
390
curDst .node ().put (newKey , newKey , newValue , 0 , common .BucketLeafFlag )
375
391
0 commit comments