block_builder.h raw

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