forked from meebey/leveldb-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB.cs
278 lines (253 loc) · 9.32 KB
/
DB.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// leveldb-sharp
//
// Copyright (c) 2011 The LevelDB Authors
// Copyright (c) 2012-2013, Mirco Bauer <meebey@meebey.net>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System;
using System.Collections;
using System.Collections.Generic;
namespace LevelDB
{
/// <remarks>
/// This type is thread safe.
///
/// Concurrency:
/// A database may only be opened by one process at a time. The leveldb
/// implementation acquires a lock from the operating system to prevent
/// misuse. Within a single process, the same DB object may be safely
/// shared by multiple concurrent threads. I.e., different threads may
/// write into or fetch iterators or call Get on the same database without
/// any external synchronization (the leveldb implementation will
/// automatically do the required synchronization). However other objects
/// (like Iterator and WriteBatch) may require external synchronization.
/// If two threads share such an object, they must protect access to it
/// using their own locking protocol.
/// </remarks>
public class DB : IDisposable, IEnumerable<KeyValuePair<string, string>>
{
/// <summary>
/// Native handle
/// </summary>
public IntPtr Handle { get; private set; }
Options Options { get; set; }
bool Disposed { get; set; }
public string this[string key] {
get {
return Get(null, key);
}
set {
Put(null, key, value);
}
}
public DB(Options options, string path)
{
if (options == null) {
options = new Options();
}
// keep a reference to options as it might contain a cache object
// which needs to stay alive as long as the DB is not closed
Options = options;
Handle = Native.leveldb_open(options.Handle, path);
}
~DB()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
var disposed = Disposed;
if (disposed) {
return;
}
Disposed = true;
if (disposing) {
// free managed
Options = null;
}
// free unmanaged
var handle = Handle;
if (handle != IntPtr.Zero) {
Handle = IntPtr.Zero;
Native.leveldb_close(handle);
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public static DB Open(Options options, string path)
{
return new DB(options, path);
}
public static void Repair(Options options, string path)
{
if (path == null) {
throw new ArgumentNullException("path");
}
if (options == null) {
options = new Options();
}
Native.leveldb_repair_db(options.Handle, path);
}
public static void Destroy(Options options, string path)
{
if (path == null) {
throw new ArgumentNullException("path");
}
if (options == null) {
options = new Options();
}
Native.leveldb_destroy_db(options.Handle, path);
}
public void Put(WriteOptions options, string key, string value)
{
CheckDisposed();
if (options == null) {
options = new WriteOptions();
}
Native.leveldb_put(Handle, options.Handle, key, value);
}
public void Put(string key, string value)
{
Put(null, key, value);
}
public void Delete(WriteOptions options, string key)
{
CheckDisposed();
if (options == null) {
options = new WriteOptions();
}
Native.leveldb_delete(Handle, options.Handle, key);
}
public void Delete(string key)
{
Delete(null, key);
}
public void Write(WriteOptions writeOptions, WriteBatch writeBatch)
{
CheckDisposed();
if (writeOptions == null) {
writeOptions = new WriteOptions();
}
if (writeBatch == null) {
throw new ArgumentNullException("writeBatch");
}
Native.leveldb_write(Handle, writeOptions.Handle, writeBatch.Handle);
}
public void Write(WriteBatch writeBatch)
{
Write(null, writeBatch);
}
public string Get(ReadOptions options, string key)
{
CheckDisposed();
if (options == null) {
options = new ReadOptions();
}
return Native.leveldb_get(Handle, options.Handle, key);
}
public string Get(string key)
{
return Get(null, key);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
CheckDisposed();
return new Iterator(this, null);
}
public Snapshot CreateSnapshot()
{
CheckDisposed();
return new Snapshot(this);
}
/// <summary>
/// Compacts the entire database.
/// </summary>
/// <seealso cref="M:DB.CompactRange"/>
public void Compact()
{
CompactRange(null, null);
}
/// <summary>
/// Compact the underlying storage for the key range [startKey,limitKey].
/// In particular, deleted and overwritten versions are discarded,
/// and the data is rearranged to reduce the cost of operations
/// needed to access the data. This operation should typically only
/// be invoked by users who understand the underlying implementation.
/// </summary>
/// <remarks>
/// CompactRange(null, null) will compact the entire database
/// </remarks>
/// <param name="startKey">
/// null is treated as a key before all keys in the database
/// </param>
/// <param name="limitKey">
/// null is treated as a key after all keys in the database
/// </param>
public void CompactRange(string startKey, string limitKey)
{
CheckDisposed();
Native.leveldb_compact_range(Handle, startKey, limitKey);
}
/// <summary>
/// DB implementations can export properties about their state via this
/// method. If "property" is a valid property understood by this DB
/// implementation, it returns its current value. Otherwise it returns
/// null.
///
/// Valid property names include:
/// "leveldb.num-files-at-level<N>" - return the number of files at level <N>,
/// where <N> is an ASCII representation of a level number (e.g. "0").
/// "leveldb.stats" - returns a multi-line string that describes statistics
/// about the internal operation of the DB.
/// "leveldb.sstables" - returns a multi-line string that describes all
/// of the sstables that make up the db contents.
/// </summary>
public string GetProperty(string property)
{
CheckDisposed();
if (property == null) {
throw new ArgumentNullException("property");
}
return Native.leveldb_property_value(Handle, property);
}
void CheckDisposed()
{
if (!Disposed) {
return;
}
throw new ObjectDisposedException(this.GetType().Name);
}
}
}