diff --git a/table/format.cc b/table/format.cc index ff6d8fa24..17add6680 100644 --- a/table/format.cc +++ b/table/format.cc @@ -34,6 +34,7 @@ Status BlockHandle::DecodeFrom(Slice* input) { return Status::Corruption("bad block handle"); } } +const BlockHandle BlockHandle::kNullBlockHandle(0, 0); void Footer::EncodeTo(std::string* dst) const { #ifndef NDEBUG diff --git a/table/format.h b/table/format.h index bac51eab4..c10cab857 100644 --- a/table/format.h +++ b/table/format.h @@ -26,6 +26,7 @@ struct ReadOptions; class BlockHandle { public: BlockHandle(); + BlockHandle(uint64_t offset, uint64_t size); // The offset of the block in the file. uint64_t offset() const { return offset_; } @@ -38,12 +39,24 @@ class BlockHandle { void EncodeTo(std::string* dst) const; Status DecodeFrom(Slice* input); + // if the block handle's offset and size are both "0", we will view it + // as a null block handle that points to no where. + bool IsNull() const { + return offset_ == 0 && size_ == 0; + } + + static const BlockHandle& NullBlockHandle() { + return kNullBlockHandle; + } + // Maximum encoding length of a BlockHandle enum { kMaxEncodedLength = 10 + 10 }; private: - uint64_t offset_; - uint64_t size_; + uint64_t offset_ = 0; + uint64_t size_ = 0; + + static const BlockHandle kNullBlockHandle; }; // Footer encapsulates the fixed information stored at the tail @@ -116,8 +129,13 @@ extern Status UncompressBlockContents(const char* data, // Implementation details follow. Clients should ignore, inline BlockHandle::BlockHandle() - : offset_(~static_cast(0)), - size_(~static_cast(0)) { + : BlockHandle(~static_cast(0), + ~static_cast(0)) { +} + +inline BlockHandle::BlockHandle(uint64_t offset, uint64_t size) + : offset_(offset), + size_(size) { } } // namespace rocksdb