github.com/status-im/status-go@v1.1.0/db/tx.go (about)

     1  package db
     2  
     3  import (
     4  	"github.com/syndtr/goleveldb/leveldb"
     5  	"github.com/syndtr/goleveldb/leveldb/iterator"
     6  	"github.com/syndtr/goleveldb/leveldb/util"
     7  )
     8  
     9  // LevelDBTx doesn't provide any read isolation. It allows committing all writes atomically (put/delete).
    10  type LevelDBTx struct {
    11  	batch *leveldb.Batch
    12  	db    LevelDBStorage
    13  }
    14  
    15  // Put adds key/value to associated batch.
    16  func (tx LevelDBTx) Put(key, buf []byte) error {
    17  	tx.batch.Put(key, buf)
    18  	return nil
    19  }
    20  
    21  // Delete adds delete operation to associated batch.
    22  func (tx LevelDBTx) Delete(key []byte) error {
    23  	tx.batch.Delete(key)
    24  	return nil
    25  }
    26  
    27  // Get reads from currently committed state.
    28  func (tx LevelDBTx) Get(key []byte) ([]byte, error) {
    29  	return tx.db.Get(key)
    30  }
    31  
    32  // NewIterator returns iterator.Iterator that will read from currently committed state.
    33  func (tx LevelDBTx) NewIterator(slice *util.Range) iterator.Iterator {
    34  	return tx.db.NewIterator(slice)
    35  }
    36  
    37  // Commit writes batch atomically.
    38  func (tx LevelDBTx) Commit() error {
    39  	return tx.db.db.Write(tx.batch, nil)
    40  }