作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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