bool Msg::write_to(BlockWriter& writer) { if (!writer.writeUInt8((uint8_t)type)) return false; if (!writer.writeSlice(key)) return false; if (type == Put) { if (!writer.writeSlice(value)) return false; } return true; }
bool LeafNode::write_buckets_info(BlockWriter& writer) { if (!writer.writeUInt32(buckets_info_.size())) return false; for (size_t i = 0; i < buckets_info_.size(); i++ ) { if (!writer.writeSlice(buckets_info_[i].key)) return false; if (!writer.writeUInt32(buckets_info_[i].offset)) return false; if (!writer.writeUInt32(buckets_info_[i].length)) return false; if (!writer.writeUInt32(buckets_info_[i].uncompressed_length)) return false; } return true; }
bool InnerNode::write_to(BlockWriter& writer, size_t& skeleton_size) { // get length of skeleton and reserve space for skeleton size_t skeleton_offset = writer.pos(); size_t skeleton_length = 1 + 4 + 8 + 4 + 4 + 4; for (size_t i = 0; i < pivots_.size(); i++) { skeleton_length += pivot_size(pivots_[i].key); } if (!writer.skip(skeleton_length)) return false; // prepare buffer if compression is enabled Slice buffer; if (tree_->compressor_) { // get buffer length to serialize msgbuf size_t buffer_length = first_msgbuf_->size(); for (size_t i = 0; i < pivots_.size(); i++) { if (pivots_[i].msgbuf->size() > buffer_length) buffer_length = pivots_[i].msgbuf->size(); } buffer = Slice::alloc(buffer_length); } // write the first msgbuf first_msgbuf_offset_ = writer.pos(); if (!write_msgbuf(writer, first_msgbuf_, buffer)) return false; first_msgbuf_length_ = writer.pos() - first_msgbuf_offset_; first_msgbuf_uncompressed_length_ = first_msgbuf_->size(); // write rest msgbufs for (size_t i = 0; i < pivots_.size(); i++) { pivots_[i].offset = writer.pos(); if (!write_msgbuf(writer, pivots_[i].msgbuf, buffer)) return false; pivots_[i].length = writer.pos() - pivots_[i].offset; pivots_[i].uncompressed_length = pivots_[i].msgbuf->size(); } if (buffer.size()) { buffer.destroy(); } size_t last_offset = writer.pos(); // seek to the head and write index writer.seek(skeleton_offset); if (!writer.writeBool(bottom_)) return false; if (!writer.writeUInt32(pivots_.size())) return false; if (!writer.writeUInt64(first_child_)) return false; if (!writer.writeUInt32(first_msgbuf_offset_)) return false; if (!writer.writeUInt32(first_msgbuf_length_)) return false; if (!writer.writeUInt32(first_msgbuf_uncompressed_length_)) return false; for (size_t i = 0; i < pivots_.size(); i++) { if (!writer.writeSlice(pivots_[i].key)) return false; if (!writer.writeUInt64(pivots_[i].child)) return false; if (!writer.writeUInt32(pivots_[i].offset)) return false; if (!writer.writeUInt32(pivots_[i].length)) return false; if (!writer.writeUInt32(pivots_[i].uncompressed_length)) return false; } writer.seek(last_offset); skeleton_size = skeleton_length; return true; }