Skip to content

Commit

Permalink
Merge pull request #167 from adamfur/mdb_cursor_count
Browse files Browse the repository at this point in the history
Expose mdb_cursor_count()
  • Loading branch information
CoreyKaylor committed Oct 11, 2023
2 parents 2b17bc8 + ce7b84d commit 7508adc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/LightningDB.Tests/CursorTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using Xunit;
using static System.Text.Encoding;
Expand Down Expand Up @@ -397,4 +397,19 @@ void ReproduceCoreIteration(LightningEnvironment environment, LightningDatabase
}
Assert.True(true, "Code would be unreachable otherwise.");
}

[Fact]
public void CountCursor()
{
_env.RunCursorScenario((_, _, c) =>
{
var key = "TestKey"u8.ToArray();
var keys = PopulateMultipleCursorValues(c);
c.SetRange(key);
c.Count(out var amount);
Assert.Equal(5, amount);
}, DatabaseOpenFlags.DuplicatesFixed | DatabaseOpenFlags.Create);
}
}
12 changes: 12 additions & 0 deletions src/LightningDB/LightningCursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,18 @@ public MDBResultCode Renew(LightningTransaction txn)

return mdb_cursor_renew(txn.Handle(), _handle);
}

/// <summary>
/// Return count of duplicates for current key.
///
/// This call is only valid on databases that support sorted duplicate data items DatabaseOpenFlags.DuplicatesFixed.
/// </summary>
/// <param name="value">Output parameter where the duplicate count will be stored.</param>
/// <returns>Returns <see cref="MDBResultCode"/></returns>
public MDBResultCode Count(out int value)
{
return mdb_cursor_count(_handle, out value);
}

/// <summary>
/// Closes the cursor and deallocates all resources associated with it.
Expand Down
8 changes: 7 additions & 1 deletion src/LightningDB/Native/Lmdb.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Runtime.InteropServices;
using System.Text;

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

[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_cursor_count(nint cursor, out int countp);

[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = new []{typeof(CallConvCdecl)})]
public static partial MDBResultCode mdb_put(nint txn, uint dbi, ref MDBValue key, ref MDBValue data, PutOptions flags);
Expand Down Expand Up @@ -300,6 +303,9 @@ public static MDBResultCode mdb_env_copy2(nint env, string path, EnvironmentCopy
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_get(nint txn, uint dbi, ref MDBValue key, out MDBValue data);

[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_cursor_count(nint cursor, out int countp);

[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_put(nint txn, uint dbi, ref MDBValue key, ref MDBValue data, PutOptions flags);

Expand Down

0 comments on commit 7508adc

Please sign in to comment.