From c8cd6f567f3dece86e11a050679a2af0595287b1 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Sat, 24 Dec 2022 11:18:49 +0700 Subject: [PATCH] save --- kv/kv_interface.go | 1 + kv/mdbx/kv_mdbx.go | 21 +++++++++++++++++++++ kv/mdbx/kv_mdbx_temporary.go | 4 ++++ kv/remotedb/kv_remote.go | 3 +++ 4 files changed, 29 insertions(+) diff --git a/kv/kv_interface.go b/kv/kv_interface.go index 4bfb6f26b..5d5e80138 100644 --- a/kv/kv_interface.go +++ b/kv/kv_interface.go @@ -188,6 +188,7 @@ type RwDB interface { RoDB Update(ctx context.Context, f func(tx RwTx) error) error + UpdateAsync(ctx context.Context, f func(tx RwTx) error) error BeginRw(ctx context.Context) (RwTx, error) BeginRwAsync(ctx context.Context) (RwTx, error) diff --git a/kv/mdbx/kv_mdbx.go b/kv/mdbx/kv_mdbx.go index 490aca468..8d1dca662 100644 --- a/kv/mdbx/kv_mdbx.go +++ b/kv/mdbx/kv_mdbx.go @@ -669,6 +669,27 @@ func (db *MdbxKV) View(ctx context.Context, f func(tx kv.Tx) error) (err error) return f(tx) } +func (db *MdbxKV) UpdateAsync(ctx context.Context, f func(tx kv.RwTx) error) (err error) { + if db.closed.Load() { + return fmt.Errorf("db closed") + } + + tx, err := db.BeginRwAsync(ctx) + if err != nil { + return err + } + defer tx.Rollback() + err = f(tx) + if err != nil { + return err + } + err = tx.Commit() + if err != nil { + return err + } + return nil +} + func (db *MdbxKV) Update(ctx context.Context, f func(tx kv.RwTx) error) (err error) { if db.closed.Load() { return fmt.Errorf("db closed") diff --git a/kv/mdbx/kv_mdbx_temporary.go b/kv/mdbx/kv_mdbx_temporary.go index 2c2a5535e..f4ba972cb 100644 --- a/kv/mdbx/kv_mdbx_temporary.go +++ b/kv/mdbx/kv_mdbx_temporary.go @@ -50,6 +50,10 @@ func (t *TemporaryMdbx) Update(ctx context.Context, f func(kv.RwTx) error) error return t.db.Update(ctx, f) } +func (t *TemporaryMdbx) UpdateAsync(ctx context.Context, f func(kv.RwTx) error) error { + return t.db.UpdateAsync(ctx, f) +} + func (t *TemporaryMdbx) BeginRw(ctx context.Context) (kv.RwTx, error) { return t.db.BeginRw(ctx) } diff --git a/kv/remotedb/kv_remote.go b/kv/remotedb/kv_remote.go index c26e29f69..2e3a76902 100644 --- a/kv/remotedb/kv_remote.go +++ b/kv/remotedb/kv_remote.go @@ -178,6 +178,9 @@ func (db *RemoteKV) View(ctx context.Context, f func(tx kv.Tx) error) (err error func (db *RemoteKV) Update(ctx context.Context, f func(tx kv.RwTx) error) (err error) { return fmt.Errorf("remote db provider doesn't support .Update method") } +func (db *RemoteKV) UpdateAsync(ctx context.Context, f func(tx kv.RwTx) error) (err error) { + return fmt.Errorf("remote db provider doesn't support .Update method") +} func (tx *remoteTx) ViewID() uint64 { return tx.id } func (tx *remoteTx) CollectMetrics() {}