小组成员:谢瑞阳、徐翔宇
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.

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