github.com/MetalBlockchain/metalgo@v1.11.9/chains/atomic/writer.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package atomic
     5  
     6  import "github.com/MetalBlockchain/metalgo/database"
     7  
     8  // WriteAll writes all of the batches to the underlying database of baseBatch.
     9  // Assumes all batches have the same underlying database.
    10  func WriteAll(baseBatch database.Batch, batches ...database.Batch) error {
    11  	baseBatch = baseBatch.Inner()
    12  	// Replay the inner batches onto [baseBatch] so that it includes all DB
    13  	// operations as they would be applied to the base database.
    14  	for _, batch := range batches {
    15  		batch = batch.Inner()
    16  		if err := batch.Replay(baseBatch); err != nil {
    17  			return err
    18  		}
    19  	}
    20  	// Write all of the combined operations in one atomic batch.
    21  	return baseBatch.Write()
    22  }