@@ -476,6 +476,7 @@ BlobWriter::BlobWriter(const Path& filename, hwy::ThreadPool& pool)
476476 // we will later write a footer, else we will update the header.
477477 std::vector<uint8_t > bytes_before_blobs = BlobStore::BytesBeforeBlobsV2 ();
478478 file_->Write (bytes_before_blobs.data (), bytes_before_blobs.size (), 0 );
479+ curr_offset_ = bytes_before_blobs.size ();
479480}
480481
481482void BlobWriter::Add (const std::string& key, const void * data, size_t bytes) {
@@ -485,32 +486,35 @@ void BlobWriter::Add(const std::string& key, const void* data, size_t bytes) {
485486 blob_sizes_.push_back (bytes);
486487
487488 std::vector<BlobIO> writes;
488- EnqueueChunks (keys_.size () - 1 , file_-> FileSize () , bytes,
489+ EnqueueChunks (keys_.size () - 1 , curr_offset_ , bytes,
489490 static_cast <const uint8_t *>(data), writes);
490491
491492 hwy::ThreadPool null_pool (0 );
492493 hwy::ThreadPool& pool_or_serial = file_->IsAppendOnly () ? null_pool : pool_;
493494 pool_or_serial.Run (
494495 0 , writes.size (), [this , &writes](uint64_t i, size_t /* thread*/ ) {
495496 const BlobRange& range = writes[i].range ;
496-
497497 if (!file_->Write (writes[i].data , range.bytes , range.offset )) {
498498 const std::string& key = StringFromKey (keys_[range.key_idx ]);
499499 HWY_ABORT (" Write failed for %s from %zu, %zu bytes to %p." ,
500500 key.c_str (), static_cast <size_t >(range.offset ), range.bytes ,
501501 writes[i].data );
502502 }
503503 });
504+ curr_offset_ = writes.back ().range .End ();
504505}
505506
506507void BlobWriter::Finalize () {
508+ if (!file_->IsAppendOnly () && curr_offset_ != file_->FileSize ()) {
509+ HWY_WARN (" Computed offset %zu does not match file size %zu." ,
510+ curr_offset_, file_->FileSize ());
511+ }
507512 const BlobStore bs = BlobStore (keys_, blob_sizes_);
508513
509514 // Write the rest of the bytes, which contains: paddings + directory + header.
510515 const auto bytes_after_blobs = bs.BytesAfterBlobs ();
511516 file_->Write (bytes_after_blobs.data (), bytes_after_blobs.size (),
512- file_->FileSize ());
513-
517+ curr_offset_);
514518 file_.reset (); // closes the file
515519}
516520
0 commit comments