LevelDB二级索引实现 姚凯文(kevinyao0901) 姜嘉祺
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.

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