@@ -30,9 +30,9 @@ pub struct ImmutableAddressStore {
3030}
3131
3232impl ImmutableAddressStore {
33- pub fn new ( path : impl AsRef < Path > ) -> Result < Self > {
33+ pub fn new ( path : impl AsRef < Path > , clear_on_start : bool ) -> Result < Self > {
3434 let path = path. as_ref ( ) ;
35- if path. exists ( ) {
35+ if path. exists ( ) && clear_on_start {
3636 std:: fs:: remove_dir_all ( path) ?;
3737 }
3838 let cfg = fjall:: Config :: new ( path) . max_write_buffer_size ( 512 * 1024 * 1024 ) ;
@@ -56,15 +56,36 @@ impl ImmutableAddressStore {
5656 /// for an entire epoch. Skips any partitions that have already stored the given epoch.
5757 /// All writes are batched and committed atomically, preventing on-disk corruption in case of failure.
5858 pub async fn persist_epoch ( & self , epoch : u64 , config : & AddressStorageConfig ) -> Result < ( ) > {
59- let persist_utxos = config. store_info
60- && !self . epoch_exists ( self . utxos . clone ( ) , ADDRESS_UTXOS_EPOCH_COUNTER , epoch) . await ?;
61- let persist_txs = config. store_transactions
62- && !self . epoch_exists ( self . txs . clone ( ) , ADDRESS_TXS_EPOCH_COUNTER , epoch) . await ?;
63- let persist_totals = config. store_totals
64- && !self . epoch_exists ( self . totals . clone ( ) , ADDRESS_TOTALS_EPOCH_COUNTER , epoch) . await ?;
59+ // Skip if all options disabled
60+ if !( config. store_info || config. store_transactions || config. store_totals ) {
61+ debug ! ( "no persistence needed for epoch {epoch} (all stores disabled)" ) ;
62+ return Ok ( ( ) ) ;
63+ }
64+
65+ // Determine which partitions need persistence
66+ let ( persist_utxos, persist_txs, persist_totals) = if config. clear_on_start {
67+ (
68+ config. store_info ,
69+ config. store_transactions ,
70+ config. store_totals ,
71+ )
72+ } else {
73+ let utxos = config. store_info
74+ && !self
75+ . epoch_exists ( self . utxos . clone ( ) , ADDRESS_UTXOS_EPOCH_COUNTER , epoch)
76+ . await ?;
77+ let txs = config. store_transactions
78+ && !self . epoch_exists ( self . txs . clone ( ) , ADDRESS_TXS_EPOCH_COUNTER , epoch) . await ?;
79+ let totals = config. store_totals
80+ && !self
81+ . epoch_exists ( self . totals . clone ( ) , ADDRESS_TOTALS_EPOCH_COUNTER , epoch)
82+ . await ?;
83+ ( utxos, txs, totals)
84+ } ;
6585
86+ // Skip if all partitions have already been persisted for the epoch
6687 if !( persist_utxos || persist_txs || persist_totals) {
67- debug ! ( "no persistence needed for epoch {epoch} (already persisted or disabled) " ) ;
88+ debug ! ( "no persistence needed for epoch {epoch}" ) ;
6889 return Ok ( ( ) ) ;
6990 }
7091
@@ -124,22 +145,14 @@ impl ImmutableAddressStore {
124145 }
125146
126147 // Metadata markers
127- if persist_utxos {
128- batch. insert (
129- & self . utxos ,
130- ADDRESS_UTXOS_EPOCH_COUNTER ,
131- epoch. to_le_bytes ( ) ,
132- ) ;
133- }
134- if persist_txs {
135- batch. insert ( & self . txs , ADDRESS_TXS_EPOCH_COUNTER , epoch. to_le_bytes ( ) ) ;
136- }
137- if persist_totals {
138- batch. insert (
139- & self . totals ,
140- ADDRESS_TOTALS_EPOCH_COUNTER ,
141- epoch. to_le_bytes ( ) ,
142- ) ;
148+ for ( enabled, part, key) in [
149+ ( persist_utxos, & self . utxos , ADDRESS_UTXOS_EPOCH_COUNTER ) ,
150+ ( persist_txs, & self . txs , ADDRESS_TXS_EPOCH_COUNTER ) ,
151+ ( persist_totals, & self . totals , ADDRESS_TOTALS_EPOCH_COUNTER ) ,
152+ ] {
153+ if enabled {
154+ batch. insert ( part, key, epoch. to_le_bytes ( ) ) ;
155+ }
143156 }
144157
145158 match batch. commit ( ) {
0 commit comments