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,99 @@
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 Cache is an interface that maps keys to values. It has internal
6
+ // synchronization and may be safely accessed concurrently from
7
+ // multiple threads. It may automatically evict entries to make room
8
+ // for new entries. Values have a specified charge against the cache
9
+ // capacity. For example, a cache where the values are variable
10
+ // length strings, may use the length of the string as the charge for
11
+ // the string.
12
+ //
13
+ // A builtin cache implementation with a least-recently-used eviction
14
+ // policy is provided. Clients may use their own implementations if
15
+ // they want something more sophisticated (like scan-resistance, a
16
+ // custom eviction policy, variable cache sizing, etc.)
17
+
18
+ #ifndef STORAGE_LEVELDB_INCLUDE_CACHE_H_
19
+ #define STORAGE_LEVELDB_INCLUDE_CACHE_H_
20
+
21
+ #include <stdint.h>
22
+ #include "leveldb/slice.h"
23
+
24
+ namespace leveldb {
25
+
26
+ class Cache;
27
+
28
+ // Create a new cache with a fixed size capacity. This implementation
29
+ // of Cache uses a least-recently-used eviction policy.
30
+ extern Cache* NewLRUCache(size_t capacity);
31
+
32
+ class Cache {
33
+ public:
34
+ Cache() { }
35
+
36
+ // Destroys all existing entries by calling the "deleter"
37
+ // function that was passed to the constructor.
38
+ virtual ~Cache();
39
+
40
+ // Opaque handle to an entry stored in the cache.
41
+ struct Handle { };
42
+
43
+ // Insert a mapping from key->value into the cache and assign it
44
+ // the specified charge against the total cache capacity.
45
+ //
46
+ // Returns a handle that corresponds to the mapping. The caller
47
+ // must call this->Release(handle) when the returned mapping is no
48
+ // longer needed.
49
+ //
50
+ // When the inserted entry is no longer needed, the key and
51
+ // value will be passed to "deleter".
52
+ virtual Handle* Insert(const Slice& key, void* value, size_t charge,
53
+ void (*deleter)(const Slice& key, void* value)) = 0;
54
+
55
+ // If the cache has no mapping for "key", returns NULL.
56
+ //
57
+ // Else return a handle that corresponds to the mapping. The caller
58
+ // must call this->Release(handle) when the returned mapping is no
59
+ // longer needed.
60
+ virtual Handle* Lookup(const Slice& key) = 0;
61
+
62
+ // Release a mapping returned by a previous Lookup().
63
+ // REQUIRES: handle must not have been released yet.
64
+ // REQUIRES: handle must have been returned by a method on *this.
65
+ virtual void Release(Handle* handle) = 0;
66
+
67
+ // Return the value encapsulated in a handle returned by a
68
+ // successful Lookup().
69
+ // REQUIRES: handle must not have been released yet.
70
+ // REQUIRES: handle must have been returned by a method on *this.
71
+ virtual void* Value(Handle* handle) = 0;
72
+
73
+ // If the cache contains entry for key, erase it. Note that the
74
+ // underlying entry will be kept around until all existing handles
75
+ // to it have been released.
76
+ virtual void Erase(const Slice& key) = 0;
77
+
78
+ // Return a new numeric id. May be used by multiple clients who are
79
+ // sharing the same cache to partition the key space. Typically the
80
+ // client will allocate a new id at startup and prepend the id to
81
+ // its cache keys.
82
+ virtual uint64_t NewId() = 0;
83
+
84
+ private:
85
+ void LRU_Remove(Handle* e);
86
+ void LRU_Append(Handle* e);
87
+ void Unref(Handle* e);
88
+
89
+ struct Rep;
90
+ Rep* rep_;
91
+
92
+ // No copying allowed
93
+ Cache(const Cache&);
94
+ void operator=(const Cache&);
95
+ };
96
+
97
+ } // namespace leveldb
98
+
99
+ #endif // STORAGE_LEVELDB_UTIL_CACHE_H_
@@ -0,0 +1,63 @@
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_COMPARATOR_H_
6
+ #define STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_
7
+
8
+ #include <string>
9
+
10
+ namespace leveldb {
11
+
12
+ class Slice;
13
+
14
+ // A Comparator object provides a total order across slices that are
15
+ // used as keys in an sstable or a database. A Comparator implementation
16
+ // must be thread-safe since leveldb may invoke its methods concurrently
17
+ // from multiple threads.
18
+ class Comparator {
19
+ public:
20
+ virtual ~Comparator();
21
+
22
+ // Three-way comparison. Returns value:
23
+ // < 0 iff "a" < "b",
24
+ // == 0 iff "a" == "b",
25
+ // > 0 iff "a" > "b"
26
+ virtual int Compare(const Slice& a, const Slice& b) const = 0;
27
+
28
+ // The name of the comparator. Used to check for comparator
29
+ // mismatches (i.e., a DB created with one comparator is
30
+ // accessed using a different comparator.
31
+ //
32
+ // The client of this package should switch to a new name whenever
33
+ // the comparator implementation changes in a way that will cause
34
+ // the relative ordering of any two keys to change.
35
+ //
36
+ // Names starting with "leveldb." are reserved and should not be used
37
+ // by any clients of this package.
38
+ virtual const char* Name() const = 0;
39
+
40
+ // Advanced functions: these are used to reduce the space requirements
41
+ // for internal data structures like index blocks.
42
+
43
+ // If *start < limit, changes *start to a short string in [start,limit).
44
+ // Simple comparator implementations may return with *start unchanged,
45
+ // i.e., an implementation of this method that does nothing is correct.
46
+ virtual void FindShortestSeparator(
47
+ std::string* start,
48
+ const Slice& limit) const = 0;
49
+
50
+ // Changes *key to a short string >= *key.
51
+ // Simple comparator implementations may return with *key unchanged,
52
+ // i.e., an implementation of this method that does nothing is correct.
53
+ virtual void FindShortSuccessor(std::string* key) const = 0;
54
+ };
55
+
56
+ // Return a builtin comparator that uses lexicographic byte-wise
57
+ // ordering. The result remains the property of this module and
58
+ // must not be deleted.
59
+ extern const Comparator* BytewiseComparator();
60
+
61
+ } // namespace leveldb
62
+
63
+ #endif // STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_
@@ -0,0 +1,161 @@
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_DB_H_
6
+ #define STORAGE_LEVELDB_INCLUDE_DB_H_
7
+
8
+ #include <stdint.h>
9
+ #include <stdio.h>
10
+ #include "leveldb/iterator.h"
11
+ #include "leveldb/options.h"
12
+
13
+ namespace leveldb {
14
+
15
+ // Update Makefile if you change these
16
+ static const int kMajorVersion = 1;
17
+ static const int kMinorVersion = 4;
18
+
19
+ struct Options;
20
+ struct ReadOptions;
21
+ struct WriteOptions;
22
+ class WriteBatch;
23
+
24
+ // Abstract handle to particular state of a DB.
25
+ // A Snapshot is an immutable object and can therefore be safely
26
+ // accessed from multiple threads without any external synchronization.
27
+ class Snapshot {
28
+ protected:
29
+ virtual ~Snapshot();
30
+ };
31
+
32
+ // A range of keys
33
+ struct Range {
34
+ Slice start; // Included in the range
35
+ Slice limit; // Not included in the range
36
+
37
+ Range() { }
38
+ Range(const Slice& s, const Slice& l) : start(s), limit(l) { }
39
+ };
40
+
41
+ // A DB is a persistent ordered map from keys to values.
42
+ // A DB is safe for concurrent access from multiple threads without
43
+ // any external synchronization.
44
+ class DB {
45
+ public:
46
+ // Open the database with the specified "name".
47
+ // Stores a pointer to a heap-allocated database in *dbptr and returns
48
+ // OK on success.
49
+ // Stores NULL in *dbptr and returns a non-OK status on error.
50
+ // Caller should delete *dbptr when it is no longer needed.
51
+ static Status Open(const Options& options,
52
+ const std::string& name,
53
+ DB** dbptr);
54
+
55
+ DB() { }
56
+ virtual ~DB();
57
+
58
+ // Set the database entry for "key" to "value". Returns OK on success,
59
+ // and a non-OK status on error.
60
+ // Note: consider setting options.sync = true.
61
+ virtual Status Put(const WriteOptions& options,
62
+ const Slice& key,
63
+ const Slice& value) = 0;
64
+
65
+ // Remove the database entry (if any) for "key". Returns OK on
66
+ // success, and a non-OK status on error. It is not an error if "key"
67
+ // did not exist in the database.
68
+ // Note: consider setting options.sync = true.
69
+ virtual Status Delete(const WriteOptions& options, const Slice& key) = 0;
70
+
71
+ // Apply the specified updates to the database.
72
+ // Returns OK on success, non-OK on failure.
73
+ // Note: consider setting options.sync = true.
74
+ virtual Status Write(const WriteOptions& options, WriteBatch* updates) = 0;
75
+
76
+ // If the database contains an entry for "key" store the
77
+ // corresponding value in *value and return OK.
78
+ //
79
+ // If there is no entry for "key" leave *value unchanged and return
80
+ // a status for which Status::IsNotFound() returns true.
81
+ //
82
+ // May return some other Status on an error.
83
+ virtual Status Get(const ReadOptions& options,
84
+ const Slice& key, std::string* value) = 0;
85
+
86
+ // Return a heap-allocated iterator over the contents of the database.
87
+ // The result of NewIterator() is initially invalid (caller must
88
+ // call one of the Seek methods on the iterator before using it).
89
+ //
90
+ // Caller should delete the iterator when it is no longer needed.
91
+ // The returned iterator should be deleted before this db is deleted.
92
+ virtual Iterator* NewIterator(const ReadOptions& options) = 0;
93
+
94
+ // Return a handle to the current DB state. Iterators created with
95
+ // this handle will all observe a stable snapshot of the current DB
96
+ // state. The caller must call ReleaseSnapshot(result) when the
97
+ // snapshot is no longer needed.
98
+ virtual const Snapshot* GetSnapshot() = 0;
99
+
100
+ // Release a previously acquired snapshot. The caller must not
101
+ // use "snapshot" after this call.
102
+ virtual void ReleaseSnapshot(const Snapshot* snapshot) = 0;
103
+
104
+ // DB implementations can export properties about their state
105
+ // via this method. If "property" is a valid property understood by this
106
+ // DB implementation, fills "*value" with its current value and returns
107
+ // true. Otherwise returns false.
108
+ //
109
+ //
110
+ // Valid property names include:
111
+ //
112
+ // "leveldb.num-files-at-level<N>" - return the number of files at level <N>,
113
+ // where <N> is an ASCII representation of a level number (e.g. "0").
114
+ // "leveldb.stats" - returns a multi-line string that describes statistics
115
+ // about the internal operation of the DB.
116
+ // "leveldb.sstables" - returns a multi-line string that describes all
117
+ // of the sstables that make up the db contents.
118
+ virtual bool GetProperty(const Slice& property, std::string* value) = 0;
119
+
120
+ // For each i in [0,n-1], store in "sizes[i]", the approximate
121
+ // file system space used by keys in "[range[i].start .. range[i].limit)".
122
+ //
123
+ // Note that the returned sizes measure file system space usage, so
124
+ // if the user data compresses by a factor of ten, the returned
125
+ // sizes will be one-tenth the size of the corresponding user data size.
126
+ //
127
+ // The results may not include the sizes of recently written data.
128
+ virtual void GetApproximateSizes(const Range* range, int n,
129
+ uint64_t* sizes) = 0;
130
+
131
+ // Compact the underlying storage for the key range [*begin,*end].
132
+ // In particular, deleted and overwritten versions are discarded,
133
+ // and the data is rearranged to reduce the cost of operations
134
+ // needed to access the data. This operation should typically only
135
+ // be invoked by users who understand the underlying implementation.
136
+ //
137
+ // begin==NULL is treated as a key before all keys in the database.
138
+ // end==NULL is treated as a key after all keys in the database.
139
+ // Therefore the following call will compact the entire database:
140
+ // db->CompactRange(NULL, NULL);
141
+ virtual void CompactRange(const Slice* begin, const Slice* end) = 0;
142
+
143
+ private:
144
+ // No copying allowed
145
+ DB(const DB&);
146
+ void operator=(const DB&);
147
+ };
148
+
149
+ // Destroy the contents of the specified database.
150
+ // Be very careful using this method.
151
+ Status DestroyDB(const std::string& name, const Options& options);
152
+
153
+ // If a DB cannot be opened, you may attempt to call this method to
154
+ // resurrect as much of the contents of the database as possible.
155
+ // Some data may be lost, so be careful when calling this function
156
+ // on a database that contains important information.
157
+ Status RepairDB(const std::string& dbname, const Options& options);
158
+
159
+ } // namespace leveldb
160
+
161
+ #endif // STORAGE_LEVELDB_INCLUDE_DB_H_
@@ -0,0 +1,323 @@
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 Env is an interface used by the leveldb implementation to access
6
+ // operating system functionality like the filesystem etc. Callers
7
+ // may wish to provide a custom Env object when opening a database to
8
+ // get fine gain control; e.g., to rate limit file system operations.
9
+ //
10
+ // All Env implementations are safe for concurrent access from
11
+ // multiple threads without any external synchronization.
12
+
13
+ #ifndef STORAGE_LEVELDB_INCLUDE_ENV_H_
14
+ #define STORAGE_LEVELDB_INCLUDE_ENV_H_
15
+
16
+ #include <cstdarg>
17
+ #include <string>
18
+ #include <vector>
19
+ #include <stdint.h>
20
+ #include "leveldb/status.h"
21
+
22
+ namespace leveldb {
23
+
24
+ class FileLock;
25
+ class Logger;
26
+ class RandomAccessFile;
27
+ class SequentialFile;
28
+ class Slice;
29
+ class WritableFile;
30
+
31
+ class Env {
32
+ public:
33
+ Env() { }
34
+ virtual ~Env();
35
+
36
+ // Return a default environment suitable for the current operating
37
+ // system. Sophisticated users may wish to provide their own Env
38
+ // implementation instead of relying on this default environment.
39
+ //
40
+ // The result of Default() belongs to leveldb and must never be deleted.
41
+ static Env* Default();
42
+
43
+ // Create a brand new sequentially-readable file with the specified name.
44
+ // On success, stores a pointer to the new file in *result and returns OK.
45
+ // On failure stores NULL in *result and returns non-OK. If the file does
46
+ // not exist, returns a non-OK status.
47
+ //
48
+ // The returned file will only be accessed by one thread at a time.
49
+ virtual Status NewSequentialFile(const std::string& fname,
50
+ SequentialFile** result) = 0;
51
+
52
+ // Create a brand new random access read-only file with the
53
+ // specified name. On success, stores a pointer to the new file in
54
+ // *result and returns OK. On failure stores NULL in *result and
55
+ // returns non-OK. If the file does not exist, returns a non-OK
56
+ // status.
57
+ //
58
+ // The returned file may be concurrently accessed by multiple threads.
59
+ virtual Status NewRandomAccessFile(const std::string& fname,
60
+ RandomAccessFile** result) = 0;
61
+
62
+ // Create an object that writes to a new file with the specified
63
+ // name. Deletes any existing file with the same name and creates a
64
+ // new file. On success, stores a pointer to the new file in
65
+ // *result and returns OK. On failure stores NULL in *result and
66
+ // returns non-OK.
67
+ //
68
+ // The returned file will only be accessed by one thread at a time.
69
+ virtual Status NewWritableFile(const std::string& fname,
70
+ WritableFile** result) = 0;
71
+
72
+ // Returns true iff the named file exists.
73
+ virtual bool FileExists(const std::string& fname) = 0;
74
+
75
+ // Store in *result the names of the children of the specified directory.
76
+ // The names are relative to "dir".
77
+ // Original contents of *results are dropped.
78
+ virtual Status GetChildren(const std::string& dir,
79
+ std::vector<std::string>* result) = 0;
80
+
81
+ // Delete the named file.
82
+ virtual Status DeleteFile(const std::string& fname) = 0;
83
+
84
+ // Create the specified directory.
85
+ virtual Status CreateDir(const std::string& dirname) = 0;
86
+
87
+ // Delete the specified directory.
88
+ virtual Status DeleteDir(const std::string& dirname) = 0;
89
+
90
+ // Store the size of fname in *file_size.
91
+ virtual Status GetFileSize(const std::string& fname, uint64_t* file_size) = 0;
92
+
93
+ // Rename file src to target.
94
+ virtual Status RenameFile(const std::string& src,
95
+ const std::string& target) = 0;
96
+
97
+ // Lock the specified file. Used to prevent concurrent access to
98
+ // the same db by multiple processes. On failure, stores NULL in
99
+ // *lock and returns non-OK.
100
+ //
101
+ // On success, stores a pointer to the object that represents the
102
+ // acquired lock in *lock and returns OK. The caller should call
103
+ // UnlockFile(*lock) to release the lock. If the process exits,
104
+ // the lock will be automatically released.
105
+ //
106
+ // If somebody else already holds the lock, finishes immediately
107
+ // with a failure. I.e., this call does not wait for existing locks
108
+ // to go away.
109
+ //
110
+ // May create the named file if it does not already exist.
111
+ virtual Status LockFile(const std::string& fname, FileLock** lock) = 0;
112
+
113
+ // Release the lock acquired by a previous successful call to LockFile.
114
+ // REQUIRES: lock was returned by a successful LockFile() call
115
+ // REQUIRES: lock has not already been unlocked.
116
+ virtual Status UnlockFile(FileLock* lock) = 0;
117
+
118
+ // Arrange to run "(*function)(arg)" once in a background thread.
119
+ //
120
+ // "function" may run in an unspecified thread. Multiple functions
121
+ // added to the same Env may run concurrently in different threads.
122
+ // I.e., the caller may not assume that background work items are
123
+ // serialized.
124
+ virtual void Schedule(
125
+ void (*function)(void* arg),
126
+ void* arg) = 0;
127
+
128
+ // Start a new thread, invoking "function(arg)" within the new thread.
129
+ // When "function(arg)" returns, the thread will be destroyed.
130
+ virtual void StartThread(void (*function)(void* arg), void* arg) = 0;
131
+
132
+ // *path is set to a temporary directory that can be used for testing. It may
133
+ // or many not have just been created. The directory may or may not differ
134
+ // between runs of the same process, but subsequent calls will return the
135
+ // same directory.
136
+ virtual Status GetTestDirectory(std::string* path) = 0;
137
+
138
+ // Create and return a log file for storing informational messages.
139
+ virtual Status NewLogger(const std::string& fname, Logger** result) = 0;
140
+
141
+ // Returns the number of micro-seconds since some fixed point in time. Only
142
+ // useful for computing deltas of time.
143
+ virtual uint64_t NowMicros() = 0;
144
+
145
+ // Sleep/delay the thread for the perscribed number of micro-seconds.
146
+ virtual void SleepForMicroseconds(int micros) = 0;
147
+
148
+ private:
149
+ // No copying allowed
150
+ Env(const Env&);
151
+ void operator=(const Env&);
152
+ };
153
+
154
+ // A file abstraction for reading sequentially through a file
155
+ class SequentialFile {
156
+ public:
157
+ SequentialFile() { }
158
+ virtual ~SequentialFile();
159
+
160
+ // Read up to "n" bytes from the file. "scratch[0..n-1]" may be
161
+ // written by this routine. Sets "*result" to the data that was
162
+ // read (including if fewer than "n" bytes were successfully read).
163
+ // May set "*result" to point at data in "scratch[0..n-1]", so
164
+ // "scratch[0..n-1]" must be live when "*result" is used.
165
+ // If an error was encountered, returns a non-OK status.
166
+ //
167
+ // REQUIRES: External synchronization
168
+ virtual Status Read(size_t n, Slice* result, char* scratch) = 0;
169
+
170
+ // Skip "n" bytes from the file. This is guaranteed to be no
171
+ // slower that reading the same data, but may be faster.
172
+ //
173
+ // If end of file is reached, skipping will stop at the end of the
174
+ // file, and Skip will return OK.
175
+ //
176
+ // REQUIRES: External synchronization
177
+ virtual Status Skip(uint64_t n) = 0;
178
+ };
179
+
180
+ // A file abstraction for randomly reading the contents of a file.
181
+ class RandomAccessFile {
182
+ public:
183
+ RandomAccessFile() { }
184
+ virtual ~RandomAccessFile();
185
+
186
+ // Read up to "n" bytes from the file starting at "offset".
187
+ // "scratch[0..n-1]" may be written by this routine. Sets "*result"
188
+ // to the data that was read (including if fewer than "n" bytes were
189
+ // successfully read). May set "*result" to point at data in
190
+ // "scratch[0..n-1]", so "scratch[0..n-1]" must be live when
191
+ // "*result" is used. If an error was encountered, returns a non-OK
192
+ // status.
193
+ //
194
+ // Safe for concurrent use by multiple threads.
195
+ virtual Status Read(uint64_t offset, size_t n, Slice* result,
196
+ char* scratch) const = 0;
197
+ };
198
+
199
+ // A file abstraction for sequential writing. The implementation
200
+ // must provide buffering since callers may append small fragments
201
+ // at a time to the file.
202
+ class WritableFile {
203
+ public:
204
+ WritableFile() { }
205
+ virtual ~WritableFile();
206
+
207
+ virtual Status Append(const Slice& data) = 0;
208
+ virtual Status Close() = 0;
209
+ virtual Status Flush() = 0;
210
+ virtual Status Sync() = 0;
211
+
212
+ private:
213
+ // No copying allowed
214
+ WritableFile(const WritableFile&);
215
+ void operator=(const WritableFile&);
216
+ };
217
+
218
+ // An interface for writing log messages.
219
+ class Logger {
220
+ public:
221
+ Logger() { }
222
+ virtual ~Logger();
223
+
224
+ // Write an entry to the log file with the specified format.
225
+ virtual void Logv(const char* format, va_list ap) = 0;
226
+
227
+ private:
228
+ // No copying allowed
229
+ Logger(const Logger&);
230
+ void operator=(const Logger&);
231
+ };
232
+
233
+
234
+ // Identifies a locked file.
235
+ class FileLock {
236
+ public:
237
+ FileLock() { }
238
+ virtual ~FileLock();
239
+ private:
240
+ // No copying allowed
241
+ FileLock(const FileLock&);
242
+ void operator=(const FileLock&);
243
+ };
244
+
245
+ // Log the specified data to *info_log if info_log is non-NULL.
246
+ extern void Log(Logger* info_log, const char* format, ...)
247
+ # if defined(__GNUC__) || defined(__clang__)
248
+ __attribute__((__format__ (__printf__, 2, 3)))
249
+ # endif
250
+ ;
251
+
252
+ // A utility routine: write "data" to the named file.
253
+ extern Status WriteStringToFile(Env* env, const Slice& data,
254
+ const std::string& fname);
255
+
256
+ // A utility routine: read contents of named file into *data
257
+ extern Status ReadFileToString(Env* env, const std::string& fname,
258
+ std::string* data);
259
+
260
+ // An implementation of Env that forwards all calls to another Env.
261
+ // May be useful to clients who wish to override just part of the
262
+ // functionality of another Env.
263
+ class EnvWrapper : public Env {
264
+ public:
265
+ // Initialize an EnvWrapper that delegates all calls to *t
266
+ explicit EnvWrapper(Env* t) : target_(t) { }
267
+ virtual ~EnvWrapper();
268
+
269
+ // Return the target to which this Env forwards all calls
270
+ Env* target() const { return target_; }
271
+
272
+ // The following text is boilerplate that forwards all methods to target()
273
+ Status NewSequentialFile(const std::string& f, SequentialFile** r) {
274
+ return target_->NewSequentialFile(f, r);
275
+ }
276
+ Status NewRandomAccessFile(const std::string& f, RandomAccessFile** r) {
277
+ return target_->NewRandomAccessFile(f, r);
278
+ }
279
+ Status NewWritableFile(const std::string& f, WritableFile** r) {
280
+ return target_->NewWritableFile(f, r);
281
+ }
282
+ bool FileExists(const std::string& f) { return target_->FileExists(f); }
283
+ Status GetChildren(const std::string& dir, std::vector<std::string>* r) {
284
+ return target_->GetChildren(dir, r);
285
+ }
286
+ Status DeleteFile(const std::string& f) { return target_->DeleteFile(f); }
287
+ Status CreateDir(const std::string& d) { return target_->CreateDir(d); }
288
+ Status DeleteDir(const std::string& d) { return target_->DeleteDir(d); }
289
+ Status GetFileSize(const std::string& f, uint64_t* s) {
290
+ return target_->GetFileSize(f, s);
291
+ }
292
+ Status RenameFile(const std::string& s, const std::string& t) {
293
+ return target_->RenameFile(s, t);
294
+ }
295
+ Status LockFile(const std::string& f, FileLock** l) {
296
+ return target_->LockFile(f, l);
297
+ }
298
+ Status UnlockFile(FileLock* l) { return target_->UnlockFile(l); }
299
+ void Schedule(void (*f)(void*), void* a) {
300
+ return target_->Schedule(f, a);
301
+ }
302
+ void StartThread(void (*f)(void*), void* a) {
303
+ return target_->StartThread(f, a);
304
+ }
305
+ virtual Status GetTestDirectory(std::string* path) {
306
+ return target_->GetTestDirectory(path);
307
+ }
308
+ virtual Status NewLogger(const std::string& fname, Logger** result) {
309
+ return target_->NewLogger(fname, result);
310
+ }
311
+ uint64_t NowMicros() {
312
+ return target_->NowMicros();
313
+ }
314
+ void SleepForMicroseconds(int micros) {
315
+ target_->SleepForMicroseconds(micros);
316
+ }
317
+ private:
318
+ Env* target_;
319
+ };
320
+
321
+ } // namespace leveldb
322
+
323
+ #endif // STORAGE_LEVELDB_INCLUDE_ENV_H_