作者: 韩晨旭 10225101440 李畅 10225102463
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.4 KiB

  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 <stdint.h>
  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. // REQUIRES: Either Finish() or Abandon() has been called.
  29. ~TableBuilder();
  30. // Change the options used by this builder. Note: only some of the
  31. // option fields can be changed after construction. If a field is
  32. // not allowed to change dynamically and its value in the structure
  33. // passed to the constructor is different from its value in the
  34. // structure passed to this method, this method will return an error
  35. // without changing any fields.
  36. Status ChangeOptions(const Options& options);
  37. // Add key,value to the table being constructed.
  38. // REQUIRES: key is after any previously added key according to comparator.
  39. // REQUIRES: Finish(), Abandon() have not been called
  40. void Add(const Slice& key, const Slice& value);
  41. // Advanced operation: flush any buffered key/value pairs to file.
  42. // Can be used to ensure that two adjacent entries never live in
  43. // the same data block. Most clients should not need to use this method.
  44. // REQUIRES: Finish(), Abandon() have not been called
  45. void Flush();
  46. // Return non-ok iff some error has been detected.
  47. Status status() const;
  48. // Finish building the table. Stops using the file passed to the
  49. // constructor after this function returns.
  50. // REQUIRES: Finish(), Abandon() have not been called
  51. Status Finish();
  52. // Indicate that the contents of this builder should be abandoned. Stops
  53. // using the file passed to the constructor after this function returns.
  54. // If the caller is not going to call Finish(), it must call Abandon()
  55. // before destroying this builder.
  56. // REQUIRES: Finish(), Abandon() have not been called
  57. void Abandon();
  58. // Number of calls to Add() so far.
  59. uint64_t NumEntries() const;
  60. // Size of the file generated so far. If invoked after a successful
  61. // Finish() call, returns the size of the final generated file.
  62. uint64_t FileSize() const;
  63. private:
  64. bool ok() const { return status().ok(); }
  65. void WriteBlock(BlockBuilder* block, BlockHandle* handle);
  66. void WriteRawBlock(const Slice& data, CompressionType, BlockHandle* handle);
  67. struct Rep;
  68. Rep* rep_;
  69. // No copying allowed
  70. TableBuilder(const TableBuilder&);
  71. void operator=(const TableBuilder&);
  72. };
  73. } // namespace leveldb
  74. #endif // STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_