1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 5 #ifndef STORAGE_LEVELDB_TABLE_BLOCK_BUILDER_H_ 6 #define STORAGE_LEVELDB_TABLE_BLOCK_BUILDER_H_ 7 8 #include <cstdint> 9 #include <vector> 10 11 #include "leveldb/slice.h" 12 13 namespace leveldb { 14 15 struct Options; 16 17 class BlockBuilder { 18 public: 19 explicit BlockBuilder(const Options* options); 20 21 BlockBuilder(const BlockBuilder&) = delete; 22 BlockBuilder& operator=(const BlockBuilder&) = delete; 23 24 // Reset the contents as if the BlockBuilder was just constructed. 25 void Reset(); 26 27 // REQUIRES: Finish() has not been called since the last call to Reset(). 28 // REQUIRES: key is larger than any previously added key 29 void Add(const Slice& key, const Slice& value); 30 31 // Finish building the block and return a slice that refers to the 32 // block contents. The returned slice will remain valid for the 33 // lifetime of this builder or until Reset() is called. 34 Slice Finish(); 35 36 // Returns an estimate of the current (uncompressed) size of the block 37 // we are building. 38 size_t CurrentSizeEstimate() const; 39 40 // Return true iff no entries have been added since the last Reset() empty()41 bool empty() const { return buffer_.empty(); } 42 43 private: 44 const Options* options_; 45 std::string buffer_; // Destination buffer 46 std::vector<uint32_t> restarts_; // Restart points 47 int counter_; // Number of entries emitted since restart 48 bool finished_; // Has Finish() been called? 49 std::string last_key_; 50 }; 51 52 } // namespace leveldb 53 54 #endif // STORAGE_LEVELDB_TABLE_BLOCK_BUILDER_H_ 55