Skip to content

Commit

Permalink
Add log into MoveBucket and clone the key
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Wang <[email protected]>
  • Loading branch information
ahrtr committed Jan 2, 2024
1 parent 0bd26bc commit 886eccb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
48 changes: 32 additions & 16 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,21 @@ func (b *Bucket) DeleteBucket(key []byte) (err error) {
return errors.ErrTxNotWritable
}

newKey := cloneBytes(key)

// Move cursor to correct position.
c := b.Cursor()
k, _, flags := c.seek(key)
k, _, flags := c.seek(newKey)

// Return an error if bucket doesn't exist or is not a bucket.
if !bytes.Equal(key, k) {
if !bytes.Equal(newKey, k) {
return errors.ErrBucketNotFound
} else if (flags & common.BucketLeafFlag) == 0 {
return errors.ErrIncompatibleValue
}

// Recursively delete all child buckets.
child := b.Bucket(key)
child := b.Bucket(newKey)
err = child.ForEachBucket(func(k []byte) error {
if err := child.DeleteBucket(k); err != nil {
return fmt.Errorf("delete bucket: %s", err)
Expand All @@ -309,15 +311,15 @@ func (b *Bucket) DeleteBucket(key []byte) (err error) {
}

// Remove cached copy.
delete(b.buckets, string(key))
delete(b.buckets, string(newKey))

// Release all bucket pages to freelist.
child.nodes = nil
child.rootNode = nil
child.free()

// Delete the node if we have a matching key.
c.node().del(key)
c.node().del(newKey)

return nil
}
Expand All @@ -328,48 +330,62 @@ func (b *Bucket) DeleteBucket(key []byte) (err error) {
// 2. or the key already exists in the destination bucket;
// 3. or the key represents a non-bucket value;
// 4. the source and destination buckets are the same.
func (b *Bucket) MoveBucket(key []byte, dstBucket *Bucket) error {
func (b *Bucket) MoveBucket(key []byte, dstBucket *Bucket) (err error) {
lg := b.tx.db.Logger()
lg.Debugf("Moving bucket %q", string(key))
defer func() {
if err != nil {
lg.Errorf("Moving bucket %q failed: %v", string(key), err)
} else {
lg.Debugf("Moving bucket %q successfully", string(key))
}
}()

if b.tx.db == nil || dstBucket.tx.db == nil {
return errors.ErrTxClosed
} else if !dstBucket.Writable() {
return errors.ErrTxNotWritable
}

newKey := cloneBytes(key)

// Move cursor to correct position.
c := b.Cursor()
k, v, flags := c.seek(key)
k, v, flags := c.seek(newKey)

// Return an error if bucket doesn't exist or is not a bucket.
if !bytes.Equal(key, k) {
if !bytes.Equal(newKey, k) {
return errors.ErrBucketNotFound
} else if (flags & common.BucketLeafFlag) == 0 {
return fmt.Errorf("key %q isn't a bucket in the source bucket: %w", key, errors.ErrIncompatibleValue)
lg.Errorf("An incompatible key %s exists in the source bucket", string(newKey))
return errors.ErrIncompatibleValue
}

// Do nothing (return true directly) if the source bucket and the
// destination bucket are actually the same bucket.
if b == dstBucket || (b.RootPage() == dstBucket.RootPage() && b.RootPage() != 0) {
return fmt.Errorf("source bucket %s and target bucket %s are the same: %w", b.String(), dstBucket.String(), errors.ErrSameBuckets)
lg.Errorf("The source bucket (%s) and the target bucket (%s) are the same bucket", b.String(), dstBucket.String())
return errors.ErrSameBuckets
}

// check whether the key already exists in the destination bucket
curDst := dstBucket.Cursor()
k, _, flags = curDst.seek(key)
k, _, flags = curDst.seek(newKey)

// Return an error if there is an existing key in the destination bucket.
if bytes.Equal(key, k) {
if bytes.Equal(newKey, k) {
if (flags & common.BucketLeafFlag) != 0 {
return errors.ErrBucketExists
}
return fmt.Errorf("key %q already exists in the target bucket: %w", key, errors.ErrIncompatibleValue)
lg.Errorf("An incompatible key %s exists in the target bucket", string(newKey))
return errors.ErrIncompatibleValue
}

// remove the sub-bucket from the source bucket
delete(b.buckets, string(key))
c.node().del(key)
delete(b.buckets, string(newKey))
c.node().del(newKey)

// add te sub-bucket to the destination bucket
newKey := cloneBytes(key)
newValue := cloneBytes(v)
curDst.node().put(newKey, newKey, newValue, 0, common.BucketLeafFlag)

Expand Down
2 changes: 1 addition & 1 deletion movebucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func createBucketAndPopulateData(t testing.TB, tx *bbolt.Tx, bk *bbolt.Bucket, b

newBucket, err := bk.CreateBucket([]byte(bucketName))
require.NoError(t, err, "failed to create bucket %s", bucketName)
populateSampleDataInBucket(t, bk, rand.Intn(4096))
populateSampleDataInBucket(t, newBucket, rand.Intn(4096))
return newBucket
}

Expand Down

0 comments on commit 886eccb

Please sign in to comment.