Skip to content

Commit 4873f97

Browse files
committed
DBOpenFlags.DuplicatesFixed include DuplicatesSort
1 parent 3f65c33 commit 4873f97

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

src/LightningDB.Tests/CursorTests.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public void ShouldIterateThroughCursorByEnumerator()
234234
public void ShouldPutMultiple()
235235
{
236236
//arrange
237-
_db = _txn.OpenDatabase(flags: DatabaseOpenFlags.DuplicatesFixed | DatabaseOpenFlags.DuplicatesSort);
237+
_db = _txn.OpenDatabase(flags: DatabaseOpenFlags.DuplicatesFixed);
238238

239239
var values = new[] { 1, 2, 3, 4, 5 };
240240
using (var cur = _txn.CreateCursor(_db))
@@ -253,5 +253,39 @@ public void ShouldPutMultiple()
253253
for (var i = 0; i < values.Length; i++)
254254
Assert.AreEqual(values[i], pairs[i].Value);
255255
}
256+
257+
[Test]
258+
public void CursorShouldPutDupValues()
259+
{
260+
261+
var db2 = _txn.OpenDatabase("dup",
262+
DatabaseOpenFlags.Create | DatabaseOpenFlags.DuplicatesSort
263+
| DatabaseOpenFlags.DuplicatesFixed | DatabaseOpenFlags.IntegerDuplicates);
264+
265+
using (var cur = _txn.CreateCursor(db2))
266+
{
267+
var keys = Enumerable.Range(1, 50000).ToArray();
268+
foreach (var k in keys)
269+
{
270+
cur.Put(Encoding.UTF8.GetBytes("key"), k, CursorPutOptions.None);
271+
}
272+
// overwrite
273+
foreach (var k in keys)
274+
{
275+
cur.Put(Encoding.UTF8.GetBytes("key"), k, CursorPutOptions.None);
276+
}
277+
278+
var kvp = cur.MoveToFirst();
279+
kvp = cur.MoveNextDuplicate();
280+
kvp = cur.MoveNextDuplicate();
281+
kvp = cur.MoveToFirstDuplicate(); // cancel the moves above and start from the beginning
282+
foreach (var k in keys)
283+
{
284+
//var kvp = cur.GetCurrent();
285+
Assert.AreEqual(k, BitConverter.ToInt32(kvp.Value.Value, 0));
286+
kvp = cur.MoveNextDuplicate();
287+
}
288+
}
289+
}
256290
}
257291
}

src/LightningDB/DatabaseOpenFlags.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using LightningDB.Native;
23

34
namespace LightningDB
45
{
@@ -21,7 +22,7 @@ public enum DatabaseOpenFlags
2122
/// <summary>
2223
/// MDB_DUPSORT. Duplicate keys may be used in the database. (Or, from another perspective, keys may have multiple data items, stored in sorted order.) By default keys must be unique and may have only a single data item.
2324
/// </summary>
24-
DuplicatesSort = 0x04,
25+
DuplicatesSort = NativeMethods.MDB_DUPSORT,
2526

2627
/// <summary>
2728
/// MDB_INTEGERKEY. Keys are binary integers in native byte order.
@@ -32,7 +33,7 @@ public enum DatabaseOpenFlags
3233
/// <summary>
3334
/// MDB_DUPFIXED. This flag may only be used in combination with MDB_DUPSORT. This option tells the library that the data items for this database are all the same size, which allows further optimizations in storage and retrieval. When all data items are the same size, the MDB_GET_MULTIPLE and MDB_NEXT_MULTIPLE cursor operations may be used to retrieve multiple items at once.
3435
/// </summary>
35-
DuplicatesFixed = 0x10,
36+
DuplicatesFixed = NativeMethods.MDB_DUPSORT | NativeMethods.MDB_DUPFIXED,
3637

3738
/// <summary>
3839
/// MDB_INTEGERDUP. This option specifies that duplicate data items are also integers, and should be sorted as such.

src/LightningDB/Native/Native.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ private static LightningVersionInfo GetVersionInfo(INativeLibraryFacade lib, out
105105
/// </summary>
106106
public const int MDB_NOTFOUND = -30798;
107107

108+
/// <summary>
109+
/// Duplicate keys may be used in the database. (Or, from another perspective, keys may have multiple data items, stored in sorted order.) By default keys must be unique and may have only a single data item.
110+
/// </summary>
111+
public const int MDB_DUPSORT = 0x04;
112+
113+
/// <summary>
114+
/// This flag may only be used in combination with MDB_DUPSORT. This option tells the library that the data items for this database are all the same size, which allows further optimizations in storage and retrieval. When all data items are the same size, the MDB_GET_MULTIPLE and MDB_NEXT_MULTIPLE cursor operations may be used to retrieve multiple items at once.
115+
/// </summary>
116+
public const int MDB_DUPFIXED = 0x10;
117+
108118
#endregion Constants
109119

110120
#region Helpers

0 commit comments

Comments
 (0)