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