db.h 14.3 KB
Newer Older
1 2 3 4
// Copyright (c) 2013, Facebook, Inc.  All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
J
jorlow@chromium.org 已提交
5 6 7 8
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

9 10
#ifndef STORAGE_ROCKSDB_INCLUDE_DB_H_
#define STORAGE_ROCKSDB_INCLUDE_DB_H_
J
jorlow@chromium.org 已提交
11 12 13

#include <stdint.h>
#include <stdio.h>
14
#include <memory>
15
#include <vector>
16 17 18 19
#include "rocksdb/iterator.h"
#include "rocksdb/options.h"
#include "rocksdb/types.h"
#include "rocksdb/transaction_log.h"
J
jorlow@chromium.org 已提交
20

21
namespace rocksdb {
J
jorlow@chromium.org 已提交
22

23 24
using std::unique_ptr;

25
// Update Makefile if you change these
M
Mayank Agarwal 已提交
26 27
static const int kMajorVersion = 2;
static const int kMinorVersion = 0;
28

J
jorlow@chromium.org 已提交
29 30 31
struct Options;
struct ReadOptions;
struct WriteOptions;
H
heyongqiang 已提交
32
struct FlushOptions;
J
jorlow@chromium.org 已提交
33 34
class WriteBatch;

35 36
// Metadata associated with each SST file.
struct LiveFileMetaData {
D
Dhruba Borthakur 已提交
37 38 39 40 41 42 43
  std::string name;        // Name of the file
  int level;               // Level at which this file resides.
  size_t size;             // File size in bytes.
  std::string smallestkey; // Smallest user defined key in the file.
  std::string largestkey;  // Largest user defined key in the file.
  SequenceNumber smallest_seqno; // smallest seqno in file
  SequenceNumber largest_seqno;  // largest seqno in file
44 45
};

46 47 48 49 50 51 52
// Abstract handle to particular state of a DB.
// A Snapshot is an immutable object and can therefore be safely
// accessed from multiple threads without any external synchronization.
class Snapshot {
 protected:
  virtual ~Snapshot();
};
J
jorlow@chromium.org 已提交
53

54
// A range of keys
J
jorlow@chromium.org 已提交
55
struct Range {
56 57
  Slice start;          // Included in the range
  Slice limit;          // Not included in the range
J
jorlow@chromium.org 已提交
58

59
  Range() { }
J
jorlow@chromium.org 已提交
60 61 62 63
  Range(const Slice& s, const Slice& l) : start(s), limit(l) { }
};

// A DB is a persistent ordered map from keys to values.
64 65
// A DB is safe for concurrent access from multiple threads without
// any external synchronization.
J
jorlow@chromium.org 已提交
66 67 68 69 70
class DB {
 public:
  // Open the database with the specified "name".
  // Stores a pointer to a heap-allocated database in *dbptr and returns
  // OK on success.
A
Abhishek Kona 已提交
71
  // Stores nullptr in *dbptr and returns a non-OK status on error.
J
jorlow@chromium.org 已提交
72 73 74 75 76
  // Caller should delete *dbptr when it is no longer needed.
  static Status Open(const Options& options,
                     const std::string& name,
                     DB** dbptr);

H
heyongqiang 已提交
77 78 79 80 81 82
  // Open the database for read only. All DB interfaces
  // that modify data, like put/delete, will return error.
  // If the db is opened in read only mode, then no compactions
  // will happen.
  static Status OpenForReadOnly(const Options& options,
      const std::string& name, DB** dbptr,
83
      bool error_if_log_file_exist = false);
H
heyongqiang 已提交
84

J
jorlow@chromium.org 已提交
85 86 87
  DB() { }
  virtual ~DB();

88 89
  // Set the database entry for "key" to "value".
  // Returns OK on success, and a non-OK status on error.
90
  // Note: consider setting options.sync = true.
J
jorlow@chromium.org 已提交
91 92 93 94 95 96 97
  virtual Status Put(const WriteOptions& options,
                     const Slice& key,
                     const Slice& value) = 0;

  // Remove the database entry (if any) for "key".  Returns OK on
  // success, and a non-OK status on error.  It is not an error if "key"
  // did not exist in the database.
98
  // Note: consider setting options.sync = true.
J
jorlow@chromium.org 已提交
99 100
  virtual Status Delete(const WriteOptions& options, const Slice& key) = 0;

101 102 103 104 105 106 107 108
  // Merge the database entry for "key" with "value".  Returns OK on success,
  // and a non-OK status on error. The semantics of this operation is
  // determined by the user provided merge_operator when opening DB.
  // Note: consider setting options.sync = true.
  virtual Status Merge(const WriteOptions& options,
                       const Slice& key,
                       const Slice& value) = 0;

J
jorlow@chromium.org 已提交
109 110
  // Apply the specified updates to the database.
  // Returns OK on success, non-OK on failure.
111
  // Note: consider setting options.sync = true.
J
jorlow@chromium.org 已提交
112 113 114 115 116 117 118 119 120 121
  virtual Status Write(const WriteOptions& options, WriteBatch* updates) = 0;

  // If the database contains an entry for "key" store the
  // corresponding value in *value and return OK.
  //
  // If there is no entry for "key" leave *value unchanged and return
  // a status for which Status::IsNotFound() returns true.
  //
  // May return some other Status on an error.
  virtual Status Get(const ReadOptions& options,
122 123
                     const Slice& key,
                     std::string* value) = 0;
J
jorlow@chromium.org 已提交
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138
  // If keys[i] does not exist in the database, then the i'th returned
  // status will be one for which Status::IsNotFound() is true, and
  // (*values)[i] will be set to some arbitrary value (often ""). Otherwise,
  // the i'th returned status will have Status::ok() true, and (*values)[i]
  // will store the value associated with keys[i].
  //
  // (*values) will always be resized to be the same size as (keys).
  // Similarly, the number of returned statuses will be the number of keys.
  // Note: keys will not be "de-duplicated". Duplicate keys will return
  // duplicate values in order.
  virtual std::vector<Status> MultiGet(const ReadOptions& options,
                                       const std::vector<Slice>& keys,
                                       std::vector<std::string>* values) = 0;

139
  // If the key definitely does not exist in the database, then this method
140 141 142 143 144 145 146 147 148 149 150 151 152
  // returns false, else true. If the caller wants to obtain value when the key
  // is found in memory, a bool for 'value_found' must be passed. 'value_found'
  // will be true on return if value has been set properly.
  // This check is potentially lighter-weight than invoking DB::Get(). One way
  // to make this lighter weight is to avoid doing any IOs.
  // Default implementation here returns true and sets 'value_found' to false
  virtual bool KeyMayExist(const ReadOptions& options,
                           const Slice& key,
                           std::string* value,
                           bool* value_found = nullptr) {
    if (value_found != nullptr) {
      *value_found = false;
    }
153 154
    return true;
  }
155

J
jorlow@chromium.org 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  // Return a heap-allocated iterator over the contents of the database.
  // The result of NewIterator() is initially invalid (caller must
  // call one of the Seek methods on the iterator before using it).
  //
  // Caller should delete the iterator when it is no longer needed.
  // The returned iterator should be deleted before this db is deleted.
  virtual Iterator* NewIterator(const ReadOptions& options) = 0;

  // Return a handle to the current DB state.  Iterators created with
  // this handle will all observe a stable snapshot of the current DB
  // state.  The caller must call ReleaseSnapshot(result) when the
  // snapshot is no longer needed.
  virtual const Snapshot* GetSnapshot() = 0;

  // Release a previously acquired snapshot.  The caller must not
  // use "snapshot" after this call.
  virtual void ReleaseSnapshot(const Snapshot* snapshot) = 0;

  // DB implementations can export properties about their state
  // via this method.  If "property" is a valid property understood by this
  // DB implementation, fills "*value" with its current value and returns
  // true.  Otherwise returns false.
  //
  //
  // Valid property names include:
  //
182
  //  "rocksdb.num-files-at-level<N>" - return the number of files at level <N>,
J
jorlow@chromium.org 已提交
183
  //     where <N> is an ASCII representation of a level number (e.g. "0").
184
  //  "rocksdb.stats" - returns a multi-line string that describes statistics
185
  //     about the internal operation of the DB.
186
  //  "rocksdb.sstables" - returns a multi-line string that describes all
G
Gabor Cselle 已提交
187
  //     of the sstables that make up the db contents.
188
  virtual bool GetProperty(const Slice& property, std::string* value) = 0;
J
jorlow@chromium.org 已提交
189 190 191 192 193 194 195 196 197 198 199 200

  // For each i in [0,n-1], store in "sizes[i]", the approximate
  // file system space used by keys in "[range[i].start .. range[i].limit)".
  //
  // Note that the returned sizes measure file system space usage, so
  // if the user data compresses by a factor of ten, the returned
  // sizes will be one-tenth the size of the corresponding user data size.
  //
  // The results may not include the sizes of recently written data.
  virtual void GetApproximateSizes(const Range* range, int n,
                                   uint64_t* sizes) = 0;

G
Gabor Cselle 已提交
201 202 203 204 205 206
  // Compact the underlying storage for the key range [*begin,*end].
  // In particular, deleted and overwritten versions are discarded,
  // and the data is rearranged to reduce the cost of operations
  // needed to access the data.  This operation should typically only
  // be invoked by users who understand the underlying implementation.
  //
A
Abhishek Kona 已提交
207 208
  // begin==nullptr is treated as a key before all keys in the database.
  // end==nullptr is treated as a key after all keys in the database.
G
Gabor Cselle 已提交
209
  // Therefore the following call will compact the entire database:
A
Abhishek Kona 已提交
210
  //    db->CompactRange(nullptr, nullptr);
211 212 213 214 215
  // Note that after the entire database is compacted, all data are pushed
  // down to the last level containing any data. If the total data size
  // after compaction is reduced, that level might not be appropriate for
  // hosting all the files. In this case, client could set reduce_level
  // to true, to move the files back to the minimum level capable of holding
216
  // the data set or a given level (specified by non-negative target_level).
217
  virtual void CompactRange(const Slice* begin, const Slice* end,
218 219
                            bool reduce_level = false,
                            int target_level = -1) = 0;
J
jorlow@chromium.org 已提交
220

221 222 223 224 225 226 227 228 229 230
  // Number of levels used for this DB.
  virtual int NumberLevels() = 0;

  // Maximum level to which a new compacted memtable is pushed if it
  // does not create overlap.
  virtual int MaxMemCompactionLevel() = 0;

  // Number of files in level-0 that would stop writes.
  virtual int Level0StopWriteTrigger() = 0;

I
Igor Canadi 已提交
231 232 233 234
  // Get DB name -- the exact same name that was provided as an argument to
  // DB::Open()
  virtual const std::string& GetName() const = 0;

235 236 237
  // Get Env object from the DB
  virtual Env* GetEnv() const = 0;

I
Igor Canadi 已提交
238 239 240
  // Get DB Options that we use
  virtual const Options& GetOptions() const = 0;

H
heyongqiang 已提交
241 242 243
  // Flush all mem-table data.
  virtual Status Flush(const FlushOptions& options) = 0;

244 245 246 247 248 249
  // Prevent file deletions. Compactions will continue to occur,
  // but no obsolete files will be deleted. Calling this multiple
  // times have the same effect as calling it once.
  virtual Status DisableFileDeletions() = 0;

  // Allow compactions to delete obselete files.
250 251 252 253 254 255 256 257 258
  // If force == true, the call to EnableFileDeletions() will guarantee that
  // file deletions are enabled after the call, even if DisableFileDeletions()
  // was called multiple times before.
  // If force == false, EnableFileDeletions will only enable file deletion
  // after it's been called at least as many times as DisableFileDeletions(),
  // enabling the two methods to be called by two threads concurrently without
  // synchronization -- i.e., file deletions will be enabled only after both
  // threads call EnableFileDeletions()
  virtual Status EnableFileDeletions(bool force = true) = 0;
259

260 261
  // GetLiveFiles followed by GetSortedWalFiles can generate a lossless backup

262 263
  // THIS METHOD IS DEPRECATED. Use the GetTableMetaData to get more
  // detailed information on the live files.
264
  // Retrieve the list of all files in the database. The files are
265 266 267 268
  // relative to the dbname and are not absolute paths. The valid size of the
  // manifest file is returned in manifest_file_size. The manifest file is an
  // ever growing file, but only the portion specified by manifest_file_size is
  // valid for this snapshot.
269 270 271 272 273 274
  // Setting flush_memtable to true does Flush before recording the live files.
  // Setting flush_memtable to false is useful when we don't want to wait for
  // flush which may have to wait for compaction to complete taking an
  // indeterminate time. But this will have to use GetSortedWalFiles after
  // GetLiveFiles to compensate for memtables missed in this snapshot due to the
  // absence of Flush, by WAL files to recover the database consistently later
275
  virtual Status GetLiveFiles(std::vector<std::string>&,
276 277
                              uint64_t* manifest_file_size,
                              bool flush_memtable = true) = 0;
278

279 280 281
  // Retrieve the sorted list of all wal files with earliest file first
  virtual Status GetSortedWalFiles(VectorLogPtr& files) = 0;

282
  // The sequence number of the most recent transaction.
283 284 285 286 287
  virtual SequenceNumber GetLatestSequenceNumber() const = 0;

  // Sets iter to an iterator that is positioned at a write-batch containing
  // seq_number. If the sequence number is non existent, it returns an iterator
  // at the first available seq_no after the requested seq_no
288
  // Returns Status::OK if iterator is valid
289 290 291 292
  // Must set WAL_ttl_seconds or WAL_size_limit_MB to large values to
  // use this api, else the WAL files will get
  // cleared aggressively and the iterator might keep getting invalid before
  // an update is read.
293
  virtual Status GetUpdatesSince(SequenceNumber seq_number,
294
                                 unique_ptr<TransactionLogIterator>* iter) = 0;
295

296 297 298 299
  // Delete the file name from the db directory and update the internal state to
  // reflect that. Supports deletion of sst and log files only. 'name' must be
  // path relative to the db directory. eg. 000001.sst, /archive/000003.log
  virtual Status DeleteFile(std::string name) = 0;
300 301 302 303 304 305 306

  // Returns a list of all table files with their level, start key
  // and end key
  virtual void GetLiveFilesMetaData(
    std::vector<LiveFileMetaData> *metadata) {
  }

307 308 309
  // Sets the globally unique ID created at database creation time by invoking
  // Env::GenerateUniqueId(), in identity. Returns Status::OK if identity could
  // be set properly
310
  virtual Status GetDbIdentity(std::string& identity) = 0;
311

J
jorlow@chromium.org 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
 private:
  // No copying allowed
  DB(const DB&);
  void operator=(const DB&);
};

// Destroy the contents of the specified database.
// Be very careful using this method.
Status DestroyDB(const std::string& name, const Options& options);

// If a DB cannot be opened, you may attempt to call this method to
// resurrect as much of the contents of the database as possible.
// Some data may be lost, so be careful when calling this function
// on a database that contains important information.
Status RepairDB(const std::string& dbname, const Options& options);

328 329
}  // namespace rocksdb

330
#endif  // STORAGE_ROCKSDB_INCLUDE_DB_H_