This commit is contained in:
alex.sharov 2022-12-24 11:18:49 +07:00
parent 5c2433d8cf
commit c8cd6f567f
4 changed files with 29 additions and 0 deletions

View File

@ -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)

View File

@ -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")

View File

@ -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)
}

View File

@ -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() {}