作者: 韩晨旭 10225101440 李畅 10225102463
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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