filiptepper-leveldb-ruby 0.14
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +24 -0
- data/README +72 -0
- data/ext/leveldb/extconf.rb +14 -0
- data/ext/leveldb/leveldb.cc +530 -0
- data/ext/leveldb/platform.rb +83 -0
- data/leveldb/Makefile +191 -0
- data/leveldb/build_detect_platform +160 -0
- data/leveldb/db/builder.cc +88 -0
- data/leveldb/db/builder.h +34 -0
- data/leveldb/db/c.cc +581 -0
- data/leveldb/db/corruption_test.cc +359 -0
- data/leveldb/db/db_bench.cc +970 -0
- data/leveldb/db/db_impl.cc +1448 -0
- data/leveldb/db/db_impl.h +194 -0
- data/leveldb/db/db_iter.cc +299 -0
- data/leveldb/db/db_iter.h +26 -0
- data/leveldb/db/db_test.cc +1901 -0
- data/leveldb/db/dbformat.cc +140 -0
- data/leveldb/db/dbformat.h +227 -0
- data/leveldb/db/dbformat_test.cc +112 -0
- data/leveldb/db/filename.cc +139 -0
- data/leveldb/db/filename.h +80 -0
- data/leveldb/db/filename_test.cc +122 -0
- data/leveldb/db/log_format.h +35 -0
- data/leveldb/db/log_reader.cc +259 -0
- data/leveldb/db/log_reader.h +108 -0
- data/leveldb/db/log_test.cc +500 -0
- data/leveldb/db/log_writer.cc +103 -0
- data/leveldb/db/log_writer.h +48 -0
- data/leveldb/db/memtable.cc +145 -0
- data/leveldb/db/memtable.h +91 -0
- data/leveldb/db/repair.cc +389 -0
- data/leveldb/db/skiplist.h +379 -0
- data/leveldb/db/skiplist_test.cc +378 -0
- data/leveldb/db/snapshot.h +66 -0
- data/leveldb/db/table_cache.cc +121 -0
- data/leveldb/db/table_cache.h +61 -0
- data/leveldb/db/version_edit.cc +266 -0
- data/leveldb/db/version_edit.h +107 -0
- data/leveldb/db/version_edit_test.cc +46 -0
- data/leveldb/db/version_set.cc +1402 -0
- data/leveldb/db/version_set.h +370 -0
- data/leveldb/db/version_set_test.cc +179 -0
- data/leveldb/db/write_batch.cc +147 -0
- data/leveldb/db/write_batch_internal.h +49 -0
- data/leveldb/db/write_batch_test.cc +120 -0
- data/leveldb/helpers/memenv/memenv.cc +374 -0
- data/leveldb/helpers/memenv/memenv.h +20 -0
- data/leveldb/helpers/memenv/memenv_test.cc +232 -0
- data/leveldb/include/leveldb/c.h +275 -0
- data/leveldb/include/leveldb/cache.h +99 -0
- data/leveldb/include/leveldb/comparator.h +63 -0
- data/leveldb/include/leveldb/db.h +161 -0
- data/leveldb/include/leveldb/env.h +323 -0
- data/leveldb/include/leveldb/filter_policy.h +70 -0
- data/leveldb/include/leveldb/iterator.h +100 -0
- data/leveldb/include/leveldb/options.h +195 -0
- data/leveldb/include/leveldb/slice.h +109 -0
- data/leveldb/include/leveldb/status.h +106 -0
- data/leveldb/include/leveldb/table.h +85 -0
- data/leveldb/include/leveldb/table_builder.h +92 -0
- data/leveldb/include/leveldb/write_batch.h +64 -0
- data/leveldb/port/atomic_pointer.h +144 -0
- data/leveldb/port/port.h +21 -0
- data/leveldb/port/port_android.cc +64 -0
- data/leveldb/port/port_android.h +159 -0
- data/leveldb/port/port_example.h +125 -0
- data/leveldb/port/port_posix.cc +50 -0
- data/leveldb/port/port_posix.h +129 -0
- data/leveldb/port/win/stdint.h +24 -0
- data/leveldb/table/block.cc +267 -0
- data/leveldb/table/block.h +44 -0
- data/leveldb/table/block_builder.cc +109 -0
- data/leveldb/table/block_builder.h +57 -0
- data/leveldb/table/filter_block.cc +111 -0
- data/leveldb/table/filter_block.h +68 -0
- data/leveldb/table/filter_block_test.cc +128 -0
- data/leveldb/table/format.cc +145 -0
- data/leveldb/table/format.h +108 -0
- data/leveldb/table/iterator.cc +67 -0
- data/leveldb/table/iterator_wrapper.h +63 -0
- data/leveldb/table/merger.cc +197 -0
- data/leveldb/table/merger.h +26 -0
- data/leveldb/table/table.cc +276 -0
- data/leveldb/table/table_builder.cc +270 -0
- data/leveldb/table/table_test.cc +838 -0
- data/leveldb/table/two_level_iterator.cc +182 -0
- data/leveldb/table/two_level_iterator.h +34 -0
- data/leveldb/util/arena.cc +68 -0
- data/leveldb/util/arena.h +68 -0
- data/leveldb/util/arena_test.cc +68 -0
- data/leveldb/util/bloom.cc +95 -0
- data/leveldb/util/bloom_test.cc +159 -0
- data/leveldb/util/cache.cc +328 -0
- data/leveldb/util/cache_test.cc +186 -0
- data/leveldb/util/coding.cc +194 -0
- data/leveldb/util/coding.h +104 -0
- data/leveldb/util/coding_test.cc +173 -0
- data/leveldb/util/comparator.cc +76 -0
- data/leveldb/util/crc32c.cc +332 -0
- data/leveldb/util/crc32c.h +45 -0
- data/leveldb/util/crc32c_test.cc +72 -0
- data/leveldb/util/env.cc +96 -0
- data/leveldb/util/env_posix.cc +609 -0
- data/leveldb/util/env_test.cc +104 -0
- data/leveldb/util/filter_policy.cc +11 -0
- data/leveldb/util/hash.cc +45 -0
- data/leveldb/util/hash.h +19 -0
- data/leveldb/util/histogram.cc +139 -0
- data/leveldb/util/histogram.h +42 -0
- data/leveldb/util/logging.cc +81 -0
- data/leveldb/util/logging.h +47 -0
- data/leveldb/util/mutexlock.h +39 -0
- data/leveldb/util/options.cc +29 -0
- data/leveldb/util/posix_logger.h +98 -0
- data/leveldb/util/random.h +59 -0
- data/leveldb/util/status.cc +75 -0
- data/leveldb/util/testharness.cc +77 -0
- data/leveldb/util/testharness.h +138 -0
- data/leveldb/util/testutil.cc +51 -0
- data/leveldb/util/testutil.h +53 -0
- data/lib/leveldb.rb +76 -0
- metadata +175 -0
@@ -0,0 +1,276 @@
|
|
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
|
+
#include "leveldb/table.h"
|
6
|
+
|
7
|
+
#include "leveldb/cache.h"
|
8
|
+
#include "leveldb/comparator.h"
|
9
|
+
#include "leveldb/env.h"
|
10
|
+
#include "leveldb/filter_policy.h"
|
11
|
+
#include "leveldb/options.h"
|
12
|
+
#include "table/block.h"
|
13
|
+
#include "table/filter_block.h"
|
14
|
+
#include "table/format.h"
|
15
|
+
#include "table/two_level_iterator.h"
|
16
|
+
#include "util/coding.h"
|
17
|
+
|
18
|
+
namespace leveldb {
|
19
|
+
|
20
|
+
struct Table::Rep {
|
21
|
+
~Rep() {
|
22
|
+
delete filter;
|
23
|
+
delete [] filter_data;
|
24
|
+
delete index_block;
|
25
|
+
}
|
26
|
+
|
27
|
+
Options options;
|
28
|
+
Status status;
|
29
|
+
RandomAccessFile* file;
|
30
|
+
uint64_t cache_id;
|
31
|
+
FilterBlockReader* filter;
|
32
|
+
const char* filter_data;
|
33
|
+
|
34
|
+
BlockHandle metaindex_handle; // Handle to metaindex_block: saved from footer
|
35
|
+
Block* index_block;
|
36
|
+
};
|
37
|
+
|
38
|
+
Status Table::Open(const Options& options,
|
39
|
+
RandomAccessFile* file,
|
40
|
+
uint64_t size,
|
41
|
+
Table** table) {
|
42
|
+
*table = NULL;
|
43
|
+
if (size < Footer::kEncodedLength) {
|
44
|
+
return Status::InvalidArgument("file is too short to be an sstable");
|
45
|
+
}
|
46
|
+
|
47
|
+
char footer_space[Footer::kEncodedLength];
|
48
|
+
Slice footer_input;
|
49
|
+
Status s = file->Read(size - Footer::kEncodedLength, Footer::kEncodedLength,
|
50
|
+
&footer_input, footer_space);
|
51
|
+
if (!s.ok()) return s;
|
52
|
+
|
53
|
+
Footer footer;
|
54
|
+
s = footer.DecodeFrom(&footer_input);
|
55
|
+
if (!s.ok()) return s;
|
56
|
+
|
57
|
+
// Read the index block
|
58
|
+
BlockContents contents;
|
59
|
+
Block* index_block = NULL;
|
60
|
+
if (s.ok()) {
|
61
|
+
s = ReadBlock(file, ReadOptions(), footer.index_handle(), &contents);
|
62
|
+
if (s.ok()) {
|
63
|
+
index_block = new Block(contents);
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
if (s.ok()) {
|
68
|
+
// We've successfully read the footer and the index block: we're
|
69
|
+
// ready to serve requests.
|
70
|
+
Rep* rep = new Table::Rep;
|
71
|
+
rep->options = options;
|
72
|
+
rep->file = file;
|
73
|
+
rep->metaindex_handle = footer.metaindex_handle();
|
74
|
+
rep->index_block = index_block;
|
75
|
+
rep->cache_id = (options.block_cache ? options.block_cache->NewId() : 0);
|
76
|
+
rep->filter_data = NULL;
|
77
|
+
rep->filter = NULL;
|
78
|
+
*table = new Table(rep);
|
79
|
+
(*table)->ReadMeta(footer);
|
80
|
+
} else {
|
81
|
+
if (index_block) delete index_block;
|
82
|
+
}
|
83
|
+
|
84
|
+
return s;
|
85
|
+
}
|
86
|
+
|
87
|
+
void Table::ReadMeta(const Footer& footer) {
|
88
|
+
if (rep_->options.filter_policy == NULL) {
|
89
|
+
return; // Do not need any metadata
|
90
|
+
}
|
91
|
+
|
92
|
+
// TODO(sanjay): Skip this if footer.metaindex_handle() size indicates
|
93
|
+
// it is an empty block.
|
94
|
+
ReadOptions opt;
|
95
|
+
BlockContents contents;
|
96
|
+
if (!ReadBlock(rep_->file, opt, footer.metaindex_handle(), &contents).ok()) {
|
97
|
+
// Do not propagate errors since meta info is not needed for operation
|
98
|
+
return;
|
99
|
+
}
|
100
|
+
Block* meta = new Block(contents);
|
101
|
+
|
102
|
+
Iterator* iter = meta->NewIterator(BytewiseComparator());
|
103
|
+
std::string key = "filter.";
|
104
|
+
key.append(rep_->options.filter_policy->Name());
|
105
|
+
iter->Seek(key);
|
106
|
+
if (iter->Valid() && iter->key() == Slice(key)) {
|
107
|
+
ReadFilter(iter->value());
|
108
|
+
}
|
109
|
+
delete iter;
|
110
|
+
delete meta;
|
111
|
+
}
|
112
|
+
|
113
|
+
void Table::ReadFilter(const Slice& filter_handle_value) {
|
114
|
+
Slice v = filter_handle_value;
|
115
|
+
BlockHandle filter_handle;
|
116
|
+
if (!filter_handle.DecodeFrom(&v).ok()) {
|
117
|
+
return;
|
118
|
+
}
|
119
|
+
|
120
|
+
// We might want to unify with ReadBlock() if we start
|
121
|
+
// requiring checksum verification in Table::Open.
|
122
|
+
ReadOptions opt;
|
123
|
+
BlockContents block;
|
124
|
+
if (!ReadBlock(rep_->file, opt, filter_handle, &block).ok()) {
|
125
|
+
return;
|
126
|
+
}
|
127
|
+
if (block.heap_allocated) {
|
128
|
+
rep_->filter_data = block.data.data(); // Will need to delete later
|
129
|
+
}
|
130
|
+
rep_->filter = new FilterBlockReader(rep_->options.filter_policy, block.data);
|
131
|
+
}
|
132
|
+
|
133
|
+
Table::~Table() {
|
134
|
+
delete rep_;
|
135
|
+
}
|
136
|
+
|
137
|
+
static void DeleteBlock(void* arg, void* ignored) {
|
138
|
+
delete reinterpret_cast<Block*>(arg);
|
139
|
+
}
|
140
|
+
|
141
|
+
static void DeleteCachedBlock(const Slice& key, void* value) {
|
142
|
+
Block* block = reinterpret_cast<Block*>(value);
|
143
|
+
delete block;
|
144
|
+
}
|
145
|
+
|
146
|
+
static void ReleaseBlock(void* arg, void* h) {
|
147
|
+
Cache* cache = reinterpret_cast<Cache*>(arg);
|
148
|
+
Cache::Handle* handle = reinterpret_cast<Cache::Handle*>(h);
|
149
|
+
cache->Release(handle);
|
150
|
+
}
|
151
|
+
|
152
|
+
// Convert an index iterator value (i.e., an encoded BlockHandle)
|
153
|
+
// into an iterator over the contents of the corresponding block.
|
154
|
+
Iterator* Table::BlockReader(void* arg,
|
155
|
+
const ReadOptions& options,
|
156
|
+
const Slice& index_value) {
|
157
|
+
Table* table = reinterpret_cast<Table*>(arg);
|
158
|
+
Cache* block_cache = table->rep_->options.block_cache;
|
159
|
+
Block* block = NULL;
|
160
|
+
Cache::Handle* cache_handle = NULL;
|
161
|
+
|
162
|
+
BlockHandle handle;
|
163
|
+
Slice input = index_value;
|
164
|
+
Status s = handle.DecodeFrom(&input);
|
165
|
+
// We intentionally allow extra stuff in index_value so that we
|
166
|
+
// can add more features in the future.
|
167
|
+
|
168
|
+
if (s.ok()) {
|
169
|
+
BlockContents contents;
|
170
|
+
if (block_cache != NULL) {
|
171
|
+
char cache_key_buffer[16];
|
172
|
+
EncodeFixed64(cache_key_buffer, table->rep_->cache_id);
|
173
|
+
EncodeFixed64(cache_key_buffer+8, handle.offset());
|
174
|
+
Slice key(cache_key_buffer, sizeof(cache_key_buffer));
|
175
|
+
cache_handle = block_cache->Lookup(key);
|
176
|
+
if (cache_handle != NULL) {
|
177
|
+
block = reinterpret_cast<Block*>(block_cache->Value(cache_handle));
|
178
|
+
} else {
|
179
|
+
s = ReadBlock(table->rep_->file, options, handle, &contents);
|
180
|
+
if (s.ok()) {
|
181
|
+
block = new Block(contents);
|
182
|
+
if (contents.cachable && options.fill_cache) {
|
183
|
+
cache_handle = block_cache->Insert(
|
184
|
+
key, block, block->size(), &DeleteCachedBlock);
|
185
|
+
}
|
186
|
+
}
|
187
|
+
}
|
188
|
+
} else {
|
189
|
+
s = ReadBlock(table->rep_->file, options, handle, &contents);
|
190
|
+
if (s.ok()) {
|
191
|
+
block = new Block(contents);
|
192
|
+
}
|
193
|
+
}
|
194
|
+
}
|
195
|
+
|
196
|
+
Iterator* iter;
|
197
|
+
if (block != NULL) {
|
198
|
+
iter = block->NewIterator(table->rep_->options.comparator);
|
199
|
+
if (cache_handle == NULL) {
|
200
|
+
iter->RegisterCleanup(&DeleteBlock, block, NULL);
|
201
|
+
} else {
|
202
|
+
iter->RegisterCleanup(&ReleaseBlock, block_cache, cache_handle);
|
203
|
+
}
|
204
|
+
} else {
|
205
|
+
iter = NewErrorIterator(s);
|
206
|
+
}
|
207
|
+
return iter;
|
208
|
+
}
|
209
|
+
|
210
|
+
Iterator* Table::NewIterator(const ReadOptions& options) const {
|
211
|
+
return NewTwoLevelIterator(
|
212
|
+
rep_->index_block->NewIterator(rep_->options.comparator),
|
213
|
+
&Table::BlockReader, const_cast<Table*>(this), options);
|
214
|
+
}
|
215
|
+
|
216
|
+
Status Table::InternalGet(const ReadOptions& options, const Slice& k,
|
217
|
+
void* arg,
|
218
|
+
void (*saver)(void*, const Slice&, const Slice&)) {
|
219
|
+
Status s;
|
220
|
+
Iterator* iiter = rep_->index_block->NewIterator(rep_->options.comparator);
|
221
|
+
iiter->Seek(k);
|
222
|
+
if (iiter->Valid()) {
|
223
|
+
Slice handle_value = iiter->value();
|
224
|
+
FilterBlockReader* filter = rep_->filter;
|
225
|
+
BlockHandle handle;
|
226
|
+
if (filter != NULL &&
|
227
|
+
handle.DecodeFrom(&handle_value).ok() &&
|
228
|
+
!filter->KeyMayMatch(handle.offset(), k)) {
|
229
|
+
// Not found
|
230
|
+
} else {
|
231
|
+
Slice handle = iiter->value();
|
232
|
+
Iterator* block_iter = BlockReader(this, options, iiter->value());
|
233
|
+
block_iter->Seek(k);
|
234
|
+
if (block_iter->Valid()) {
|
235
|
+
(*saver)(arg, block_iter->key(), block_iter->value());
|
236
|
+
}
|
237
|
+
s = block_iter->status();
|
238
|
+
delete block_iter;
|
239
|
+
}
|
240
|
+
}
|
241
|
+
if (s.ok()) {
|
242
|
+
s = iiter->status();
|
243
|
+
}
|
244
|
+
delete iiter;
|
245
|
+
return s;
|
246
|
+
}
|
247
|
+
|
248
|
+
|
249
|
+
uint64_t Table::ApproximateOffsetOf(const Slice& key) const {
|
250
|
+
Iterator* index_iter =
|
251
|
+
rep_->index_block->NewIterator(rep_->options.comparator);
|
252
|
+
index_iter->Seek(key);
|
253
|
+
uint64_t result;
|
254
|
+
if (index_iter->Valid()) {
|
255
|
+
BlockHandle handle;
|
256
|
+
Slice input = index_iter->value();
|
257
|
+
Status s = handle.DecodeFrom(&input);
|
258
|
+
if (s.ok()) {
|
259
|
+
result = handle.offset();
|
260
|
+
} else {
|
261
|
+
// Strange: we can't decode the block handle in the index block.
|
262
|
+
// We'll just return the offset of the metaindex block, which is
|
263
|
+
// close to the whole file size for this case.
|
264
|
+
result = rep_->metaindex_handle.offset();
|
265
|
+
}
|
266
|
+
} else {
|
267
|
+
// key is past the last key in the file. Approximate the offset
|
268
|
+
// by returning the offset of the metaindex block (which is
|
269
|
+
// right near the end of the file).
|
270
|
+
result = rep_->metaindex_handle.offset();
|
271
|
+
}
|
272
|
+
delete index_iter;
|
273
|
+
return result;
|
274
|
+
}
|
275
|
+
|
276
|
+
} // namespace leveldb
|
@@ -0,0 +1,270 @@
|
|
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
|
+
#include "leveldb/table_builder.h"
|
6
|
+
|
7
|
+
#include <assert.h>
|
8
|
+
#include "leveldb/comparator.h"
|
9
|
+
#include "leveldb/env.h"
|
10
|
+
#include "leveldb/filter_policy.h"
|
11
|
+
#include "leveldb/options.h"
|
12
|
+
#include "table/block_builder.h"
|
13
|
+
#include "table/filter_block.h"
|
14
|
+
#include "table/format.h"
|
15
|
+
#include "util/coding.h"
|
16
|
+
#include "util/crc32c.h"
|
17
|
+
|
18
|
+
namespace leveldb {
|
19
|
+
|
20
|
+
struct TableBuilder::Rep {
|
21
|
+
Options options;
|
22
|
+
Options index_block_options;
|
23
|
+
WritableFile* file;
|
24
|
+
uint64_t offset;
|
25
|
+
Status status;
|
26
|
+
BlockBuilder data_block;
|
27
|
+
BlockBuilder index_block;
|
28
|
+
std::string last_key;
|
29
|
+
int64_t num_entries;
|
30
|
+
bool closed; // Either Finish() or Abandon() has been called.
|
31
|
+
FilterBlockBuilder* filter_block;
|
32
|
+
|
33
|
+
// We do not emit the index entry for a block until we have seen the
|
34
|
+
// first key for the next data block. This allows us to use shorter
|
35
|
+
// keys in the index block. For example, consider a block boundary
|
36
|
+
// between the keys "the quick brown fox" and "the who". We can use
|
37
|
+
// "the r" as the key for the index block entry since it is >= all
|
38
|
+
// entries in the first block and < all entries in subsequent
|
39
|
+
// blocks.
|
40
|
+
//
|
41
|
+
// Invariant: r->pending_index_entry is true only if data_block is empty.
|
42
|
+
bool pending_index_entry;
|
43
|
+
BlockHandle pending_handle; // Handle to add to index block
|
44
|
+
|
45
|
+
std::string compressed_output;
|
46
|
+
|
47
|
+
Rep(const Options& opt, WritableFile* f)
|
48
|
+
: options(opt),
|
49
|
+
index_block_options(opt),
|
50
|
+
file(f),
|
51
|
+
offset(0),
|
52
|
+
data_block(&options),
|
53
|
+
index_block(&index_block_options),
|
54
|
+
num_entries(0),
|
55
|
+
closed(false),
|
56
|
+
filter_block(opt.filter_policy == NULL ? NULL
|
57
|
+
: new FilterBlockBuilder(opt.filter_policy)),
|
58
|
+
pending_index_entry(false) {
|
59
|
+
index_block_options.block_restart_interval = 1;
|
60
|
+
}
|
61
|
+
};
|
62
|
+
|
63
|
+
TableBuilder::TableBuilder(const Options& options, WritableFile* file)
|
64
|
+
: rep_(new Rep(options, file)) {
|
65
|
+
if (rep_->filter_block != NULL) {
|
66
|
+
rep_->filter_block->StartBlock(0);
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
TableBuilder::~TableBuilder() {
|
71
|
+
assert(rep_->closed); // Catch errors where caller forgot to call Finish()
|
72
|
+
delete rep_->filter_block;
|
73
|
+
delete rep_;
|
74
|
+
}
|
75
|
+
|
76
|
+
Status TableBuilder::ChangeOptions(const Options& options) {
|
77
|
+
// Note: if more fields are added to Options, update
|
78
|
+
// this function to catch changes that should not be allowed to
|
79
|
+
// change in the middle of building a Table.
|
80
|
+
if (options.comparator != rep_->options.comparator) {
|
81
|
+
return Status::InvalidArgument("changing comparator while building table");
|
82
|
+
}
|
83
|
+
|
84
|
+
// Note that any live BlockBuilders point to rep_->options and therefore
|
85
|
+
// will automatically pick up the updated options.
|
86
|
+
rep_->options = options;
|
87
|
+
rep_->index_block_options = options;
|
88
|
+
rep_->index_block_options.block_restart_interval = 1;
|
89
|
+
return Status::OK();
|
90
|
+
}
|
91
|
+
|
92
|
+
void TableBuilder::Add(const Slice& key, const Slice& value) {
|
93
|
+
Rep* r = rep_;
|
94
|
+
assert(!r->closed);
|
95
|
+
if (!ok()) return;
|
96
|
+
if (r->num_entries > 0) {
|
97
|
+
assert(r->options.comparator->Compare(key, Slice(r->last_key)) > 0);
|
98
|
+
}
|
99
|
+
|
100
|
+
if (r->pending_index_entry) {
|
101
|
+
assert(r->data_block.empty());
|
102
|
+
r->options.comparator->FindShortestSeparator(&r->last_key, key);
|
103
|
+
std::string handle_encoding;
|
104
|
+
r->pending_handle.EncodeTo(&handle_encoding);
|
105
|
+
r->index_block.Add(r->last_key, Slice(handle_encoding));
|
106
|
+
r->pending_index_entry = false;
|
107
|
+
}
|
108
|
+
|
109
|
+
if (r->filter_block != NULL) {
|
110
|
+
r->filter_block->AddKey(key);
|
111
|
+
}
|
112
|
+
|
113
|
+
r->last_key.assign(key.data(), key.size());
|
114
|
+
r->num_entries++;
|
115
|
+
r->data_block.Add(key, value);
|
116
|
+
|
117
|
+
const size_t estimated_block_size = r->data_block.CurrentSizeEstimate();
|
118
|
+
if (estimated_block_size >= r->options.block_size) {
|
119
|
+
Flush();
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
void TableBuilder::Flush() {
|
124
|
+
Rep* r = rep_;
|
125
|
+
assert(!r->closed);
|
126
|
+
if (!ok()) return;
|
127
|
+
if (r->data_block.empty()) return;
|
128
|
+
assert(!r->pending_index_entry);
|
129
|
+
WriteBlock(&r->data_block, &r->pending_handle);
|
130
|
+
if (ok()) {
|
131
|
+
r->pending_index_entry = true;
|
132
|
+
r->status = r->file->Flush();
|
133
|
+
}
|
134
|
+
if (r->filter_block != NULL) {
|
135
|
+
r->filter_block->StartBlock(r->offset);
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
void TableBuilder::WriteBlock(BlockBuilder* block, BlockHandle* handle) {
|
140
|
+
// File format contains a sequence of blocks where each block has:
|
141
|
+
// block_data: uint8[n]
|
142
|
+
// type: uint8
|
143
|
+
// crc: uint32
|
144
|
+
assert(ok());
|
145
|
+
Rep* r = rep_;
|
146
|
+
Slice raw = block->Finish();
|
147
|
+
|
148
|
+
Slice block_contents;
|
149
|
+
CompressionType type = r->options.compression;
|
150
|
+
// TODO(postrelease): Support more compression options: zlib?
|
151
|
+
switch (type) {
|
152
|
+
case kNoCompression:
|
153
|
+
block_contents = raw;
|
154
|
+
break;
|
155
|
+
|
156
|
+
case kSnappyCompression: {
|
157
|
+
std::string* compressed = &r->compressed_output;
|
158
|
+
if (port::Snappy_Compress(raw.data(), raw.size(), compressed) &&
|
159
|
+
compressed->size() < raw.size() - (raw.size() / 8u)) {
|
160
|
+
block_contents = *compressed;
|
161
|
+
} else {
|
162
|
+
// Snappy not supported, or compressed less than 12.5%, so just
|
163
|
+
// store uncompressed form
|
164
|
+
block_contents = raw;
|
165
|
+
type = kNoCompression;
|
166
|
+
}
|
167
|
+
break;
|
168
|
+
}
|
169
|
+
}
|
170
|
+
WriteRawBlock(block_contents, type, handle);
|
171
|
+
r->compressed_output.clear();
|
172
|
+
block->Reset();
|
173
|
+
}
|
174
|
+
|
175
|
+
void TableBuilder::WriteRawBlock(const Slice& block_contents,
|
176
|
+
CompressionType type,
|
177
|
+
BlockHandle* handle) {
|
178
|
+
Rep* r = rep_;
|
179
|
+
handle->set_offset(r->offset);
|
180
|
+
handle->set_size(block_contents.size());
|
181
|
+
r->status = r->file->Append(block_contents);
|
182
|
+
if (r->status.ok()) {
|
183
|
+
char trailer[kBlockTrailerSize];
|
184
|
+
trailer[0] = type;
|
185
|
+
uint32_t crc = crc32c::Value(block_contents.data(), block_contents.size());
|
186
|
+
crc = crc32c::Extend(crc, trailer, 1); // Extend crc to cover block type
|
187
|
+
EncodeFixed32(trailer+1, crc32c::Mask(crc));
|
188
|
+
r->status = r->file->Append(Slice(trailer, kBlockTrailerSize));
|
189
|
+
if (r->status.ok()) {
|
190
|
+
r->offset += block_contents.size() + kBlockTrailerSize;
|
191
|
+
}
|
192
|
+
}
|
193
|
+
}
|
194
|
+
|
195
|
+
Status TableBuilder::status() const {
|
196
|
+
return rep_->status;
|
197
|
+
}
|
198
|
+
|
199
|
+
Status TableBuilder::Finish() {
|
200
|
+
Rep* r = rep_;
|
201
|
+
Flush();
|
202
|
+
assert(!r->closed);
|
203
|
+
r->closed = true;
|
204
|
+
|
205
|
+
BlockHandle filter_block_handle, metaindex_block_handle, index_block_handle;
|
206
|
+
|
207
|
+
// Write filter block
|
208
|
+
if (ok() && r->filter_block != NULL) {
|
209
|
+
WriteRawBlock(r->filter_block->Finish(), kNoCompression,
|
210
|
+
&filter_block_handle);
|
211
|
+
}
|
212
|
+
|
213
|
+
// Write metaindex block
|
214
|
+
if (ok()) {
|
215
|
+
BlockBuilder meta_index_block(&r->options);
|
216
|
+
if (r->filter_block != NULL) {
|
217
|
+
// Add mapping from "filter.Name" to location of filter data
|
218
|
+
std::string key = "filter.";
|
219
|
+
key.append(r->options.filter_policy->Name());
|
220
|
+
std::string handle_encoding;
|
221
|
+
filter_block_handle.EncodeTo(&handle_encoding);
|
222
|
+
meta_index_block.Add(key, handle_encoding);
|
223
|
+
}
|
224
|
+
|
225
|
+
// TODO(postrelease): Add stats and other meta blocks
|
226
|
+
WriteBlock(&meta_index_block, &metaindex_block_handle);
|
227
|
+
}
|
228
|
+
|
229
|
+
// Write index block
|
230
|
+
if (ok()) {
|
231
|
+
if (r->pending_index_entry) {
|
232
|
+
r->options.comparator->FindShortSuccessor(&r->last_key);
|
233
|
+
std::string handle_encoding;
|
234
|
+
r->pending_handle.EncodeTo(&handle_encoding);
|
235
|
+
r->index_block.Add(r->last_key, Slice(handle_encoding));
|
236
|
+
r->pending_index_entry = false;
|
237
|
+
}
|
238
|
+
WriteBlock(&r->index_block, &index_block_handle);
|
239
|
+
}
|
240
|
+
|
241
|
+
// Write footer
|
242
|
+
if (ok()) {
|
243
|
+
Footer footer;
|
244
|
+
footer.set_metaindex_handle(metaindex_block_handle);
|
245
|
+
footer.set_index_handle(index_block_handle);
|
246
|
+
std::string footer_encoding;
|
247
|
+
footer.EncodeTo(&footer_encoding);
|
248
|
+
r->status = r->file->Append(footer_encoding);
|
249
|
+
if (r->status.ok()) {
|
250
|
+
r->offset += footer_encoding.size();
|
251
|
+
}
|
252
|
+
}
|
253
|
+
return r->status;
|
254
|
+
}
|
255
|
+
|
256
|
+
void TableBuilder::Abandon() {
|
257
|
+
Rep* r = rep_;
|
258
|
+
assert(!r->closed);
|
259
|
+
r->closed = true;
|
260
|
+
}
|
261
|
+
|
262
|
+
uint64_t TableBuilder::NumEntries() const {
|
263
|
+
return rep_->num_entries;
|
264
|
+
}
|
265
|
+
|
266
|
+
uint64_t TableBuilder::FileSize() const {
|
267
|
+
return rep_->offset;
|
268
|
+
}
|
269
|
+
|
270
|
+
} // namespace leveldb
|