Skip to content

Commit 7508adc

Browse files
authored
Merge pull request #167 from adamfur/mdb_cursor_count
Expose mdb_cursor_count()
2 parents 2b17bc8 + ce7b84d commit 7508adc

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/LightningDB.Tests/CursorTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Linq;
33
using Xunit;
44
using static System.Text.Encoding;
@@ -397,4 +397,19 @@ void ReproduceCoreIteration(LightningEnvironment environment, LightningDatabase
397397
}
398398
Assert.True(true, "Code would be unreachable otherwise.");
399399
}
400+
401+
[Fact]
402+
public void CountCursor()
403+
{
404+
_env.RunCursorScenario((_, _, c) =>
405+
{
406+
var key = "TestKey"u8.ToArray();
407+
var keys = PopulateMultipleCursorValues(c);
408+
409+
c.SetRange(key);
410+
c.Count(out var amount);
411+
412+
Assert.Equal(5, amount);
413+
}, DatabaseOpenFlags.DuplicatesFixed | DatabaseOpenFlags.Create);
414+
}
400415
}

src/LightningDB/LightningCursor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,18 @@ public MDBResultCode Renew(LightningTransaction txn)
497497

498498
return mdb_cursor_renew(txn.Handle(), _handle);
499499
}
500+
501+
/// <summary>
502+
/// Return count of duplicates for current key.
503+
///
504+
/// This call is only valid on databases that support sorted duplicate data items DatabaseOpenFlags.DuplicatesFixed.
505+
/// </summary>
506+
/// <param name="value">Output parameter where the duplicate count will be stored.</param>
507+
/// <returns>Returns <see cref="MDBResultCode"/></returns>
508+
public MDBResultCode Count(out int value)
509+
{
510+
return mdb_cursor_count(_handle, out value);
511+
}
500512

501513
/// <summary>
502514
/// Closes the cursor and deallocates all resources associated with it.

src/LightningDB/Native/Lmdb.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Runtime.InteropServices;
33
using System.Text;
44

@@ -104,6 +104,9 @@ public static partial class Lmdb
104104
[UnmanagedCallConv(CallConvs = new []{typeof(CallConvCdecl)})]
105105
public static partial MDBResultCode mdb_get(nint txn, uint dbi, ref MDBValue key, out MDBValue data);
106106

107+
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
108+
public static extern MDBResultCode mdb_cursor_count(nint cursor, out int countp);
109+
107110
[LibraryImport(MDB_DLL_NAME)]
108111
[UnmanagedCallConv(CallConvs = new []{typeof(CallConvCdecl)})]
109112
public static partial MDBResultCode mdb_put(nint txn, uint dbi, ref MDBValue key, ref MDBValue data, PutOptions flags);
@@ -300,6 +303,9 @@ public static MDBResultCode mdb_env_copy2(nint env, string path, EnvironmentCopy
300303
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
301304
public static extern MDBResultCode mdb_get(nint txn, uint dbi, ref MDBValue key, out MDBValue data);
302305

306+
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
307+
public static extern MDBResultCode mdb_cursor_count(nint cursor, out int countp);
308+
303309
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
304310
public static extern MDBResultCode mdb_put(nint txn, uint dbi, ref MDBValue key, ref MDBValue data, PutOptions flags);
305311

0 commit comments

Comments
 (0)