leveldb-ruby 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +17 -0
- data/ext/leveldb/extconf.rb +10 -0
- data/ext/leveldb/leveldb.cc +181 -0
- data/leveldb/Makefile +172 -0
- data/leveldb/db/builder.cc +90 -0
- data/leveldb/db/builder.h +36 -0
- data/leveldb/db/corruption_test.cc +354 -0
- data/leveldb/db/db_bench.cc +677 -0
- data/leveldb/db/db_impl.cc +1236 -0
- data/leveldb/db/db_impl.h +180 -0
- data/leveldb/db/db_iter.cc +298 -0
- data/leveldb/db/db_iter.h +26 -0
- data/leveldb/db/db_test.cc +1192 -0
- data/leveldb/db/dbformat.cc +87 -0
- data/leveldb/db/dbformat.h +165 -0
- data/leveldb/db/dbformat_test.cc +112 -0
- data/leveldb/db/filename.cc +135 -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 +254 -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 +108 -0
- data/leveldb/db/memtable.h +85 -0
- data/leveldb/db/repair.cc +384 -0
- data/leveldb/db/skiplist.h +378 -0
- data/leveldb/db/skiplist_test.cc +378 -0
- data/leveldb/db/snapshot.h +66 -0
- data/leveldb/db/table_cache.cc +95 -0
- data/leveldb/db/table_cache.h +50 -0
- data/leveldb/db/version_edit.cc +268 -0
- data/leveldb/db/version_edit.h +106 -0
- data/leveldb/db/version_edit_test.cc +46 -0
- data/leveldb/db/version_set.cc +1060 -0
- data/leveldb/db/version_set.h +306 -0
- data/leveldb/db/write_batch.cc +138 -0
- data/leveldb/db/write_batch_internal.h +45 -0
- data/leveldb/db/write_batch_test.cc +89 -0
- data/leveldb/include/leveldb/cache.h +99 -0
- data/leveldb/include/leveldb/comparator.h +63 -0
- data/leveldb/include/leveldb/db.h +148 -0
- data/leveldb/include/leveldb/env.h +302 -0
- data/leveldb/include/leveldb/iterator.h +100 -0
- data/leveldb/include/leveldb/options.h +198 -0
- data/leveldb/include/leveldb/slice.h +109 -0
- data/leveldb/include/leveldb/status.h +100 -0
- data/leveldb/include/leveldb/table.h +70 -0
- data/leveldb/include/leveldb/table_builder.h +91 -0
- data/leveldb/include/leveldb/write_batch.h +64 -0
- data/leveldb/port/port.h +23 -0
- data/leveldb/port/port_android.cc +64 -0
- data/leveldb/port/port_android.h +150 -0
- data/leveldb/port/port_chromium.cc +80 -0
- data/leveldb/port/port_chromium.h +97 -0
- data/leveldb/port/port_example.h +115 -0
- data/leveldb/port/port_osx.cc +50 -0
- data/leveldb/port/port_osx.h +125 -0
- data/leveldb/port/port_posix.cc +50 -0
- data/leveldb/port/port_posix.h +94 -0
- data/leveldb/port/sha1_portable.cc +298 -0
- data/leveldb/port/sha1_portable.h +25 -0
- data/leveldb/port/sha1_test.cc +39 -0
- data/leveldb/port/win/stdint.h +24 -0
- data/leveldb/table/block.cc +263 -0
- data/leveldb/table/block.h +43 -0
- data/leveldb/table/block_builder.cc +109 -0
- data/leveldb/table/block_builder.h +57 -0
- data/leveldb/table/format.cc +131 -0
- data/leveldb/table/format.h +103 -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 +175 -0
- data/leveldb/table/table_builder.cc +227 -0
- data/leveldb/table/table_test.cc +845 -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/cache.cc +255 -0
- data/leveldb/util/cache_test.cc +169 -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 +72 -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 +77 -0
- data/leveldb/util/env_chromium.cc +612 -0
- data/leveldb/util/env_posix.cc +606 -0
- data/leveldb/util/env_test.cc +102 -0
- data/leveldb/util/hash.cc +45 -0
- data/leveldb/util/hash.h +19 -0
- data/leveldb/util/histogram.cc +128 -0
- data/leveldb/util/histogram.h +41 -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 +28 -0
- data/leveldb/util/random.h +59 -0
- data/leveldb/util/status.cc +75 -0
- data/leveldb/util/testharness.cc +65 -0
- data/leveldb/util/testharness.h +129 -0
- data/leveldb/util/testutil.cc +51 -0
- data/leveldb/util/testutil.h +53 -0
- data/lib/leveldb.rb +36 -0
- metadata +183 -0
@@ -0,0 +1,80 @@
|
|
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
|
+
// File names used by DB code
|
6
|
+
|
7
|
+
#ifndef STORAGE_LEVELDB_DB_FILENAME_H_
|
8
|
+
#define STORAGE_LEVELDB_DB_FILENAME_H_
|
9
|
+
|
10
|
+
#include <stdint.h>
|
11
|
+
#include <string>
|
12
|
+
#include "leveldb/slice.h"
|
13
|
+
#include "leveldb/status.h"
|
14
|
+
#include "port/port.h"
|
15
|
+
|
16
|
+
namespace leveldb {
|
17
|
+
|
18
|
+
class Env;
|
19
|
+
|
20
|
+
enum FileType {
|
21
|
+
kLogFile,
|
22
|
+
kDBLockFile,
|
23
|
+
kTableFile,
|
24
|
+
kDescriptorFile,
|
25
|
+
kCurrentFile,
|
26
|
+
kTempFile,
|
27
|
+
kInfoLogFile, // Either the current one, or an old one
|
28
|
+
};
|
29
|
+
|
30
|
+
// Return the name of the log file with the specified number
|
31
|
+
// in the db named by "dbname". The result will be prefixed with
|
32
|
+
// "dbname".
|
33
|
+
extern std::string LogFileName(const std::string& dbname, uint64_t number);
|
34
|
+
|
35
|
+
// Return the name of the sstable with the specified number
|
36
|
+
// in the db named by "dbname". The result will be prefixed with
|
37
|
+
// "dbname".
|
38
|
+
extern std::string TableFileName(const std::string& dbname, uint64_t number);
|
39
|
+
|
40
|
+
// Return the name of the descriptor file for the db named by
|
41
|
+
// "dbname" and the specified incarnation number. The result will be
|
42
|
+
// prefixed with "dbname".
|
43
|
+
extern std::string DescriptorFileName(const std::string& dbname,
|
44
|
+
uint64_t number);
|
45
|
+
|
46
|
+
// Return the name of the current file. This file contains the name
|
47
|
+
// of the current manifest file. The result will be prefixed with
|
48
|
+
// "dbname".
|
49
|
+
extern std::string CurrentFileName(const std::string& dbname);
|
50
|
+
|
51
|
+
// Return the name of the lock file for the db named by
|
52
|
+
// "dbname". The result will be prefixed with "dbname".
|
53
|
+
extern std::string LockFileName(const std::string& dbname);
|
54
|
+
|
55
|
+
// Return the name of a temporary file owned by the db named "dbname".
|
56
|
+
// The result will be prefixed with "dbname".
|
57
|
+
extern std::string TempFileName(const std::string& dbname, uint64_t number);
|
58
|
+
|
59
|
+
// Return the name of the info log file for "dbname".
|
60
|
+
extern std::string InfoLogFileName(const std::string& dbname);
|
61
|
+
|
62
|
+
// Return the name of the old info log file for "dbname".
|
63
|
+
extern std::string OldInfoLogFileName(const std::string& dbname);
|
64
|
+
|
65
|
+
// If filename is a leveldb file, store the type of the file in *type.
|
66
|
+
// The number encoded in the filename is stored in *number. If the
|
67
|
+
// filename was successfully parsed, returns true. Else return false.
|
68
|
+
extern bool ParseFileName(const std::string& filename,
|
69
|
+
uint64_t* number,
|
70
|
+
FileType* type);
|
71
|
+
|
72
|
+
// Make the CURRENT file point to the descriptor file with the
|
73
|
+
// specified number.
|
74
|
+
extern Status SetCurrentFile(Env* env, const std::string& dbname,
|
75
|
+
uint64_t descriptor_number);
|
76
|
+
|
77
|
+
|
78
|
+
}
|
79
|
+
|
80
|
+
#endif // STORAGE_LEVELDB_DB_FILENAME_H_
|
@@ -0,0 +1,122 @@
|
|
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 "db/filename.h"
|
6
|
+
|
7
|
+
#include "db/dbformat.h"
|
8
|
+
#include "port/port.h"
|
9
|
+
#include "util/logging.h"
|
10
|
+
#include "util/testharness.h"
|
11
|
+
|
12
|
+
namespace leveldb {
|
13
|
+
|
14
|
+
class FileNameTest { };
|
15
|
+
|
16
|
+
TEST(FileNameTest, Parse) {
|
17
|
+
Slice db;
|
18
|
+
FileType type;
|
19
|
+
uint64_t number;
|
20
|
+
|
21
|
+
// Successful parses
|
22
|
+
static struct {
|
23
|
+
const char* fname;
|
24
|
+
uint64_t number;
|
25
|
+
FileType type;
|
26
|
+
} cases[] = {
|
27
|
+
{ "100.log", 100, kLogFile },
|
28
|
+
{ "0.log", 0, kLogFile },
|
29
|
+
{ "0.sst", 0, kTableFile },
|
30
|
+
{ "CURRENT", 0, kCurrentFile },
|
31
|
+
{ "LOCK", 0, kDBLockFile },
|
32
|
+
{ "MANIFEST-2", 2, kDescriptorFile },
|
33
|
+
{ "MANIFEST-7", 7, kDescriptorFile },
|
34
|
+
{ "LOG", 0, kInfoLogFile },
|
35
|
+
{ "LOG.old", 0, kInfoLogFile },
|
36
|
+
{ "18446744073709551615.log", 18446744073709551615ull, kLogFile },
|
37
|
+
};
|
38
|
+
for (int i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
|
39
|
+
std::string f = cases[i].fname;
|
40
|
+
ASSERT_TRUE(ParseFileName(f, &number, &type)) << f;
|
41
|
+
ASSERT_EQ(cases[i].type, type) << f;
|
42
|
+
ASSERT_EQ(cases[i].number, number) << f;
|
43
|
+
}
|
44
|
+
|
45
|
+
// Errors
|
46
|
+
static const char* errors[] = {
|
47
|
+
"",
|
48
|
+
"foo",
|
49
|
+
"foo-dx-100.log",
|
50
|
+
".log",
|
51
|
+
"",
|
52
|
+
"manifest",
|
53
|
+
"CURREN",
|
54
|
+
"CURRENTX",
|
55
|
+
"MANIFES",
|
56
|
+
"MANIFEST",
|
57
|
+
"MANIFEST-",
|
58
|
+
"XMANIFEST-3",
|
59
|
+
"MANIFEST-3x",
|
60
|
+
"LOC",
|
61
|
+
"LOCKx",
|
62
|
+
"LO",
|
63
|
+
"LOGx",
|
64
|
+
"18446744073709551616.log",
|
65
|
+
"184467440737095516150.log",
|
66
|
+
"100",
|
67
|
+
"100.",
|
68
|
+
"100.lop"
|
69
|
+
};
|
70
|
+
for (int i = 0; i < sizeof(errors) / sizeof(errors[0]); i++) {
|
71
|
+
std::string f = errors[i];
|
72
|
+
ASSERT_TRUE(!ParseFileName(f, &number, &type)) << f;
|
73
|
+
};
|
74
|
+
}
|
75
|
+
|
76
|
+
TEST(FileNameTest, Construction) {
|
77
|
+
uint64_t number;
|
78
|
+
FileType type;
|
79
|
+
std::string fname;
|
80
|
+
|
81
|
+
fname = CurrentFileName("foo");
|
82
|
+
ASSERT_EQ("foo/", std::string(fname.data(), 4));
|
83
|
+
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
84
|
+
ASSERT_EQ(0, number);
|
85
|
+
ASSERT_EQ(kCurrentFile, type);
|
86
|
+
|
87
|
+
fname = LockFileName("foo");
|
88
|
+
ASSERT_EQ("foo/", std::string(fname.data(), 4));
|
89
|
+
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
90
|
+
ASSERT_EQ(0, number);
|
91
|
+
ASSERT_EQ(kDBLockFile, type);
|
92
|
+
|
93
|
+
fname = LogFileName("foo", 192);
|
94
|
+
ASSERT_EQ("foo/", std::string(fname.data(), 4));
|
95
|
+
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
96
|
+
ASSERT_EQ(192, number);
|
97
|
+
ASSERT_EQ(kLogFile, type);
|
98
|
+
|
99
|
+
fname = TableFileName("bar", 200);
|
100
|
+
ASSERT_EQ("bar/", std::string(fname.data(), 4));
|
101
|
+
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
102
|
+
ASSERT_EQ(200, number);
|
103
|
+
ASSERT_EQ(kTableFile, type);
|
104
|
+
|
105
|
+
fname = DescriptorFileName("bar", 100);
|
106
|
+
ASSERT_EQ("bar/", std::string(fname.data(), 4));
|
107
|
+
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
108
|
+
ASSERT_EQ(100, number);
|
109
|
+
ASSERT_EQ(kDescriptorFile, type);
|
110
|
+
|
111
|
+
fname = TempFileName("tmp", 999);
|
112
|
+
ASSERT_EQ("tmp/", std::string(fname.data(), 4));
|
113
|
+
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
114
|
+
ASSERT_EQ(999, number);
|
115
|
+
ASSERT_EQ(kTempFile, type);
|
116
|
+
}
|
117
|
+
|
118
|
+
}
|
119
|
+
|
120
|
+
int main(int argc, char** argv) {
|
121
|
+
return leveldb::test::RunAllTests();
|
122
|
+
}
|
@@ -0,0 +1,35 @@
|
|
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
|
+
// Log format information shared by reader and writer.
|
6
|
+
// See ../doc/log_format.txt for more detail.
|
7
|
+
|
8
|
+
#ifndef STORAGE_LEVELDB_DB_LOG_FORMAT_H_
|
9
|
+
#define STORAGE_LEVELDB_DB_LOG_FORMAT_H_
|
10
|
+
|
11
|
+
namespace leveldb {
|
12
|
+
namespace log {
|
13
|
+
|
14
|
+
enum RecordType {
|
15
|
+
// Zero is reserved for preallocated files
|
16
|
+
kZeroType = 0,
|
17
|
+
|
18
|
+
kFullType = 1,
|
19
|
+
|
20
|
+
// For fragments
|
21
|
+
kFirstType = 2,
|
22
|
+
kMiddleType = 3,
|
23
|
+
kLastType = 4,
|
24
|
+
};
|
25
|
+
static const int kMaxRecordType = kLastType;
|
26
|
+
|
27
|
+
static const int kBlockSize = 32768;
|
28
|
+
|
29
|
+
// Header is checksum (4 bytes), type (1 byte), length (2 bytes).
|
30
|
+
static const int kHeaderSize = 4 + 1 + 2;
|
31
|
+
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
#endif // STORAGE_LEVELDB_DB_LOG_FORMAT_H_
|
@@ -0,0 +1,254 @@
|
|
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 "db/log_reader.h"
|
6
|
+
|
7
|
+
#include "leveldb/env.h"
|
8
|
+
#include "util/coding.h"
|
9
|
+
#include "util/crc32c.h"
|
10
|
+
|
11
|
+
namespace leveldb {
|
12
|
+
namespace log {
|
13
|
+
|
14
|
+
Reader::Reporter::~Reporter() {
|
15
|
+
}
|
16
|
+
|
17
|
+
Reader::Reader(SequentialFile* file, Reporter* reporter, bool checksum,
|
18
|
+
uint64_t initial_offset)
|
19
|
+
: file_(file),
|
20
|
+
reporter_(reporter),
|
21
|
+
checksum_(checksum),
|
22
|
+
backing_store_(new char[kBlockSize]),
|
23
|
+
buffer_(),
|
24
|
+
eof_(false),
|
25
|
+
last_record_offset_(0),
|
26
|
+
end_of_buffer_offset_(0),
|
27
|
+
initial_offset_(initial_offset) {
|
28
|
+
}
|
29
|
+
|
30
|
+
Reader::~Reader() {
|
31
|
+
delete[] backing_store_;
|
32
|
+
}
|
33
|
+
|
34
|
+
bool Reader::SkipToInitialBlock() {
|
35
|
+
size_t offset_in_block = initial_offset_ % kBlockSize;
|
36
|
+
uint64_t block_start_location = initial_offset_ - offset_in_block;
|
37
|
+
|
38
|
+
// Don't search a block if we'd be in the trailer
|
39
|
+
if (offset_in_block > kBlockSize - 6) {
|
40
|
+
offset_in_block = 0;
|
41
|
+
block_start_location += kBlockSize;
|
42
|
+
}
|
43
|
+
|
44
|
+
end_of_buffer_offset_ = block_start_location;
|
45
|
+
|
46
|
+
// Skip to start of first block that can contain the initial record
|
47
|
+
if (block_start_location > 0) {
|
48
|
+
Status skip_status = file_->Skip(block_start_location);
|
49
|
+
if (!skip_status.ok()) {
|
50
|
+
ReportDrop(block_start_location, skip_status);
|
51
|
+
return false;
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
return true;
|
56
|
+
}
|
57
|
+
|
58
|
+
bool Reader::ReadRecord(Slice* record, std::string* scratch) {
|
59
|
+
if (last_record_offset_ < initial_offset_) {
|
60
|
+
if (!SkipToInitialBlock()) {
|
61
|
+
return false;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
scratch->clear();
|
66
|
+
record->clear();
|
67
|
+
bool in_fragmented_record = false;
|
68
|
+
// Record offset of the logical record that we're reading
|
69
|
+
// 0 is a dummy value to make compilers happy
|
70
|
+
uint64_t prospective_record_offset = 0;
|
71
|
+
|
72
|
+
Slice fragment;
|
73
|
+
while (true) {
|
74
|
+
uint64_t physical_record_offset = end_of_buffer_offset_ - buffer_.size();
|
75
|
+
switch (ReadPhysicalRecord(&fragment)) {
|
76
|
+
case kFullType:
|
77
|
+
if (in_fragmented_record) {
|
78
|
+
// Handle bug in earlier versions of log::Writer where
|
79
|
+
// it could emit an empty kFirstType record at the tail end
|
80
|
+
// of a block followed by a kFullType or kFirstType record
|
81
|
+
// at the beginning of the next block.
|
82
|
+
if (scratch->empty()) {
|
83
|
+
in_fragmented_record = false;
|
84
|
+
} else {
|
85
|
+
ReportCorruption(scratch->size(), "partial record without end(1)");
|
86
|
+
}
|
87
|
+
}
|
88
|
+
prospective_record_offset = physical_record_offset;
|
89
|
+
scratch->clear();
|
90
|
+
*record = fragment;
|
91
|
+
last_record_offset_ = prospective_record_offset;
|
92
|
+
return true;
|
93
|
+
|
94
|
+
case kFirstType:
|
95
|
+
if (in_fragmented_record) {
|
96
|
+
// Handle bug in earlier versions of log::Writer where
|
97
|
+
// it could emit an empty kFirstType record at the tail end
|
98
|
+
// of a block followed by a kFullType or kFirstType record
|
99
|
+
// at the beginning of the next block.
|
100
|
+
if (scratch->empty()) {
|
101
|
+
in_fragmented_record = false;
|
102
|
+
} else {
|
103
|
+
ReportCorruption(scratch->size(), "partial record without end(2)");
|
104
|
+
}
|
105
|
+
}
|
106
|
+
prospective_record_offset = physical_record_offset;
|
107
|
+
scratch->assign(fragment.data(), fragment.size());
|
108
|
+
in_fragmented_record = true;
|
109
|
+
break;
|
110
|
+
|
111
|
+
case kMiddleType:
|
112
|
+
if (!in_fragmented_record) {
|
113
|
+
ReportCorruption(fragment.size(),
|
114
|
+
"missing start of fragmented record(1)");
|
115
|
+
} else {
|
116
|
+
scratch->append(fragment.data(), fragment.size());
|
117
|
+
}
|
118
|
+
break;
|
119
|
+
|
120
|
+
case kLastType:
|
121
|
+
if (!in_fragmented_record) {
|
122
|
+
ReportCorruption(fragment.size(),
|
123
|
+
"missing start of fragmented record(2)");
|
124
|
+
} else {
|
125
|
+
scratch->append(fragment.data(), fragment.size());
|
126
|
+
*record = Slice(*scratch);
|
127
|
+
last_record_offset_ = prospective_record_offset;
|
128
|
+
return true;
|
129
|
+
}
|
130
|
+
break;
|
131
|
+
|
132
|
+
case kEof:
|
133
|
+
if (in_fragmented_record) {
|
134
|
+
ReportCorruption(scratch->size(), "partial record without end(3)");
|
135
|
+
scratch->clear();
|
136
|
+
}
|
137
|
+
return false;
|
138
|
+
|
139
|
+
case kBadRecord:
|
140
|
+
if (in_fragmented_record) {
|
141
|
+
ReportCorruption(scratch->size(), "error in middle of record");
|
142
|
+
in_fragmented_record = false;
|
143
|
+
scratch->clear();
|
144
|
+
}
|
145
|
+
break;
|
146
|
+
|
147
|
+
default:
|
148
|
+
ReportCorruption(
|
149
|
+
(fragment.size() + (in_fragmented_record ? scratch->size() : 0)),
|
150
|
+
"unknown record type");
|
151
|
+
in_fragmented_record = false;
|
152
|
+
scratch->clear();
|
153
|
+
break;
|
154
|
+
}
|
155
|
+
}
|
156
|
+
return false;
|
157
|
+
}
|
158
|
+
|
159
|
+
uint64_t Reader::LastRecordOffset() {
|
160
|
+
return last_record_offset_;
|
161
|
+
}
|
162
|
+
|
163
|
+
void Reader::ReportCorruption(size_t bytes, const char* reason) {
|
164
|
+
ReportDrop(bytes, Status::Corruption(reason));
|
165
|
+
}
|
166
|
+
|
167
|
+
void Reader::ReportDrop(size_t bytes, const Status& reason) {
|
168
|
+
if (reporter_ != NULL &&
|
169
|
+
end_of_buffer_offset_ - buffer_.size() - bytes >= initial_offset_) {
|
170
|
+
reporter_->Corruption(bytes, reason);
|
171
|
+
}
|
172
|
+
}
|
173
|
+
|
174
|
+
unsigned int Reader::ReadPhysicalRecord(Slice* result) {
|
175
|
+
while (true) {
|
176
|
+
if (buffer_.size() < kHeaderSize) {
|
177
|
+
if (!eof_) {
|
178
|
+
// Last read was a full read, so this is a trailer to skip
|
179
|
+
buffer_.clear();
|
180
|
+
Status status = file_->Read(kBlockSize, &buffer_, backing_store_);
|
181
|
+
end_of_buffer_offset_ += buffer_.size();
|
182
|
+
if (!status.ok()) {
|
183
|
+
buffer_.clear();
|
184
|
+
ReportDrop(kBlockSize, status);
|
185
|
+
eof_ = true;
|
186
|
+
return kEof;
|
187
|
+
} else if (buffer_.size() < kBlockSize) {
|
188
|
+
eof_ = true;
|
189
|
+
}
|
190
|
+
continue;
|
191
|
+
} else if (buffer_.size() == 0) {
|
192
|
+
// End of file
|
193
|
+
return kEof;
|
194
|
+
} else {
|
195
|
+
size_t drop_size = buffer_.size();
|
196
|
+
buffer_.clear();
|
197
|
+
ReportCorruption(drop_size, "truncated record at end of file");
|
198
|
+
return kEof;
|
199
|
+
}
|
200
|
+
}
|
201
|
+
|
202
|
+
// Parse the header
|
203
|
+
const char* header = buffer_.data();
|
204
|
+
const uint32_t a = static_cast<uint32_t>(header[4]) & 0xff;
|
205
|
+
const uint32_t b = static_cast<uint32_t>(header[5]) & 0xff;
|
206
|
+
const unsigned int type = header[6];
|
207
|
+
const uint32_t length = a | (b << 8);
|
208
|
+
if (kHeaderSize + length > buffer_.size()) {
|
209
|
+
size_t drop_size = buffer_.size();
|
210
|
+
buffer_.clear();
|
211
|
+
ReportCorruption(drop_size, "bad record length");
|
212
|
+
return kBadRecord;
|
213
|
+
}
|
214
|
+
|
215
|
+
// Check crc
|
216
|
+
if (checksum_) {
|
217
|
+
if (type == kZeroType && length == 0) {
|
218
|
+
// Skip zero length record without reporting any drops since
|
219
|
+
// such records are produced by the mmap based writing code in
|
220
|
+
// env_posix.cc that preallocates file regions.
|
221
|
+
buffer_.clear();
|
222
|
+
return kBadRecord;
|
223
|
+
}
|
224
|
+
|
225
|
+
uint32_t expected_crc = crc32c::Unmask(DecodeFixed32(header));
|
226
|
+
uint32_t actual_crc = crc32c::Value(header + 6, 1 + length);
|
227
|
+
if (actual_crc != expected_crc) {
|
228
|
+
// Drop the rest of the buffer since "length" itself may have
|
229
|
+
// been corrupted and if we trust it, we could find some
|
230
|
+
// fragment of a real log record that just happens to look
|
231
|
+
// like a valid log record.
|
232
|
+
size_t drop_size = buffer_.size();
|
233
|
+
buffer_.clear();
|
234
|
+
ReportCorruption(drop_size, "checksum mismatch");
|
235
|
+
return kBadRecord;
|
236
|
+
}
|
237
|
+
}
|
238
|
+
|
239
|
+
buffer_.remove_prefix(kHeaderSize + length);
|
240
|
+
|
241
|
+
// Skip physical record that started before initial_offset_
|
242
|
+
if (end_of_buffer_offset_ - buffer_.size() - kHeaderSize - length <
|
243
|
+
initial_offset_) {
|
244
|
+
result->clear();
|
245
|
+
return kBadRecord;
|
246
|
+
}
|
247
|
+
|
248
|
+
*result = Slice(header + kHeaderSize, length);
|
249
|
+
return type;
|
250
|
+
}
|
251
|
+
}
|
252
|
+
|
253
|
+
}
|
254
|
+
}
|