├── .gitattributes
├── .gitignore
├── LevelDB.net
├── Cache.cs
├── Comparator.cs
├── CompressionLevel.cs
├── DB.cs
├── DBExtensions.cs
├── Env.cs
├── Iterator.cs
├── LeastRecentlyUsedSet.cs
├── LevelDB.NET.csproj
├── LevelDB.NET.vpj
├── LevelDBHandle.cs
├── LevelDBInterop.cs
├── LevelDbFreeHandle.cs
├── Logger.cs
├── NativePointer.cs
├── Options.cs
├── PinnedSafeHandle.cs
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
├── ReadOptions.cs
├── Result.cs
├── SnapShot.cs
├── WriteBatch.cs
├── WriteOptions.cs
└── packages.config
├── LevelDB.sln
├── LevelDBUnitTests
├── LevelDBUnitTests.csproj
├── LevelDBUnitTests.vpj
├── Properties
│ └── AssemblyInfo.cs
└── Tests.cs
├── leveldbNative
├── AUTHORS
├── LICENSE
├── LevelDB.def
├── Makefile
├── NEWS
├── NativeLevelDB
│ ├── Exports.def
│ ├── NativeLevelDB.cpp
│ ├── NativeLevelDB.vcxproj
│ ├── ReadMe.txt
│ ├── dllmain.cpp
│ ├── stdafx.cpp
│ ├── stdafx.h
│ └── targetver.h
├── README
├── TODO
├── Test
│ ├── AssemblyInfo.cpp
│ ├── ReadMe.txt
│ ├── Stdafx.cpp
│ ├── Stdafx.h
│ ├── Test.cpp
│ ├── Test.h
│ ├── Test.vcxproj
│ └── resource.h
├── WINDOWS
├── build_detect_platform
├── db
│ ├── builder.cc
│ ├── builder.h
│ ├── c.cc
│ ├── c_test.c
│ ├── corruption_test.cc
│ ├── db_bench.cc
│ ├── db_impl.cc
│ ├── db_impl.h
│ ├── db_iter.cc
│ ├── db_iter.h
│ ├── db_test.cc
│ ├── dbformat.cc
│ ├── dbformat.h
│ ├── dbformat_test.cc
│ ├── extensions.cc
│ ├── filename.cc
│ ├── filename.h
│ ├── filename_test.cc
│ ├── log_format.h
│ ├── log_reader.cc
│ ├── log_reader.h
│ ├── log_test.cc
│ ├── log_writer.cc
│ ├── log_writer.h
│ ├── memtable.cc
│ ├── memtable.h
│ ├── repair.cc
│ ├── skiplist.h
│ ├── skiplist_test.cc
│ ├── snapshot.h
│ ├── table_cache.cc
│ ├── table_cache.h
│ ├── version_edit.cc
│ ├── version_edit.h
│ ├── version_edit_test.cc
│ ├── version_set.cc
│ ├── version_set.h
│ ├── version_set_test.cc
│ ├── write_batch.cc
│ ├── write_batch_internal.h
│ └── write_batch_test.cc
├── doc
│ ├── bench
│ │ ├── db_bench_sqlite3.cc
│ │ └── db_bench_tree_db.cc
│ ├── benchmark.html
│ ├── doc.css
│ ├── impl.html
│ ├── index.html
│ ├── log_format.txt
│ └── table_format.txt
├── include
│ └── leveldb
│ │ ├── c.h
│ │ ├── cache.h
│ │ ├── comparator.h
│ │ ├── db.h
│ │ ├── env.h
│ │ ├── extensions.h
│ │ ├── iterator.h
│ │ ├── options.h
│ │ ├── slice.h
│ │ ├── status.h
│ │ ├── table.h
│ │ ├── table_builder.h
│ │ └── write_batch.h
├── leveldb.vcxproj
├── pingme.txt
├── port
│ ├── README
│ ├── atomic_pointer.h
│ ├── port.h
│ ├── port_android.cc
│ ├── port_android.h
│ ├── port_example.h
│ ├── port_posix.cc
│ ├── port_posix.h
│ ├── port_win.cc
│ ├── port_win.h
│ ├── sha1_portable.cc
│ ├── sha1_portable.h
│ ├── sha1_test.cc
│ └── win
│ │ └── stdint.h
├── table
│ ├── block.cc
│ ├── block.h
│ ├── block_builder.cc
│ ├── block_builder.h
│ ├── format.cc
│ ├── format.h
│ ├── iterator.cc
│ ├── iterator_wrapper.h
│ ├── merger.cc
│ ├── merger.h
│ ├── table.cc
│ ├── table_builder.cc
│ ├── table_test.cc
│ ├── two_level_iterator.cc
│ └── two_level_iterator.h
└── util
│ ├── arena.cc
│ ├── arena.h
│ ├── arena_test.cc
│ ├── cache.cc
│ ├── cache_test.cc
│ ├── coding.cc
│ ├── coding.h
│ ├── coding_test.cc
│ ├── comparator.cc
│ ├── crc32c.cc
│ ├── crc32c.h
│ ├── crc32c_test.cc
│ ├── env.cc
│ ├── env_boost.cc
│ ├── env_posix.cc
│ ├── env_test.cc
│ ├── hash.cc
│ ├── hash.h
│ ├── histogram.cc
│ ├── histogram.h
│ ├── logging.cc
│ ├── logging.h
│ ├── mutexlock.h
│ ├── options.cc
│ ├── posix_logger.h
│ ├── random.h
│ ├── status.cc
│ ├── testharness.cc
│ ├── testharness.h
│ ├── testutil.cc
│ ├── testutil.h
│ ├── win_logger.cc
│ └── win_logger.h
└── readme.md
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 | *.sln merge=union
7 | *.csproj merge=union
8 | *.vbproj merge=union
9 | *.fsproj merge=union
10 | *.dbproj merge=union
11 |
12 | # Standard to msysgit
13 | *.doc diff=astextplain
14 | *.DOC diff=astextplain
15 | *.docx diff=astextplain
16 | *.DOCX diff=astextplain
17 | *.dot diff=astextplain
18 | *.DOT diff=astextplain
19 | *.pdf diff=astextplain
20 | *.PDF diff=astextplain
21 | *.rtf diff=astextplain
22 | *.RTF diff=astextplain
23 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | #################
2 | ## Eclipse
3 | #################
4 |
5 | *.pydevproject
6 | .project
7 | .metadata
8 | bin/
9 | tmp/
10 | *.tmp
11 | *.bak
12 | *.swp
13 | *~.nib
14 | local.properties
15 | .classpath
16 | .settings/
17 | .loadpath
18 |
19 | # External tool builders
20 | .externalToolBuilders/
21 |
22 | # Locally stored "Eclipse launch configurations"
23 | *.launch
24 |
25 | # CDT-specific
26 | .cproject
27 |
28 | # PDT-specific
29 | .buildpath
30 |
31 |
32 | #################
33 | ## Visual Studio
34 | #################
35 |
36 | ## Ignore Visual Studio temporary files, build results, and
37 | ## files generated by popular Visual Studio add-ons.
38 |
39 | # User-specific files
40 | *.suo
41 | *.user
42 | *.sln.docstates
43 |
44 | # Build results
45 | [Dd]ebug/
46 | [Rr]elease/
47 | *_i.c
48 | *_p.c
49 | *.ilk
50 | *.meta
51 | *.obj
52 | *.pch
53 | *.pdb
54 | *.pgc
55 | *.pgd
56 | *.rsp
57 | *.sbr
58 | *.tlb
59 | *.tli
60 | *.tlh
61 | *.tmp
62 | *.vspscc
63 | .builds
64 | *.dotCover
65 |
66 | ## TODO: If you have NuGet Package Restore enabled, uncomment this
67 | #packages/
68 |
69 | # Visual C++ cache files
70 | ipch/
71 | *.aps
72 | *.ncb
73 | *.opensdf
74 | *.sdf
75 |
76 | # Visual Studio profiler
77 | *.psess
78 | *.vsp
79 |
80 | # ReSharper is a .NET coding add-in
81 | _ReSharper*
82 |
83 | # Installshield output folder
84 | [Ee]xpress
85 |
86 | # DocProject is a documentation generator add-in
87 | DocProject/buildhelp/
88 | DocProject/Help/*.HxT
89 | DocProject/Help/*.HxC
90 | DocProject/Help/*.hhc
91 | DocProject/Help/*.hhk
92 | DocProject/Help/*.hhp
93 | DocProject/Help/Html2
94 | DocProject/Help/html
95 |
96 | # Click-Once directory
97 | publish
98 |
99 | # Others
100 | [Bb]in
101 | [Oo]bj
102 | sql
103 | TestResults
104 | *.Cache
105 | ClientBin
106 | stylecop.*
107 | ~$*
108 | *.dbmdl
109 | Generated_Code #added for RIA/Silverlight projects
110 |
111 | # Backup & report files from converting an old project file to a newer
112 | # Visual Studio version. Backup files are not needed, because we have git ;-)
113 | _UpgradeReport_Files/
114 | Backup*/
115 | UpgradeLog*.XML
116 |
117 |
118 |
119 | ############
120 | ## Windows
121 | ############
122 |
123 | # Windows image file caches
124 | Thumbs.db
125 |
126 | # Folder config file
127 | Desktop.ini
128 |
129 |
130 | #############
131 | ## Python
132 | #############
133 |
134 | *.py[co]
135 |
136 | # Packages
137 | *.egg
138 | *.egg-info
139 | dist
140 | build
141 | eggs
142 | parts
143 | bin
144 | var
145 | sdist
146 | develop-eggs
147 | .installed.cfg
148 |
149 | # Installer logs
150 | pip-log.txt
151 |
152 | # Unit test / coverage reports
153 | .coverage
154 | .tox
155 |
156 | #Translations
157 | *.mo
158 |
159 | #Mr Developer
160 | .mr.developer.cfg
161 |
162 | # Mac crap
163 | .DS_Store
164 |
--------------------------------------------------------------------------------
/LevelDB.net/Cache.cs:
--------------------------------------------------------------------------------
1 | namespace LevelDB
2 | {
3 | ///
4 | /// A Cache is an interface that maps keys to values. It has internal
5 | /// synchronization and may be safely accessed concurrently from
6 | /// multiple threads. It may automatically evict entries to make room
7 | /// for new entries. Values have a specified charge against the cache
8 | /// capacity. For example, a cache where the values are variable
9 | /// length strings, may use the length of the string as the charge for
10 | /// the string.
11 | ///
12 | /// A builtin cache implementation with a least-recently-used eviction
13 | /// policy is provided. Clients may use their own implementations if
14 | /// they want something more sophisticated (like scan-resistance, a
15 | /// custom eviction policy, variable cache sizing, etc.)
16 | ///
17 | public class Cache : LevelDBHandle
18 | {
19 | ///
20 | /// Create a new cache with a fixed size capacity. This implementation
21 | /// of Cache uses a LRU eviction policy.
22 | ///
23 | public Cache(int capacity)
24 | {
25 | this.Handle = LevelDBInterop.leveldb_cache_create_lru(capacity);
26 | }
27 |
28 | protected override void FreeUnManagedObjects()
29 | {
30 | LevelDBInterop.leveldb_cache_destroy(this.Handle);
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/LevelDB.net/CompressionLevel.cs:
--------------------------------------------------------------------------------
1 | namespace LevelDB
2 | {
3 | ///
4 | /// DB contents are stored in a set of blocks, each of which holds a
5 | /// sequence of key,value pairs. Each block may be compressed before
6 | /// being stored in a file. The following enum describes which
7 | /// compression method (if any) is used to compress a block.
8 | ///
9 | public enum CompressionLevel
10 | {
11 | NoCompression = 0,
12 | SnappyCompression = 1
13 | }
14 | }
--------------------------------------------------------------------------------
/LevelDB.net/DBExtensions.cs:
--------------------------------------------------------------------------------
1 | namespace LevelDB
2 | {
3 | public static class DBExtensions
4 | {
5 | public static void CopyToByteArray(this int source, byte[] destination, int offset)
6 | {
7 | //if (destination == null) throw new ArgumentException("Destination array cannot be null");
8 |
9 | // check if there is enough space for all the 4 bytes we will copy
10 | //if (destination.Length < offset + 4) throw new ArgumentException("Not enough room in the destination array");
11 |
12 | destination[offset] = (byte)(source >> 24); // fourth byte
13 | destination[offset + 1] = (byte)(source >> 16); // third byte
14 | destination[offset + 2] = (byte)(source >> 8); // second byte
15 | destination[offset + 3] = (byte)source; // last byte is already in proper position
16 | }
17 | }
18 | }
--------------------------------------------------------------------------------
/LevelDB.net/Env.cs:
--------------------------------------------------------------------------------
1 | namespace LevelDB
2 | {
3 | ///
4 | /// A default environment to access operating system functionality like
5 | /// the filesystem etc of the current operating system.
6 | ///
7 | public class Env : LevelDBHandle
8 | {
9 | public Env()
10 | {
11 | this.Handle = LevelDBInterop.leveldb_create_default_env();
12 | }
13 |
14 | protected override void FreeUnManagedObjects()
15 | {
16 | LevelDBInterop.leveldb_env_destroy(this.Handle);
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/LevelDB.net/LeastRecentlyUsedSet.cs:
--------------------------------------------------------------------------------
1 | #if false
2 | namespace LevelDB
3 | {
4 | public class LeastRecentlyUsedSet : IEnumerable
5 | {
6 | public delegate void WriteAction(Action add, Action remove);
7 |
8 | readonly LinkedList inOrder = new LinkedList();
9 | readonly int maxEntries;
10 | readonly ReaderWriterLockSlim readerWriterLock = new ReaderWriterLockSlim();
11 | readonly HashSet set;
12 |
13 | public LeastRecentlyUsedSet(IEqualityComparer comparer)
14 | : this(comparer, 100)
15 | {
16 | }
17 |
18 | public LeastRecentlyUsedSet(int maxEntries)
19 | : this(EqualityComparer.Default, maxEntries)
20 | {
21 | this.maxEntries = maxEntries;
22 | }
23 |
24 | public LeastRecentlyUsedSet(IEqualityComparer comparer, int maxEntries)
25 | {
26 | set = new HashSet(comparer);
27 | this.maxEntries = maxEntries;
28 | }
29 |
30 | public LeastRecentlyUsedSet()
31 | : this(EqualityComparer.Default)
32 | {
33 | }
34 |
35 | public IEnumerator GetEnumerator()
36 | {
37 | readerWriterLock.EnterReadLock();
38 | try
39 | {
40 | return set.ToList().GetEnumerator();
41 | }
42 | finally
43 | {
44 | readerWriterLock.ExitReadLock();
45 | }
46 | }
47 |
48 | IEnumerator IEnumerable.GetEnumerator()
49 | {
50 | return GetEnumerator();
51 | }
52 |
53 | public void Write(WriteAction action)
54 | {
55 | readerWriterLock.EnterWriteLock();
56 | try
57 | {
58 | action(Add, Remove);
59 | }
60 | finally
61 | {
62 | readerWriterLock.ExitWriteLock();
63 | }
64 | }
65 |
66 | private void Add(T item)
67 | {
68 | set.Add(item);
69 | inOrder.AddLast(item);
70 | if (inOrder.Count <= maxEntries)
71 | return;
72 |
73 | set.Remove(inOrder.First.Value);
74 | inOrder.RemoveFirst();
75 | }
76 |
77 | public bool Contains(T item)
78 | {
79 | readerWriterLock.EnterReadLock();
80 | try
81 | {
82 | return set.Contains(item);
83 | }
84 | finally
85 | {
86 | readerWriterLock.ExitReadLock();
87 | }
88 | }
89 |
90 | private void Remove(T item)
91 | {
92 | set.Remove(item);
93 | }
94 | }
95 | }
96 | #endif
--------------------------------------------------------------------------------
/LevelDB.net/LevelDB.NET.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | x86
6 | 8.0.30703
7 | 2.0
8 | {66B68F7D-BD4C-4080-9DB9-C0E03D58E171}
9 | Exe
10 | Properties
11 | LevelDB
12 | LevelDBTest
13 | v4.0
14 | Client
15 | 512
16 |
17 |
18 | x86
19 | true
20 | full
21 | false
22 | ..\Debug\
23 | DEBUG;TRACE
24 | prompt
25 | 4
26 | false
27 | false
28 | true
29 |
30 |
31 | x86
32 | pdbonly
33 | true
34 | ..\Release\
35 | TRACE
36 | prompt
37 | 4
38 | true
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 |
88 |
--------------------------------------------------------------------------------
/LevelDB.net/LevelDBHandle.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace LevelDB
4 | {
5 | ///
6 | /// Base class for all LevelDB objects
7 | /// Implement IDisposable as prescribed by http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx by overriding the two additional virtual methods
8 | ///
9 | public abstract class LevelDBHandle : IDisposable
10 | {
11 | public IntPtr Handle { protected set; get; }
12 |
13 | public void Dispose()
14 | {
15 | Dispose(true);
16 | GC.SuppressFinalize(this);
17 | }
18 |
19 | protected virtual void FreeManagedObjects()
20 | {
21 | }
22 |
23 | protected virtual void FreeUnManagedObjects()
24 | {
25 | }
26 |
27 | bool _disposed = false;
28 | void Dispose(bool disposing)
29 | {
30 | if (!_disposed)
31 | {
32 | if (disposing)
33 | {
34 | FreeManagedObjects();
35 | }
36 | if (this.Handle != IntPtr.Zero)
37 | {
38 | FreeUnManagedObjects();
39 | this.Handle = IntPtr.Zero;
40 | }
41 | _disposed = true;
42 | }
43 | }
44 |
45 | ~LevelDBHandle()
46 | {
47 | Dispose(false);
48 | }
49 | }
50 | }
--------------------------------------------------------------------------------
/LevelDB.net/LevelDbFreeHandle.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Runtime.ConstrainedExecution;
5 | using System.Runtime.InteropServices;
6 | using System.Text;
7 | using Microsoft.Win32.SafeHandles;
8 |
9 | namespace LevelDB
10 | {
11 | // Wraps pointers to be freed with leveldb_free (e.g. returned by leveldb_get)
12 | //
13 | // reference on safe handles: http://blogs.msdn.com/b/bclteam/archive/2006/06/23/644343.aspx
14 | internal class LevelDbFreeHandle : SafeHandle
15 | {
16 | public LevelDbFreeHandle()
17 | : base(default(IntPtr), true)
18 | {
19 | }
20 |
21 | [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
22 | override protected bool ReleaseHandle()
23 | {
24 | if (this.handle != default(IntPtr))
25 | LevelDBInterop.leveldb_free(this.handle);
26 | this.handle = default(IntPtr);
27 | return true;
28 | }
29 |
30 | public override bool IsInvalid
31 | {
32 | get { return this.handle != default(IntPtr); }
33 | }
34 |
35 | public new void SetHandle(IntPtr p)
36 | {
37 | if(this.handle != default(IntPtr))
38 | ReleaseHandle();
39 |
40 | base.SetHandle(p);
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/LevelDB.net/Logger.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace LevelDB
5 | {
6 | [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
7 | public delegate void Log(string msg);
8 |
9 | public class Logger : LevelDBHandle
10 | {
11 | public Logger(Log log)
12 | {
13 | var p = Marshal.GetFunctionPointerForDelegate(log);
14 | this.Handle = LevelDBInterop.leveldb_logger_create(p);
15 | }
16 |
17 | public static implicit operator Logger(Log log)
18 | {
19 | return new Logger(log);
20 | }
21 |
22 | protected override void FreeUnManagedObjects()
23 | {
24 | if (this.Handle != default(IntPtr))
25 | LevelDBInterop.leveldb_logger_destroy(this.Handle);
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/LevelDB.net/PinnedSafeHandle.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Runtime.InteropServices;
5 | using System.Text;
6 | using LevelDB.NativePointer;
7 |
8 | namespace LevelDB
9 | {
10 | internal class PinnedSafeHandle : SafeHandle
11 | where T : struct
12 | {
13 | private GCHandle pinnedRawData;
14 |
15 | public PinnedSafeHandle(T[] arr)
16 | : base(default(IntPtr), true)
17 | {
18 | pinnedRawData = GCHandle.Alloc(arr, GCHandleType.Pinned);
19 |
20 | // initialize handle last; ensure we only free initialized GCHandles.
21 | handle = pinnedRawData.AddrOfPinnedObject();
22 | }
23 |
24 | public Ptr Ptr
25 | {
26 | get { return (Ptr) handle; }
27 | }
28 |
29 | public override bool IsInvalid
30 | {
31 | get { return handle == default(IntPtr); }
32 | }
33 |
34 | protected override bool ReleaseHandle()
35 | {
36 | if (handle != default(IntPtr))
37 | {
38 | pinnedRawData.Free();
39 | handle = default(IntPtr);
40 | }
41 | return true;
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/LevelDB.net/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace LevelDB
5 | {
6 | class Program
7 | {
8 | static string testPath = @"C:\Temp\Test";
9 | static string CleanTestDB()
10 | {
11 | DB.Destroy(new Options { CreateIfMissing = true }, testPath);
12 | return testPath;
13 | }
14 |
15 | static void Main3()
16 | {
17 | var path = CleanTestDB();
18 |
19 | using (var db = new DB(new Options {CreateIfMissing = true}, path))
20 | {
21 | db.Put(1, new[] {2, 3}, new WriteOptions());
22 | db.Put(2, new[] {1, 2, 4});
23 | db.Put(3, new[] {1, 3});
24 | db.Put(4, new[] {2, 5, 7});
25 | db.Put(5, new[] {4, 6, 7, 8});
26 | db.Put(6, new[] {5});
27 | db.Put(7, new[] {4, 5, 8});
28 | db.Put(8, new[] {5, 7});
29 |
30 | var a = db.Get(1);
31 | var b = db.Get(2);
32 | var c = db.Get(3);
33 | var d = db.Get(4);
34 | var e = db.Get(5);
35 | var f = db.Get(6);
36 | var g = db.Get(7);
37 | var h = db.Get(8);
38 |
39 | }
40 | }
41 |
42 | static void Main()
43 | {
44 | var l = new Logger(s => Console.WriteLine(s));
45 | var x = new Options
46 | {
47 | CreateIfMissing = true,
48 | RestartInterval = 13,
49 | MaxOpenFiles = 100,
50 | InfoLog = l
51 | };
52 |
53 | var db = new DB(x, @"C:\Temp\A");
54 | db.Put("hello", "world");
55 | var world = db.Get("hello");
56 | Console.WriteLine(world);
57 |
58 | for (var j = 0; j < 5; j++)
59 | {
60 | var r = new Random(0);
61 | var data = "";
62 |
63 | for (int i = 0; i < 1024; i++)
64 | {
65 | data += 'a' + r.Next(26);
66 | }
67 | for (int i = 0; i < 5*1024; i++)
68 | {
69 | db.Put(string.Format("row{0}", i), data);
70 | }
71 | Thread.Sleep(100);
72 | }
73 | Console.WriteLine();
74 |
75 | //using(var logger = new Logger(Console.WriteLine))
76 | //{
77 | // Console.WriteLine("hello");
78 | //}
79 |
80 | db.Dispose();
81 | GC.KeepAlive(l);
82 | }
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/LevelDB.net/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("LevelDB")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("Microsoft")]
12 | [assembly: AssemblyProduct("LevelDB")]
13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("261f1bda-5190-43b2-a106-51c64b429f24")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
38 | [assembly: System.Runtime.CompilerServices.InternalsVisibleTo("LevelDBUnitTests")]
--------------------------------------------------------------------------------
/LevelDB.net/ReadOptions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Runtime.InteropServices;
6 | namespace LevelDB
7 | {
8 | ///
9 | /// Options that control read operations.
10 | ///
11 | public class ReadOptions : LevelDBHandle
12 | {
13 | public ReadOptions()
14 | {
15 | this.Handle = LevelDBInterop.leveldb_readoptions_create();
16 | }
17 |
18 | ///
19 | /// If true, all data read from underlying storage will be
20 | /// verified against corresponding checksums.
21 | ///
22 | public bool VerifyCheckSums
23 | {
24 | set { LevelDBInterop.leveldb_readoptions_set_verify_checksums(this.Handle, value ? (byte)1 : (byte)0); }
25 | }
26 |
27 | ///
28 | /// Should the data read for this iteration be cached in memory?
29 | /// Callers may wish to set this field to false for bulk scans.
30 | /// Default: true
31 | ///
32 | public bool FillCache
33 | {
34 | set { LevelDBInterop.leveldb_readoptions_set_fill_cache(this.Handle, value ? (byte)1 : (byte)0); }
35 | }
36 |
37 | ///
38 | /// If "snapshot" is provides, read as of the supplied snapshot
39 | /// (which must belong to the DB that is being read and which must
40 | /// not have been released).
41 | /// If "snapshot" is not set, use an implicit
42 | /// snapshot of the state at the beginning of this read operation.
43 | ///
44 | public SnapShot Snapshot
45 | {
46 | set { LevelDBInterop.leveldb_readoptions_set_snapshot(this.Handle, value.Handle); }
47 | }
48 |
49 | protected override void FreeUnManagedObjects()
50 | {
51 | LevelDBInterop.leveldb_readoptions_destroy(this.Handle);
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/LevelDB.net/Result.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 | namespace LevelDB
5 | {
6 | //public class Result : LevelDBHandle, IEnumerable, IEnumerable
7 | //{
8 | // private int length;
9 | // public Result(IntPtr handle, int length)
10 | // {
11 | // this.Handle = handle;
12 | // this.length = length;
13 |
14 | // BitConverter.ToInt32(null, 0);
15 | // }
16 |
17 |
18 | // public byte[] Get(byte[] key, ReadOptions options)
19 | // {
20 | // IntPtr error;
21 | // int length;
22 | // var v = LevelDBInterop.leveldb_get(this.Handle, options.Handle, key, key.Length, out length, out error);
23 | // Throw(error);
24 |
25 | // if (v != IntPtr.Zero)
26 | // {
27 | // var bytes = new byte[length];
28 | // Marshal.Copy(v, bytes, 0, length);
29 | // Marshal.FreeHGlobal(v);
30 | // return bytes;
31 | // }
32 | // return null;
33 | // }
34 | //}
35 | }
--------------------------------------------------------------------------------
/LevelDB.net/SnapShot.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace LevelDB
4 | {
5 | ///
6 | /// A Snapshot is an immutable object and can therefore be safely
7 | /// accessed from multiple threads without any external synchronization.
8 | ///
9 | public class SnapShot : LevelDBHandle
10 | {
11 | // pointer to parent so that we can call ReleaseSnapshot(this) when disposed
12 | public WeakReference Parent; // as DB
13 |
14 | internal SnapShot(IntPtr Handle, DB parent)
15 | {
16 | this.Handle = Handle;
17 | this.Parent = new WeakReference(parent);
18 | }
19 |
20 | internal SnapShot(IntPtr Handle)
21 | {
22 | this.Handle = Handle;
23 | Parent = new WeakReference(null);
24 | }
25 |
26 | protected override void FreeUnManagedObjects()
27 | {
28 | if (Parent.IsAlive)
29 | {
30 | var parent = Parent.Target as DB;
31 | if (parent != null) LevelDBInterop.leveldb_release_snapshot(parent.Handle, this.Handle);
32 | }
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/LevelDB.net/WriteBatch.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 |
4 | namespace LevelDB
5 | {
6 | ///
7 | /// WriteBatch holds a collection of updates to apply atomically to a DB.
8 | ///
9 | /// The updates are applied in the order in which they are added
10 | /// to the WriteBatch. For example, the value of "key" will be "v3"
11 | /// after the following batch is written:
12 | ///
13 | /// batch.Put("key", "v1");
14 | /// batch.Delete("key");
15 | /// batch.Put("key", "v2");
16 | /// batch.Put("key", "v3");
17 | ///
18 | public class WriteBatch : LevelDBHandle
19 | {
20 | public WriteBatch()
21 | {
22 | this.Handle = LevelDBInterop.leveldb_writebatch_create();
23 | }
24 |
25 | ///
26 | /// Clear all updates buffered in this batch.
27 | ///
28 | public void Clear()
29 | {
30 | LevelDBInterop.leveldb_writebatch_clear(this.Handle);
31 | }
32 |
33 | ///
34 | /// Store the mapping "key->value" in the database.
35 | ///
36 | public WriteBatch Put(string key, string value)
37 | {
38 | return Put(Encoding.ASCII.GetBytes(key), Encoding.ASCII.GetBytes(value));
39 | }
40 |
41 | ///
42 | /// Store the mapping "key->value" in the database.
43 | ///
44 | public WriteBatch Put(byte[] key, byte[] value)
45 | {
46 | LevelDBInterop.leveldb_writebatch_put(this.Handle, key, key.Length, value, value.Length);
47 | return this;
48 | }
49 |
50 | ///
51 | /// If the database contains a mapping for "key", erase it.
52 | /// Else do nothing.
53 | ///
54 | public WriteBatch Delete(string key)
55 | {
56 | return Delete(Encoding.ASCII.GetBytes(key));
57 | }
58 |
59 | ///
60 | /// If the database contains a mapping for "key", erase it.
61 | /// Else do nothing.
62 | ///
63 | public WriteBatch Delete(byte[] key)
64 | {
65 | LevelDBInterop.leveldb_writebatch_delete(this.Handle, key, key.Length);
66 | return this;
67 | }
68 |
69 | ///
70 | /// Support for iterating over a batch.
71 | ///
72 | public void Iterate(object state, Action