leveldb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.md +95 -0
  4. data/ext/Rakefile +11 -0
  5. data/ext/leveldb/LICENSE +27 -0
  6. data/ext/leveldb/Makefile +206 -0
  7. data/ext/leveldb/build_config.mk +13 -0
  8. data/ext/leveldb/db/builder.cc +88 -0
  9. data/ext/leveldb/db/builder.h +34 -0
  10. data/ext/leveldb/db/c.cc +595 -0
  11. data/ext/leveldb/db/c_test.c +390 -0
  12. data/ext/leveldb/db/corruption_test.cc +359 -0
  13. data/ext/leveldb/db/db_bench.cc +979 -0
  14. data/ext/leveldb/db/db_impl.cc +1485 -0
  15. data/ext/leveldb/db/db_impl.h +203 -0
  16. data/ext/leveldb/db/db_iter.cc +299 -0
  17. data/ext/leveldb/db/db_iter.h +26 -0
  18. data/ext/leveldb/db/db_test.cc +2092 -0
  19. data/ext/leveldb/db/dbformat.cc +140 -0
  20. data/ext/leveldb/db/dbformat.h +227 -0
  21. data/ext/leveldb/db/dbformat_test.cc +112 -0
  22. data/ext/leveldb/db/filename.cc +139 -0
  23. data/ext/leveldb/db/filename.h +80 -0
  24. data/ext/leveldb/db/filename_test.cc +122 -0
  25. data/ext/leveldb/db/leveldb_main.cc +238 -0
  26. data/ext/leveldb/db/log_format.h +35 -0
  27. data/ext/leveldb/db/log_reader.cc +259 -0
  28. data/ext/leveldb/db/log_reader.h +108 -0
  29. data/ext/leveldb/db/log_test.cc +500 -0
  30. data/ext/leveldb/db/log_writer.cc +103 -0
  31. data/ext/leveldb/db/log_writer.h +48 -0
  32. data/ext/leveldb/db/memtable.cc +145 -0
  33. data/ext/leveldb/db/memtable.h +91 -0
  34. data/ext/leveldb/db/repair.cc +389 -0
  35. data/ext/leveldb/db/skiplist.h +379 -0
  36. data/ext/leveldb/db/skiplist_test.cc +378 -0
  37. data/ext/leveldb/db/snapshot.h +66 -0
  38. data/ext/leveldb/db/table_cache.cc +121 -0
  39. data/ext/leveldb/db/table_cache.h +61 -0
  40. data/ext/leveldb/db/version_edit.cc +266 -0
  41. data/ext/leveldb/db/version_edit.h +107 -0
  42. data/ext/leveldb/db/version_edit_test.cc +46 -0
  43. data/ext/leveldb/db/version_set.cc +1443 -0
  44. data/ext/leveldb/db/version_set.h +383 -0
  45. data/ext/leveldb/db/version_set_test.cc +179 -0
  46. data/ext/leveldb/db/write_batch.cc +147 -0
  47. data/ext/leveldb/db/write_batch_internal.h +49 -0
  48. data/ext/leveldb/db/write_batch_test.cc +120 -0
  49. data/ext/leveldb/doc/bench/db_bench_sqlite3.cc +718 -0
  50. data/ext/leveldb/doc/bench/db_bench_tree_db.cc +528 -0
  51. data/ext/leveldb/helpers/memenv/memenv.cc +384 -0
  52. data/ext/leveldb/helpers/memenv/memenv.h +20 -0
  53. data/ext/leveldb/helpers/memenv/memenv_test.cc +232 -0
  54. data/ext/leveldb/include/leveldb/c.h +291 -0
  55. data/ext/leveldb/include/leveldb/cache.h +99 -0
  56. data/ext/leveldb/include/leveldb/comparator.h +63 -0
  57. data/ext/leveldb/include/leveldb/db.h +161 -0
  58. data/ext/leveldb/include/leveldb/env.h +333 -0
  59. data/ext/leveldb/include/leveldb/filter_policy.h +70 -0
  60. data/ext/leveldb/include/leveldb/iterator.h +100 -0
  61. data/ext/leveldb/include/leveldb/options.h +195 -0
  62. data/ext/leveldb/include/leveldb/slice.h +109 -0
  63. data/ext/leveldb/include/leveldb/status.h +106 -0
  64. data/ext/leveldb/include/leveldb/table.h +85 -0
  65. data/ext/leveldb/include/leveldb/table_builder.h +92 -0
  66. data/ext/leveldb/include/leveldb/write_batch.h +64 -0
  67. data/ext/leveldb/issues/issue178_test.cc +92 -0
  68. data/ext/leveldb/port/atomic_pointer.h +224 -0
  69. data/ext/leveldb/port/port.h +19 -0
  70. data/ext/leveldb/port/port_example.h +135 -0
  71. data/ext/leveldb/port/port_posix.cc +54 -0
  72. data/ext/leveldb/port/port_posix.h +157 -0
  73. data/ext/leveldb/port/thread_annotations.h +59 -0
  74. data/ext/leveldb/port/win/stdint.h +24 -0
  75. data/ext/leveldb/table/block.cc +268 -0
  76. data/ext/leveldb/table/block.h +44 -0
  77. data/ext/leveldb/table/block_builder.cc +109 -0
  78. data/ext/leveldb/table/block_builder.h +57 -0
  79. data/ext/leveldb/table/filter_block.cc +111 -0
  80. data/ext/leveldb/table/filter_block.h +68 -0
  81. data/ext/leveldb/table/filter_block_test.cc +128 -0
  82. data/ext/leveldb/table/format.cc +145 -0
  83. data/ext/leveldb/table/format.h +108 -0
  84. data/ext/leveldb/table/iterator.cc +67 -0
  85. data/ext/leveldb/table/iterator_wrapper.h +63 -0
  86. data/ext/leveldb/table/merger.cc +197 -0
  87. data/ext/leveldb/table/merger.h +26 -0
  88. data/ext/leveldb/table/table.cc +275 -0
  89. data/ext/leveldb/table/table_builder.cc +270 -0
  90. data/ext/leveldb/table/table_test.cc +868 -0
  91. data/ext/leveldb/table/two_level_iterator.cc +182 -0
  92. data/ext/leveldb/table/two_level_iterator.h +34 -0
  93. data/ext/leveldb/util/arena.cc +68 -0
  94. data/ext/leveldb/util/arena.h +68 -0
  95. data/ext/leveldb/util/arena_test.cc +68 -0
  96. data/ext/leveldb/util/bloom.cc +95 -0
  97. data/ext/leveldb/util/bloom_test.cc +160 -0
  98. data/ext/leveldb/util/cache.cc +325 -0
  99. data/ext/leveldb/util/cache_test.cc +186 -0
  100. data/ext/leveldb/util/coding.cc +194 -0
  101. data/ext/leveldb/util/coding.h +104 -0
  102. data/ext/leveldb/util/coding_test.cc +196 -0
  103. data/ext/leveldb/util/comparator.cc +81 -0
  104. data/ext/leveldb/util/crc32c.cc +332 -0
  105. data/ext/leveldb/util/crc32c.h +45 -0
  106. data/ext/leveldb/util/crc32c_test.cc +72 -0
  107. data/ext/leveldb/util/env.cc +96 -0
  108. data/ext/leveldb/util/env_posix.cc +698 -0
  109. data/ext/leveldb/util/env_test.cc +104 -0
  110. data/ext/leveldb/util/filter_policy.cc +11 -0
  111. data/ext/leveldb/util/hash.cc +52 -0
  112. data/ext/leveldb/util/hash.h +19 -0
  113. data/ext/leveldb/util/histogram.cc +139 -0
  114. data/ext/leveldb/util/histogram.h +42 -0
  115. data/ext/leveldb/util/logging.cc +81 -0
  116. data/ext/leveldb/util/logging.h +47 -0
  117. data/ext/leveldb/util/mutexlock.h +41 -0
  118. data/ext/leveldb/util/options.cc +29 -0
  119. data/ext/leveldb/util/posix_logger.h +98 -0
  120. data/ext/leveldb/util/random.h +59 -0
  121. data/ext/leveldb/util/status.cc +75 -0
  122. data/ext/leveldb/util/testharness.cc +77 -0
  123. data/ext/leveldb/util/testharness.h +138 -0
  124. data/ext/leveldb/util/testutil.cc +51 -0
  125. data/ext/leveldb/util/testutil.h +53 -0
  126. data/lib/leveldb/version.rb +3 -0
  127. data/lib/leveldb.rb +1006 -0
  128. metadata +228 -0
@@ -0,0 +1,195 @@
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
+
5
+ #ifndef STORAGE_LEVELDB_INCLUDE_OPTIONS_H_
6
+ #define STORAGE_LEVELDB_INCLUDE_OPTIONS_H_
7
+
8
+ #include <stddef.h>
9
+
10
+ namespace leveldb {
11
+
12
+ class Cache;
13
+ class Comparator;
14
+ class Env;
15
+ class FilterPolicy;
16
+ class Logger;
17
+ class Snapshot;
18
+
19
+ // DB contents are stored in a set of blocks, each of which holds a
20
+ // sequence of key,value pairs. Each block may be compressed before
21
+ // being stored in a file. The following enum describes which
22
+ // compression method (if any) is used to compress a block.
23
+ enum CompressionType {
24
+ // NOTE: do not change the values of existing entries, as these are
25
+ // part of the persistent format on disk.
26
+ kNoCompression = 0x0,
27
+ kSnappyCompression = 0x1
28
+ };
29
+
30
+ // Options to control the behavior of a database (passed to DB::Open)
31
+ struct Options {
32
+ // -------------------
33
+ // Parameters that affect behavior
34
+
35
+ // Comparator used to define the order of keys in the table.
36
+ // Default: a comparator that uses lexicographic byte-wise ordering
37
+ //
38
+ // REQUIRES: The client must ensure that the comparator supplied
39
+ // here has the same name and orders keys *exactly* the same as the
40
+ // comparator provided to previous open calls on the same DB.
41
+ const Comparator* comparator;
42
+
43
+ // If true, the database will be created if it is missing.
44
+ // Default: false
45
+ bool create_if_missing;
46
+
47
+ // If true, an error is raised if the database already exists.
48
+ // Default: false
49
+ bool error_if_exists;
50
+
51
+ // If true, the implementation will do aggressive checking of the
52
+ // data it is processing and will stop early if it detects any
53
+ // errors. This may have unforeseen ramifications: for example, a
54
+ // corruption of one DB entry may cause a large number of entries to
55
+ // become unreadable or for the entire DB to become unopenable.
56
+ // Default: false
57
+ bool paranoid_checks;
58
+
59
+ // Use the specified object to interact with the environment,
60
+ // e.g. to read/write files, schedule background work, etc.
61
+ // Default: Env::Default()
62
+ Env* env;
63
+
64
+ // Any internal progress/error information generated by the db will
65
+ // be written to info_log if it is non-NULL, or to a file stored
66
+ // in the same directory as the DB contents if info_log is NULL.
67
+ // Default: NULL
68
+ Logger* info_log;
69
+
70
+ // -------------------
71
+ // Parameters that affect performance
72
+
73
+ // Amount of data to build up in memory (backed by an unsorted log
74
+ // on disk) before converting to a sorted on-disk file.
75
+ //
76
+ // Larger values increase performance, especially during bulk loads.
77
+ // Up to two write buffers may be held in memory at the same time,
78
+ // so you may wish to adjust this parameter to control memory usage.
79
+ // Also, a larger write buffer will result in a longer recovery time
80
+ // the next time the database is opened.
81
+ //
82
+ // Default: 4MB
83
+ size_t write_buffer_size;
84
+
85
+ // Number of open files that can be used by the DB. You may need to
86
+ // increase this if your database has a large working set (budget
87
+ // one open file per 2MB of working set).
88
+ //
89
+ // Default: 1000
90
+ int max_open_files;
91
+
92
+ // Control over blocks (user data is stored in a set of blocks, and
93
+ // a block is the unit of reading from disk).
94
+
95
+ // If non-NULL, use the specified cache for blocks.
96
+ // If NULL, leveldb will automatically create and use an 8MB internal cache.
97
+ // Default: NULL
98
+ Cache* block_cache;
99
+
100
+ // Approximate size of user data packed per block. Note that the
101
+ // block size specified here corresponds to uncompressed data. The
102
+ // actual size of the unit read from disk may be smaller if
103
+ // compression is enabled. This parameter can be changed dynamically.
104
+ //
105
+ // Default: 4K
106
+ size_t block_size;
107
+
108
+ // Number of keys between restart points for delta encoding of keys.
109
+ // This parameter can be changed dynamically. Most clients should
110
+ // leave this parameter alone.
111
+ //
112
+ // Default: 16
113
+ int block_restart_interval;
114
+
115
+ // Compress blocks using the specified compression algorithm. This
116
+ // parameter can be changed dynamically.
117
+ //
118
+ // Default: kSnappyCompression, which gives lightweight but fast
119
+ // compression.
120
+ //
121
+ // Typical speeds of kSnappyCompression on an Intel(R) Core(TM)2 2.4GHz:
122
+ // ~200-500MB/s compression
123
+ // ~400-800MB/s decompression
124
+ // Note that these speeds are significantly faster than most
125
+ // persistent storage speeds, and therefore it is typically never
126
+ // worth switching to kNoCompression. Even if the input data is
127
+ // incompressible, the kSnappyCompression implementation will
128
+ // efficiently detect that and will switch to uncompressed mode.
129
+ CompressionType compression;
130
+
131
+ // If non-NULL, use the specified filter policy to reduce disk reads.
132
+ // Many applications will benefit from passing the result of
133
+ // NewBloomFilterPolicy() here.
134
+ //
135
+ // Default: NULL
136
+ const FilterPolicy* filter_policy;
137
+
138
+ // Create an Options object with default values for all fields.
139
+ Options();
140
+ };
141
+
142
+ // Options that control read operations
143
+ struct ReadOptions {
144
+ // If true, all data read from underlying storage will be
145
+ // verified against corresponding checksums.
146
+ // Default: false
147
+ bool verify_checksums;
148
+
149
+ // Should the data read for this iteration be cached in memory?
150
+ // Callers may wish to set this field to false for bulk scans.
151
+ // Default: true
152
+ bool fill_cache;
153
+
154
+ // If "snapshot" is non-NULL, read as of the supplied snapshot
155
+ // (which must belong to the DB that is being read and which must
156
+ // not have been released). If "snapshot" is NULL, use an impliicit
157
+ // snapshot of the state at the beginning of this read operation.
158
+ // Default: NULL
159
+ const Snapshot* snapshot;
160
+
161
+ ReadOptions()
162
+ : verify_checksums(false),
163
+ fill_cache(true),
164
+ snapshot(NULL) {
165
+ }
166
+ };
167
+
168
+ // Options that control write operations
169
+ struct WriteOptions {
170
+ // If true, the write will be flushed from the operating system
171
+ // buffer cache (by calling WritableFile::Sync()) before the write
172
+ // is considered complete. If this flag is true, writes will be
173
+ // slower.
174
+ //
175
+ // If this flag is false, and the machine crashes, some recent
176
+ // writes may be lost. Note that if it is just the process that
177
+ // crashes (i.e., the machine does not reboot), no writes will be
178
+ // lost even if sync==false.
179
+ //
180
+ // In other words, a DB write with sync==false has similar
181
+ // crash semantics as the "write()" system call. A DB write
182
+ // with sync==true has similar crash semantics to a "write()"
183
+ // system call followed by "fsync()".
184
+ //
185
+ // Default: false
186
+ bool sync;
187
+
188
+ WriteOptions()
189
+ : sync(false) {
190
+ }
191
+ };
192
+
193
+ } // namespace leveldb
194
+
195
+ #endif // STORAGE_LEVELDB_INCLUDE_OPTIONS_H_
@@ -0,0 +1,109 @@
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
+ //
5
+ // Slice is a simple structure containing a pointer into some external
6
+ // storage and a size. The user of a Slice must ensure that the slice
7
+ // is not used after the corresponding external storage has been
8
+ // deallocated.
9
+ //
10
+ // Multiple threads can invoke const methods on a Slice without
11
+ // external synchronization, but if any of the threads may call a
12
+ // non-const method, all threads accessing the same Slice must use
13
+ // external synchronization.
14
+
15
+ #ifndef STORAGE_LEVELDB_INCLUDE_SLICE_H_
16
+ #define STORAGE_LEVELDB_INCLUDE_SLICE_H_
17
+
18
+ #include <assert.h>
19
+ #include <stddef.h>
20
+ #include <string.h>
21
+ #include <string>
22
+
23
+ namespace leveldb {
24
+
25
+ class Slice {
26
+ public:
27
+ // Create an empty slice.
28
+ Slice() : data_(""), size_(0) { }
29
+
30
+ // Create a slice that refers to d[0,n-1].
31
+ Slice(const char* d, size_t n) : data_(d), size_(n) { }
32
+
33
+ // Create a slice that refers to the contents of "s"
34
+ Slice(const std::string& s) : data_(s.data()), size_(s.size()) { }
35
+
36
+ // Create a slice that refers to s[0,strlen(s)-1]
37
+ Slice(const char* s) : data_(s), size_(strlen(s)) { }
38
+
39
+ // Return a pointer to the beginning of the referenced data
40
+ const char* data() const { return data_; }
41
+
42
+ // Return the length (in bytes) of the referenced data
43
+ size_t size() const { return size_; }
44
+
45
+ // Return true iff the length of the referenced data is zero
46
+ bool empty() const { return size_ == 0; }
47
+
48
+ // Return the ith byte in the referenced data.
49
+ // REQUIRES: n < size()
50
+ char operator[](size_t n) const {
51
+ assert(n < size());
52
+ return data_[n];
53
+ }
54
+
55
+ // Change this slice to refer to an empty array
56
+ void clear() { data_ = ""; size_ = 0; }
57
+
58
+ // Drop the first "n" bytes from this slice.
59
+ void remove_prefix(size_t n) {
60
+ assert(n <= size());
61
+ data_ += n;
62
+ size_ -= n;
63
+ }
64
+
65
+ // Return a string that contains the copy of the referenced data.
66
+ std::string ToString() const { return std::string(data_, size_); }
67
+
68
+ // Three-way comparison. Returns value:
69
+ // < 0 iff "*this" < "b",
70
+ // == 0 iff "*this" == "b",
71
+ // > 0 iff "*this" > "b"
72
+ int compare(const Slice& b) const;
73
+
74
+ // Return true iff "x" is a prefix of "*this"
75
+ bool starts_with(const Slice& x) const {
76
+ return ((size_ >= x.size_) &&
77
+ (memcmp(data_, x.data_, x.size_) == 0));
78
+ }
79
+
80
+ private:
81
+ const char* data_;
82
+ size_t size_;
83
+
84
+ // Intentionally copyable
85
+ };
86
+
87
+ inline bool operator==(const Slice& x, const Slice& y) {
88
+ return ((x.size() == y.size()) &&
89
+ (memcmp(x.data(), y.data(), x.size()) == 0));
90
+ }
91
+
92
+ inline bool operator!=(const Slice& x, const Slice& y) {
93
+ return !(x == y);
94
+ }
95
+
96
+ inline int Slice::compare(const Slice& b) const {
97
+ const int min_len = (size_ < b.size_) ? size_ : b.size_;
98
+ int r = memcmp(data_, b.data_, min_len);
99
+ if (r == 0) {
100
+ if (size_ < b.size_) r = -1;
101
+ else if (size_ > b.size_) r = +1;
102
+ }
103
+ return r;
104
+ }
105
+
106
+ } // namespace leveldb
107
+
108
+
109
+ #endif // STORAGE_LEVELDB_INCLUDE_SLICE_H_
@@ -0,0 +1,106 @@
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
+ //
5
+ // A Status encapsulates the result of an operation. It may indicate success,
6
+ // or it may indicate an error with an associated error message.
7
+ //
8
+ // Multiple threads can invoke const methods on a Status without
9
+ // external synchronization, but if any of the threads may call a
10
+ // non-const method, all threads accessing the same Status must use
11
+ // external synchronization.
12
+
13
+ #ifndef STORAGE_LEVELDB_INCLUDE_STATUS_H_
14
+ #define STORAGE_LEVELDB_INCLUDE_STATUS_H_
15
+
16
+ #include <string>
17
+ #include "leveldb/slice.h"
18
+
19
+ namespace leveldb {
20
+
21
+ class Status {
22
+ public:
23
+ // Create a success status.
24
+ Status() : state_(NULL) { }
25
+ ~Status() { delete[] state_; }
26
+
27
+ // Copy the specified status.
28
+ Status(const Status& s);
29
+ void operator=(const Status& s);
30
+
31
+ // Return a success status.
32
+ static Status OK() { return Status(); }
33
+
34
+ // Return error status of an appropriate type.
35
+ static Status NotFound(const Slice& msg, const Slice& msg2 = Slice()) {
36
+ return Status(kNotFound, msg, msg2);
37
+ }
38
+ static Status Corruption(const Slice& msg, const Slice& msg2 = Slice()) {
39
+ return Status(kCorruption, msg, msg2);
40
+ }
41
+ static Status NotSupported(const Slice& msg, const Slice& msg2 = Slice()) {
42
+ return Status(kNotSupported, msg, msg2);
43
+ }
44
+ static Status InvalidArgument(const Slice& msg, const Slice& msg2 = Slice()) {
45
+ return Status(kInvalidArgument, msg, msg2);
46
+ }
47
+ static Status IOError(const Slice& msg, const Slice& msg2 = Slice()) {
48
+ return Status(kIOError, msg, msg2);
49
+ }
50
+
51
+ // Returns true iff the status indicates success.
52
+ bool ok() const { return (state_ == NULL); }
53
+
54
+ // Returns true iff the status indicates a NotFound error.
55
+ bool IsNotFound() const { return code() == kNotFound; }
56
+
57
+ // Returns true iff the status indicates a Corruption error.
58
+ bool IsCorruption() const { return code() == kCorruption; }
59
+
60
+ // Returns true iff the status indicates an IOError.
61
+ bool IsIOError() const { return code() == kIOError; }
62
+
63
+ // Return a string representation of this status suitable for printing.
64
+ // Returns the string "OK" for success.
65
+ std::string ToString() const;
66
+
67
+ private:
68
+ // OK status has a NULL state_. Otherwise, state_ is a new[] array
69
+ // of the following form:
70
+ // state_[0..3] == length of message
71
+ // state_[4] == code
72
+ // state_[5..] == message
73
+ const char* state_;
74
+
75
+ enum Code {
76
+ kOk = 0,
77
+ kNotFound = 1,
78
+ kCorruption = 2,
79
+ kNotSupported = 3,
80
+ kInvalidArgument = 4,
81
+ kIOError = 5
82
+ };
83
+
84
+ Code code() const {
85
+ return (state_ == NULL) ? kOk : static_cast<Code>(state_[4]);
86
+ }
87
+
88
+ Status(Code code, const Slice& msg, const Slice& msg2);
89
+ static const char* CopyState(const char* s);
90
+ };
91
+
92
+ inline Status::Status(const Status& s) {
93
+ state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
94
+ }
95
+ inline void Status::operator=(const Status& s) {
96
+ // The following condition catches both aliasing (when this == &s),
97
+ // and the common case where both s and *this are ok.
98
+ if (state_ != s.state_) {
99
+ delete[] state_;
100
+ state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
101
+ }
102
+ }
103
+
104
+ } // namespace leveldb
105
+
106
+ #endif // STORAGE_LEVELDB_INCLUDE_STATUS_H_
@@ -0,0 +1,85 @@
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
+
5
+ #ifndef STORAGE_LEVELDB_INCLUDE_TABLE_H_
6
+ #define STORAGE_LEVELDB_INCLUDE_TABLE_H_
7
+
8
+ #include <stdint.h>
9
+ #include "leveldb/iterator.h"
10
+
11
+ namespace leveldb {
12
+
13
+ class Block;
14
+ class BlockHandle;
15
+ class Footer;
16
+ struct Options;
17
+ class RandomAccessFile;
18
+ struct ReadOptions;
19
+ class TableCache;
20
+
21
+ // A Table is a sorted map from strings to strings. Tables are
22
+ // immutable and persistent. A Table may be safely accessed from
23
+ // multiple threads without external synchronization.
24
+ class Table {
25
+ public:
26
+ // Attempt to open the table that is stored in bytes [0..file_size)
27
+ // of "file", and read the metadata entries necessary to allow
28
+ // retrieving data from the table.
29
+ //
30
+ // If successful, returns ok and sets "*table" to the newly opened
31
+ // table. The client should delete "*table" when no longer needed.
32
+ // If there was an error while initializing the table, sets "*table"
33
+ // to NULL and returns a non-ok status. Does not take ownership of
34
+ // "*source", but the client must ensure that "source" remains live
35
+ // for the duration of the returned table's lifetime.
36
+ //
37
+ // *file must remain live while this Table is in use.
38
+ static Status Open(const Options& options,
39
+ RandomAccessFile* file,
40
+ uint64_t file_size,
41
+ Table** table);
42
+
43
+ ~Table();
44
+
45
+ // Returns a new iterator over the table contents.
46
+ // The result of NewIterator() is initially invalid (caller must
47
+ // call one of the Seek methods on the iterator before using it).
48
+ Iterator* NewIterator(const ReadOptions&) const;
49
+
50
+ // Given a key, return an approximate byte offset in the file where
51
+ // the data for that key begins (or would begin if the key were
52
+ // present in the file). The returned value is in terms of file
53
+ // bytes, and so includes effects like compression of the underlying data.
54
+ // E.g., the approximate offset of the last key in the table will
55
+ // be close to the file length.
56
+ uint64_t ApproximateOffsetOf(const Slice& key) const;
57
+
58
+ private:
59
+ struct Rep;
60
+ Rep* rep_;
61
+
62
+ explicit Table(Rep* rep) { rep_ = rep; }
63
+ static Iterator* BlockReader(void*, const ReadOptions&, const Slice&);
64
+
65
+ // Calls (*handle_result)(arg, ...) with the entry found after a call
66
+ // to Seek(key). May not make such a call if filter policy says
67
+ // that key is not present.
68
+ friend class TableCache;
69
+ Status InternalGet(
70
+ const ReadOptions&, const Slice& key,
71
+ void* arg,
72
+ void (*handle_result)(void* arg, const Slice& k, const Slice& v));
73
+
74
+
75
+ void ReadMeta(const Footer& footer);
76
+ void ReadFilter(const Slice& filter_handle_value);
77
+
78
+ // No copying allowed
79
+ Table(const Table&);
80
+ void operator=(const Table&);
81
+ };
82
+
83
+ } // namespace leveldb
84
+
85
+ #endif // STORAGE_LEVELDB_INCLUDE_TABLE_H_
@@ -0,0 +1,92 @@
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
+ //
5
+ // TableBuilder provides the interface used to build a Table
6
+ // (an immutable and sorted map from keys to values).
7
+ //
8
+ // Multiple threads can invoke const methods on a TableBuilder without
9
+ // external synchronization, but if any of the threads may call a
10
+ // non-const method, all threads accessing the same TableBuilder must use
11
+ // external synchronization.
12
+
13
+ #ifndef STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_
14
+ #define STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_
15
+
16
+ #include <stdint.h>
17
+ #include "leveldb/options.h"
18
+ #include "leveldb/status.h"
19
+
20
+ namespace leveldb {
21
+
22
+ class BlockBuilder;
23
+ class BlockHandle;
24
+ class WritableFile;
25
+
26
+ class TableBuilder {
27
+ public:
28
+ // Create a builder that will store the contents of the table it is
29
+ // building in *file. Does not close the file. It is up to the
30
+ // caller to close the file after calling Finish().
31
+ TableBuilder(const Options& options, WritableFile* file);
32
+
33
+ // REQUIRES: Either Finish() or Abandon() has been called.
34
+ ~TableBuilder();
35
+
36
+ // Change the options used by this builder. Note: only some of the
37
+ // option fields can be changed after construction. If a field is
38
+ // not allowed to change dynamically and its value in the structure
39
+ // passed to the constructor is different from its value in the
40
+ // structure passed to this method, this method will return an error
41
+ // without changing any fields.
42
+ Status ChangeOptions(const Options& options);
43
+
44
+ // Add key,value to the table being constructed.
45
+ // REQUIRES: key is after any previously added key according to comparator.
46
+ // REQUIRES: Finish(), Abandon() have not been called
47
+ void Add(const Slice& key, const Slice& value);
48
+
49
+ // Advanced operation: flush any buffered key/value pairs to file.
50
+ // Can be used to ensure that two adjacent entries never live in
51
+ // the same data block. Most clients should not need to use this method.
52
+ // REQUIRES: Finish(), Abandon() have not been called
53
+ void Flush();
54
+
55
+ // Return non-ok iff some error has been detected.
56
+ Status status() const;
57
+
58
+ // Finish building the table. Stops using the file passed to the
59
+ // constructor after this function returns.
60
+ // REQUIRES: Finish(), Abandon() have not been called
61
+ Status Finish();
62
+
63
+ // Indicate that the contents of this builder should be abandoned. Stops
64
+ // using the file passed to the constructor after this function returns.
65
+ // If the caller is not going to call Finish(), it must call Abandon()
66
+ // before destroying this builder.
67
+ // REQUIRES: Finish(), Abandon() have not been called
68
+ void Abandon();
69
+
70
+ // Number of calls to Add() so far.
71
+ uint64_t NumEntries() const;
72
+
73
+ // Size of the file generated so far. If invoked after a successful
74
+ // Finish() call, returns the size of the final generated file.
75
+ uint64_t FileSize() const;
76
+
77
+ private:
78
+ bool ok() const { return status().ok(); }
79
+ void WriteBlock(BlockBuilder* block, BlockHandle* handle);
80
+ void WriteRawBlock(const Slice& data, CompressionType, BlockHandle* handle);
81
+
82
+ struct Rep;
83
+ Rep* rep_;
84
+
85
+ // No copying allowed
86
+ TableBuilder(const TableBuilder&);
87
+ void operator=(const TableBuilder&);
88
+ };
89
+
90
+ } // namespace leveldb
91
+
92
+ #endif // STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_
@@ -0,0 +1,64 @@
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
+ //
5
+ // WriteBatch holds a collection of updates to apply atomically to a DB.
6
+ //
7
+ // The updates are applied in the order in which they are added
8
+ // to the WriteBatch. For example, the value of "key" will be "v3"
9
+ // after the following batch is written:
10
+ //
11
+ // batch.Put("key", "v1");
12
+ // batch.Delete("key");
13
+ // batch.Put("key", "v2");
14
+ // batch.Put("key", "v3");
15
+ //
16
+ // Multiple threads can invoke const methods on a WriteBatch without
17
+ // external synchronization, but if any of the threads may call a
18
+ // non-const method, all threads accessing the same WriteBatch must use
19
+ // external synchronization.
20
+
21
+ #ifndef STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_
22
+ #define STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_
23
+
24
+ #include <string>
25
+ #include "leveldb/status.h"
26
+
27
+ namespace leveldb {
28
+
29
+ class Slice;
30
+
31
+ class WriteBatch {
32
+ public:
33
+ WriteBatch();
34
+ ~WriteBatch();
35
+
36
+ // Store the mapping "key->value" in the database.
37
+ void Put(const Slice& key, const Slice& value);
38
+
39
+ // If the database contains a mapping for "key", erase it. Else do nothing.
40
+ void Delete(const Slice& key);
41
+
42
+ // Clear all updates buffered in this batch.
43
+ void Clear();
44
+
45
+ // Support for iterating over the contents of a batch.
46
+ class Handler {
47
+ public:
48
+ virtual ~Handler();
49
+ virtual void Put(const Slice& key, const Slice& value) = 0;
50
+ virtual void Delete(const Slice& key) = 0;
51
+ };
52
+ Status Iterate(Handler* handler) const;
53
+
54
+ private:
55
+ friend class WriteBatchInternal;
56
+
57
+ std::string rep_; // See comment in write_batch.cc for the format of rep_
58
+
59
+ // Intentionally copyable
60
+ };
61
+
62
+ } // namespace leveldb
63
+
64
+ #endif // STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_