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_OPTIONS_H_
6179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#define STORAGE_LEVELDB_INCLUDE_OPTIONS_H_
7179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
8179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include <stddef.h>
9179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
10179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace leveldb {
11179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
12179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass Cache;
13179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass Comparator;
14179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass Env;
1599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.comclass FilterPolicy;
16f65a55c8d0744b95be29a65d06b59b22b012f37bgabor@google.comclass Logger;
17179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass Snapshot;
18179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
19179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// DB contents are stored in a set of blocks, each of which holds a
20179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// sequence of key,value pairs.  Each block may be compressed before
21179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// being stored in a file.  The following enum describes which
22179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// compression method (if any) is used to compress a block.
23179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgenum CompressionType {
24179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // NOTE: do not change the values of existing entries, as these are
25179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // part of the persistent format on disk.
2607f3bcfb9764be2a339cc02cf0a0d6edb151defbjorlow@chromium.org  kNoCompression     = 0x0,
2751f892d349df9ed408f5a0e6e012667e5eae5f8bgabor@google.com  kSnappyCompression = 0x1
28179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org};
29179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
30179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Options to control the behavior of a database (passed to DB::Open)
31179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstruct Options {
32179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // -------------------
33179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Parameters that affect behavior
34179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
35179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Comparator used to define the order of keys in the table.
36179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Default: a comparator that uses lexicographic byte-wise ordering
37179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  //
38179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // REQUIRES: The client must ensure that the comparator supplied
39179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // here has the same name and orders keys *exactly* the same as the
40179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // comparator provided to previous open calls on the same DB.
41179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  const Comparator* comparator;
42179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
43179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // If true, the database will be created if it is missing.
44179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Default: false
45179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  bool create_if_missing;
46179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
47179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // If true, an error is raised if the database already exists.
48179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Default: false
49179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  bool error_if_exists;
50179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
51179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // If true, the implementation will do aggressive checking of the
52179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // data it is processing and will stop early if it detects any
53179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // errors.  This may have unforeseen ramifications: for example, a
54179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // corruption of one DB entry may cause a large number of entries to
55179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // become unreadable or for the entire DB to become unopenable.
56179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Default: false
57179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  bool paranoid_checks;
58179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
59179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Use the specified object to interact with the environment,
60179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // e.g. to read/write files, schedule background work, etc.
61179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Default: Env::Default()
62179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  Env* env;
63179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
64179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Any internal progress/error information generated by the db will
65f65a55c8d0744b95be29a65d06b59b22b012f37bgabor@google.com  // be written to info_log if it is non-NULL, or to a file stored
66179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // in the same directory as the DB contents if info_log is NULL.
67179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Default: NULL
68f65a55c8d0744b95be29a65d06b59b22b012f37bgabor@google.com  Logger* info_log;
69179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
70179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // -------------------
71179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Parameters that affect performance
72179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
7395e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  // Amount of data to build up in memory (backed by an unsorted log
7495e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  // on disk) before converting to a sorted on-disk file.
75179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  //
7695e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  // Larger values increase performance, especially during bulk loads.
7795e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  // Up to two write buffers may be held in memory at the same time,
7895e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  // so you may wish to adjust this parameter to control memory usage.
79917b88dd720b6e658c1fd7812bc61c605f315124gabor@google.com  // Also, a larger write buffer will result in a longer recovery time
80917b88dd720b6e658c1fd7812bc61c605f315124gabor@google.com  // the next time the database is opened.
81179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  //
8295e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  // Default: 4MB
83179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  size_t write_buffer_size;
84179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
85179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Number of open files that can be used by the DB.  You may need to
86179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // increase this if your database has a large working set (budget
87179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // one open file per 2MB of working set).
88179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  //
89179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Default: 1000
90179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  int max_open_files;
91179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
92179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Control over blocks (user data is stored in a set of blocks, and
93179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // a block is the unit of reading from disk).
94179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
9595e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  // If non-NULL, use the specified cache for blocks.
9695e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  // If NULL, leveldb will automatically create and use an 8MB internal cache.
97179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Default: NULL
98179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  Cache* block_cache;
99179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
100179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Approximate size of user data packed per block.  Note that the
101179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // block size specified here corresponds to uncompressed data.  The
102179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // actual size of the unit read from disk may be smaller if
103179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // compression is enabled.  This parameter can be changed dynamically.
104179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  //
10595e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  // Default: 4K
1061511be6edb54b6ade2bfad94256f76bc191e92ecdgrogan@chromium.org  size_t block_size;
107179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
108179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Number of keys between restart points for delta encoding of keys.
109179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // This parameter can be changed dynamically.  Most clients should
110179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // leave this parameter alone.
111179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  //
112179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Default: 16
113179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  int block_restart_interval;
114179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
115179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Compress blocks using the specified compression algorithm.  This
116179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // parameter can be changed dynamically.
117179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  //
11807f3bcfb9764be2a339cc02cf0a0d6edb151defbjorlow@chromium.org  // Default: kSnappyCompression, which gives lightweight but fast
119179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // compression.
120179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  //
12107f3bcfb9764be2a339cc02cf0a0d6edb151defbjorlow@chromium.org  // Typical speeds of kSnappyCompression on an Intel(R) Core(TM)2 2.4GHz:
122179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  //    ~200-500MB/s compression
123179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  //    ~400-800MB/s decompression
124179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Note that these speeds are significantly faster than most
125179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // persistent storage speeds, and therefore it is typically never
126179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // worth switching to kNoCompression.  Even if the input data is
12707f3bcfb9764be2a339cc02cf0a0d6edb151defbjorlow@chromium.org  // incompressible, the kSnappyCompression implementation will
128179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // efficiently detect that and will switch to uncompressed mode.
129179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  CompressionType compression;
130179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
13199a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  // If non-NULL, use the specified filter policy to reduce disk reads.
13299a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  // Many applications will benefit from passing the result of
13399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  // NewBloomFilterPolicy() here.
13499a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  //
13599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  // Default: NULL
13699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  const FilterPolicy* filter_policy;
13799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com
138179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Create an Options object with default values for all fields.
139179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  Options();
140179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org};
141179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
142179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Options that control read operations
143179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstruct ReadOptions {
144179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // If true, all data read from underlying storage will be
145179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // verified against corresponding checksums.
146179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Default: false
147179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  bool verify_checksums;
148179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
149179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Should the data read for this iteration be cached in memory?
150179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Callers may wish to set this field to false for bulk scans.
151179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Default: true
152179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  bool fill_cache;
153179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
154179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // If "snapshot" is non-NULL, read as of the supplied snapshot
155179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // (which must belong to the DB that is being read and which must
156179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // not have been released).  If "snapshot" is NULL, use an impliicit
157179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // snapshot of the state at the beginning of this read operation.
158179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Default: NULL
159179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  const Snapshot* snapshot;
160179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
161179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  ReadOptions()
162179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org      : verify_checksums(false),
163179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org        fill_cache(true),
164179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org        snapshot(NULL) {
165179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  }
166179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org};
167179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
168179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Options that control write operations
169179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstruct WriteOptions {
170179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // If true, the write will be flushed from the operating system
171179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // buffer cache (by calling WritableFile::Sync()) before the write
172179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // is considered complete.  If this flag is true, writes will be
173179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // slower.
174179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  //
175179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // If this flag is false, and the machine crashes, some recent
176179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // writes may be lost.  Note that if it is just the process that
177179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // crashes (i.e., the machine does not reboot), no writes will be
178179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // lost even if sync==false.
179179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  //
18095e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  // In other words, a DB write with sync==false has similar
18195e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  // crash semantics as the "write()" system call.  A DB write
18295e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  // with sync==true has similar crash semantics to a "write()"
18395e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  // system call followed by "fsync()".
18495e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  //
18595e21f32367748825123e382172ecbfd492ddb23dgrogan@chromium.org  // Default: false
186179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  bool sync;
187179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
188179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  WriteOptions()
1895fb21ed7ac9e91010d473ac77e132ae68f348d6agabor@google.com      : sync(false) {
190179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  }
191179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org};
192179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
19345b9940be332834440bd5299419f396e38085ebehans@chromium.org}  // namespace leveldb
194179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
195179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#endif  // STORAGE_LEVELDB_INCLUDE_OPTIONS_H_
196