作者: 谢瑞阳 10225101483 徐翔宇 10225101535

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