Skip to content

Commit 20b1674

Browse files
nitingcopybara-github
authored andcommitted
Internal change.
PiperOrigin-RevId: 822643310
1 parent 64a82ed commit 20b1674

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

io/blob_store.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

481482
void 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

506507
void 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

io/blob_store.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ class BlobWriter {
130130
std::vector<hwy::uint128_t> keys_;
131131
std::vector<size_t> blob_sizes_;
132132
hwy::ThreadPool& pool_;
133+
// Current offset in the file used for writing.
134+
int64_t curr_offset_ = 0;
133135
};
134136

135137
} // namespace gcpp

0 commit comments

Comments
 (0)