Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 4 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 45 additions and 1 deletion.
  1. +20 −1 DB.cs
  2. +16 −0 tests/DBTests.cs
  3. +9 −0 tests/NativeTests.cs
View
21 DB.cs
@@ -37,6 +37,7 @@ namespace LevelDB
public class DB : IDisposable, IEnumerable<KeyValuePair<string, string>>
{
public IntPtr Handle { get; private set; }
+ bool Disposed { get; set; }
public string this[string key] {
get {
@@ -62,6 +63,12 @@ public DB(Options options, string path)
protected virtual void Dispose(bool disposing)
{
+ var disposed = Disposed;
+ if (disposed) {
+ return;
+ }
+ Disposed = true;
+
if (disposing) {
// free managed
}
@@ -86,6 +93,7 @@ public static DB Open(Options options, string path)
public void Put(WriteOptions options, string key, string value)
{
+ CheckDisposed();
if (options == null) {
options = new WriteOptions();
}
@@ -99,6 +107,7 @@ public void Put(string key, string value)
public void Delete(WriteOptions options, string key)
{
+ CheckDisposed();
if (options == null) {
options = new WriteOptions();
}
@@ -112,6 +121,7 @@ public void Delete(string key)
public string Get(ReadOptions options, string key)
{
+ CheckDisposed();
if (options == null) {
options = new ReadOptions();
}
@@ -120,7 +130,7 @@ public string Get(ReadOptions options, string key)
public string Get(string key)
{
- return Get (null, key);
+ return Get(null, key);
}
IEnumerator IEnumerable.GetEnumerator()
@@ -130,6 +140,7 @@ IEnumerator IEnumerable.GetEnumerator()
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
+ CheckDisposed();
var options = new ReadOptions();
IntPtr iter = IntPtr.Zero;
try {
@@ -148,5 +159,13 @@ IEnumerator IEnumerable.GetEnumerator()
}
}
}
+
+ void CheckDisposed()
+ {
+ if (!Disposed) {
+ return;
+ }
+ throw new ObjectDisposedException(this.GetType().Name);
+ }
}
}
View
16 tests/DBTests.cs
@@ -56,6 +56,7 @@ public void SetUp()
[TearDown]
public void TearDown()
{
+ Database.Dispose();
if (Directory.Exists(DatabasePath)) {
Directory.Delete(DatabasePath, true);
}
@@ -68,6 +69,21 @@ public void Constructor()
}
[Test]
+ public void Close()
+ {
+ // test double close
+ Database.Dispose();
+ }
+
+ [Test]
+ [ExpectedException(typeof(ObjectDisposedException))]
+ public void DisposeChecks()
+ {
+ Database.Dispose();
+ Database.Get("key1");
+ }
+
+ [Test]
public void Put()
{
Database.Put(null, "key1", "value1");
View
9 tests/NativeTests.cs
@@ -71,6 +71,15 @@ public void Open()
}
[Test]
+ public void Reopen()
+ {
+ Native.leveldb_close(Database);
+ var options = Native.leveldb_options_create();
+ Database = Native.leveldb_open(options, DatabasePath);
+ Native.leveldb_get(Database, options, "key1");
+ }
+
+ [Test]
public void Put()
{
var options = Native.leveldb_writeoptions_create();

No commit comments for this range

Something went wrong with that request. Please try again.