leveldb-ruby 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. data/README +17 -0
  2. data/ext/leveldb/extconf.rb +10 -0
  3. data/ext/leveldb/leveldb.cc +181 -0
  4. data/leveldb/Makefile +172 -0
  5. data/leveldb/db/builder.cc +90 -0
  6. data/leveldb/db/builder.h +36 -0
  7. data/leveldb/db/corruption_test.cc +354 -0
  8. data/leveldb/db/db_bench.cc +677 -0
  9. data/leveldb/db/db_impl.cc +1236 -0
  10. data/leveldb/db/db_impl.h +180 -0
  11. data/leveldb/db/db_iter.cc +298 -0
  12. data/leveldb/db/db_iter.h +26 -0
  13. data/leveldb/db/db_test.cc +1192 -0
  14. data/leveldb/db/dbformat.cc +87 -0
  15. data/leveldb/db/dbformat.h +165 -0
  16. data/leveldb/db/dbformat_test.cc +112 -0
  17. data/leveldb/db/filename.cc +135 -0
  18. data/leveldb/db/filename.h +80 -0
  19. data/leveldb/db/filename_test.cc +122 -0
  20. data/leveldb/db/log_format.h +35 -0
  21. data/leveldb/db/log_reader.cc +254 -0
  22. data/leveldb/db/log_reader.h +108 -0
  23. data/leveldb/db/log_test.cc +500 -0
  24. data/leveldb/db/log_writer.cc +103 -0
  25. data/leveldb/db/log_writer.h +48 -0
  26. data/leveldb/db/memtable.cc +108 -0
  27. data/leveldb/db/memtable.h +85 -0
  28. data/leveldb/db/repair.cc +384 -0
  29. data/leveldb/db/skiplist.h +378 -0
  30. data/leveldb/db/skiplist_test.cc +378 -0
  31. data/leveldb/db/snapshot.h +66 -0
  32. data/leveldb/db/table_cache.cc +95 -0
  33. data/leveldb/db/table_cache.h +50 -0
  34. data/leveldb/db/version_edit.cc +268 -0
  35. data/leveldb/db/version_edit.h +106 -0
  36. data/leveldb/db/version_edit_test.cc +46 -0
  37. data/leveldb/db/version_set.cc +1060 -0
  38. data/leveldb/db/version_set.h +306 -0
  39. data/leveldb/db/write_batch.cc +138 -0
  40. data/leveldb/db/write_batch_internal.h +45 -0
  41. data/leveldb/db/write_batch_test.cc +89 -0
  42. data/leveldb/include/leveldb/cache.h +99 -0
  43. data/leveldb/include/leveldb/comparator.h +63 -0
  44. data/leveldb/include/leveldb/db.h +148 -0
  45. data/leveldb/include/leveldb/env.h +302 -0
  46. data/leveldb/include/leveldb/iterator.h +100 -0
  47. data/leveldb/include/leveldb/options.h +198 -0
  48. data/leveldb/include/leveldb/slice.h +109 -0
  49. data/leveldb/include/leveldb/status.h +100 -0
  50. data/leveldb/include/leveldb/table.h +70 -0
  51. data/leveldb/include/leveldb/table_builder.h +91 -0
  52. data/leveldb/include/leveldb/write_batch.h +64 -0
  53. data/leveldb/port/port.h +23 -0
  54. data/leveldb/port/port_android.cc +64 -0
  55. data/leveldb/port/port_android.h +150 -0
  56. data/leveldb/port/port_chromium.cc +80 -0
  57. data/leveldb/port/port_chromium.h +97 -0
  58. data/leveldb/port/port_example.h +115 -0
  59. data/leveldb/port/port_osx.cc +50 -0
  60. data/leveldb/port/port_osx.h +125 -0
  61. data/leveldb/port/port_posix.cc +50 -0
  62. data/leveldb/port/port_posix.h +94 -0
  63. data/leveldb/port/sha1_portable.cc +298 -0
  64. data/leveldb/port/sha1_portable.h +25 -0
  65. data/leveldb/port/sha1_test.cc +39 -0
  66. data/leveldb/port/win/stdint.h +24 -0
  67. data/leveldb/table/block.cc +263 -0
  68. data/leveldb/table/block.h +43 -0
  69. data/leveldb/table/block_builder.cc +109 -0
  70. data/leveldb/table/block_builder.h +57 -0
  71. data/leveldb/table/format.cc +131 -0
  72. data/leveldb/table/format.h +103 -0
  73. data/leveldb/table/iterator.cc +67 -0
  74. data/leveldb/table/iterator_wrapper.h +63 -0
  75. data/leveldb/table/merger.cc +197 -0
  76. data/leveldb/table/merger.h +26 -0
  77. data/leveldb/table/table.cc +175 -0
  78. data/leveldb/table/table_builder.cc +227 -0
  79. data/leveldb/table/table_test.cc +845 -0
  80. data/leveldb/table/two_level_iterator.cc +182 -0
  81. data/leveldb/table/two_level_iterator.h +34 -0
  82. data/leveldb/util/arena.cc +68 -0
  83. data/leveldb/util/arena.h +68 -0
  84. data/leveldb/util/arena_test.cc +68 -0
  85. data/leveldb/util/cache.cc +255 -0
  86. data/leveldb/util/cache_test.cc +169 -0
  87. data/leveldb/util/coding.cc +194 -0
  88. data/leveldb/util/coding.h +104 -0
  89. data/leveldb/util/coding_test.cc +173 -0
  90. data/leveldb/util/comparator.cc +72 -0
  91. data/leveldb/util/crc32c.cc +332 -0
  92. data/leveldb/util/crc32c.h +45 -0
  93. data/leveldb/util/crc32c_test.cc +72 -0
  94. data/leveldb/util/env.cc +77 -0
  95. data/leveldb/util/env_chromium.cc +612 -0
  96. data/leveldb/util/env_posix.cc +606 -0
  97. data/leveldb/util/env_test.cc +102 -0
  98. data/leveldb/util/hash.cc +45 -0
  99. data/leveldb/util/hash.h +19 -0
  100. data/leveldb/util/histogram.cc +128 -0
  101. data/leveldb/util/histogram.h +41 -0
  102. data/leveldb/util/logging.cc +81 -0
  103. data/leveldb/util/logging.h +47 -0
  104. data/leveldb/util/mutexlock.h +39 -0
  105. data/leveldb/util/options.cc +28 -0
  106. data/leveldb/util/random.h +59 -0
  107. data/leveldb/util/status.cc +75 -0
  108. data/leveldb/util/testharness.cc +65 -0
  109. data/leveldb/util/testharness.h +129 -0
  110. data/leveldb/util/testutil.cc +51 -0
  111. data/leveldb/util/testutil.h +53 -0
  112. data/lib/leveldb.rb +36 -0
  113. 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
+ #include "port/port_chromium.h"
6
+
7
+ #include "util/logging.h"
8
+
9
+ #if defined(USE_SNAPPY)
10
+ # include "third_party/snappy/src/snappy.h"
11
+ #endif
12
+
13
+ namespace leveldb {
14
+ namespace port {
15
+
16
+ Mutex::Mutex() {
17
+ }
18
+
19
+ Mutex::~Mutex() {
20
+ }
21
+
22
+ void Mutex::Lock() {
23
+ mu_.Acquire();
24
+ }
25
+
26
+ void Mutex::Unlock() {
27
+ mu_.Release();
28
+ }
29
+
30
+ void Mutex::AssertHeld() {
31
+ mu_.AssertAcquired();
32
+ }
33
+
34
+ CondVar::CondVar(Mutex* mu)
35
+ : cv_(&mu->mu_) {
36
+ }
37
+
38
+ CondVar::~CondVar() { }
39
+
40
+ void CondVar::Wait() {
41
+ cv_.Wait();
42
+ }
43
+
44
+ void CondVar::Signal(){
45
+ cv_.Signal();
46
+ }
47
+
48
+ void CondVar::SignalAll() {
49
+ cv_.Broadcast();
50
+ }
51
+
52
+ bool Snappy_Compress(const char* input, size_t input_length,
53
+ std::string* output) {
54
+ #if defined(USE_SNAPPY)
55
+ output->resize(snappy::MaxCompressedLength(input_length));
56
+ size_t outlen;
57
+ snappy::RawCompress(input, input_length, &(*output)[0], &outlen);
58
+ output->resize(outlen);
59
+ return true;
60
+ #else
61
+ return false;
62
+ #endif
63
+ }
64
+
65
+ bool Snappy_Uncompress(const char* input_data, size_t input_length,
66
+ std::string* output) {
67
+ #if defined(USE_SNAPPY)
68
+ size_t ulength;
69
+ if (!snappy::GetUncompressedLength(input_data, input_length, &ulength)) {
70
+ return false;
71
+ }
72
+ output->resize(ulength);
73
+ return snappy::RawUncompress(input_data, input_length, &(*output)[0]);
74
+ #else
75
+ return false;
76
+ #endif
77
+ }
78
+
79
+ }
80
+ }
@@ -0,0 +1,97 @@
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
+ // See port_example.h for documentation for the following types/functions.
6
+
7
+ #ifndef STORAGE_LEVELDB_PORT_PORT_CHROMIUM_H_
8
+ #define STORAGE_LEVELDB_PORT_PORT_CHROMIUM_H_
9
+
10
+ #include <stdint.h>
11
+ #include <string>
12
+ #include <cstring>
13
+ #include "base/atomicops.h"
14
+ #include "base/basictypes.h"
15
+ #include "base/logging.h"
16
+ #include "base/synchronization/condition_variable.h"
17
+ #include "base/synchronization/lock.h"
18
+
19
+ // Linux's ThreadIdentifier() needs this.
20
+ #if defined(OS_LINUX)
21
+ # include <linux/unistd.h>
22
+ #endif
23
+
24
+ #if defined(OS_WIN)
25
+ #define snprintf _snprintf
26
+ #define va_copy(a, b) do { (a) = (b); } while (0)
27
+ #endif
28
+
29
+ namespace leveldb {
30
+ namespace port {
31
+
32
+ // Chromium only supports little endian.
33
+ static const bool kLittleEndian = true;
34
+
35
+ class Mutex {
36
+ public:
37
+ Mutex();
38
+ ~Mutex();
39
+ void Lock();
40
+ void Unlock();
41
+ void AssertHeld();
42
+
43
+ private:
44
+ base::Lock mu_;
45
+
46
+ friend class CondVar;
47
+ DISALLOW_COPY_AND_ASSIGN(Mutex);
48
+ };
49
+
50
+ class CondVar {
51
+ public:
52
+ explicit CondVar(Mutex* mu);
53
+ ~CondVar();
54
+ void Wait();
55
+ void Signal();
56
+ void SignalAll();
57
+
58
+ private:
59
+ base::ConditionVariable cv_;
60
+
61
+ DISALLOW_COPY_AND_ASSIGN(CondVar);
62
+ };
63
+
64
+ class AtomicPointer {
65
+ private:
66
+ typedef base::subtle::AtomicWord Rep;
67
+ Rep rep_;
68
+ public:
69
+ AtomicPointer() { }
70
+ explicit AtomicPointer(void* p) : rep_(reinterpret_cast<Rep>(p)) {}
71
+ inline void* Acquire_Load() const {
72
+ return reinterpret_cast<void*>(::base::subtle::Acquire_Load(&rep_));
73
+ }
74
+ inline void Release_Store(void* v) {
75
+ ::base::subtle::Release_Store(&rep_, reinterpret_cast<Rep>(v));
76
+ }
77
+ inline void* NoBarrier_Load() const {
78
+ return reinterpret_cast<void*>(::base::subtle::NoBarrier_Load(&rep_));
79
+ }
80
+ inline void NoBarrier_Store(void* v) {
81
+ ::base::subtle::NoBarrier_Store(&rep_, reinterpret_cast<Rep>(v));
82
+ }
83
+ };
84
+
85
+ bool Snappy_Compress(const char* input, size_t input_length,
86
+ std::string* output);
87
+ bool Snappy_Uncompress(const char* input_data, size_t input_length,
88
+ std::string* output);
89
+
90
+ inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
91
+ return false;
92
+ }
93
+
94
+ }
95
+ }
96
+
97
+ #endif // STORAGE_LEVELDB_PORT_PORT_CHROMIUM_H_
@@ -0,0 +1,115 @@
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
+ // This file contains the specification, but not the implementations,
6
+ // of the types/operations/etc. that should be defined by a platform
7
+ // specific port_<platform>.h file. Use this file as a reference for
8
+ // how to port this package to a new platform.
9
+
10
+ #ifndef STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_
11
+ #define STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_
12
+
13
+ namespace leveldb {
14
+ namespace port {
15
+
16
+ // TODO(jorlow): Many of these belong more in the environment class rather than
17
+ // here. We should try moving them and see if it affects perf.
18
+
19
+ // The following boolean constant must be true on a little-endian machine
20
+ // and false otherwise.
21
+ static const bool kLittleEndian = true /* or some other expression */;
22
+
23
+ // ------------------ Threading -------------------
24
+
25
+ // A Mutex represents an exclusive lock.
26
+ class Mutex {
27
+ public:
28
+ Mutex();
29
+ ~Mutex();
30
+
31
+ // Lock the mutex. Waits until other lockers have exited.
32
+ // Will deadlock if the mutex is already locked by this thread.
33
+ void Lock();
34
+
35
+ // Unlock the mutex.
36
+ // REQUIRES: This mutex was locked by this thread.
37
+ void Unlock();
38
+
39
+ // Optionally crash if this thread does not hold this mutex.
40
+ // The implementation must be fast, especially if NDEBUG is
41
+ // defined. The implementation is allowed to skip all checks.
42
+ void AssertHeld();
43
+ };
44
+
45
+ class CondVar {
46
+ public:
47
+ explicit CondVar(Mutex* mu);
48
+ ~CondVar();
49
+
50
+ // Atomically release *mu and block on this condition variable until
51
+ // either a call to SignalAll(), or a call to Signal() that picks
52
+ // this thread to wakeup.
53
+ // REQUIRES: this thread holds *mu
54
+ void Wait();
55
+
56
+ // If there are some threads waiting, wake up at least one of them.
57
+ void Signal();
58
+
59
+ // Wake up all waiting threads.
60
+ void SignallAll();
61
+ };
62
+
63
+ // A type that holds a pointer that can be read or written atomically
64
+ // (i.e., without word-tearing.)
65
+ class AtomicPointer {
66
+ private:
67
+ intptr_t rep_;
68
+ public:
69
+ // Initialize to arbitrary value
70
+ AtomicPointer();
71
+
72
+ // Initialize to hold v
73
+ explicit AtomicPointer(void* v) : rep_(v) { }
74
+
75
+ // Read and return the stored pointer with the guarantee that no
76
+ // later memory access (read or write) by this thread can be
77
+ // reordered ahead of this read.
78
+ void* Acquire_Load() const;
79
+
80
+ // Set v as the stored pointer with the guarantee that no earlier
81
+ // memory access (read or write) by this thread can be reordered
82
+ // after this store.
83
+ void Release_Store(void* v);
84
+
85
+ // Read the stored pointer with no ordering guarantees.
86
+ void* NoBarrier_Load() const;
87
+
88
+ // Set va as the stored pointer with no ordering guarantees.
89
+ void NoBarrier_Store(void* v);
90
+ };
91
+
92
+ // ------------------ Compression -------------------
93
+
94
+ // Store the snappy compression of "input[0,input_length-1]" in *output.
95
+ // Returns false if snappy is not supported by this port.
96
+ extern bool Snappy_Compress(const char* input, size_t input_length,
97
+ std::string* output);
98
+
99
+ // Attempt to snappy uncompress input[0,input_length-1] into *output.
100
+ // Returns true if successful, false if the input is invalid lightweight
101
+ // compressed data.
102
+ extern bool Snappy_Uncompress(const char* input_data, size_t input_length,
103
+ std::string* output);
104
+
105
+ // ------------------ Miscellaneous -------------------
106
+
107
+ // If heap profiling is not supported, returns false.
108
+ // Else repeatedly calls (*func)(arg, data, n) and then returns true.
109
+ // The concatenation of all "data[0,n-1]" fragments is the heap profile.
110
+ extern bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg);
111
+
112
+ }
113
+ }
114
+
115
+ #endif // STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_
@@ -0,0 +1,50 @@
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 "port_osx.h"
6
+
7
+ #include <cstdlib>
8
+ #include <stdio.h>
9
+ #include <string.h>
10
+ #include "util/logging.h"
11
+
12
+ namespace leveldb {
13
+ namespace port {
14
+
15
+ static void PthreadCall(const char* label, int result) {
16
+ if (result != 0) {
17
+ fprintf(stderr, "pthread %s: %s\n", label, strerror(result));
18
+ abort();
19
+ }
20
+ }
21
+
22
+ Mutex::Mutex() { PthreadCall("init mutex", pthread_mutex_init(&mu_, NULL)); }
23
+
24
+ Mutex::~Mutex() { PthreadCall("destroy mutex", pthread_mutex_destroy(&mu_)); }
25
+
26
+ void Mutex::Lock() { PthreadCall("lock", pthread_mutex_lock(&mu_)); }
27
+
28
+ void Mutex::Unlock() { PthreadCall("unlock", pthread_mutex_unlock(&mu_)); }
29
+
30
+ CondVar::CondVar(Mutex* mu)
31
+ : mu_(mu) {
32
+ PthreadCall("init cv", pthread_cond_init(&cv_, NULL));
33
+ }
34
+
35
+ CondVar::~CondVar() { PthreadCall("destroy cv", pthread_cond_destroy(&cv_)); }
36
+
37
+ void CondVar::Wait() {
38
+ PthreadCall("wait", pthread_cond_wait(&cv_, &mu_->mu_));
39
+ }
40
+
41
+ void CondVar::Signal() {
42
+ PthreadCall("signal", pthread_cond_signal(&cv_));
43
+ }
44
+
45
+ void CondVar::SignalAll() {
46
+ PthreadCall("broadcast", pthread_cond_broadcast(&cv_));
47
+ }
48
+
49
+ }
50
+ }
@@ -0,0 +1,125 @@
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
+ // See port_example.h for documentation for the following types/functions.
6
+
7
+ #ifndef STORAGE_LEVELDB_PORT_PORT_OSX_H_
8
+ #define STORAGE_LEVELDB_PORT_PORT_OSX_H_
9
+
10
+ #include <libkern/OSAtomic.h>
11
+ #include <machine/endian.h>
12
+ #include <pthread.h>
13
+ #include <stdint.h>
14
+
15
+ #include <string>
16
+
17
+ namespace leveldb {
18
+
19
+ // The following 4 methods implemented here for the benefit of env_posix.cc.
20
+ inline size_t fread_unlocked(void *a, size_t b, size_t c, FILE *d) {
21
+ return fread(a, b, c, d);
22
+ }
23
+
24
+ inline size_t fwrite_unlocked(const void *a, size_t b, size_t c, FILE *d) {
25
+ return fwrite(a, b, c, d);
26
+ }
27
+
28
+ inline int fflush_unlocked(FILE *f) {
29
+ return fflush(f);
30
+ }
31
+
32
+ inline int fdatasync(int fd) {
33
+ return fsync(fd);
34
+ }
35
+
36
+ namespace port {
37
+
38
+ static const bool kLittleEndian = (__DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN);
39
+
40
+ // ------------------ Threading -------------------
41
+
42
+ // A Mutex represents an exclusive lock.
43
+ class Mutex {
44
+ public:
45
+ Mutex();
46
+ ~Mutex();
47
+
48
+ void Lock();
49
+ void Unlock();
50
+ void AssertHeld() { }
51
+
52
+ private:
53
+ friend class CondVar;
54
+ pthread_mutex_t mu_;
55
+
56
+ // No copying
57
+ Mutex(const Mutex&);
58
+ void operator=(const Mutex&);
59
+ };
60
+
61
+ class CondVar {
62
+ public:
63
+ explicit CondVar(Mutex* mu);
64
+ ~CondVar();
65
+
66
+ void Wait();
67
+ void Signal();
68
+ void SignalAll();
69
+
70
+ private:
71
+ pthread_cond_t cv_;
72
+ Mutex* mu_;
73
+ };
74
+
75
+ inline void MemoryBarrier() {
76
+ #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
77
+ // See http://gcc.gnu.org/ml/gcc/2003-04/msg01180.html for a discussion on
78
+ // this idiom. Also see http://en.wikipedia.org/wiki/Memory_ordering.
79
+ __asm__ __volatile__("" : : : "memory");
80
+ #else
81
+ OSMemoryBarrier();
82
+ #endif
83
+ }
84
+
85
+ class AtomicPointer {
86
+ private:
87
+ void* ptr_;
88
+ public:
89
+ AtomicPointer() { }
90
+ explicit AtomicPointer(void* p) : ptr_(p) {}
91
+ inline void* Acquire_Load() const {
92
+ void* ptr = ptr_;
93
+ MemoryBarrier();
94
+ return ptr;
95
+ }
96
+ inline void Release_Store(void* v) {
97
+ MemoryBarrier();
98
+ ptr_ = v;
99
+ }
100
+ inline void* NoBarrier_Load() const {
101
+ return ptr_;
102
+ }
103
+ inline void NoBarrier_Store(void* v) {
104
+ ptr_ = v;
105
+ }
106
+ };
107
+
108
+ inline bool Snappy_Compress(const char* input, size_t input_length,
109
+ std::string* output) {
110
+ return false;
111
+ }
112
+
113
+ inline bool Snappy_Uncompress(const char* input_data, size_t input_length,
114
+ std::string* output) {
115
+ return false;
116
+ }
117
+
118
+ inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
119
+ return false;
120
+ }
121
+
122
+ }
123
+ }
124
+
125
+ #endif // STORAGE_LEVELDB_PORT_PORT_OSX_H_