Skip to content

Commit

Permalink
Mutation: implement StatelessRwTx (erigontech#1670)
Browse files Browse the repository at this point in the history
  • Loading branch information
vorot93 authored and sam committed Apr 5, 2021
1 parent bd037bd commit c5b9250
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions ethdb/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ type MutationItem struct {
value []byte
}

func NewBatch(tx RwTx) StatelessRwTx {
return &mutation{
db: &TxDb{tx: tx},
puts: btree.New(32),
}
}

func (mi *MutationItem) Less(than btree.Item) bool {
i := than.(*MutationItem)
c := strings.Compare(mi.table, i.table)
Expand Down Expand Up @@ -103,17 +110,37 @@ func (m *mutation) ReadSequence(bucket string) (res uint64, err error) {
}

// Can only be called from the worker thread
func (m *mutation) Get(table string, key []byte) ([]byte, error) {
func (m *mutation) GetOne(table string, key []byte) ([]byte, error) {
if value, ok := m.getMem(table, key); ok {
if value == nil {
return nil, ErrKeyNotFound
return nil, nil
}
return value, nil
}
if m.db != nil {
return m.db.Get(table, key)
// TODO: simplify when tx can no longer be parent of mutation
value, err := m.db.Get(table, key)
if err != nil && !errors.Is(err, ErrKeyNotFound) {
return nil, err
}

return value, nil
}
return nil, ErrKeyNotFound
return nil, nil
}

// Can only be called from the worker thread
func (m *mutation) Get(table string, key []byte) ([]byte, error) {
value, err := m.GetOne(table, key)
if err != nil {
return nil, err
}

if value == nil {
return nil, ErrKeyNotFound
}

return value, nil
}

func (m *mutation) Last(table string) ([]byte, []byte, error) {
Expand Down

0 comments on commit c5b9250

Please sign in to comment.