mdbx: add BeginRwAsync method (#767)

This commit is contained in:
Alex Sharov 2022-12-07 11:19:08 +07:00 committed by GitHub
parent bf912d4eaa
commit 2dcec83222
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 9 deletions

View File

@ -180,6 +180,7 @@ type RwDB interface {
Update(ctx context.Context, f func(tx RwTx) error) error
BeginRw(ctx context.Context) (RwTx, error)
BeginRwAsync(ctx context.Context) (RwTx, error)
}
type StatelessReadTx interface {

View File

@ -471,7 +471,10 @@ func (db *MdbxKV) BeginRo(ctx context.Context) (txn kv.Tx, err error) {
}, nil
}
func (db *MdbxKV) BeginRw(_ context.Context) (txn kv.RwTx, err error) {
func (db *MdbxKV) BeginRw(_ context.Context) (kv.RwTx, error) { return db.beginRw(0) }
func (db *MdbxKV) BeginRwAsync(_ context.Context) (kv.RwTx, error) { return db.beginRw(mdbx.TxNoSync) }
func (db *MdbxKV) beginRw(flags uint) (txn kv.RwTx, err error) {
if db.closed.Load() {
return nil, fmt.Errorf("db closed")
}
@ -482,7 +485,7 @@ func (db *MdbxKV) BeginRw(_ context.Context) (txn kv.RwTx, err error) {
}
}()
tx, err := db.env.BeginTxn(nil, 0)
tx, err := db.env.BeginTxn(nil, flags)
if err != nil {
runtime.UnlockOSThread() // unlock only in case of error. normal flow is "defer .Rollback()"
return nil, fmt.Errorf("%w, lable: %s, trace: %s", err, db.opts.label.String(), stack2.Trace().String())

View File

@ -25,24 +25,24 @@ import (
)
type TemporaryMdbx struct {
db kv.RwDB
chaindata string
db kv.RwDB
path string
}
func NewTemporaryMdbx() (kv.RwDB, error) {
chaindata, err := os.MkdirTemp("", "mdbx-temp")
path, err := os.MkdirTemp("", "mdbx-temp")
if err != nil {
return &TemporaryMdbx{}, err
}
db, err := Open(chaindata, log.Root(), false)
db, err := Open(path, log.Root(), false)
if err != nil {
return &TemporaryMdbx{}, err
}
return &TemporaryMdbx{
db: db,
chaindata: chaindata,
db: db,
path: path,
}, nil
}
@ -53,6 +53,9 @@ func (t *TemporaryMdbx) Update(ctx context.Context, f func(kv.RwTx) error) error
func (t *TemporaryMdbx) BeginRw(ctx context.Context) (kv.RwTx, error) {
return t.db.BeginRw(ctx)
}
func (t *TemporaryMdbx) BeginRwAsync(ctx context.Context) (kv.RwTx, error) {
return t.db.BeginRwAsync(ctx)
}
func (t *TemporaryMdbx) View(ctx context.Context, f func(kv.Tx) error) error {
return t.db.View(ctx, f)
@ -72,5 +75,5 @@ func (t *TemporaryMdbx) PageSize() uint64 {
func (t *TemporaryMdbx) Close() {
t.db.Close()
os.RemoveAll(t.chaindata)
os.RemoveAll(t.path)
}

View File

@ -160,6 +160,9 @@ func (db *RemoteKV) BeginRo(ctx context.Context) (txn kv.Tx, err error) {
func (db *RemoteKV) BeginRw(ctx context.Context) (kv.RwTx, error) {
return nil, fmt.Errorf("remote db provider doesn't support .BeginRw method")
}
func (db *RemoteKV) BeginRwAsync(ctx context.Context) (kv.RwTx, error) {
return nil, fmt.Errorf("remote db provider doesn't support .BeginRw method")
}
func (db *RemoteKV) View(ctx context.Context, f func(tx kv.Tx) error) (err error) {
tx, err := db.BeginRo(ctx)