github.com/MetalBlockchain/metalgo@v1.11.9/x/archivedb/batch.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package archivedb
     5  
     6  import "github.com/MetalBlockchain/metalgo/database"
     7  
     8  var _ database.Batch = (*batch)(nil)
     9  
    10  // batch is how a user performs modifications to the database.
    11  //
    12  // It consumes puts and deletes at a specified height. When committing, an
    13  // atomic operation is created which registers the modifications at the
    14  // specified height and updates the last tracked height to be equal to this
    15  // batch's height.
    16  type batch struct {
    17  	db     *Database
    18  	height uint64
    19  	database.BatchOps
    20  }
    21  
    22  func (c *batch) Write() error {
    23  	batch := c.db.db.NewBatch()
    24  	for _, op := range c.Ops {
    25  		key, _ := newDBKeyFromUser(op.Key, c.height)
    26  		var value []byte
    27  		if !op.Delete {
    28  			value = newDBValue(op.Value)
    29  		}
    30  		if err := batch.Put(key, value); err != nil {
    31  			return err
    32  		}
    33  	}
    34  
    35  	if err := database.PutUInt64(batch, heightKey, c.height); err != nil {
    36  		return err
    37  	}
    38  
    39  	return batch.Write()
    40  }
    41  
    42  func (c *batch) Inner() database.Batch {
    43  	return c
    44  }