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.

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