Skip to content

Commit

Permalink
Merge pull request #646 from ahrtr/logger_bbolt_20231207
Browse files Browse the repository at this point in the history
Add log messages
  • Loading branch information
ahrtr authored Dec 29, 2023
2 parents de6c42b + 4c7075e commit c1eb992
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 42 deletions.
60 changes: 54 additions & 6 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,16 @@ func (b *Bucket) openBucket(value []byte) *Bucket {
// CreateBucket creates a new bucket at the given key and returns the new bucket.
// Returns an error if the key already exists, if the bucket name is blank, or if the bucket name is too long.
// The bucket instance is only valid for the lifetime of the transaction.
func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
func (b *Bucket) CreateBucket(key []byte) (rb *Bucket, err error) {
lg := b.tx.db.Logger()
lg.Debugf("Creating bucket %q", string(key))
defer func() {
if err != nil {
lg.Errorf("Creating bucket %q failed: %v", string(key), err)
} else {
lg.Debugf("Creating bucket %q successfully", string(key))
}
}()
if b.tx.db == nil {
return nil, errors.ErrTxClosed
} else if !b.tx.writable {
Expand Down Expand Up @@ -192,7 +201,17 @@ func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
// CreateBucketIfNotExists creates a new bucket if it doesn't already exist and returns a reference to it.
// Returns an error if the bucket name is blank, or if the bucket name is too long.
// The bucket instance is only valid for the lifetime of the transaction.
func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) {
func (b *Bucket) CreateBucketIfNotExists(key []byte) (rb *Bucket, err error) {
lg := b.tx.db.Logger()
lg.Debugf("Creating bucket if not exist %q", string(key))
defer func() {
if err != nil {
lg.Errorf("Creating bucket if not exist %q failed: %v", string(key), err)
} else {
lg.Debugf("Creating bucket if not exist %q successfully", string(key))
}
}()

if b.tx.db == nil {
return nil, errors.ErrTxClosed
} else if !b.tx.writable {
Expand Down Expand Up @@ -249,7 +268,17 @@ func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) {

// DeleteBucket deletes a bucket at the given key.
// Returns an error if the bucket does not exist, or if the key represents a non-bucket value.
func (b *Bucket) DeleteBucket(key []byte) error {
func (b *Bucket) DeleteBucket(key []byte) (err error) {
lg := b.tx.db.Logger()
lg.Debugf("Deleting bucket %q", string(key))
defer func() {
if err != nil {
lg.Errorf("Deleting bucket %q failed: %v", string(key), err)
} else {
lg.Debugf("Deleting bucket %q successfully", string(key))
}
}()

if b.tx.db == nil {
return errors.ErrTxClosed
} else if !b.Writable() {
Expand All @@ -269,7 +298,7 @@ func (b *Bucket) DeleteBucket(key []byte) error {

// Recursively delete all child buckets.
child := b.Bucket(key)
err := child.ForEachBucket(func(k []byte) error {
err = child.ForEachBucket(func(k []byte) error {
if err := child.DeleteBucket(k); err != nil {
return fmt.Errorf("delete bucket: %s", err)
}
Expand Down Expand Up @@ -316,7 +345,16 @@ func (b *Bucket) Get(key []byte) []byte {
// If the key exist then its previous value will be overwritten.
// Supplied value must remain valid for the life of the transaction.
// 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.
func (b *Bucket) Put(key []byte, value []byte) error {
func (b *Bucket) Put(key []byte, value []byte) (err error) {
lg := b.tx.db.Logger()
lg.Debugf("Putting key %q", string(key))
defer func() {
if err != nil {
lg.Errorf("Putting key %q failed: %v", string(key), err)
} else {
lg.Debugf("Putting key %q successfully", string(key))
}
}()
if b.tx.db == nil {
return errors.ErrTxClosed
} else if !b.Writable() {
Expand Down Expand Up @@ -353,7 +391,17 @@ func (b *Bucket) Put(key []byte, value []byte) error {
// Delete removes a key from the bucket.
// If the key does not exist then nothing is done and a nil error is returned.
// Returns an error if the bucket was created from a read-only transaction.
func (b *Bucket) Delete(key []byte) error {
func (b *Bucket) Delete(key []byte) (err error) {
lg := b.tx.db.Logger()
lg.Debugf("Deleting key %q", string(key))
defer func() {
if err != nil {
lg.Errorf("Deleting key %q failed: %v", string(key), err)
} else {
lg.Debugf("Deleting key %q successfully", string(key))
}
}()

if b.tx.db == nil {
return errors.ErrTxClosed
} else if !b.Writable() {
Expand Down
Loading

0 comments on commit c1eb992

Please sign in to comment.