filiptepper-leveldb-ruby 0.14
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/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
data/leveldb/port/port.h
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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_PORT_PORT_H_
|
|
6
|
+
#define STORAGE_LEVELDB_PORT_PORT_H_
|
|
7
|
+
|
|
8
|
+
#include <string.h>
|
|
9
|
+
|
|
10
|
+
// Include the appropriate platform specific file below. If you are
|
|
11
|
+
// porting to a new platform, see "port_example.h" for documentation
|
|
12
|
+
// of what the new port_<platform>.h file must provide.
|
|
13
|
+
#if defined(LEVELDB_PLATFORM_POSIX)
|
|
14
|
+
# include "port/port_posix.h"
|
|
15
|
+
#elif defined(LEVELDB_PLATFORM_CHROMIUM)
|
|
16
|
+
# include "port/port_chromium.h"
|
|
17
|
+
#elif defined(LEVELDB_PLATFORM_ANDROID)
|
|
18
|
+
# include "port/port_android.h"
|
|
19
|
+
#endif
|
|
20
|
+
|
|
21
|
+
#endif // STORAGE_LEVELDB_PORT_PORT_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
|
+
#include "port/port_android.h"
|
|
6
|
+
|
|
7
|
+
#include <cstdlib>
|
|
8
|
+
|
|
9
|
+
extern "C" {
|
|
10
|
+
size_t fread_unlocked(void *a, size_t b, size_t c, FILE *d) {
|
|
11
|
+
return fread(a, b, c, d);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
size_t fwrite_unlocked(const void *a, size_t b, size_t c, FILE *d) {
|
|
15
|
+
return fwrite(a, b, c, d);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
int fflush_unlocked(FILE *f) {
|
|
19
|
+
return fflush(f);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
int fdatasync(int fd) {
|
|
23
|
+
return fsync(fd);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
namespace leveldb {
|
|
28
|
+
namespace port {
|
|
29
|
+
|
|
30
|
+
static void PthreadCall(const char* label, int result) {
|
|
31
|
+
if (result != 0) {
|
|
32
|
+
fprintf(stderr, "pthread %s: %s\n", label, strerror(result));
|
|
33
|
+
abort();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
Mutex::Mutex() { PthreadCall("init mutex", pthread_mutex_init(&mu_, NULL)); }
|
|
38
|
+
Mutex::~Mutex() { PthreadCall("destroy mutex", pthread_mutex_destroy(&mu_)); }
|
|
39
|
+
void Mutex::Lock() { PthreadCall("lock", pthread_mutex_lock(&mu_)); }
|
|
40
|
+
void Mutex::Unlock() { PthreadCall("unlock", pthread_mutex_unlock(&mu_)); }
|
|
41
|
+
|
|
42
|
+
CondVar::CondVar(Mutex* mu)
|
|
43
|
+
: mu_(mu) {
|
|
44
|
+
PthreadCall("init cv", pthread_cond_init(&cv_, NULL));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
CondVar::~CondVar() {
|
|
48
|
+
PthreadCall("destroy cv", pthread_cond_destroy(&cv_));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
void CondVar::Wait() {
|
|
52
|
+
PthreadCall("wait", pthread_cond_wait(&cv_, &mu_->mu_));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
void CondVar::Signal(){
|
|
56
|
+
PthreadCall("signal", pthread_cond_signal(&cv_));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
void CondVar::SignalAll() {
|
|
60
|
+
PthreadCall("broadcast", pthread_cond_broadcast(&cv_));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
} // namespace port
|
|
64
|
+
} // namespace leveldb
|
|
@@ -0,0 +1,159 @@
|
|
|
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_ANDROID_H_
|
|
8
|
+
#define STORAGE_LEVELDB_PORT_PORT_ANDROID_H_
|
|
9
|
+
|
|
10
|
+
#include <endian.h>
|
|
11
|
+
#include <pthread.h>
|
|
12
|
+
#include <stdint.h>
|
|
13
|
+
#include <unistd.h>
|
|
14
|
+
#include <string>
|
|
15
|
+
#include <cctype>
|
|
16
|
+
|
|
17
|
+
// Collapse the plethora of ARM flavors available to an easier to manage set
|
|
18
|
+
// Defs reference is at https://wiki.edubuntu.org/ARM/Thumb2PortingHowto
|
|
19
|
+
#if defined(__ARM_ARCH_6__) || \
|
|
20
|
+
defined(__ARM_ARCH_6J__) || \
|
|
21
|
+
defined(__ARM_ARCH_6K__) || \
|
|
22
|
+
defined(__ARM_ARCH_6Z__) || \
|
|
23
|
+
defined(__ARM_ARCH_6T2__) || \
|
|
24
|
+
defined(__ARM_ARCH_6ZK__) || \
|
|
25
|
+
defined(__ARM_ARCH_7__) || \
|
|
26
|
+
defined(__ARM_ARCH_7R__) || \
|
|
27
|
+
defined(__ARM_ARCH_7A__)
|
|
28
|
+
#define ARMV6_OR_7 1
|
|
29
|
+
#endif
|
|
30
|
+
|
|
31
|
+
extern "C" {
|
|
32
|
+
size_t fread_unlocked(void *a, size_t b, size_t c, FILE *d);
|
|
33
|
+
size_t fwrite_unlocked(const void *a, size_t b, size_t c, FILE *d);
|
|
34
|
+
int fflush_unlocked(FILE *f);
|
|
35
|
+
int fdatasync (int fd);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
namespace leveldb {
|
|
39
|
+
namespace port {
|
|
40
|
+
|
|
41
|
+
static const bool kLittleEndian = __BYTE_ORDER == __LITTLE_ENDIAN;
|
|
42
|
+
|
|
43
|
+
class CondVar;
|
|
44
|
+
|
|
45
|
+
class Mutex {
|
|
46
|
+
public:
|
|
47
|
+
Mutex();
|
|
48
|
+
~Mutex();
|
|
49
|
+
|
|
50
|
+
void Lock();
|
|
51
|
+
void Unlock();
|
|
52
|
+
void AssertHeld() {
|
|
53
|
+
//TODO(gabor): How can I implement this?
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private:
|
|
57
|
+
friend class CondVar;
|
|
58
|
+
pthread_mutex_t mu_;
|
|
59
|
+
|
|
60
|
+
// No copying
|
|
61
|
+
Mutex(const Mutex&);
|
|
62
|
+
void operator=(const Mutex&);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
class CondVar {
|
|
66
|
+
public:
|
|
67
|
+
explicit CondVar(Mutex* mu);
|
|
68
|
+
~CondVar();
|
|
69
|
+
void Wait();
|
|
70
|
+
void Signal();
|
|
71
|
+
void SignalAll();
|
|
72
|
+
private:
|
|
73
|
+
Mutex* mu_;
|
|
74
|
+
pthread_cond_t cv_;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
#ifndef ARMV6_OR_7
|
|
78
|
+
// On ARM chipsets <V6, 0xffff0fa0 is the hard coded address of a
|
|
79
|
+
// memory barrier function provided by the kernel.
|
|
80
|
+
typedef void (*LinuxKernelMemoryBarrierFunc)(void);
|
|
81
|
+
// TODO(user): ATTRIBUTE_WEAK is undefined, so this fails to build on
|
|
82
|
+
// non-ARMV6_OR_7. We may be able to replace it with __attribute__((weak)) for
|
|
83
|
+
// older ARM builds, but x86 builds will require a different memory barrier.
|
|
84
|
+
LinuxKernelMemoryBarrierFunc pLinuxKernelMemoryBarrier ATTRIBUTE_WEAK =
|
|
85
|
+
(LinuxKernelMemoryBarrierFunc) 0xffff0fa0;
|
|
86
|
+
#endif
|
|
87
|
+
|
|
88
|
+
// Storage for a lock-free pointer
|
|
89
|
+
class AtomicPointer {
|
|
90
|
+
private:
|
|
91
|
+
void* rep_;
|
|
92
|
+
|
|
93
|
+
inline void MemoryBarrier() const {
|
|
94
|
+
// TODO(gabor): This only works on Android instruction sets >= V6
|
|
95
|
+
#ifdef ARMV6_OR_7
|
|
96
|
+
__asm__ __volatile__("dmb" : : : "memory");
|
|
97
|
+
#else
|
|
98
|
+
pLinuxKernelMemoryBarrier();
|
|
99
|
+
#endif
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public:
|
|
103
|
+
AtomicPointer() { }
|
|
104
|
+
explicit AtomicPointer(void* v) : rep_(v) { }
|
|
105
|
+
inline void* Acquire_Load() const {
|
|
106
|
+
void* r = rep_;
|
|
107
|
+
MemoryBarrier();
|
|
108
|
+
return r;
|
|
109
|
+
}
|
|
110
|
+
inline void Release_Store(void* v) {
|
|
111
|
+
MemoryBarrier();
|
|
112
|
+
rep_ = v;
|
|
113
|
+
}
|
|
114
|
+
inline void* NoBarrier_Load() const {
|
|
115
|
+
void* r = rep_;
|
|
116
|
+
return r;
|
|
117
|
+
}
|
|
118
|
+
inline void NoBarrier_Store(void* v) {
|
|
119
|
+
rep_ = v;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// TODO(gabor): Implement compress
|
|
124
|
+
inline bool Snappy_Compress(
|
|
125
|
+
const char* input,
|
|
126
|
+
size_t input_length,
|
|
127
|
+
std::string* output) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// TODO(gabor): Implement uncompress
|
|
132
|
+
inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
|
|
133
|
+
size_t* result) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// TODO(gabor): Implement uncompress
|
|
138
|
+
inline bool Snappy_Uncompress(
|
|
139
|
+
const char* input_data,
|
|
140
|
+
size_t input_length,
|
|
141
|
+
char* output) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
inline uint64_t ThreadIdentifier() {
|
|
146
|
+
pthread_t tid = pthread_self();
|
|
147
|
+
uint64_t r = 0;
|
|
148
|
+
memcpy(&r, &tid, sizeof(r) < sizeof(tid) ? sizeof(r) : sizeof(tid));
|
|
149
|
+
return r;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
} // namespace port
|
|
157
|
+
} // namespace leveldb
|
|
158
|
+
|
|
159
|
+
#endif // STORAGE_LEVELDB_PORT_PORT_ANDROID_H_
|
|
@@ -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
|
+
// 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
|
+
// If input[0,input_length-1] looks like a valid snappy compressed
|
|
100
|
+
// buffer, store the size of the uncompressed data in *result and
|
|
101
|
+
// return true. Else return false.
|
|
102
|
+
extern bool Snappy_GetUncompressedLength(const char* input, size_t length,
|
|
103
|
+
size_t* result);
|
|
104
|
+
|
|
105
|
+
// Attempt to snappy uncompress input[0,input_length-1] into *output.
|
|
106
|
+
// Returns true if successful, false if the input is invalid lightweight
|
|
107
|
+
// compressed data.
|
|
108
|
+
//
|
|
109
|
+
// REQUIRES: at least the first "n" bytes of output[] must be writable
|
|
110
|
+
// where "n" is the result of a successful call to
|
|
111
|
+
// Snappy_GetUncompressedLength.
|
|
112
|
+
extern bool Snappy_Uncompress(const char* input_data, size_t input_length,
|
|
113
|
+
char* output);
|
|
114
|
+
|
|
115
|
+
// ------------------ Miscellaneous -------------------
|
|
116
|
+
|
|
117
|
+
// If heap profiling is not supported, returns false.
|
|
118
|
+
// Else repeatedly calls (*func)(arg, data, n) and then returns true.
|
|
119
|
+
// The concatenation of all "data[0,n-1]" fragments is the heap profile.
|
|
120
|
+
extern bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg);
|
|
121
|
+
|
|
122
|
+
} // namespace port
|
|
123
|
+
} // namespace leveldb
|
|
124
|
+
|
|
125
|
+
#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/port_posix.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
|
+
} // namespace port
|
|
50
|
+
} // namespace leveldb
|
|
@@ -0,0 +1,129 @@
|
|
|
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_POSIX_H_
|
|
8
|
+
#define STORAGE_LEVELDB_PORT_PORT_POSIX_H_
|
|
9
|
+
|
|
10
|
+
#if defined(OS_MACOSX)
|
|
11
|
+
#include <machine/endian.h>
|
|
12
|
+
#elif defined(OS_SOLARIS)
|
|
13
|
+
#include <sys/isa_defs.h>
|
|
14
|
+
#ifdef _LITTLE_ENDIAN
|
|
15
|
+
#define LITTLE_ENDIAN
|
|
16
|
+
#else
|
|
17
|
+
#define BIG_ENDIAN
|
|
18
|
+
#endif
|
|
19
|
+
#elif defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) ||\
|
|
20
|
+
defined(OS_DRAGONFLYBSD)
|
|
21
|
+
#include <sys/types.h>
|
|
22
|
+
#include <sys/endian.h>
|
|
23
|
+
#else
|
|
24
|
+
#include <endian.h>
|
|
25
|
+
#endif
|
|
26
|
+
#include <pthread.h>
|
|
27
|
+
#ifdef SNAPPY
|
|
28
|
+
#include <snappy.h>
|
|
29
|
+
#endif
|
|
30
|
+
#include <stdint.h>
|
|
31
|
+
#include <string>
|
|
32
|
+
#include "port/atomic_pointer.h"
|
|
33
|
+
|
|
34
|
+
#ifdef LITTLE_ENDIAN
|
|
35
|
+
#define IS_LITTLE_ENDIAN true
|
|
36
|
+
#else
|
|
37
|
+
#define IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
|
|
38
|
+
#endif
|
|
39
|
+
|
|
40
|
+
#if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) ||\
|
|
41
|
+
defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
|
|
42
|
+
// Use fread/fwrite/fflush on platforms without _unlocked variants
|
|
43
|
+
#define fread_unlocked fread
|
|
44
|
+
#define fwrite_unlocked fwrite
|
|
45
|
+
#define fflush_unlocked fflush
|
|
46
|
+
#endif
|
|
47
|
+
|
|
48
|
+
#if defined(OS_MACOSX) || defined(OS_FREEBSD) ||\
|
|
49
|
+
defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
|
|
50
|
+
// Use fsync() on platforms without fdatasync()
|
|
51
|
+
#define fdatasync fsync
|
|
52
|
+
#endif
|
|
53
|
+
|
|
54
|
+
namespace leveldb {
|
|
55
|
+
namespace port {
|
|
56
|
+
|
|
57
|
+
static const bool kLittleEndian = IS_LITTLE_ENDIAN;
|
|
58
|
+
|
|
59
|
+
class CondVar;
|
|
60
|
+
|
|
61
|
+
class Mutex {
|
|
62
|
+
public:
|
|
63
|
+
Mutex();
|
|
64
|
+
~Mutex();
|
|
65
|
+
|
|
66
|
+
void Lock();
|
|
67
|
+
void Unlock();
|
|
68
|
+
void AssertHeld() { }
|
|
69
|
+
|
|
70
|
+
private:
|
|
71
|
+
friend class CondVar;
|
|
72
|
+
pthread_mutex_t mu_;
|
|
73
|
+
|
|
74
|
+
// No copying
|
|
75
|
+
Mutex(const Mutex&);
|
|
76
|
+
void operator=(const Mutex&);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
class CondVar {
|
|
80
|
+
public:
|
|
81
|
+
explicit CondVar(Mutex* mu);
|
|
82
|
+
~CondVar();
|
|
83
|
+
void Wait();
|
|
84
|
+
void Signal();
|
|
85
|
+
void SignalAll();
|
|
86
|
+
private:
|
|
87
|
+
pthread_cond_t cv_;
|
|
88
|
+
Mutex* mu_;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
inline bool Snappy_Compress(const char* input, size_t length,
|
|
92
|
+
::std::string* output) {
|
|
93
|
+
#ifdef SNAPPY
|
|
94
|
+
output->resize(snappy::MaxCompressedLength(length));
|
|
95
|
+
size_t outlen;
|
|
96
|
+
snappy::RawCompress(input, length, &(*output)[0], &outlen);
|
|
97
|
+
output->resize(outlen);
|
|
98
|
+
return true;
|
|
99
|
+
#endif
|
|
100
|
+
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
|
|
105
|
+
size_t* result) {
|
|
106
|
+
#ifdef SNAPPY
|
|
107
|
+
return snappy::GetUncompressedLength(input, length, result);
|
|
108
|
+
#else
|
|
109
|
+
return false;
|
|
110
|
+
#endif
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
inline bool Snappy_Uncompress(const char* input, size_t length,
|
|
114
|
+
char* output) {
|
|
115
|
+
#ifdef SNAPPY
|
|
116
|
+
return snappy::RawUncompress(input, length, output);
|
|
117
|
+
#else
|
|
118
|
+
return false;
|
|
119
|
+
#endif
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
} // namespace port
|
|
127
|
+
} // namespace leveldb
|
|
128
|
+
|
|
129
|
+
#endif // STORAGE_LEVELDB_PORT_PORT_POSIX_H_
|