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 <vector> 9 10 #include <stdint.h> 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 // Reset the contents as if the BlockBuilder was just constructed. 22 void Reset(); 23 24 // REQUIRES: Finish() has not been callled since the last call to Reset(). 25 // REQUIRES: key is larger than any previously added key 26 void Add(const Slice& key, const Slice& value); 27 28 // Finish building the block and return a slice that refers to the 29 // block contents. The returned slice will remain valid for the 30 // lifetime of this builder or until Reset() is called. 31 Slice Finish(); 32 33 // Returns an estimate of the current (uncompressed) size of the block 34 // we are building. 35 size_t CurrentSizeEstimate() const; 36 37 // Return true iff no entries have been added since the last Reset() empty()38 bool empty() const { 39 return buffer_.empty(); 40 } 41 42 private: 43 const Options* options_; 44 std::string buffer_; // Destination buffer 45 std::vector<uint32_t> restarts_; // Restart points 46 int counter_; // Number of entries emitted since restart 47 bool finished_; // Has Finish() been called? 48 std::string last_key_; 49 50 // No copying allowed 51 BlockBuilder(const BlockBuilder&); 52 void operator=(const BlockBuilder&); 53 }; 54 55 } // namespace leveldb 56 57 #endif // STORAGE_LEVELDB_TABLE_BLOCK_BUILDER_H_ 58