Summary
The manual batch size estimation of CDBBatch
serialized size was added when LevelDB didn’t expose this functionality yet.
The PR refactors the logic to use the native leveldb::WriteBatch::ApproximateSize()
function, structured in 3 focused commits to incrementally replace the old behavior safely.
Context
The previous manual size calculation initialized the estimate to 0, instead of LevelDB’s header size (containing an 8-byte sequence number followed by a 4-byte count). This PR corrects that and transitions to the now-available native LevelDB function for improved accuracy and maintainability.
Approach
The fix and refactor follow a strangle pattern over three commits:
- correct the initialization bug in the existing manual calculation, isolating the fix and ensuring the subsequent assertions use the corrected logic;
- introduce the native
ApproximateSize()
method alongside the corrected manual one, adding assertions to verify their equivalence at runtime; - remove the verified manual calculation logic and assertions, leaving only the native method.