leveldb-ruby 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,102 @@
|
|
|
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/env.h"
|
|
6
|
+
|
|
7
|
+
#include "port/port.h"
|
|
8
|
+
#include "util/testharness.h"
|
|
9
|
+
|
|
10
|
+
namespace leveldb {
|
|
11
|
+
|
|
12
|
+
static const int kDelayMicros = 100000;
|
|
13
|
+
|
|
14
|
+
class EnvPosixTest {
|
|
15
|
+
private:
|
|
16
|
+
port::Mutex mu_;
|
|
17
|
+
std::string events_;
|
|
18
|
+
|
|
19
|
+
public:
|
|
20
|
+
Env* env_;
|
|
21
|
+
EnvPosixTest() : env_(Env::Default()) { }
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
static void SetBool(void* ptr) {
|
|
25
|
+
*(reinterpret_cast<bool*>(ptr)) = true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
TEST(EnvPosixTest, RunImmediately) {
|
|
29
|
+
bool called = false;
|
|
30
|
+
env_->Schedule(&SetBool, &called);
|
|
31
|
+
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
|
32
|
+
ASSERT_TRUE(called);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
TEST(EnvPosixTest, RunMany) {
|
|
36
|
+
int last_id = 0;
|
|
37
|
+
|
|
38
|
+
struct CB {
|
|
39
|
+
int* last_id_ptr; // Pointer to shared slot
|
|
40
|
+
int id; // Order# for the execution of this callback
|
|
41
|
+
|
|
42
|
+
CB(int* p, int i) : last_id_ptr(p), id(i) { }
|
|
43
|
+
|
|
44
|
+
static void Run(void* v) {
|
|
45
|
+
CB* cb = reinterpret_cast<CB*>(v);
|
|
46
|
+
ASSERT_EQ(cb->id-1, *cb->last_id_ptr);
|
|
47
|
+
*cb->last_id_ptr = cb->id;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// Schedule in different order than start time
|
|
52
|
+
CB cb1(&last_id, 1);
|
|
53
|
+
CB cb2(&last_id, 2);
|
|
54
|
+
CB cb3(&last_id, 3);
|
|
55
|
+
CB cb4(&last_id, 4);
|
|
56
|
+
env_->Schedule(&CB::Run, &cb1);
|
|
57
|
+
env_->Schedule(&CB::Run, &cb2);
|
|
58
|
+
env_->Schedule(&CB::Run, &cb3);
|
|
59
|
+
env_->Schedule(&CB::Run, &cb4);
|
|
60
|
+
|
|
61
|
+
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
|
62
|
+
ASSERT_EQ(4, last_id);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
struct State {
|
|
66
|
+
port::Mutex mu;
|
|
67
|
+
int val;
|
|
68
|
+
int num_running;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
static void ThreadBody(void* arg) {
|
|
72
|
+
State* s = reinterpret_cast<State*>(arg);
|
|
73
|
+
s->mu.Lock();
|
|
74
|
+
s->val += 1;
|
|
75
|
+
s->num_running -= 1;
|
|
76
|
+
s->mu.Unlock();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
TEST(EnvPosixTest, StartThread) {
|
|
80
|
+
State state;
|
|
81
|
+
state.val = 0;
|
|
82
|
+
state.num_running = 3;
|
|
83
|
+
for (int i = 0; i < 3; i++) {
|
|
84
|
+
env_->StartThread(&ThreadBody, &state);
|
|
85
|
+
}
|
|
86
|
+
while (true) {
|
|
87
|
+
state.mu.Lock();
|
|
88
|
+
int num = state.num_running;
|
|
89
|
+
state.mu.Unlock();
|
|
90
|
+
if (num == 0) {
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
|
94
|
+
}
|
|
95
|
+
ASSERT_EQ(state.val, 3);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
int main(int argc, char** argv) {
|
|
101
|
+
return leveldb::test::RunAllTests();
|
|
102
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
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 <string.h>
|
|
6
|
+
#include "util/coding.h"
|
|
7
|
+
#include "util/hash.h"
|
|
8
|
+
|
|
9
|
+
namespace leveldb {
|
|
10
|
+
|
|
11
|
+
uint32_t Hash(const char* data, size_t n, uint32_t seed) {
|
|
12
|
+
// Similar to murmur hash
|
|
13
|
+
const uint32_t m = 0xc6a4a793;
|
|
14
|
+
const uint32_t r = 24;
|
|
15
|
+
const char* limit = data + n;
|
|
16
|
+
uint32_t h = seed ^ (n * m);
|
|
17
|
+
|
|
18
|
+
// Pick up four bytes at a time
|
|
19
|
+
while (data + 4 <= limit) {
|
|
20
|
+
uint32_t w = DecodeFixed32(data);
|
|
21
|
+
data += 4;
|
|
22
|
+
h += w;
|
|
23
|
+
h *= m;
|
|
24
|
+
h ^= (h >> 16);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Pick up remaining bytes
|
|
28
|
+
switch (limit - data) {
|
|
29
|
+
case 3:
|
|
30
|
+
h += data[2] << 16;
|
|
31
|
+
// fall through
|
|
32
|
+
case 2:
|
|
33
|
+
h += data[1] << 8;
|
|
34
|
+
// fall through
|
|
35
|
+
case 1:
|
|
36
|
+
h += data[0];
|
|
37
|
+
h *= m;
|
|
38
|
+
h ^= (h >> r);
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
return h;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
}
|
data/leveldb/util/hash.h
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
// Simple hash function used for internal data structures
|
|
6
|
+
|
|
7
|
+
#ifndef STORAGE_LEVELDB_UTIL_HASH_H_
|
|
8
|
+
#define STORAGE_LEVELDB_UTIL_HASH_H_
|
|
9
|
+
|
|
10
|
+
#include <stddef.h>
|
|
11
|
+
#include <stdint.h>
|
|
12
|
+
|
|
13
|
+
namespace leveldb {
|
|
14
|
+
|
|
15
|
+
extern uint32_t Hash(const char* data, size_t n, uint32_t seed);
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
#endif // STORAGE_LEVELDB_UTIL_HASH_H_
|
|
@@ -0,0 +1,128 @@
|
|
|
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 <math.h>
|
|
6
|
+
#include <stdio.h>
|
|
7
|
+
#include "port/port.h"
|
|
8
|
+
#include "util/histogram.h"
|
|
9
|
+
|
|
10
|
+
namespace leveldb {
|
|
11
|
+
|
|
12
|
+
const double Histogram::kBucketLimit[kNumBuckets] = {
|
|
13
|
+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 20, 25, 30, 35, 40, 45,
|
|
14
|
+
50, 60, 70, 80, 90, 100, 120, 140, 160, 180, 200, 250, 300, 350, 400, 450,
|
|
15
|
+
500, 600, 700, 800, 900, 1000, 1200, 1400, 1600, 1800, 2000, 2500, 3000,
|
|
16
|
+
3500, 4000, 4500, 5000, 6000, 7000, 8000, 9000, 10000, 12000, 14000,
|
|
17
|
+
16000, 18000, 20000, 25000, 30000, 35000, 40000, 45000, 50000, 60000,
|
|
18
|
+
70000, 80000, 90000, 100000, 120000, 140000, 160000, 180000, 200000,
|
|
19
|
+
250000, 300000, 350000, 400000, 450000, 500000, 600000, 700000, 800000,
|
|
20
|
+
900000, 1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2500000,
|
|
21
|
+
3000000, 3500000, 4000000, 4500000, 5000000, 6000000, 7000000, 8000000,
|
|
22
|
+
9000000, 10000000, 12000000, 14000000, 16000000, 18000000, 20000000,
|
|
23
|
+
25000000, 30000000, 35000000, 40000000, 45000000, 50000000, 60000000,
|
|
24
|
+
70000000, 80000000, 90000000, 100000000, 120000000, 140000000, 160000000,
|
|
25
|
+
180000000, 200000000, 250000000, 300000000, 350000000, 400000000,
|
|
26
|
+
450000000, 500000000, 600000000, 700000000, 800000000, 900000000,
|
|
27
|
+
1000000000, 1200000000, 1400000000, 1600000000, 1800000000, 2000000000,
|
|
28
|
+
2500000000.0, 3000000000.0, 3500000000.0, 4000000000.0, 4500000000.0,
|
|
29
|
+
5000000000.0, 6000000000.0, 7000000000.0, 8000000000.0, 9000000000.0,
|
|
30
|
+
1e200,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
void Histogram::Clear() {
|
|
34
|
+
min_ = kBucketLimit[kNumBuckets-1];
|
|
35
|
+
max_ = 0;
|
|
36
|
+
num_ = 0;
|
|
37
|
+
sum_ = 0;
|
|
38
|
+
sum_squares_ = 0;
|
|
39
|
+
for (int i = 0; i < kNumBuckets; i++) {
|
|
40
|
+
buckets_[i] = 0;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
void Histogram::Add(double value) {
|
|
45
|
+
// Linear search is fast enough for our usage in db_bench
|
|
46
|
+
int b = 0;
|
|
47
|
+
while (b < kNumBuckets - 1 && kBucketLimit[b] <= value) {
|
|
48
|
+
b++;
|
|
49
|
+
}
|
|
50
|
+
buckets_[b] += 1.0;
|
|
51
|
+
if (min_ > value) min_ = value;
|
|
52
|
+
if (max_ < value) max_ = value;
|
|
53
|
+
num_++;
|
|
54
|
+
sum_ += value;
|
|
55
|
+
sum_squares_ += (value * value);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
double Histogram::Median() const {
|
|
59
|
+
return Percentile(50.0);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
double Histogram::Percentile(double p) const {
|
|
63
|
+
double threshold = num_ * (p / 100.0);
|
|
64
|
+
double sum = 0;
|
|
65
|
+
for (int b = 0; b < kNumBuckets; b++) {
|
|
66
|
+
sum += buckets_[b];
|
|
67
|
+
if (sum >= threshold) {
|
|
68
|
+
// Scale linearly within this bucket
|
|
69
|
+
double left_point = (b == 0) ? 0 : kBucketLimit[b-1];
|
|
70
|
+
double right_point = kBucketLimit[b];
|
|
71
|
+
double left_sum = sum - buckets_[b];
|
|
72
|
+
double right_sum = sum;
|
|
73
|
+
double pos = (threshold - left_sum) / (right_sum - left_sum);
|
|
74
|
+
double r = left_point + (right_point - left_point) * pos;
|
|
75
|
+
if (r < min_) r = min_;
|
|
76
|
+
if (r > max_) r = max_;
|
|
77
|
+
return r;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return max_;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
double Histogram::Average() const {
|
|
84
|
+
if (num_ == 0.0) return 0;
|
|
85
|
+
return sum_ / num_;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
double Histogram::StandardDeviation() const {
|
|
89
|
+
if (num_ == 0.0) return 0;
|
|
90
|
+
double variance = (sum_squares_ * num_ - sum_ * sum_) / (num_ * num_);
|
|
91
|
+
return sqrt(variance);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
std::string Histogram::ToString() const {
|
|
95
|
+
std::string r;
|
|
96
|
+
char buf[200];
|
|
97
|
+
snprintf(buf, sizeof(buf),
|
|
98
|
+
"Count: %.0f Average: %.4f StdDev: %.2f\n",
|
|
99
|
+
num_, Average(), StandardDeviation());
|
|
100
|
+
r.append(buf);
|
|
101
|
+
snprintf(buf, sizeof(buf),
|
|
102
|
+
"Min: %.4f Median: %.4f Max: %.4f\n",
|
|
103
|
+
(num_ == 0.0 ? 0.0 : min_), Median(), max_);
|
|
104
|
+
r.append(buf);
|
|
105
|
+
r.append("------------------------------------------------------\n");
|
|
106
|
+
const double mult = 100.0 / num_;
|
|
107
|
+
double sum = 0;
|
|
108
|
+
for (int b = 0; b < kNumBuckets; b++) {
|
|
109
|
+
if (buckets_[b] <= 0.0) continue;
|
|
110
|
+
sum += buckets_[b];
|
|
111
|
+
snprintf(buf, sizeof(buf),
|
|
112
|
+
"[ %7.0f, %7.0f ) %7.0f %7.3f%% %7.3f%% ",
|
|
113
|
+
((b == 0) ? 0.0 : kBucketLimit[b-1]), // left
|
|
114
|
+
kBucketLimit[b], // right
|
|
115
|
+
buckets_[b], // count
|
|
116
|
+
mult * buckets_[b], // percentage
|
|
117
|
+
mult * sum); // cumulative percentage
|
|
118
|
+
r.append(buf);
|
|
119
|
+
|
|
120
|
+
// Add hash marks based on percentage; 20 marks for 100%.
|
|
121
|
+
int marks = static_cast<int>(20*(buckets_[b] / num_) + 0.5);
|
|
122
|
+
r.append(marks, '#');
|
|
123
|
+
r.push_back('\n');
|
|
124
|
+
}
|
|
125
|
+
return r;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
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_UTIL_HISTOGRAM_H_
|
|
6
|
+
#define STORAGE_LEVELDB_UTIL_HISTOGRAM_H_
|
|
7
|
+
|
|
8
|
+
#include <string>
|
|
9
|
+
|
|
10
|
+
namespace leveldb {
|
|
11
|
+
|
|
12
|
+
class Histogram {
|
|
13
|
+
public:
|
|
14
|
+
Histogram() { }
|
|
15
|
+
~Histogram() { }
|
|
16
|
+
|
|
17
|
+
void Clear();
|
|
18
|
+
void Add(double value);
|
|
19
|
+
|
|
20
|
+
std::string ToString() const;
|
|
21
|
+
|
|
22
|
+
private:
|
|
23
|
+
double min_;
|
|
24
|
+
double max_;
|
|
25
|
+
double num_;
|
|
26
|
+
double sum_;
|
|
27
|
+
double sum_squares_;
|
|
28
|
+
|
|
29
|
+
enum { kNumBuckets = 154 };
|
|
30
|
+
static const double kBucketLimit[kNumBuckets];
|
|
31
|
+
double buckets_[kNumBuckets];
|
|
32
|
+
|
|
33
|
+
double Median() const;
|
|
34
|
+
double Percentile(double p) const;
|
|
35
|
+
double Average() const;
|
|
36
|
+
double StandardDeviation() const;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
#endif // STORAGE_LEVELDB_UTIL_HISTOGRAM_H_
|
|
@@ -0,0 +1,81 @@
|
|
|
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 "util/logging.h"
|
|
6
|
+
|
|
7
|
+
#include <errno.h>
|
|
8
|
+
#include <stdarg.h>
|
|
9
|
+
#include <stdio.h>
|
|
10
|
+
#include <stdlib.h>
|
|
11
|
+
#include "leveldb/env.h"
|
|
12
|
+
#include "leveldb/slice.h"
|
|
13
|
+
|
|
14
|
+
namespace leveldb {
|
|
15
|
+
|
|
16
|
+
void AppendNumberTo(std::string* str, uint64_t num) {
|
|
17
|
+
char buf[30];
|
|
18
|
+
snprintf(buf, sizeof(buf), "%llu", (unsigned long long) num);
|
|
19
|
+
str->append(buf);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
void AppendEscapedStringTo(std::string* str, const Slice& value) {
|
|
23
|
+
for (size_t i = 0; i < value.size(); i++) {
|
|
24
|
+
char c = value[i];
|
|
25
|
+
if (c >= ' ' && c <= '~') {
|
|
26
|
+
str->push_back(c);
|
|
27
|
+
} else {
|
|
28
|
+
char buf[10];
|
|
29
|
+
snprintf(buf, sizeof(buf), "\\x%02x",
|
|
30
|
+
static_cast<unsigned int>(c) & 0xff);
|
|
31
|
+
str->append(buf);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
std::string NumberToString(uint64_t num) {
|
|
37
|
+
std::string r;
|
|
38
|
+
AppendNumberTo(&r, num);
|
|
39
|
+
return r;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
std::string EscapeString(const Slice& value) {
|
|
43
|
+
std::string r;
|
|
44
|
+
AppendEscapedStringTo(&r, value);
|
|
45
|
+
return r;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
bool ConsumeChar(Slice* in, char c) {
|
|
49
|
+
if (!in->empty() && (*in)[0] == c) {
|
|
50
|
+
in->remove_prefix(1);
|
|
51
|
+
return true;
|
|
52
|
+
} else {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
bool ConsumeDecimalNumber(Slice* in, uint64_t* val) {
|
|
58
|
+
uint64_t v = 0;
|
|
59
|
+
int digits = 0;
|
|
60
|
+
while (!in->empty()) {
|
|
61
|
+
char c = (*in)[0];
|
|
62
|
+
if (c >= '0' && c <= '9') {
|
|
63
|
+
++digits;
|
|
64
|
+
const int delta = (c - '0');
|
|
65
|
+
static const uint64_t kMaxUint64 = ~static_cast<uint64_t>(0);
|
|
66
|
+
if (v > kMaxUint64/10 ||
|
|
67
|
+
(v == kMaxUint64/10 && delta > kMaxUint64%10)) {
|
|
68
|
+
// Overflow
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
v = (v * 10) + delta;
|
|
72
|
+
in->remove_prefix(1);
|
|
73
|
+
} else {
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
*val = v;
|
|
78
|
+
return (digits > 0);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
// Must not be included from any .h files to avoid polluting the namespace
|
|
6
|
+
// with macros.
|
|
7
|
+
|
|
8
|
+
#ifndef STORAGE_LEVELDB_UTIL_LOGGING_H_
|
|
9
|
+
#define STORAGE_LEVELDB_UTIL_LOGGING_H_
|
|
10
|
+
|
|
11
|
+
#include <stdio.h>
|
|
12
|
+
#include <stdint.h>
|
|
13
|
+
#include <string>
|
|
14
|
+
#include "port/port.h"
|
|
15
|
+
|
|
16
|
+
namespace leveldb {
|
|
17
|
+
|
|
18
|
+
class Slice;
|
|
19
|
+
class WritableFile;
|
|
20
|
+
|
|
21
|
+
// Append a human-readable printout of "num" to *str
|
|
22
|
+
extern void AppendNumberTo(std::string* str, uint64_t num);
|
|
23
|
+
|
|
24
|
+
// Append a human-readable printout of "value" to *str.
|
|
25
|
+
// Escapes any non-printable characters found in "value".
|
|
26
|
+
extern void AppendEscapedStringTo(std::string* str, const Slice& value);
|
|
27
|
+
|
|
28
|
+
// Return a human-readable printout of "num"
|
|
29
|
+
extern std::string NumberToString(uint64_t num);
|
|
30
|
+
|
|
31
|
+
// Return a human-readable version of "value".
|
|
32
|
+
// Escapes any non-printable characters found in "value".
|
|
33
|
+
extern std::string EscapeString(const Slice& value);
|
|
34
|
+
|
|
35
|
+
// If *in starts with "c", advances *in past the first character and
|
|
36
|
+
// returns true. Otherwise, returns false.
|
|
37
|
+
extern bool ConsumeChar(Slice* in, char c);
|
|
38
|
+
|
|
39
|
+
// Parse a human-readable number from "*in" into *value. On success,
|
|
40
|
+
// advances "*in" past the consumed number and sets "*val" to the
|
|
41
|
+
// numeric value. Otherwise, returns false and leaves *in in an
|
|
42
|
+
// unspecified state.
|
|
43
|
+
extern bool ConsumeDecimalNumber(Slice* in, uint64_t* val);
|
|
44
|
+
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
#endif // STORAGE_LEVELDB_UTIL_LOGGING_H_
|