作者: 韩晨旭@ArcueidType(Arcueid) 10225101440 李畅@wesley 10225102463 设计文档为PLAN.md,md版本报告为README.md,pdf版本报告为Report.pdf
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.

178 lines
7.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. #ifndef STORAGE_LEVELDB_INCLUDE_DB_H_
  5. #define STORAGE_LEVELDB_INCLUDE_DB_H_
  6. #include "db/fields.h"
  7. #include <cstdint>
  8. #include <cstdio>
  9. #include "leveldb/export.h"
  10. #include "leveldb/iterator.h"
  11. #include "leveldb/options.h"
  12. namespace leveldb {
  13. // Update CMakeLists.txt if you change these
  14. static const int kMajorVersion = 1;
  15. static const int kMinorVersion = 23;
  16. struct Options;
  17. struct ReadOptions;
  18. struct WriteOptions;
  19. class WriteBatch;
  20. // Abstract handle to particular state of a DB.
  21. // A Snapshot is an immutable object and can therefore be safely
  22. // accessed from multiple threads without any external synchronization.
  23. class LEVELDB_EXPORT Snapshot {
  24. protected:
  25. virtual ~Snapshot();
  26. };
  27. // A range of keys
  28. struct LEVELDB_EXPORT Range {
  29. Range() = default;
  30. Range(const Slice& s, const Slice& l) : start(s), limit(l) {}
  31. Slice start; // Included in the range
  32. Slice limit; // Not included in the range
  33. };
  34. // A DB is a persistent ordered map from keys to values.
  35. // A DB is safe for concurrent access from multiple threads without
  36. // any external synchronization.
  37. class LEVELDB_EXPORT DB {
  38. public:
  39. // Open the database with the specified "name".
  40. // Stores a pointer to a heap-allocated database in *dbptr and returns
  41. // OK on success.
  42. // Stores nullptr in *dbptr and returns a non-OK status on error.
  43. // Caller should delete *dbptr when it is no longer needed.
  44. static Status Open(const Options& options, const std::string& name,
  45. DB** dbptr);
  46. DB() = default;
  47. DB(const DB&) = delete;
  48. DB& operator=(const DB&) = delete;
  49. virtual ~DB();
  50. // Set the database entry for "key" to "value". Returns OK on success,
  51. // and a non-OK status on error.
  52. // Note: consider setting options.sync = true.
  53. virtual Status Put(const WriteOptions& options, const Slice& key,
  54. const Slice& value) = 0;
  55. // Fields Put method
  56. virtual Status Put(const WriteOptions& options, const Slice& key,
  57. const Fields& fields) = 0;
  58. // Remove the database entry (if any) for "key". Returns OK on
  59. // success, and a non-OK status on error. It is not an error if "key"
  60. // did not exist in the database.
  61. // Note: consider setting options.sync = true.
  62. virtual Status Delete(const WriteOptions& options, const Slice& key) = 0;
  63. // Apply the specified updates to the database.
  64. // Returns OK on success, non-OK on failure.
  65. // Note: consider setting options.sync = true.
  66. virtual Status Write(const WriteOptions& options, WriteBatch* updates) = 0;
  67. // If the database contains an entry for "key" store the
  68. // corresponding value in *value and return OK.
  69. //
  70. // If there is no entry for "key" leave *value unchanged and return
  71. // a status for which Status::IsNotFound() returns true.
  72. //
  73. // May return some other Status on an error.
  74. virtual Status Get(const ReadOptions& options, const Slice& key,
  75. Fields *fields) = 0;
  76. virtual Status Get(const ReadOptions& options, const Slice& key,
  77. std::string *value) = 0;
  78. // Search keys from value
  79. virtual std::vector<std::string> FindKeysByField(Field &field) = 0;
  80. // Return a heap-allocated iterator over the contents of the database.
  81. // The result of NewIterator() is initially invalid (caller must
  82. // call one of the Seek methods on the iterator before using it).
  83. //
  84. // Caller should delete the iterator when it is no longer needed.
  85. // The returned iterator should be deleted before this db is deleted.
  86. virtual Iterator* NewIterator(const ReadOptions& options) = 0;
  87. // Return a handle to the current DB state. Iterators created with
  88. // this handle will all observe a stable snapshot of the current DB
  89. // state. The caller must call ReleaseSnapshot(result) when the
  90. // snapshot is no longer needed.
  91. virtual const Snapshot* GetSnapshot() = 0;
  92. // Release a previously acquired snapshot. The caller must not
  93. // use "snapshot" after this call.
  94. virtual void ReleaseSnapshot(const Snapshot* snapshot) = 0;
  95. // DB implementations can export properties about their state
  96. // via this method. If "property" is a valid property understood by this
  97. // DB implementation, fills "*value" with its current value and returns
  98. // true. Otherwise returns false.
  99. //
  100. //
  101. // Valid property names include:
  102. //
  103. // "leveldb.num-files-at-level<N>" - return the number of files at level <N>,
  104. // where <N> is an ASCII representation of a level number (e.g. "0").
  105. // "leveldb.stats" - returns a multi-line string that describes statistics
  106. // about the internal operation of the DB.
  107. // "leveldb.sstables" - returns a multi-line string that describes all
  108. // of the sstables that make up the db contents.
  109. // "leveldb.approximate-memory-usage" - returns the approximate number of
  110. // bytes of memory in use by the DB.
  111. virtual bool GetProperty(const Slice& property, std::string* value) = 0;
  112. // For each i in [0,n-1], store in "sizes[i]", the approximate
  113. // file system space used by keys in "[range[i].start .. range[i].limit)".
  114. //
  115. // Note that the returned sizes measure file system space usage, so
  116. // if the user data compresses by a factor of ten, the returned
  117. // sizes will be one-tenth the size of the corresponding user data size.
  118. //
  119. // The results may not include the sizes of recently written data.
  120. virtual void GetApproximateSizes(const Range* range, int n,
  121. uint64_t* sizes) = 0;
  122. // Compact the underlying storage for the key range [*begin,*end].
  123. // In particular, deleted and overwritten versions are discarded,
  124. // and the data is rearranged to reduce the cost of operations
  125. // needed to access the data. This operation should typically only
  126. // be invoked by users who understand the underlying implementation.
  127. //
  128. // begin==nullptr is treated as a key before all keys in the database.
  129. // end==nullptr is treated as a key after all keys in the database.
  130. // Therefore the following call will compact the entire database:
  131. // db->CompactRange(nullptr, nullptr);
  132. virtual void CompactRange(const Slice* begin, const Slice* end) = 0;
  133. };
  134. // Destroy the contents of the specified database.
  135. // Be very careful using this method.
  136. //
  137. // Note: For backwards compatibility, if DestroyDB is unable to list the
  138. // database files, Status::OK() will still be returned masking this failure.
  139. LEVELDB_EXPORT Status DestroyDB(const std::string& name,
  140. const Options& options);
  141. // If a DB cannot be opened, you may attempt to call this method to
  142. // resurrect as much of the contents of the database as possible.
  143. // Some data may be lost, so be careful when calling this function
  144. // on a database that contains important information.
  145. LEVELDB_EXPORT Status RepairDB(const std::string& dbname,
  146. const Options& options);
  147. } // namespace leveldb
  148. #endif // STORAGE_LEVELDB_INCLUDE_DB_H_