-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
480 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,27 @@ | ||
package bbolt | ||
|
||
import ( | ||
"context" | ||
|
||
"go.etcd.io/bbolt" | ||
) | ||
|
||
func dbView[T any](db *bbolt.DB, bucketName string, createBucketIfNotExists bool, callback func(*bbolt.Tx, *bbolt.Bucket) (T, error)) (result T, err error) { | ||
err = db.View(func(tx *bbolt.Tx) error { | ||
var bucket *bbolt.Bucket | ||
if createBucketIfNotExists { | ||
bucket, err = tx.CreateBucketIfNotExists([]byte(bucketName)) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
bucket = tx.Bucket([]byte(bucketName)) | ||
if bucket == nil { | ||
return nil | ||
} | ||
} | ||
result, err = callback(tx, bucket) | ||
return err | ||
}) | ||
return result, err | ||
} | ||
"github.com/UnAfraid/wg-ui/pkg/dbx" | ||
) | ||
|
||
func dbUpdate[T any](db *bbolt.DB, bucketName string, createBucketIfNotExists bool, callback func(*bbolt.Tx, *bbolt.Bucket) (T, error)) (result T, err error) { | ||
err = db.Update(func(tx *bbolt.Tx) error { | ||
func dbTx[T any](ctx context.Context, db *bbolt.DB, bucketName string, createBucketIfNotExists bool, callback func(*bbolt.Tx, *bbolt.Bucket) (T, error)) (T, error) { | ||
return dbx.InBBoltTransactionScopeWithResult(ctx, db, func(ctx context.Context, tx *bbolt.Tx) (result T, err error) { | ||
var bucket *bbolt.Bucket | ||
if createBucketIfNotExists { | ||
bucket, err = tx.CreateBucketIfNotExists([]byte(bucketName)) | ||
if err != nil { | ||
return err | ||
return result, err | ||
} | ||
} else { | ||
bucket = tx.Bucket([]byte(bucketName)) | ||
if bucket == nil { | ||
return nil | ||
return result, nil | ||
} | ||
} | ||
result, err = callback(tx, bucket) | ||
return err | ||
return callback(tx, bucket) | ||
}) | ||
return result, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package dbx | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"go.etcd.io/bbolt" | ||
) | ||
|
||
type contextKey struct{ name string } | ||
|
||
var bboltTxKey = contextKey{name: "bboltTxKey"} | ||
|
||
type bboltTransactionScope struct { | ||
db *bbolt.DB | ||
} | ||
|
||
func NewBBoltTransactionScope(db *bbolt.DB) TransactionScoper { | ||
return &bboltTransactionScope{ | ||
db: db, | ||
} | ||
} | ||
|
||
func (txScope *bboltTransactionScope) InTransactionScope(ctx context.Context, transactionScope func(ctx context.Context) error) (err error) { | ||
return InBBoltTransactionScope(ctx, txScope.db, func(ctx context.Context, tx *bbolt.Tx) error { | ||
return transactionScope(ctx) | ||
}) | ||
} | ||
|
||
func InBBoltTransactionScope(ctx context.Context, db *bbolt.DB, transactionScope func(ctx context.Context, tx *bbolt.Tx) error) (retErr error) { | ||
tx, transactionCloser, err := useOrStartBBoltTransaction(ctx, db) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
retErr = transactionCloser(retErr) | ||
}() | ||
|
||
return transactionScope(context.WithValue(ctx, bboltTxKey, tx), tx) | ||
} | ||
|
||
func InBBoltTransactionScopeWithResult[T any](ctx context.Context, db *bbolt.DB, transactionScope func(ctx context.Context, tx *bbolt.Tx) (T, error)) (result T, err error) { | ||
tx, transactionCloser, err := useOrStartBBoltTransaction(ctx, db) | ||
if err != nil { | ||
return result, err | ||
} | ||
|
||
defer func() { | ||
err = transactionCloser(err) | ||
}() | ||
|
||
return transactionScope(context.WithValue(ctx, bboltTxKey, tx), tx) | ||
} | ||
|
||
func useOrStartBBoltTransaction(ctx context.Context, db *bbolt.DB) (*bbolt.Tx, func(err error) error, error) { | ||
tx, ok := ctx.Value(bboltTxKey).(*bbolt.Tx) | ||
if !ok { | ||
tx, err := db.Begin(true) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
transactionScope := func(err error) error { | ||
if err != nil { | ||
if txErr := tx.Rollback(); txErr != nil { | ||
err = errors.Join(err, txErr) | ||
} | ||
} else { | ||
if txErr := tx.Commit(); txErr != nil { | ||
err = txErr | ||
} | ||
} | ||
return err | ||
} | ||
|
||
return tx, transactionScope, nil | ||
} else { | ||
transactionCloser := func(err error) error { | ||
return err | ||
} | ||
return tx, transactionCloser, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dbx | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type TransactionScoper interface { | ||
InTransactionScope(ctx context.Context, transactionScope func(ctx context.Context) error) error | ||
} | ||
|
||
func InTransactionScopeWithResult[T any](ctx context.Context, transactionScoper TransactionScoper, transactionScope func(ctx context.Context) (T, error)) (result T, err error) { | ||
err = transactionScoper.InTransactionScope(ctx, func(ctx context.Context) error { | ||
result, err = transactionScope(ctx) | ||
return err | ||
}) | ||
return result, err | ||
} |
Oops, something went wrong.