@@ -50,7 +50,7 @@ func (u *IndexUpdate) Merge(update *IndexUpdate) {
5050 u .BlockBitmap [indexValue ].Or (bm )
5151 }
5252
53- if u .LastBlockNum < update .LastBlockNum {
53+ if u .LastBlockNum == NoBlockNum || u . LastBlockNum < update .LastBlockNum {
5454 u .LastBlockNum = update .LastBlockNum
5555 }
5656}
@@ -97,7 +97,7 @@ func (i *Index[T]) IndexBlock(ctx context.Context, fs storage.FS, block Block[T]
9797 return nil , fmt .Errorf ("unexpected: failed to get number of blocks indexed: %w" , err )
9898 }
9999
100- if block .Number <= numBlocksIndexed {
100+ if numBlocksIndexed != NoBlockNum && block .Number <= numBlocksIndexed {
101101 return nil , nil
102102 }
103103 }
@@ -148,7 +148,7 @@ func (i *Index[T]) Store(ctx context.Context, fs storage.FS, indexUpdate *IndexU
148148 if err != nil {
149149 return fmt .Errorf ("failed to get number of blocks indexed: %w" , err )
150150 }
151- if lastBlockNumIndexed >= indexUpdate .LastBlockNum {
151+ if lastBlockNumIndexed != NoBlockNum && lastBlockNumIndexed >= indexUpdate .LastBlockNum {
152152 return nil
153153 }
154154
@@ -169,12 +169,14 @@ func (i *Index[T]) Store(ctx context.Context, fs storage.FS, indexUpdate *IndexU
169169
170170 bmap .Or (bmUpdate )
171171
172+ fmt .Println ("---- writing bm" , i .name , indexValue , bmap .GetCardinality ())
172173 err = file .Write (ctx , bmap )
173174 if err != nil {
174175 return err
175176 }
176177 }
177178
179+ fmt .Println ("---- writing last block num" , i .name , indexUpdate .LastBlockNum )
178180 err = i .storeLastBlockNumIndexed (ctx , fs , indexUpdate .LastBlockNum )
179181 if err != nil {
180182 return fmt .Errorf ("failed to index number of blocks indexed: %w" , err )
@@ -191,19 +193,19 @@ func (i *Index[T]) LastBlockNumIndexed(ctx context.Context, fs storage.FS) (uint
191193 file , err := fs .Open (ctx , indexedBlockNumFilePath (string (i .name )), nil )
192194 if err != nil {
193195 // file doesn't exist
194- return 0 , nil
196+ return NoBlockNum , nil
195197 }
196198 defer file .Close ()
197199
198200 buf , err := io .ReadAll (file )
199201 if err != nil {
200- return 0 , fmt .Errorf ("failed to read IndexBlock file: %w" , err )
202+ return NoBlockNum , fmt .Errorf ("failed to read IndexBlock file: %w" , err )
201203 }
202204
203205 var numBlocksIndexed uint64
204206 err = binary .Read (bytes .NewReader (buf ), binary .BigEndian , & numBlocksIndexed )
205207 if err != nil {
206- return 0 , fmt .Errorf ("failed to unmarshal bitmap: %w" , err )
208+ return NoBlockNum , fmt .Errorf ("failed to unmarshal bitmap: %w" , err )
207209 }
208210
209211 i .numBlocksIndexed = & atomic.Uint64 {}
@@ -219,7 +221,7 @@ func (i *Index[T]) storeLastBlockNumIndexed(ctx context.Context, fs storage.FS,
219221 prevBlockIndexed = blocksIndexed
220222 }
221223
222- if prevBlockIndexed >= numBlocksIndexed {
224+ if prevBlockIndexed != NoBlockNum && prevBlockIndexed >= numBlocksIndexed {
223225 return nil
224226 }
225227
0 commit comments