filiptepper-leveldb-ruby 0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. data/LICENSE +24 -0
  2. data/README +72 -0
  3. data/ext/leveldb/extconf.rb +14 -0
  4. data/ext/leveldb/leveldb.cc +530 -0
  5. data/ext/leveldb/platform.rb +83 -0
  6. data/leveldb/Makefile +191 -0
  7. data/leveldb/build_detect_platform +160 -0
  8. data/leveldb/db/builder.cc +88 -0
  9. data/leveldb/db/builder.h +34 -0
  10. data/leveldb/db/c.cc +581 -0
  11. data/leveldb/db/corruption_test.cc +359 -0
  12. data/leveldb/db/db_bench.cc +970 -0
  13. data/leveldb/db/db_impl.cc +1448 -0
  14. data/leveldb/db/db_impl.h +194 -0
  15. data/leveldb/db/db_iter.cc +299 -0
  16. data/leveldb/db/db_iter.h +26 -0
  17. data/leveldb/db/db_test.cc +1901 -0
  18. data/leveldb/db/dbformat.cc +140 -0
  19. data/leveldb/db/dbformat.h +227 -0
  20. data/leveldb/db/dbformat_test.cc +112 -0
  21. data/leveldb/db/filename.cc +139 -0
  22. data/leveldb/db/filename.h +80 -0
  23. data/leveldb/db/filename_test.cc +122 -0
  24. data/leveldb/db/log_format.h +35 -0
  25. data/leveldb/db/log_reader.cc +259 -0
  26. data/leveldb/db/log_reader.h +108 -0
  27. data/leveldb/db/log_test.cc +500 -0
  28. data/leveldb/db/log_writer.cc +103 -0
  29. data/leveldb/db/log_writer.h +48 -0
  30. data/leveldb/db/memtable.cc +145 -0
  31. data/leveldb/db/memtable.h +91 -0
  32. data/leveldb/db/repair.cc +389 -0
  33. data/leveldb/db/skiplist.h +379 -0
  34. data/leveldb/db/skiplist_test.cc +378 -0
  35. data/leveldb/db/snapshot.h +66 -0
  36. data/leveldb/db/table_cache.cc +121 -0
  37. data/leveldb/db/table_cache.h +61 -0
  38. data/leveldb/db/version_edit.cc +266 -0
  39. data/leveldb/db/version_edit.h +107 -0
  40. data/leveldb/db/version_edit_test.cc +46 -0
  41. data/leveldb/db/version_set.cc +1402 -0
  42. data/leveldb/db/version_set.h +370 -0
  43. data/leveldb/db/version_set_test.cc +179 -0
  44. data/leveldb/db/write_batch.cc +147 -0
  45. data/leveldb/db/write_batch_internal.h +49 -0
  46. data/leveldb/db/write_batch_test.cc +120 -0
  47. data/leveldb/helpers/memenv/memenv.cc +374 -0
  48. data/leveldb/helpers/memenv/memenv.h +20 -0
  49. data/leveldb/helpers/memenv/memenv_test.cc +232 -0
  50. data/leveldb/include/leveldb/c.h +275 -0
  51. data/leveldb/include/leveldb/cache.h +99 -0
  52. data/leveldb/include/leveldb/comparator.h +63 -0
  53. data/leveldb/include/leveldb/db.h +161 -0
  54. data/leveldb/include/leveldb/env.h +323 -0
  55. data/leveldb/include/leveldb/filter_policy.h +70 -0
  56. data/leveldb/include/leveldb/iterator.h +100 -0
  57. data/leveldb/include/leveldb/options.h +195 -0
  58. data/leveldb/include/leveldb/slice.h +109 -0
  59. data/leveldb/include/leveldb/status.h +106 -0
  60. data/leveldb/include/leveldb/table.h +85 -0
  61. data/leveldb/include/leveldb/table_builder.h +92 -0
  62. data/leveldb/include/leveldb/write_batch.h +64 -0
  63. data/leveldb/port/atomic_pointer.h +144 -0
  64. data/leveldb/port/port.h +21 -0
  65. data/leveldb/port/port_android.cc +64 -0
  66. data/leveldb/port/port_android.h +159 -0
  67. data/leveldb/port/port_example.h +125 -0
  68. data/leveldb/port/port_posix.cc +50 -0
  69. data/leveldb/port/port_posix.h +129 -0
  70. data/leveldb/port/win/stdint.h +24 -0
  71. data/leveldb/table/block.cc +267 -0
  72. data/leveldb/table/block.h +44 -0
  73. data/leveldb/table/block_builder.cc +109 -0
  74. data/leveldb/table/block_builder.h +57 -0
  75. data/leveldb/table/filter_block.cc +111 -0
  76. data/leveldb/table/filter_block.h +68 -0
  77. data/leveldb/table/filter_block_test.cc +128 -0
  78. data/leveldb/table/format.cc +145 -0
  79. data/leveldb/table/format.h +108 -0
  80. data/leveldb/table/iterator.cc +67 -0
  81. data/leveldb/table/iterator_wrapper.h +63 -0
  82. data/leveldb/table/merger.cc +197 -0
  83. data/leveldb/table/merger.h +26 -0
  84. data/leveldb/table/table.cc +276 -0
  85. data/leveldb/table/table_builder.cc +270 -0
  86. data/leveldb/table/table_test.cc +838 -0
  87. data/leveldb/table/two_level_iterator.cc +182 -0
  88. data/leveldb/table/two_level_iterator.h +34 -0
  89. data/leveldb/util/arena.cc +68 -0
  90. data/leveldb/util/arena.h +68 -0
  91. data/leveldb/util/arena_test.cc +68 -0
  92. data/leveldb/util/bloom.cc +95 -0
  93. data/leveldb/util/bloom_test.cc +159 -0
  94. data/leveldb/util/cache.cc +328 -0
  95. data/leveldb/util/cache_test.cc +186 -0
  96. data/leveldb/util/coding.cc +194 -0
  97. data/leveldb/util/coding.h +104 -0
  98. data/leveldb/util/coding_test.cc +173 -0
  99. data/leveldb/util/comparator.cc +76 -0
  100. data/leveldb/util/crc32c.cc +332 -0
  101. data/leveldb/util/crc32c.h +45 -0
  102. data/leveldb/util/crc32c_test.cc +72 -0
  103. data/leveldb/util/env.cc +96 -0
  104. data/leveldb/util/env_posix.cc +609 -0
  105. data/leveldb/util/env_test.cc +104 -0
  106. data/leveldb/util/filter_policy.cc +11 -0
  107. data/leveldb/util/hash.cc +45 -0
  108. data/leveldb/util/hash.h +19 -0
  109. data/leveldb/util/histogram.cc +139 -0
  110. data/leveldb/util/histogram.h +42 -0
  111. data/leveldb/util/logging.cc +81 -0
  112. data/leveldb/util/logging.h +47 -0
  113. data/leveldb/util/mutexlock.h +39 -0
  114. data/leveldb/util/options.cc +29 -0
  115. data/leveldb/util/posix_logger.h +98 -0
  116. data/leveldb/util/random.h +59 -0
  117. data/leveldb/util/status.cc +75 -0
  118. data/leveldb/util/testharness.cc +77 -0
  119. data/leveldb/util/testharness.h +138 -0
  120. data/leveldb/util/testutil.cc +51 -0
  121. data/leveldb/util/testutil.h +53 -0
  122. data/lib/leveldb.rb +76 -0
  123. metadata +175 -0
@@ -0,0 +1,70 @@
1
+ // Copyright (c) 2012 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
+ // A database can be configured with a custom FilterPolicy object.
6
+ // This object is responsible for creating a small filter from a set
7
+ // of keys. These filters are stored in leveldb and are consulted
8
+ // automatically by leveldb to decide whether or not to read some
9
+ // information from disk. In many cases, a filter can cut down the
10
+ // number of disk seeks form a handful to a single disk seek per
11
+ // DB::Get() call.
12
+ //
13
+ // Most people will want to use the builtin bloom filter support (see
14
+ // NewBloomFilterPolicy() below).
15
+
16
+ #ifndef STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_
17
+ #define STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_
18
+
19
+ #include <string>
20
+
21
+ namespace leveldb {
22
+
23
+ class Slice;
24
+
25
+ class FilterPolicy {
26
+ public:
27
+ virtual ~FilterPolicy();
28
+
29
+ // Return the name of this policy. Note that if the filter encoding
30
+ // changes in an incompatible way, the name returned by this method
31
+ // must be changed. Otherwise, old incompatible filters may be
32
+ // passed to methods of this type.
33
+ virtual const char* Name() const = 0;
34
+
35
+ // keys[0,n-1] contains a list of keys (potentially with duplicates)
36
+ // that are ordered according to the user supplied comparator.
37
+ // Append a filter that summarizes keys[0,n-1] to *dst.
38
+ //
39
+ // Warning: do not change the initial contents of *dst. Instead,
40
+ // append the newly constructed filter to *dst.
41
+ virtual void CreateFilter(const Slice* keys, int n, std::string* dst)
42
+ const = 0;
43
+
44
+ // "filter" contains the data appended by a preceding call to
45
+ // CreateFilter() on this class. This method must return true if
46
+ // the key was in the list of keys passed to CreateFilter().
47
+ // This method may return true or false if the key was not on the
48
+ // list, but it should aim to return false with a high probability.
49
+ virtual bool KeyMayMatch(const Slice& key, const Slice& filter) const = 0;
50
+ };
51
+
52
+ // Return a new filter policy that uses a bloom filter with approximately
53
+ // the specified number of bits per key. A good value for bits_per_key
54
+ // is 10, which yields a filter with ~ 1% false positive rate.
55
+ //
56
+ // Callers must delete the result after any database that is using the
57
+ // result has been closed.
58
+ //
59
+ // Note: if you are using a custom comparator that ignores some parts
60
+ // of the keys being compared, you must not use NewBloomFilterPolicy()
61
+ // and must provide your own FilterPolicy that also ignores the
62
+ // corresponding parts of the keys. For example, if the comparator
63
+ // ignores trailing spaces, it would be incorrect to use a
64
+ // FilterPolicy (like NewBloomFilterPolicy) that does not ignore
65
+ // trailing spaces in keys.
66
+ extern const FilterPolicy* NewBloomFilterPolicy(int bits_per_key);
67
+
68
+ }
69
+
70
+ #endif // STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_
@@ -0,0 +1,100 @@
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
+ // An iterator yields a sequence of key/value pairs from a source.
6
+ // The following class defines the interface. Multiple implementations
7
+ // are provided by this library. In particular, iterators are provided
8
+ // to access the contents of a Table or a DB.
9
+ //
10
+ // Multiple threads can invoke const methods on an Iterator without
11
+ // external synchronization, but if any of the threads may call a
12
+ // non-const method, all threads accessing the same Iterator must use
13
+ // external synchronization.
14
+
15
+ #ifndef STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
16
+ #define STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
17
+
18
+ #include "leveldb/slice.h"
19
+ #include "leveldb/status.h"
20
+
21
+ namespace leveldb {
22
+
23
+ class Iterator {
24
+ public:
25
+ Iterator();
26
+ virtual ~Iterator();
27
+
28
+ // An iterator is either positioned at a key/value pair, or
29
+ // not valid. This method returns true iff the iterator is valid.
30
+ virtual bool Valid() const = 0;
31
+
32
+ // Position at the first key in the source. The iterator is Valid()
33
+ // after this call iff the source is not empty.
34
+ virtual void SeekToFirst() = 0;
35
+
36
+ // Position at the last key in the source. The iterator is
37
+ // Valid() after this call iff the source is not empty.
38
+ virtual void SeekToLast() = 0;
39
+
40
+ // Position at the first key in the source that at or past target
41
+ // The iterator is Valid() after this call iff the source contains
42
+ // an entry that comes at or past target.
43
+ virtual void Seek(const Slice& target) = 0;
44
+
45
+ // Moves to the next entry in the source. After this call, Valid() is
46
+ // true iff the iterator was not positioned at the last entry in the source.
47
+ // REQUIRES: Valid()
48
+ virtual void Next() = 0;
49
+
50
+ // Moves to the previous entry in the source. After this call, Valid() is
51
+ // true iff the iterator was not positioned at the first entry in source.
52
+ // REQUIRES: Valid()
53
+ virtual void Prev() = 0;
54
+
55
+ // Return the key for the current entry. The underlying storage for
56
+ // the returned slice is valid only until the next modification of
57
+ // the iterator.
58
+ // REQUIRES: Valid()
59
+ virtual Slice key() const = 0;
60
+
61
+ // Return the value for the current entry. The underlying storage for
62
+ // the returned slice is valid only until the next modification of
63
+ // the iterator.
64
+ // REQUIRES: !AtEnd() && !AtStart()
65
+ virtual Slice value() const = 0;
66
+
67
+ // If an error has occurred, return it. Else return an ok status.
68
+ virtual Status status() const = 0;
69
+
70
+ // Clients are allowed to register function/arg1/arg2 triples that
71
+ // will be invoked when this iterator is destroyed.
72
+ //
73
+ // Note that unlike all of the preceding methods, this method is
74
+ // not abstract and therefore clients should not override it.
75
+ typedef void (*CleanupFunction)(void* arg1, void* arg2);
76
+ void RegisterCleanup(CleanupFunction function, void* arg1, void* arg2);
77
+
78
+ private:
79
+ struct Cleanup {
80
+ CleanupFunction function;
81
+ void* arg1;
82
+ void* arg2;
83
+ Cleanup* next;
84
+ };
85
+ Cleanup cleanup_;
86
+
87
+ // No copying allowed
88
+ Iterator(const Iterator&);
89
+ void operator=(const Iterator&);
90
+ };
91
+
92
+ // Return an empty iterator (yields nothing).
93
+ extern Iterator* NewEmptyIterator();
94
+
95
+ // Return an empty iterator with the specified status.
96
+ extern Iterator* NewErrorIterator(const Status& status);
97
+
98
+ } // namespace leveldb
99
+
100
+ #endif // STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
@@ -0,0 +1,195 @@
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_INCLUDE_OPTIONS_H_
6
+ #define STORAGE_LEVELDB_INCLUDE_OPTIONS_H_
7
+
8
+ #include <stddef.h>
9
+
10
+ namespace leveldb {
11
+
12
+ class Cache;
13
+ class Comparator;
14
+ class Env;
15
+ class FilterPolicy;
16
+ class Logger;
17
+ class Snapshot;
18
+
19
+ // DB contents are stored in a set of blocks, each of which holds a
20
+ // sequence of key,value pairs. Each block may be compressed before
21
+ // being stored in a file. The following enum describes which
22
+ // compression method (if any) is used to compress a block.
23
+ enum CompressionType {
24
+ // NOTE: do not change the values of existing entries, as these are
25
+ // part of the persistent format on disk.
26
+ kNoCompression = 0x0,
27
+ kSnappyCompression = 0x1
28
+ };
29
+
30
+ // Options to control the behavior of a database (passed to DB::Open)
31
+ struct Options {
32
+ // -------------------
33
+ // Parameters that affect behavior
34
+
35
+ // Comparator used to define the order of keys in the table.
36
+ // Default: a comparator that uses lexicographic byte-wise ordering
37
+ //
38
+ // REQUIRES: The client must ensure that the comparator supplied
39
+ // here has the same name and orders keys *exactly* the same as the
40
+ // comparator provided to previous open calls on the same DB.
41
+ const Comparator* comparator;
42
+
43
+ // If true, the database will be created if it is missing.
44
+ // Default: false
45
+ bool create_if_missing;
46
+
47
+ // If true, an error is raised if the database already exists.
48
+ // Default: false
49
+ bool error_if_exists;
50
+
51
+ // If true, the implementation will do aggressive checking of the
52
+ // data it is processing and will stop early if it detects any
53
+ // errors. This may have unforeseen ramifications: for example, a
54
+ // corruption of one DB entry may cause a large number of entries to
55
+ // become unreadable or for the entire DB to become unopenable.
56
+ // Default: false
57
+ bool paranoid_checks;
58
+
59
+ // Use the specified object to interact with the environment,
60
+ // e.g. to read/write files, schedule background work, etc.
61
+ // Default: Env::Default()
62
+ Env* env;
63
+
64
+ // Any internal progress/error information generated by the db will
65
+ // be written to info_log if it is non-NULL, or to a file stored
66
+ // in the same directory as the DB contents if info_log is NULL.
67
+ // Default: NULL
68
+ Logger* info_log;
69
+
70
+ // -------------------
71
+ // Parameters that affect performance
72
+
73
+ // Amount of data to build up in memory (backed by an unsorted log
74
+ // on disk) before converting to a sorted on-disk file.
75
+ //
76
+ // Larger values increase performance, especially during bulk loads.
77
+ // Up to two write buffers may be held in memory at the same time,
78
+ // so you may wish to adjust this parameter to control memory usage.
79
+ // Also, a larger write buffer will result in a longer recovery time
80
+ // the next time the database is opened.
81
+ //
82
+ // Default: 4MB
83
+ size_t write_buffer_size;
84
+
85
+ // Number of open files that can be used by the DB. You may need to
86
+ // increase this if your database has a large working set (budget
87
+ // one open file per 2MB of working set).
88
+ //
89
+ // Default: 1000
90
+ int max_open_files;
91
+
92
+ // Control over blocks (user data is stored in a set of blocks, and
93
+ // a block is the unit of reading from disk).
94
+
95
+ // If non-NULL, use the specified cache for blocks.
96
+ // If NULL, leveldb will automatically create and use an 8MB internal cache.
97
+ // Default: NULL
98
+ Cache* block_cache;
99
+
100
+ // Approximate size of user data packed per block. Note that the
101
+ // block size specified here corresponds to uncompressed data. The
102
+ // actual size of the unit read from disk may be smaller if
103
+ // compression is enabled. This parameter can be changed dynamically.
104
+ //
105
+ // Default: 4K
106
+ size_t block_size;
107
+
108
+ // Number of keys between restart points for delta encoding of keys.
109
+ // This parameter can be changed dynamically. Most clients should
110
+ // leave this parameter alone.
111
+ //
112
+ // Default: 16
113
+ int block_restart_interval;
114
+
115
+ // Compress blocks using the specified compression algorithm. This
116
+ // parameter can be changed dynamically.
117
+ //
118
+ // Default: kSnappyCompression, which gives lightweight but fast
119
+ // compression.
120
+ //
121
+ // Typical speeds of kSnappyCompression on an Intel(R) Core(TM)2 2.4GHz:
122
+ // ~200-500MB/s compression
123
+ // ~400-800MB/s decompression
124
+ // Note that these speeds are significantly faster than most
125
+ // persistent storage speeds, and therefore it is typically never
126
+ // worth switching to kNoCompression. Even if the input data is
127
+ // incompressible, the kSnappyCompression implementation will
128
+ // efficiently detect that and will switch to uncompressed mode.
129
+ CompressionType compression;
130
+
131
+ // If non-NULL, use the specified filter policy to reduce disk reads.
132
+ // Many applications will benefit from passing the result of
133
+ // NewBloomFilterPolicy() here.
134
+ //
135
+ // Default: NULL
136
+ const FilterPolicy* filter_policy;
137
+
138
+ // Create an Options object with default values for all fields.
139
+ Options();
140
+ };
141
+
142
+ // Options that control read operations
143
+ struct ReadOptions {
144
+ // If true, all data read from underlying storage will be
145
+ // verified against corresponding checksums.
146
+ // Default: false
147
+ bool verify_checksums;
148
+
149
+ // Should the data read for this iteration be cached in memory?
150
+ // Callers may wish to set this field to false for bulk scans.
151
+ // Default: true
152
+ bool fill_cache;
153
+
154
+ // If "snapshot" is non-NULL, read as of the supplied snapshot
155
+ // (which must belong to the DB that is being read and which must
156
+ // not have been released). If "snapshot" is NULL, use an impliicit
157
+ // snapshot of the state at the beginning of this read operation.
158
+ // Default: NULL
159
+ const Snapshot* snapshot;
160
+
161
+ ReadOptions()
162
+ : verify_checksums(false),
163
+ fill_cache(true),
164
+ snapshot(NULL) {
165
+ }
166
+ };
167
+
168
+ // Options that control write operations
169
+ struct WriteOptions {
170
+ // If true, the write will be flushed from the operating system
171
+ // buffer cache (by calling WritableFile::Sync()) before the write
172
+ // is considered complete. If this flag is true, writes will be
173
+ // slower.
174
+ //
175
+ // If this flag is false, and the machine crashes, some recent
176
+ // writes may be lost. Note that if it is just the process that
177
+ // crashes (i.e., the machine does not reboot), no writes will be
178
+ // lost even if sync==false.
179
+ //
180
+ // In other words, a DB write with sync==false has similar
181
+ // crash semantics as the "write()" system call. A DB write
182
+ // with sync==true has similar crash semantics to a "write()"
183
+ // system call followed by "fsync()".
184
+ //
185
+ // Default: false
186
+ bool sync;
187
+
188
+ WriteOptions()
189
+ : sync(false) {
190
+ }
191
+ };
192
+
193
+ } // namespace leveldb
194
+
195
+ #endif // STORAGE_LEVELDB_INCLUDE_OPTIONS_H_
@@ -0,0 +1,109 @@
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
+ // Slice is a simple structure containing a pointer into some external
6
+ // storage and a size. The user of a Slice must ensure that the slice
7
+ // is not used after the corresponding external storage has been
8
+ // deallocated.
9
+ //
10
+ // Multiple threads can invoke const methods on a Slice without
11
+ // external synchronization, but if any of the threads may call a
12
+ // non-const method, all threads accessing the same Slice must use
13
+ // external synchronization.
14
+
15
+ #ifndef STORAGE_LEVELDB_INCLUDE_SLICE_H_
16
+ #define STORAGE_LEVELDB_INCLUDE_SLICE_H_
17
+
18
+ #include <assert.h>
19
+ #include <stddef.h>
20
+ #include <string.h>
21
+ #include <string>
22
+
23
+ namespace leveldb {
24
+
25
+ class Slice {
26
+ public:
27
+ // Create an empty slice.
28
+ Slice() : data_(""), size_(0) { }
29
+
30
+ // Create a slice that refers to d[0,n-1].
31
+ Slice(const char* d, size_t n) : data_(d), size_(n) { }
32
+
33
+ // Create a slice that refers to the contents of "s"
34
+ Slice(const std::string& s) : data_(s.data()), size_(s.size()) { }
35
+
36
+ // Create a slice that refers to s[0,strlen(s)-1]
37
+ Slice(const char* s) : data_(s), size_(strlen(s)) { }
38
+
39
+ // Return a pointer to the beginning of the referenced data
40
+ const char* data() const { return data_; }
41
+
42
+ // Return the length (in bytes) of the referenced data
43
+ size_t size() const { return size_; }
44
+
45
+ // Return true iff the length of the referenced data is zero
46
+ bool empty() const { return size_ == 0; }
47
+
48
+ // Return the ith byte in the referenced data.
49
+ // REQUIRES: n < size()
50
+ char operator[](size_t n) const {
51
+ assert(n < size());
52
+ return data_[n];
53
+ }
54
+
55
+ // Change this slice to refer to an empty array
56
+ void clear() { data_ = ""; size_ = 0; }
57
+
58
+ // Drop the first "n" bytes from this slice.
59
+ void remove_prefix(size_t n) {
60
+ assert(n <= size());
61
+ data_ += n;
62
+ size_ -= n;
63
+ }
64
+
65
+ // Return a string that contains the copy of the referenced data.
66
+ std::string ToString() const { return std::string(data_, size_); }
67
+
68
+ // Three-way comparison. Returns value:
69
+ // < 0 iff "*this" < "b",
70
+ // == 0 iff "*this" == "b",
71
+ // > 0 iff "*this" > "b"
72
+ int compare(const Slice& b) const;
73
+
74
+ // Return true iff "x" is a prefix of "*this"
75
+ bool starts_with(const Slice& x) const {
76
+ return ((size_ >= x.size_) &&
77
+ (memcmp(data_, x.data_, x.size_) == 0));
78
+ }
79
+
80
+ private:
81
+ const char* data_;
82
+ size_t size_;
83
+
84
+ // Intentionally copyable
85
+ };
86
+
87
+ inline bool operator==(const Slice& x, const Slice& y) {
88
+ return ((x.size() == y.size()) &&
89
+ (memcmp(x.data(), y.data(), x.size()) == 0));
90
+ }
91
+
92
+ inline bool operator!=(const Slice& x, const Slice& y) {
93
+ return !(x == y);
94
+ }
95
+
96
+ inline int Slice::compare(const Slice& b) const {
97
+ const int min_len = (size_ < b.size_) ? size_ : b.size_;
98
+ int r = memcmp(data_, b.data_, min_len);
99
+ if (r == 0) {
100
+ if (size_ < b.size_) r = -1;
101
+ else if (size_ > b.size_) r = +1;
102
+ }
103
+ return r;
104
+ }
105
+
106
+ } // namespace leveldb
107
+
108
+
109
+ #endif // STORAGE_LEVELDB_INCLUDE_SLICE_H_