table_builder.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // TableBuilder provides the interface used to build a Table
  6. // (an immutable and sorted map from keys to values).
  7. //
  8. // Multiple threads can invoke const methods on a TableBuilder without
  9. // external synchronization, but if any of the threads may call a
  10. // non-const method, all threads accessing the same TableBuilder must use
  11. // external synchronization.
  12. #ifndef STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_
  13. #define STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_
  14. #include <cstdint>
  15. #include "leveldb/export.h"
  16. #include "leveldb/options.h"
  17. #include "leveldb/status.h"
  18. namespace leveldb {
  19. class BlockBuilder;
  20. class BlockHandle;
  21. class WritableFile;
  22. class LEVELDB_EXPORT TableBuilder {
  23. public:
  24. // Create a builder that will store the contents of the table it is
  25. // building in *file. Does not close the file. It is up to the
  26. // caller to close the file after calling Finish().
  27. TableBuilder(const Options& options, WritableFile* file);
  28. TableBuilder(const TableBuilder&) = delete;
  29. TableBuilder& operator=(const TableBuilder&) = delete;
  30. // REQUIRES: Either Finish() or Abandon() has been called.
  31. ~TableBuilder();
  32. // Change the options used by this builder. Note: only some of the
  33. // option fields can be changed after construction. If a field is
  34. // not allowed to change dynamically and its value in the structure
  35. // passed to the constructor is different from its value in the
  36. // structure passed to this method, this method will return an error
  37. // without changing any fields.
  38. Status ChangeOptions(const Options& options);
  39. // Add key,value to the table being constructed.
  40. // REQUIRES: key is after any previously added key according to comparator.
  41. // REQUIRES: Finish(), Abandon() have not been called
  42. void Add(const Slice& key, const Slice& value);
  43. // Advanced operation: flush any buffered key/value pairs to file.
  44. // Can be used to ensure that two adjacent entries never live in
  45. // the same data block. Most clients should not need to use this method.
  46. // REQUIRES: Finish(), Abandon() have not been called
  47. void Flush();
  48. // Return non-ok iff some error has been detected.
  49. Status status() const;
  50. // Finish building the table. Stops using the file passed to the
  51. // constructor after this function returns.
  52. // REQUIRES: Finish(), Abandon() have not been called
  53. Status Finish();
  54. // Indicate that the contents of this builder should be abandoned. Stops
  55. // using the file passed to the constructor after this function returns.
  56. // If the caller is not going to call Finish(), it must call Abandon()
  57. // before destroying this builder.
  58. // REQUIRES: Finish(), Abandon() have not been called
  59. void Abandon();
  60. // Number of calls to Add() so far.
  61. uint64_t NumEntries() const;
  62. // Size of the file generated so far. If invoked after a successful
  63. // Finish() call, returns the size of the final generated file.
  64. uint64_t FileSize() const;
  65. private:
  66. bool ok() const { return status().ok(); }
  67. void WriteBlock(BlockBuilder* block, BlockHandle* handle);
  68. void WriteRawBlock(const Slice& data, CompressionType, BlockHandle* handle);
  69. struct Rep;
  70. Rep* rep_;
  71. };
  72. } // namespace leveldb
  73. #endif // STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_