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.
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,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
+ }
99
+
100
+ #endif // STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
@@ -0,0 +1,198 @@
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 Snapshot;
16
+ class WritableFile;
17
+
18
+ // DB contents are stored in a set of blocks, each of which holds a
19
+ // sequence of key,value pairs. Each block may be compressed before
20
+ // being stored in a file. The following enum describes which
21
+ // compression method (if any) is used to compress a block.
22
+ enum CompressionType {
23
+ // NOTE: do not change the values of existing entries, as these are
24
+ // part of the persistent format on disk.
25
+ kNoCompression = 0x0,
26
+ kSnappyCompression = 0x1,
27
+ };
28
+
29
+ // Options to control the behavior of a database (passed to DB::Open)
30
+ struct Options {
31
+ // -------------------
32
+ // Parameters that affect behavior
33
+
34
+ // Comparator used to define the order of keys in the table.
35
+ // Default: a comparator that uses lexicographic byte-wise ordering
36
+ //
37
+ // REQUIRES: The client must ensure that the comparator supplied
38
+ // here has the same name and orders keys *exactly* the same as the
39
+ // comparator provided to previous open calls on the same DB.
40
+ const Comparator* comparator;
41
+
42
+ // If true, the database will be created if it is missing.
43
+ // Default: false
44
+ bool create_if_missing;
45
+
46
+ // If true, an error is raised if the database already exists.
47
+ // Default: false
48
+ bool error_if_exists;
49
+
50
+ // If true, the implementation will do aggressive checking of the
51
+ // data it is processing and will stop early if it detects any
52
+ // errors. This may have unforeseen ramifications: for example, a
53
+ // corruption of one DB entry may cause a large number of entries to
54
+ // become unreadable or for the entire DB to become unopenable.
55
+ // Default: false
56
+ bool paranoid_checks;
57
+
58
+ // Use the specified object to interact with the environment,
59
+ // e.g. to read/write files, schedule background work, etc.
60
+ // Default: Env::Default()
61
+ Env* env;
62
+
63
+ // Any internal progress/error information generated by the db will
64
+ // be to written to info_log if it is non-NULL, or to a file stored
65
+ // in the same directory as the DB contents if info_log is NULL.
66
+ // Default: NULL
67
+ WritableFile* info_log;
68
+
69
+ // -------------------
70
+ // Parameters that affect performance
71
+
72
+ // Amount of data to build up in memory (backed by an unsorted log
73
+ // on disk) before converting to a sorted on-disk file.
74
+ //
75
+ // Larger values increase performance, especially during bulk loads.
76
+ // Up to two write buffers may be held in memory at the same time,
77
+ // so you may wish to adjust this parameter to control memory usage.
78
+ //
79
+ // Default: 4MB
80
+ size_t write_buffer_size;
81
+
82
+ // Number of open files that can be used by the DB. You may need to
83
+ // increase this if your database has a large working set (budget
84
+ // one open file per 2MB of working set).
85
+ //
86
+ // Default: 1000
87
+ int max_open_files;
88
+
89
+ // Control over blocks (user data is stored in a set of blocks, and
90
+ // a block is the unit of reading from disk).
91
+
92
+ // If non-NULL, use the specified cache for blocks.
93
+ // If NULL, leveldb will automatically create and use an 8MB internal cache.
94
+ // Default: NULL
95
+ Cache* block_cache;
96
+
97
+ // Approximate size of user data packed per block. Note that the
98
+ // block size specified here corresponds to uncompressed data. The
99
+ // actual size of the unit read from disk may be smaller if
100
+ // compression is enabled. This parameter can be changed dynamically.
101
+ //
102
+ // Default: 4K
103
+ size_t block_size;
104
+
105
+ // Number of keys between restart points for delta encoding of keys.
106
+ // This parameter can be changed dynamically. Most clients should
107
+ // leave this parameter alone.
108
+ //
109
+ // Default: 16
110
+ int block_restart_interval;
111
+
112
+ // Compress blocks using the specified compression algorithm. This
113
+ // parameter can be changed dynamically.
114
+ //
115
+ // Default: kSnappyCompression, which gives lightweight but fast
116
+ // compression.
117
+ //
118
+ // Typical speeds of kSnappyCompression on an Intel(R) Core(TM)2 2.4GHz:
119
+ // ~200-500MB/s compression
120
+ // ~400-800MB/s decompression
121
+ // Note that these speeds are significantly faster than most
122
+ // persistent storage speeds, and therefore it is typically never
123
+ // worth switching to kNoCompression. Even if the input data is
124
+ // incompressible, the kSnappyCompression implementation will
125
+ // efficiently detect that and will switch to uncompressed mode.
126
+ CompressionType compression;
127
+
128
+ // Create an Options object with default values for all fields.
129
+ Options();
130
+ };
131
+
132
+ // Options that control read operations
133
+ struct ReadOptions {
134
+ // If true, all data read from underlying storage will be
135
+ // verified against corresponding checksums.
136
+ // Default: false
137
+ bool verify_checksums;
138
+
139
+ // Should the data read for this iteration be cached in memory?
140
+ // Callers may wish to set this field to false for bulk scans.
141
+ // Default: true
142
+ bool fill_cache;
143
+
144
+ // If "snapshot" is non-NULL, read as of the supplied snapshot
145
+ // (which must belong to the DB that is being read and which must
146
+ // not have been released). If "snapshot" is NULL, use an impliicit
147
+ // snapshot of the state at the beginning of this read operation.
148
+ // Default: NULL
149
+ const Snapshot* snapshot;
150
+
151
+ ReadOptions()
152
+ : verify_checksums(false),
153
+ fill_cache(true),
154
+ snapshot(NULL) {
155
+ }
156
+ };
157
+
158
+ // Options that control write operations
159
+ struct WriteOptions {
160
+ // If true, the write will be flushed from the operating system
161
+ // buffer cache (by calling WritableFile::Sync()) before the write
162
+ // is considered complete. If this flag is true, writes will be
163
+ // slower.
164
+ //
165
+ // If this flag is false, and the machine crashes, some recent
166
+ // writes may be lost. Note that if it is just the process that
167
+ // crashes (i.e., the machine does not reboot), no writes will be
168
+ // lost even if sync==false.
169
+ //
170
+ // In other words, a DB write with sync==false has similar
171
+ // crash semantics as the "write()" system call. A DB write
172
+ // with sync==true has similar crash semantics to a "write()"
173
+ // system call followed by "fsync()".
174
+ //
175
+ // Default: false
176
+ bool sync;
177
+
178
+ // If "post_write_snapshot" is non-NULL, and the write succeeds,
179
+ // *post_write_snapshot will be modified to point to a snapshot of
180
+ // the DB state immediately after this write. The caller must call
181
+ // DB::ReleaseSnapshot(*post_write_snapshotsnapshot) when the
182
+ // snapshot is no longer needed.
183
+ //
184
+ // If "post_write_snapshot" is non-NULL, and the write fails,
185
+ // *post_write_snapshot will be set to NULL.
186
+ //
187
+ // Default: NULL
188
+ const Snapshot** post_write_snapshot;
189
+
190
+ WriteOptions()
191
+ : sync(false),
192
+ post_write_snapshot(NULL) {
193
+ }
194
+ };
195
+
196
+ }
197
+
198
+ #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 data[0,n-1].
31
+ Slice(const char* data, size_t n) : data_(data), 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
+ }
107
+
108
+
109
+ #endif // STORAGE_LEVELDB_INCLUDE_SLICE_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
+ // A Status encapsulates the result of an operation. It may indicate success,
6
+ // or it may indicate an error with an associated error message.
7
+ //
8
+ // Multiple threads can invoke const methods on a Status without
9
+ // external synchronization, but if any of the threads may call a
10
+ // non-const method, all threads accessing the same Status must use
11
+ // external synchronization.
12
+
13
+ #ifndef STORAGE_LEVELDB_INCLUDE_STATUS_H_
14
+ #define STORAGE_LEVELDB_INCLUDE_STATUS_H_
15
+
16
+ #include <string>
17
+ #include "leveldb/slice.h"
18
+
19
+ namespace leveldb {
20
+
21
+ class Status {
22
+ public:
23
+ // Create a success status.
24
+ Status() : state_(NULL) { }
25
+ ~Status() { delete[] state_; }
26
+
27
+ // Copy the specified status.
28
+ Status(const Status& s);
29
+ void operator=(const Status& s);
30
+
31
+ // Return a success status.
32
+ static Status OK() { return Status(); }
33
+
34
+ // Return error status of an appropriate type.
35
+ static Status NotFound(const Slice& msg, const Slice& msg2 = Slice()) {
36
+ return Status(kNotFound, msg, msg2);
37
+ }
38
+ static Status Corruption(const Slice& msg, const Slice& msg2 = Slice()) {
39
+ return Status(kCorruption, msg, msg2);
40
+ }
41
+ static Status NotSupported(const Slice& msg, const Slice& msg2 = Slice()) {
42
+ return Status(kNotSupported, msg, msg2);
43
+ }
44
+ static Status InvalidArgument(const Slice& msg, const Slice& msg2 = Slice()) {
45
+ return Status(kInvalidArgument, msg, msg2);
46
+ }
47
+ static Status IOError(const Slice& msg, const Slice& msg2 = Slice()) {
48
+ return Status(kIOError, msg, msg2);
49
+ }
50
+
51
+ // Returns true iff the status indicates success.
52
+ bool ok() const { return (state_ == NULL); }
53
+
54
+ // Returns true iff the status indicates a NotFound error.
55
+ bool IsNotFound() const { return code() == kNotFound; }
56
+
57
+ // Return a string representation of this status suitable for printing.
58
+ // Returns the string "OK" for success.
59
+ std::string ToString() const;
60
+
61
+ private:
62
+ // OK status has a NULL state_. Otherwise, state_ is a new[] array
63
+ // of the following form:
64
+ // state_[0..3] == length of message
65
+ // state_[4] == code
66
+ // state_[5..] == message
67
+ const char* state_;
68
+
69
+ enum Code {
70
+ kOk = 0,
71
+ kNotFound = 1,
72
+ kCorruption = 2,
73
+ kNotSupported = 3,
74
+ kInvalidArgument = 4,
75
+ kIOError = 5,
76
+ };
77
+
78
+ Code code() const {
79
+ return (state_ == NULL) ? kOk : static_cast<Code>(state_[4]);
80
+ }
81
+
82
+ Status(Code code, const Slice& msg, const Slice& msg2);
83
+ static const char* CopyState(const char* s);
84
+ };
85
+
86
+ inline Status::Status(const Status& s) {
87
+ state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
88
+ }
89
+ inline void Status::operator=(const Status& s) {
90
+ // The following condition catches both aliasing (when this == &s),
91
+ // and the common case where both s and *this are ok.
92
+ if (state_ != s.state_) {
93
+ delete[] state_;
94
+ state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
95
+ }
96
+ }
97
+
98
+ }
99
+
100
+ #endif // STORAGE_LEVELDB_INCLUDE_STATUS_H_