2020-08-17 06:45:52 +00:00
|
|
|
package ethdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-10-27 22:30:18 +00:00
|
|
|
"github.com/google/btree"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/log"
|
2020-08-17 06:45:52 +00:00
|
|
|
)
|
|
|
|
|
2021-03-30 09:53:54 +00:00
|
|
|
// Implements ethdb.Getter for Tx
|
|
|
|
type roTxDb struct {
|
|
|
|
tx Tx
|
2021-06-16 04:00:35 +00:00
|
|
|
top bool
|
2021-03-30 09:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRoTxDb(tx Tx) *roTxDb {
|
|
|
|
return &roTxDb{tx: tx, top: true}
|
|
|
|
}
|
|
|
|
|
2021-04-05 13:04:58 +00:00
|
|
|
func (m *roTxDb) GetOne(bucket string, key []byte) ([]byte, error) {
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := m.tx.Cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-30 09:53:54 +00:00
|
|
|
defer c.Close()
|
|
|
|
_, v, err := c.SeekExact(key)
|
2021-04-05 13:04:58 +00:00
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *roTxDb) Get(bucket string, key []byte) ([]byte, error) {
|
|
|
|
dat, err := m.GetOne(bucket, key)
|
|
|
|
return getOneWrapper(dat, err)
|
2021-03-30 09:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *roTxDb) Has(bucket string, key []byte) (bool, error) {
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := m.tx.Cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-03-30 09:53:54 +00:00
|
|
|
defer c.Close()
|
|
|
|
_, v, err := c.SeekExact(key)
|
|
|
|
|
|
|
|
return v != nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *roTxDb) Walk(bucket string, startkey []byte, fixedbits int, walker func([]byte, []byte) (bool, error)) error {
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := m.tx.Cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-30 09:53:54 +00:00
|
|
|
defer c.Close()
|
|
|
|
return Walk(c, startkey, fixedbits, walker)
|
|
|
|
}
|
|
|
|
|
2021-06-05 15:17:04 +00:00
|
|
|
func (m *roTxDb) ForEach(bucket string, fromPrefix []byte, walker func(k, v []byte) error) error {
|
|
|
|
return m.tx.ForEach(bucket, fromPrefix, walker)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *roTxDb) ForPrefix(bucket string, prefix []byte, walker func(k, v []byte) error) error {
|
|
|
|
return m.tx.ForPrefix(bucket, prefix, walker)
|
|
|
|
}
|
|
|
|
func (m *roTxDb) ForAmount(bucket string, prefix []byte, amount uint32, walker func(k, v []byte) error) error {
|
|
|
|
return m.tx.ForAmount(bucket, prefix, amount, walker)
|
|
|
|
}
|
|
|
|
|
2021-04-03 06:26:00 +00:00
|
|
|
func (m *roTxDb) BeginGetter(ctx context.Context) (GetterTx, error) {
|
2021-03-30 09:53:54 +00:00
|
|
|
return &roTxDb{tx: m.tx, top: false}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *roTxDb) Rollback() {
|
|
|
|
if m.top {
|
|
|
|
m.tx.Rollback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *roTxDb) Tx() Tx {
|
|
|
|
return m.tx
|
|
|
|
}
|
|
|
|
|
2021-04-20 14:41:46 +00:00
|
|
|
func NewRwTxDb(tx Tx) *TxDb {
|
|
|
|
return &TxDb{tx: tx, cursors: map[string]Cursor{}}
|
|
|
|
}
|
|
|
|
|
2020-08-17 06:45:52 +00:00
|
|
|
// TxDb - provides Database interface around ethdb.Tx
|
|
|
|
// It's not thread-safe!
|
2020-08-24 11:07:59 +00:00
|
|
|
// TxDb not usable after .Commit()/.Rollback() call, but usable after .CommitAndBegin() call
|
2021-03-22 06:47:01 +00:00
|
|
|
// you can put unlimited amount of data into this class
|
2020-08-17 06:45:52 +00:00
|
|
|
// Walk and MultiWalk methods - work outside of Tx object yet, will implement it later
|
|
|
|
type TxDb struct {
|
2021-02-10 17:04:22 +00:00
|
|
|
db Database
|
|
|
|
tx Tx
|
|
|
|
cursors map[string]Cursor
|
2021-06-16 04:00:35 +00:00
|
|
|
txFlags TxFlags
|
2021-02-10 17:04:22 +00:00
|
|
|
len uint64
|
2020-08-17 06:45:52 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 20:01:01 +00:00
|
|
|
func WrapIntoTxDB(tx RwTx) *TxDb {
|
2021-05-01 07:42:23 +00:00
|
|
|
return &TxDb{tx: tx, cursors: map[string]Cursor{}}
|
|
|
|
}
|
|
|
|
|
2020-08-17 06:45:52 +00:00
|
|
|
func (m *TxDb) Close() {
|
|
|
|
panic("don't call me")
|
|
|
|
}
|
|
|
|
|
2020-08-26 06:02:10 +00:00
|
|
|
// NewTxDbWithoutTransaction creates TxDb object without opening transaction,
|
|
|
|
// such TxDb not usable before .Begin() call on it
|
|
|
|
// It allows inject TxDb object into class hierarchy, but open write transaction later
|
2021-03-21 05:20:44 +00:00
|
|
|
func NewTxDbWithoutTransaction(db Database, flags TxFlags) DbWithPendingMutations {
|
2020-10-25 08:38:55 +00:00
|
|
|
return &TxDb{db: db, txFlags: flags}
|
2020-08-26 06:02:10 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 08:38:55 +00:00
|
|
|
func (m *TxDb) Begin(ctx context.Context, flags TxFlags) (DbWithPendingMutations, error) {
|
2020-08-26 06:02:10 +00:00
|
|
|
batch := m
|
|
|
|
if m.tx != nil {
|
2021-02-10 17:04:22 +00:00
|
|
|
panic("nested transactions not supported")
|
2020-08-26 06:02:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 17:04:22 +00:00
|
|
|
if err := batch.begin(ctx, flags); err != nil {
|
2020-08-17 06:45:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return batch, nil
|
|
|
|
}
|
|
|
|
|
2021-04-03 06:26:00 +00:00
|
|
|
func (m *TxDb) BeginGetter(ctx context.Context) (GetterTx, error) {
|
2021-03-30 09:53:54 +00:00
|
|
|
batch := m
|
|
|
|
if m.tx != nil {
|
|
|
|
panic("nested transactions not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := batch.begin(ctx, RO); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return batch, nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 06:36:49 +00:00
|
|
|
func (m *TxDb) cursor(bucket string) (Cursor, error) {
|
2021-01-15 09:38:09 +00:00
|
|
|
c, ok := m.cursors[bucket]
|
|
|
|
if !ok {
|
2021-04-02 06:36:49 +00:00
|
|
|
var err error
|
|
|
|
c, err = m.tx.Cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-15 09:38:09 +00:00
|
|
|
m.cursors[bucket] = c
|
|
|
|
}
|
2021-04-02 06:36:49 +00:00
|
|
|
return c, nil
|
2021-01-15 09:38:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-20 14:12:54 +00:00
|
|
|
func (m *TxDb) IncrementSequence(bucket string, amount uint64) (res uint64, err error) {
|
2021-03-21 13:15:25 +00:00
|
|
|
return m.tx.(RwTx).IncrementSequence(bucket, amount)
|
2021-03-20 14:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) ReadSequence(bucket string) (res uint64, err error) {
|
|
|
|
return m.tx.ReadSequence(bucket)
|
2020-11-14 13:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 06:45:52 +00:00
|
|
|
func (m *TxDb) Put(bucket string, key []byte, value []byte) error {
|
|
|
|
m.len += uint64(len(key) + len(value))
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := m.cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.(RwCursor).Put(key, value)
|
2020-08-17 06:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) Append(bucket string, key []byte, value []byte) error {
|
|
|
|
m.len += uint64(len(key) + len(value))
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := m.cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.(RwCursor).Append(key, value)
|
2020-11-28 14:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) AppendDup(bucket string, key []byte, value []byte) error {
|
|
|
|
m.len += uint64(len(key) + len(value))
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := m.cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.(RwCursorDupSort).AppendDup(key, value)
|
2020-08-17 06:45:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 13:19:31 +00:00
|
|
|
func (m *TxDb) Delete(bucket string, k, v []byte) error {
|
|
|
|
m.len += uint64(len(k))
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := m.cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.(RwCursor).Delete(k, v)
|
2020-08-17 06:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) NewBatch() DbWithPendingMutations {
|
|
|
|
return &mutation{
|
|
|
|
db: m,
|
2020-10-27 22:30:18 +00:00
|
|
|
puts: btree.New(32),
|
2020-08-17 06:45:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 17:04:22 +00:00
|
|
|
func (m *TxDb) begin(ctx context.Context, flags TxFlags) error {
|
2021-03-30 09:53:54 +00:00
|
|
|
kv := m.db.(HasRwKV).RwKV()
|
2021-03-21 13:15:25 +00:00
|
|
|
|
|
|
|
var tx Tx
|
|
|
|
var err error
|
|
|
|
if flags&RO != 0 {
|
2021-04-03 06:26:00 +00:00
|
|
|
tx, err = kv.BeginRo(ctx)
|
2021-03-21 13:15:25 +00:00
|
|
|
} else {
|
|
|
|
tx, err = kv.BeginRw(ctx)
|
|
|
|
}
|
2020-08-17 06:45:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-26 06:02:10 +00:00
|
|
|
m.tx = tx
|
2020-09-10 12:35:58 +00:00
|
|
|
m.cursors = make(map[string]Cursor, 16)
|
2020-08-17 06:45:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-30 09:53:54 +00:00
|
|
|
func (m *TxDb) RwKV() RwKV {
|
2020-08-26 06:02:10 +00:00
|
|
|
panic("not allowed to get KV interface because you will loose transaction, please use .Tx() method")
|
2020-08-17 06:45:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 04:11:28 +00:00
|
|
|
// Last can only be called from the transaction thread
|
2020-08-17 06:45:52 +00:00
|
|
|
func (m *TxDb) Last(bucket string) ([]byte, []byte, error) {
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := m.cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, nil, err
|
|
|
|
}
|
|
|
|
return c.Last()
|
2020-08-17 06:45:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 13:04:58 +00:00
|
|
|
func (m *TxDb) GetOne(bucket string, key []byte) ([]byte, error) {
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := m.cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, v, err := c.SeekExact(key)
|
2021-04-05 13:04:58 +00:00
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) Get(bucket string, key []byte) ([]byte, error) {
|
|
|
|
dat, err := m.GetOne(bucket, key)
|
|
|
|
return getOneWrapper(dat, err)
|
2020-08-17 06:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) Has(bucket string, key []byte) (bool, error) {
|
|
|
|
v, err := m.Get(bucket, key)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return v != nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) MultiPut(tuples ...[]byte) (uint64, error) {
|
2021-03-21 13:15:25 +00:00
|
|
|
return 0, MultiPut(m.tx.(RwTx), tuples...)
|
2020-08-17 06:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) BatchSize() int {
|
|
|
|
return int(m.len)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) Walk(bucket string, startkey []byte, fixedbits int, walker func([]byte, []byte) (bool, error)) error {
|
2021-03-12 09:27:04 +00:00
|
|
|
// get cursor out of pool, then calls txDb.Put/Get/Delete on same bucket inside Walk callback - will not affect state of Walk
|
|
|
|
c, ok := m.cursors[bucket]
|
|
|
|
if ok {
|
|
|
|
delete(m.cursors, bucket)
|
|
|
|
} else {
|
2021-04-02 06:36:49 +00:00
|
|
|
var err error
|
|
|
|
c, err = m.tx.Cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-12 09:27:04 +00:00
|
|
|
}
|
|
|
|
defer func() { // put cursor back to pool if can
|
|
|
|
if _, ok = m.cursors[bucket]; ok {
|
|
|
|
c.Close()
|
|
|
|
} else {
|
|
|
|
m.cursors[bucket] = c
|
|
|
|
}
|
|
|
|
}()
|
2020-10-25 08:38:55 +00:00
|
|
|
return Walk(c, startkey, fixedbits, walker)
|
2020-08-17 06:45:52 +00:00
|
|
|
}
|
2021-06-05 15:17:04 +00:00
|
|
|
func (m *TxDb) ForEach(bucket string, fromPrefix []byte, walker func(k, v []byte) error) error {
|
|
|
|
return m.tx.ForEach(bucket, fromPrefix, walker)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) ForPrefix(bucket string, prefix []byte, walker func(k, v []byte) error) error {
|
|
|
|
return m.tx.ForPrefix(bucket, prefix, walker)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) ForAmount(bucket string, prefix []byte, amount uint32, walker func(k, v []byte) error) error {
|
|
|
|
return m.tx.ForAmount(bucket, prefix, amount, walker)
|
|
|
|
}
|
2020-08-17 06:45:52 +00:00
|
|
|
|
2020-09-28 17:18:36 +00:00
|
|
|
func (m *TxDb) CommitAndBegin(ctx context.Context) error {
|
2021-03-22 12:41:52 +00:00
|
|
|
err := m.Commit()
|
2020-08-24 11:07:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-10 17:04:22 +00:00
|
|
|
return m.begin(ctx, m.txFlags)
|
2020-09-28 17:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) RollbackAndBegin(ctx context.Context) error {
|
|
|
|
m.Rollback()
|
2021-02-10 17:04:22 +00:00
|
|
|
return m.begin(ctx, m.txFlags)
|
2020-08-24 11:07:59 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 12:41:52 +00:00
|
|
|
func (m *TxDb) Commit() error {
|
2020-08-26 06:02:10 +00:00
|
|
|
if m.tx == nil {
|
2021-03-22 12:41:52 +00:00
|
|
|
return fmt.Errorf("second call .Commit() on same transaction")
|
2020-08-17 06:45:52 +00:00
|
|
|
}
|
2021-04-03 06:26:00 +00:00
|
|
|
if err := m.tx.Commit(); err != nil {
|
2021-03-22 12:41:52 +00:00
|
|
|
return err
|
2020-08-17 06:45:52 +00:00
|
|
|
}
|
2020-08-26 06:02:10 +00:00
|
|
|
m.tx = nil
|
2020-08-17 06:45:52 +00:00
|
|
|
m.cursors = nil
|
|
|
|
m.len = 0
|
2021-03-22 12:41:52 +00:00
|
|
|
return nil
|
2020-08-17 06:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) Rollback() {
|
2020-08-26 06:02:10 +00:00
|
|
|
if m.tx == nil {
|
2020-08-17 06:45:52 +00:00
|
|
|
return
|
|
|
|
}
|
2020-08-26 06:02:10 +00:00
|
|
|
m.tx.Rollback()
|
2020-08-17 06:45:52 +00:00
|
|
|
m.cursors = nil
|
2020-08-26 06:02:10 +00:00
|
|
|
m.tx = nil
|
2020-08-17 06:45:52 +00:00
|
|
|
m.len = 0
|
|
|
|
}
|
|
|
|
|
2020-08-26 06:02:10 +00:00
|
|
|
func (m *TxDb) Tx() Tx {
|
|
|
|
return m.tx
|
|
|
|
}
|
|
|
|
|
2020-08-17 06:45:52 +00:00
|
|
|
func (m *TxDb) Keys() ([][]byte, error) {
|
|
|
|
panic("don't use me")
|
|
|
|
}
|
|
|
|
|
2020-09-08 19:39:43 +00:00
|
|
|
func (m *TxDb) BucketExists(name string) (bool, error) {
|
|
|
|
exists := false
|
|
|
|
migrator, ok := m.tx.(BucketMigrator)
|
|
|
|
if !ok {
|
|
|
|
return false, fmt.Errorf("%T doesn't implement ethdb.TxMigrator interface", m.tx)
|
|
|
|
}
|
|
|
|
exists = migrator.ExistsBucket(name)
|
|
|
|
return exists, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) ClearBuckets(buckets ...string) error {
|
|
|
|
for i := range buckets {
|
|
|
|
name := buckets[i]
|
|
|
|
|
|
|
|
migrator, ok := m.tx.(BucketMigrator)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T doesn't implement ethdb.TxMigrator interface", m.tx)
|
|
|
|
}
|
|
|
|
if err := migrator.ClearBucket(name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxDb) DropBuckets(buckets ...string) error {
|
|
|
|
for i := range buckets {
|
|
|
|
name := buckets[i]
|
|
|
|
log.Info("Dropping bucket", "name", name)
|
|
|
|
migrator, ok := m.tx.(BucketMigrator)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T doesn't implement ethdb.TxMigrator interface", m.tx)
|
|
|
|
}
|
|
|
|
if err := migrator.DropBucket(name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|