github.com/MetalBlockchain/subnet-evm@v0.4.9/ethdb/batch.go (about)

     1  // (c) 2020-2021, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2018 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package ethdb
    28  
    29  // IdealBatchSize defines the size of the data batches should ideally add in one
    30  // write.
    31  const IdealBatchSize = 100 * 1024
    32  
    33  // Batch is a write-only database that commits changes to its host database
    34  // when Write is called. A batch cannot be used concurrently.
    35  type Batch interface {
    36  	KeyValueWriter
    37  
    38  	// ValueSize retrieves the amount of data queued up for writing.
    39  	ValueSize() int
    40  
    41  	// Write flushes any accumulated data to disk.
    42  	Write() error
    43  
    44  	// Reset resets the batch for reuse.
    45  	Reset()
    46  
    47  	// Replay replays the batch contents.
    48  	Replay(w KeyValueWriter) error
    49  }
    50  
    51  // Batcher wraps the NewBatch method of a backing data store.
    52  type Batcher interface {
    53  	// NewBatch creates a write-only database that buffers changes to its host db
    54  	// until a final write is called.
    55  	NewBatch() Batch
    56  
    57  	// NewBatchWithSize creates a write-only database batch with pre-allocated buffer.
    58  	NewBatchWithSize(size int) Batch
    59  }
    60  
    61  // HookedBatch wraps an arbitrary batch where each operation may be hooked into
    62  // to monitor from black box code.
    63  type HookedBatch struct {
    64  	Batch
    65  
    66  	OnPut    func(key []byte, value []byte) // Callback if a key is inserted
    67  	OnDelete func(key []byte)               // Callback if a key is deleted
    68  }
    69  
    70  // Put inserts the given value into the key-value data store.
    71  func (b HookedBatch) Put(key []byte, value []byte) error {
    72  	if b.OnPut != nil {
    73  		b.OnPut(key, value)
    74  	}
    75  	return b.Batch.Put(key, value)
    76  }
    77  
    78  // Delete removes the key from the key-value data store.
    79  func (b HookedBatch) Delete(key []byte) error {
    80  	if b.OnDelete != nil {
    81  		b.OnDelete(key)
    82  	}
    83  	return b.Batch.Delete(key)
    84  }