Skip to content

Commit 21c452e

Browse files
committed
Adding more test coverage and fixing minor issues
1 parent bf662e1 commit 21c452e

8 files changed

+99
-17
lines changed

src/LightningDB.Tests/EnvironmentTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ namespace LightningDB.Tests;
66

77
public class EnvironmentTests : TestBase
88
{
9+
public void CanGetEnvironmentVersion()
10+
{
11+
using var env = CreateEnvironment();
12+
var version = env.Version;
13+
version.ShouldNotBeNull();
14+
version.Minor.ShouldBeGreaterThan(0);
15+
}
16+
917
public void EnvironmentShouldBeCreatedIfWithoutFlags()
1018
{
1119
using var env = CreateEnvironment();
@@ -101,6 +109,27 @@ public void EnvironmentShouldBeClosed()
101109
env.IsOpened.ShouldBeFalse();
102110
}
103111

112+
public void CanPutMultipleKeyValuePairsAndFlushSuccessfully()
113+
{
114+
using var env = CreateEnvironment();
115+
env.Open();
116+
117+
using var tx = env.BeginTransaction();
118+
using var db = tx.OpenDatabase(null, new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create });
119+
120+
for (int i = 0; i < 5; i++)
121+
{
122+
var key = BitConverter.GetBytes(i);
123+
var value = BitConverter.GetBytes(i * 10);
124+
tx.Put(db, key, value);
125+
}
126+
127+
tx.Commit();
128+
129+
var result = env.Flush(force: true);
130+
result.ShouldBe(MDBResultCode.Success);
131+
}
132+
104133
public void EnvironmentShouldBeCopied()
105134
{
106135
void CopyTest(bool compact)

src/LightningDB.Tests/TransactionTests.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,41 @@ public void ChildTransactionShouldBeAbortedIfParentIsAborted()
8686
result.ShouldBe(MDBResultCode.BadTxn);
8787
});
8888
}
89+
90+
public void TryGetShouldVerifyFindingAndNotFindingValues()
91+
{
92+
using var env = CreateEnvironment();
93+
env.Open();
94+
env.RunTransactionScenario((tx, db) =>
95+
{
96+
var key = MemoryMarshal.Cast<char, byte>("key1");
97+
var value = MemoryMarshal.Cast<char, byte>("value1");
98+
99+
tx.Put(db, key, value);
100+
101+
tx.TryGet(db, key.ToArray(), out var retrievedValue).ShouldBeTrue();
102+
retrievedValue.ShouldBe(value.ToArray());
103+
104+
var missingKey = MemoryMarshal.Cast<char, byte>("key2");
105+
tx.TryGet(db, missingKey.ToArray(), out _).ShouldBeFalse();
106+
});
107+
}
108+
109+
public void TryGetWithKeyAndValueShouldBeFound()
110+
{
111+
using var env = CreateEnvironment();
112+
env.Open();
113+
env.RunTransactionScenario((tx, db) =>
114+
{
115+
var key = MemoryMarshal.Cast<char, byte>("key3");
116+
var value = MemoryMarshal.Cast<char, byte>("value3");
117+
tx.Put(db, key, value);
118+
119+
var resultBuffer = new byte[value.Length];
120+
tx.TryGet(db, key.ToArray(), resultBuffer).ShouldBeTrue();
121+
resultBuffer.ShouldBe(value.ToArray());
122+
});
123+
}
89124

90125
public void ChildTransactionShouldBeAbortedIfParentIsCommitted()
91126
{
@@ -228,4 +263,27 @@ public void TransactionShouldSupportCustomDupSorter()
228263
BitConverter.ToInt32(result.Item3.CopyToNewArray()).ShouldBe(valuesSorted[order++]);
229264
}
230265
}
266+
public void DatabaseShouldBeEmptyAfterTruncate()
267+
{
268+
using var env = CreateEnvironment();
269+
env.Open();
270+
env.RunTransactionScenario((tx, db) =>
271+
{
272+
// Insert several key-value pairs
273+
var key1 = MemoryMarshal.Cast<char, byte>("key1");
274+
var key2 = MemoryMarshal.Cast<char, byte>("key2");
275+
var value1 = MemoryMarshal.Cast<char, byte>("value1");
276+
var value2 = MemoryMarshal.Cast<char, byte>("value2");
277+
278+
tx.Put(db, key1, value1);
279+
tx.Put(db, key2, value2);
280+
281+
// Truncate the database
282+
tx.TruncateDatabase(db);
283+
284+
// Verify the database is empty
285+
tx.ContainsKey(db, key1).ShouldBeFalse();
286+
tx.ContainsKey(db, key2).ShouldBeFalse();
287+
});
288+
}
231289
}

src/LightningDB/LightningCursor.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace LightningDB;
1212
public class LightningCursor : IDisposable
1313
{
1414
private nint _handle;
15-
private bool _disposed = false;
15+
private bool _disposed;
1616

1717
/// <summary>
1818
/// Creates new instance of LightningCursor
@@ -33,14 +33,6 @@ internal LightningCursor(LightningDatabase db, LightningTransaction txn)
3333
Transaction = txn;
3434
}
3535

36-
/// <summary>
37-
/// Gets the native handle of the cursor
38-
/// </summary>
39-
public nint Handle()
40-
{
41-
return _handle;
42-
}
43-
4436
/// <summary>
4537
/// Cursor's transaction.
4638
/// </summary>
@@ -549,7 +541,8 @@ private void Dispose(bool disposing)
549541
}
550542
_handle = default;
551543

552-
GC.SuppressFinalize(this);
544+
if(disposing)
545+
GC.SuppressFinalize(this);
553546
}
554547

555548
/// <summary>

src/LightningDB/LightningDatabase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace LightningDB;
1111
public sealed class LightningDatabase : IDisposable
1212
{
1313
internal uint _handle;
14-
private bool _disposed = false;
14+
private bool _disposed;
1515
private readonly DatabaseConfiguration _configuration;
1616
private readonly bool _closeOnDispose;
1717
private readonly LightningTransaction _transaction;
@@ -101,7 +101,8 @@ private void Dispose(bool disposing)
101101
if (_closeOnDispose)
102102
mdb_dbi_close(Environment._handle, _handle);
103103

104-
GC.SuppressFinalize(this);
104+
if(disposing)
105+
GC.SuppressFinalize(this);
105106
_handle = default;
106107
}
107108

src/LightningDB/LightningEnvironment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace LightningDB;
1212
public sealed class LightningEnvironment : IDisposable
1313
{
1414
private readonly EnvironmentConfiguration _config = new();
15-
private bool _disposed = false;
15+
private bool _disposed;
1616

1717
internal nint _handle;
1818

src/LightningDB/LightningExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public static bool TryGet(this LightningTransaction tx, LightningDatabase db, Re
146146
value = mdbValue.CopyToNewArray();
147147
return true;
148148
}
149-
value = default;
149+
value = null;
150150
return false;
151151
}
152152

src/LightningDB/LightningTransaction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public sealed class LightningTransaction : IDisposable
1818

1919
internal nint _handle;
2020
private readonly nint _originalHandle;
21-
private bool _disposed = false;
21+
private bool _disposed;
2222

2323
/// <summary>
2424
/// Created new instance of LightningTransaction

src/LightningDB/Native/Lmdb.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ public static partial class Lmdb
106106
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
107107
public static partial MDBResultCode mdb_get(nint txn, uint dbi, ref MDBValue key, out MDBValue data);
108108

109-
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
110-
public static extern MDBResultCode mdb_cursor_count(nint cursor, out int countp);
109+
[LibraryImport(MDB_DLL_NAME)]
110+
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
111+
public static partial MDBResultCode mdb_cursor_count(nint cursor, out int countp);
111112

112113
[LibraryImport(MDB_DLL_NAME)]
113114
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]

0 commit comments

Comments
 (0)