*: skip logging if logger is discardLogger

If there is no logger defined (discardLogger), skip logging altogether for
highly frequent called methods (Put, Delete, CreateBucket,
CreateBucketIfNotExists, DeleteBucket, Begin, Commit, Open, MoveBucket, Sync).

Signed-off-by: Ivan Valdes <ivan@vald.es>
pull/741/head
Ivan Valdes 2024-04-29 15:13:06 -07:00
parent 2112e9c931
commit fbea5d610d
No known key found for this signature in database
GPG Key ID: 4037D37741ED0CC5
3 changed files with 103 additions and 88 deletions

119
bucket.go
View File

@ -146,15 +146,16 @@ func (b *Bucket) openBucket(value []byte) *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) (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 lg := b.tx.db.Logger(); lg != discardLogger {
lg.Debugf("Creating bucket %q", key)
defer func() {
if err != nil {
lg.Errorf("Creating bucket %q failed: %v", key, err)
} else {
lg.Debugf("Creating bucket %q successfully", key)
}
}()
}
if b.tx.db == nil {
return nil, errors.ErrTxClosed
} else if !b.tx.writable {
@ -202,15 +203,16 @@ func (b *Bucket) CreateBucket(key []byte) (rb *Bucket, err error) {
// 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) (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 lg := b.tx.db.Logger(); lg != discardLogger {
lg.Debugf("Creating bucket if not exist %q", key)
defer func() {
if err != nil {
lg.Errorf("Creating bucket if not exist %q failed: %v", key, err)
} else {
lg.Debugf("Creating bucket if not exist %q successfully", key)
}
}()
}
if b.tx.db == nil {
return nil, errors.ErrTxClosed
@ -269,15 +271,16 @@ func (b *Bucket) CreateBucketIfNotExists(key []byte) (rb *Bucket, err 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) (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 lg := b.tx.db.Logger(); lg != discardLogger {
lg.Debugf("Deleting bucket %q", key)
defer func() {
if err != nil {
lg.Errorf("Deleting bucket %q failed: %v", key, err)
} else {
lg.Debugf("Deleting bucket %q successfully", key)
}
}()
}
if b.tx.db == nil {
return errors.ErrTxClosed
@ -332,14 +335,16 @@ func (b *Bucket) DeleteBucket(key []byte) (err error) {
// 4. the source and destination buckets are the same.
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 lg != discardLogger {
lg.Debugf("Moving bucket %q", key)
defer func() {
if err != nil {
lg.Errorf("Moving bucket %q failed: %v", key, err)
} else {
lg.Debugf("Moving bucket %q successfully", key)
}
}()
}
if b.tx.db == nil || dstBucket.tx.db == nil {
return errors.ErrTxClosed
@ -362,14 +367,14 @@ func (b *Bucket) MoveBucket(key []byte, dstBucket *Bucket) (err error) {
if !bytes.Equal(newKey, k) {
return errors.ErrBucketNotFound
} else if (flags & common.BucketLeafFlag) == 0 {
lg.Errorf("An incompatible key %s exists in the source bucket", string(newKey))
lg.Errorf("An incompatible key %s exists in the source bucket", 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) {
lg.Errorf("The source bucket (%s) and the target bucket (%s) are the same bucket", b.String(), dstBucket.String())
lg.Errorf("The source bucket (%s) and the target bucket (%s) are the same bucket", b, dstBucket)
return errors.ErrSameBuckets
}
@ -382,7 +387,7 @@ func (b *Bucket) MoveBucket(key []byte, dstBucket *Bucket) (err error) {
if (flags & common.BucketLeafFlag) != 0 {
return errors.ErrBucketExists
}
lg.Errorf("An incompatible key %s exists in the target bucket", string(newKey))
lg.Errorf("An incompatible key %s exists in the target bucket", newKey)
return errors.ErrIncompatibleValue
}
@ -445,15 +450,16 @@ func (b *Bucket) Get(key []byte) []byte {
// 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) (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 lg := b.tx.db.Logger(); lg != discardLogger {
lg.Debugf("Putting key %q", key)
defer func() {
if err != nil {
lg.Errorf("Putting key %q failed: %v", key, err)
} else {
lg.Debugf("Putting key %q successfully", key)
}
}()
}
if b.tx.db == nil {
return errors.ErrTxClosed
} else if !b.Writable() {
@ -491,15 +497,16 @@ func (b *Bucket) Put(key []byte, value []byte) (err error) {
// 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) (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 lg := b.tx.db.Logger(); lg != discardLogger {
lg.Debugf("Deleting key %q", key)
defer func() {
if err != nil {
lg.Errorf("Deleting key %q failed: %v", key, err)
} else {
lg.Debugf("Deleting key %q successfully", key)
}
}()
}
if b.tx.db == nil {
return errors.ErrTxClosed

54
db.go
View File

@ -204,14 +204,16 @@ func Open(path string, mode os.FileMode, options *Options) (db *DB, err error) {
}
lg := db.Logger()
lg.Infof("Opening db file (%s) with mode %x and with options: %s", path, mode, options)
defer func() {
if err != nil {
lg.Errorf("Opening bbolt db (%s) failed: %v", path, err)
} else {
lg.Infof("Opening bbolt db (%s) successfully", path)
}
}()
if lg != discardLogger {
lg.Infof("Opening db file (%s) with mode %x and with options: %s", path, mode, options)
defer func() {
if err != nil {
lg.Errorf("Opening bbolt db (%s) failed: %v", path, err)
} else {
lg.Infof("Opening bbolt db (%s) successfully", path)
}
}()
}
flag := os.O_RDWR
if options.ReadOnly {
@ -739,14 +741,16 @@ func (db *DB) close() error {
// IMPORTANT: You must close read-only transactions after you are finished or
// else the database will not reclaim old pages.
func (db *DB) Begin(writable bool) (t *Tx, err error) {
db.Logger().Debugf("Starting a new transaction [writable: %t]", writable)
defer func() {
if err != nil {
db.Logger().Errorf("Starting a new transaction [writable: %t] failed: %v", writable, err)
} else {
db.Logger().Debugf("Starting a new transaction [writable: %t] successfully", writable)
}
}()
if lg := db.Logger(); lg != discardLogger {
lg.Debugf("Starting a new transaction [writable: %t]", writable)
defer func() {
if err != nil {
lg.Errorf("Starting a new transaction [writable: %t] failed: %v", writable, err)
} else {
lg.Debugf("Starting a new transaction [writable: %t] successfully", writable)
}
}()
}
if writable {
return db.beginRWTx()
@ -1095,14 +1099,16 @@ func safelyCall(fn func(*Tx) error, tx *Tx) (err error) {
// This is not necessary under normal operation, however, if you use NoSync
// then it allows you to force the database file to sync against the disk.
func (db *DB) Sync() (err error) {
db.Logger().Debug("Syncing bbolt db (%s)", db.path)
defer func() {
if err != nil {
db.Logger().Errorf("[GOOS: %s, GOARCH: %s] syncing bbolt db (%s) failed: %v", runtime.GOOS, runtime.GOARCH, db.path, err)
} else {
db.Logger().Debugf("Syncing bbolt db (%s) successfully", db.path)
}
}()
if lg := db.Logger(); lg != discardLogger {
lg.Debug("Syncing bbolt db (%s)", db.path)
defer func() {
if err != nil {
lg.Errorf("[GOOS: %s, GOARCH: %s] syncing bbolt db (%s) failed: %v", runtime.GOOS, runtime.GOARCH, db.path, err)
} else {
lg.Debugf("Syncing bbolt db (%s) successfully", db.path)
}
}()
}
return fdatasync(db)
}

18
tx.go
View File

@ -170,14 +170,16 @@ func (tx *Tx) OnCommit(fn func()) {
func (tx *Tx) Commit() (err error) {
txId := tx.ID()
lg := tx.db.Logger()
lg.Debugf("Committing transaction %d", txId)
defer func() {
if err != nil {
lg.Errorf("Committing transaction failed: %v", err)
} else {
lg.Debugf("Committing transaction %d successfully", txId)
}
}()
if lg != discardLogger {
lg.Debugf("Committing transaction %d", txId)
defer func() {
if err != nil {
lg.Errorf("Committing transaction failed: %v", err)
} else {
lg.Debugf("Committing transaction %d successfully", txId)
}
}()
}
common.Assert(!tx.managed, "managed tx commit not allowed")
if tx.db == nil {