作者: 韩晨旭 10225101440 李畅 10225102463
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.

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