1179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Use of this source code is governed by a BSD-style license that can be 3179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// found in the LICENSE file. See the AUTHORS file for names of contributors. 4179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 5179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#ifndef STORAGE_LEVELDB_INCLUDE_DB_H_ 6179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#define STORAGE_LEVELDB_INCLUDE_DB_H_ 7179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 8179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include <stdint.h> 9179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include <stdio.h> 101ca60b12c68a71aac695b15e329b2a76a63cbb0ajorlow@chromium.org#include "leveldb/iterator.h" 111ca60b12c68a71aac695b15e329b2a76a63cbb0ajorlow@chromium.org#include "leveldb/options.h" 12179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 13179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace leveldb { 14179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 15bf10812927b9a05a7b671eb32504eaa5972725d7sanjay@google.com// Update Makefile if you change these 16b887f640bae906abfb77fdf418be63350b4c5e1fjorlow@chromium.orgstatic const int kMajorVersion = 1; 173f77584eb3f9754bbb7079070873ece3f30a1e6bcmumford@chromium.orgstatic const int kMinorVersion = 17; 18b887f640bae906abfb77fdf418be63350b4c5e1fjorlow@chromium.org 19179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstruct Options; 20179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstruct ReadOptions; 21179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstruct WriteOptions; 22179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass WriteBatch; 23179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 24a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// Abstract handle to particular state of a DB. 25a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// A Snapshot is an immutable object and can therefore be safely 26a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// accessed from multiple threads without any external synchronization. 27a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.orgclass Snapshot { 28a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org protected: 29a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org virtual ~Snapshot(); 30a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org}; 31179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 32a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// A range of keys 33179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstruct Range { 34a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org Slice start; // Included in the range 35a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org Slice limit; // Not included in the range 36179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 37d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com Range() { } 38179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org Range(const Slice& s, const Slice& l) : start(s), limit(l) { } 39179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org}; 40179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 41179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// A DB is a persistent ordered map from keys to values. 42a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// A DB is safe for concurrent access from multiple threads without 43a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// any external synchronization. 44179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass DB { 45179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org public: 46179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Open the database with the specified "name". 47179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Stores a pointer to a heap-allocated database in *dbptr and returns 48179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // OK on success. 49179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Stores NULL in *dbptr and returns a non-OK status on error. 50179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Caller should delete *dbptr when it is no longer needed. 51179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org static Status Open(const Options& options, 52179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org const std::string& name, 53179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org DB** dbptr); 54179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 55179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org DB() { } 56179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org virtual ~DB(); 57179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 58179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Set the database entry for "key" to "value". Returns OK on success, 59179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // and a non-OK status on error. 6095e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org // Note: consider setting options.sync = true. 61179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org virtual Status Put(const WriteOptions& options, 62179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org const Slice& key, 63179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org const Slice& value) = 0; 64179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 65179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Remove the database entry (if any) for "key". Returns OK on 66179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // success, and a non-OK status on error. It is not an error if "key" 67179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // did not exist in the database. 6895e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org // Note: consider setting options.sync = true. 69179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org virtual Status Delete(const WriteOptions& options, const Slice& key) = 0; 70179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 71179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Apply the specified updates to the database. 72179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Returns OK on success, non-OK on failure. 7395e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org // Note: consider setting options.sync = true. 74179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org virtual Status Write(const WriteOptions& options, WriteBatch* updates) = 0; 75179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 76179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // If the database contains an entry for "key" store the 77179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // corresponding value in *value and return OK. 78179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // 79179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // If there is no entry for "key" leave *value unchanged and return 80179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // a status for which Status::IsNotFound() returns true. 81179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // 82179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // May return some other Status on an error. 83179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org virtual Status Get(const ReadOptions& options, 84179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org const Slice& key, std::string* value) = 0; 85179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 86179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Return a heap-allocated iterator over the contents of the database. 87179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // The result of NewIterator() is initially invalid (caller must 88179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // call one of the Seek methods on the iterator before using it). 89179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // 90179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Caller should delete the iterator when it is no longer needed. 91179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // The returned iterator should be deleted before this db is deleted. 92179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org virtual Iterator* NewIterator(const ReadOptions& options) = 0; 93179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 94179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Return a handle to the current DB state. Iterators created with 95179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // this handle will all observe a stable snapshot of the current DB 96179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // state. The caller must call ReleaseSnapshot(result) when the 97179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // snapshot is no longer needed. 98179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org virtual const Snapshot* GetSnapshot() = 0; 99179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 100179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Release a previously acquired snapshot. The caller must not 101179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // use "snapshot" after this call. 102179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org virtual void ReleaseSnapshot(const Snapshot* snapshot) = 0; 103179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 104179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // DB implementations can export properties about their state 105179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // via this method. If "property" is a valid property understood by this 106179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // DB implementation, fills "*value" with its current value and returns 107179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // true. Otherwise returns false. 108179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // 109179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // 110179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Valid property names include: 111179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // 112179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // "leveldb.num-files-at-level<N>" - return the number of files at level <N>, 113179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // where <N> is an ASCII representation of a level number (e.g. "0"). 11495e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org // "leveldb.stats" - returns a multi-line string that describes statistics 11595e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org // about the internal operation of the DB. 1165fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com // "leveldb.sstables" - returns a multi-line string that describes all 1175fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com // of the sstables that make up the db contents. 11895e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org virtual bool GetProperty(const Slice& property, std::string* value) = 0; 119179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 120179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // For each i in [0,n-1], store in "sizes[i]", the approximate 121179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // file system space used by keys in "[range[i].start .. range[i].limit)". 122179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // 123179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Note that the returned sizes measure file system space usage, so 124179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // if the user data compresses by a factor of ten, the returned 125179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // sizes will be one-tenth the size of the corresponding user data size. 126179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // 127179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // The results may not include the sizes of recently written data. 128179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org virtual void GetApproximateSizes(const Range* range, int n, 129179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org uint64_t* sizes) = 0; 130179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 1315fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com // Compact the underlying storage for the key range [*begin,*end]. 1325fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com // In particular, deleted and overwritten versions are discarded, 1335fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com // and the data is rearranged to reduce the cost of operations 1345fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com // needed to access the data. This operation should typically only 1355fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com // be invoked by users who understand the underlying implementation. 1365fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com // 1375fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com // begin==NULL is treated as a key before all keys in the database. 1385fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com // end==NULL is treated as a key after all keys in the database. 1395fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com // Therefore the following call will compact the entire database: 1405fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com // db->CompactRange(NULL, NULL); 1415fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com virtual void CompactRange(const Slice* begin, const Slice* end) = 0; 142179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 143179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org private: 144179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // No copying allowed 145179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org DB(const DB&); 146179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org void operator=(const DB&); 147179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org}; 148179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 149179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Destroy the contents of the specified database. 150179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Be very careful using this method. 151179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgStatus DestroyDB(const std::string& name, const Options& options); 152179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 153179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// If a DB cannot be opened, you may attempt to call this method to 154179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// resurrect as much of the contents of the database as possible. 155179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Some data may be lost, so be careful when calling this function 156179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// on a database that contains important information. 157179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgStatus RepairDB(const std::string& dbname, const Options& options); 158179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 15945b9940be332834440bd5299419f396e38085ebehans@chromium.org} // namespace leveldb 160179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 161179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#endif // STORAGE_LEVELDB_INCLUDE_DB_H_ 162