10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
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.

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