Skip to content

Commit 4ea188a

Browse files
committed
implemented cursor get_multiple
1 parent 49c9294 commit 4ea188a

File tree

8 files changed

+174
-114
lines changed

8 files changed

+174
-114
lines changed

src/LightningDB.Tests/CursorTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,5 +253,29 @@ 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 ShouldGetMultiple()
259+
{
260+
//arrange
261+
_db = _txn.OpenDatabase(options: new DatabaseOptions { Flags = DatabaseOpenFlags.DuplicatesFixed });
262+
263+
var values = new[] { 1, 2, 3, 4, 5 };
264+
using (var cur = _txn.CreateCursor(_db))
265+
cur.PutMultiple("TestKey", values);
266+
267+
bool result;
268+
int[] resultArray;
269+
using (var cur = _txn.CreateCursor(_db))
270+
{
271+
KeyValuePair<string, int> pair;
272+
cur.MoveNext(out pair);
273+
274+
result = cur.GetMultiple(out resultArray);
275+
}
276+
277+
Assert.IsTrue(result);
278+
CollectionAssert.AreEqual(values, resultArray);
279+
}
256280
}
257281
}

src/LightningDB/GetByOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ public TValue Value<TValue>()
3030
{
3131
return _db.FromBytes<TValue>(_rawValue);
3232
}
33-
}
33+
}
3434
}

src/LightningDB/LightningCursor.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,7 @@ public byte[] MoveToLastDuplicate()
155155
/// <returns>All the duplicate data items at the current cursor position.</returns>
156156
public byte[] GetMultiple()
157157
{
158-
var result = this.Get(CursorOperation.GetMultiple);
159-
if (!result.HasValue)
160-
return null;
161-
162-
return result.Value.Value;
158+
return this.GetValue(CursorOperation.GetMultiple);
163159
}
164160

165161
/// <summary>

src/LightningDB/LightningCursorExtensions.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
5+
using System.Runtime.InteropServices;
46
using System.Text;
57

68
namespace LightningDB
@@ -63,6 +65,29 @@ public static void PutMultiple<TKey, TValue>(this LightningCursor cur, TKey key,
6365
cur.PutMultiple(keyBytes, valueBytes);
6466
}
6567

68+
public static MultipleGetByOperation GetMultipleBy<TValue>(this LightningCursor cur)
69+
{
70+
var bytes = cur.GetMultiple();
71+
if (bytes == null)
72+
return null;
73+
74+
return new MultipleGetByOperation(cur.Database, bytes);
75+
}
76+
77+
public static bool GetMultiple<TValue>(this LightningCursor cur, out TValue[] values)
78+
{
79+
var op = cur.GetMultipleBy<TValue>();
80+
if (op == null)
81+
{
82+
values = null;
83+
return false;
84+
}
85+
86+
values = op.Values<TValue>();
87+
88+
return true;
89+
}
90+
6691
internal static byte[] ToBytes<T>(this LightningCursor cur, T instance)
6792
{
6893
return cur.Database.ToBytes(instance);
@@ -72,5 +97,51 @@ internal static T FromBytes<T>(this LightningCursor cur, byte[] bytes)
7297
{
7398
return cur.Database.FromBytes<T>(bytes);
7499
}
100+
101+
internal static CursorGetByOperation CursorMoveBy(this LightningCursor cur, Func<KeyValuePair<byte[], byte[]>?> mover)
102+
{
103+
return new CursorGetByOperation(cur, mover.Invoke());
104+
}
105+
106+
internal static GetByOperation CursorMoveValueBy(this LightningCursor cur, Func<byte[]> mover)
107+
{
108+
var value = mover.Invoke();
109+
if (value == null)
110+
return null;
111+
112+
return new GetByOperation(cur.Database, value);
113+
}
114+
115+
internal static bool CursorMove<TKey, TValue>(this LightningCursor cur, Func<KeyValuePair<byte[], byte[]>?> mover, out KeyValuePair<TKey, TValue> pair)
116+
{
117+
var op = CursorMoveBy(cur, mover);
118+
119+
if (!op.PairExists)
120+
{
121+
pair = default(KeyValuePair<TKey, TValue>);
122+
return false;
123+
}
124+
else
125+
{
126+
pair = op.Pair<TKey, TValue>();
127+
return true;
128+
}
129+
}
130+
131+
internal static bool CursorMoveValue<TValue>(this LightningCursor cur, Func<byte[]> mover, out TValue value)
132+
{
133+
var op = CursorMoveValueBy(cur, mover);
134+
135+
if (op == null)
136+
{
137+
value = default(TValue);
138+
return false;
139+
}
140+
else
141+
{
142+
value = op.Value<TValue>();
143+
return true;
144+
}
145+
}
75146
}
76147
}

src/LightningDB/LightningCursorMoveExtensions.cs

Lines changed: 22 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,156 +7,115 @@ namespace LightningDB
77
{
88
public static class LightningCursorMoveExtensions
99
{
10-
private static CursorGetByOperation CursorMoveBy(LightningCursor cur, Func<KeyValuePair<byte[], byte[]>?> mover)
11-
{
12-
return new CursorGetByOperation(cur, mover.Invoke());
13-
}
14-
15-
private static GetByOperation CursorMoveValueBy(LightningCursor cur, Func<byte[]> mover)
16-
{
17-
return new GetByOperation(cur.Database, mover.Invoke());
18-
}
19-
20-
private static bool CursorMove<TKey, TValue>(LightningCursor cur, Func<KeyValuePair<byte[], byte[]>?> mover, out KeyValuePair<TKey, TValue> pair)
21-
{
22-
var op = CursorMoveBy(cur, mover);
23-
24-
if (!op.PairExists)
25-
{
26-
pair = default(KeyValuePair<TKey, TValue>);
27-
return false;
28-
}
29-
else
30-
{
31-
pair = op.Pair<TKey, TValue>();
32-
return true;
33-
}
34-
}
35-
36-
private static bool CursorMoveValue<TValue>(LightningCursor cur, Func<byte[]> mover, out TValue value)
37-
{
38-
var op = CursorMoveValueBy(cur, mover);
39-
40-
if (op == null)
41-
{
42-
value = default(TValue);
43-
return false;
44-
}
45-
else
46-
{
47-
value = op.Value< TValue>();
48-
return true;
49-
}
50-
}
5110

5211
public static CursorGetByOperation MoveToFirstBy(this LightningCursor cur)
5312
{
54-
return CursorMoveBy(cur, cur.MoveToFirst);
13+
return cur.CursorMoveBy(cur.MoveToFirst);
5514
}
5615

5716
public static bool MoveToFirst<TKey, TValue>(this LightningCursor cur, out KeyValuePair<TKey, TValue> pair)
5817
{
59-
return CursorMove<TKey, TValue>(cur, cur.MoveToFirst, out pair);
18+
return cur.CursorMove<TKey, TValue>(cur.MoveToFirst, out pair);
6019
}
6120

6221
public static CursorGetByOperation MoveToLastBy(this LightningCursor cur)
6322
{
64-
return CursorMoveBy(cur, cur.MoveToLast);
23+
return cur.CursorMoveBy(cur.MoveToLast);
6524
}
6625

6726
public static bool MoveToLast<TKey, TValue>(this LightningCursor cur, out KeyValuePair<TKey, TValue> pair)
6827
{
69-
return CursorMove<TKey, TValue>(cur, cur.MoveToLast, out pair);
28+
return cur.CursorMove<TKey, TValue>(cur.MoveToLast, out pair);
7029
}
7130

7231
public static CursorGetByOperation GetCurrentBy(this LightningCursor cur)
7332
{
74-
return CursorMoveBy(cur, cur.GetCurrent);
33+
return cur.CursorMoveBy(cur.GetCurrent);
7534
}
7635

7736
public static bool GetCurrent<TKey, TValue>(this LightningCursor cur, out KeyValuePair<TKey, TValue> pair)
7837
{
79-
return CursorMove<TKey, TValue>(cur, cur.GetCurrent, out pair);
38+
return cur.CursorMove<TKey, TValue>(cur.GetCurrent, out pair);
8039
}
8140

8241
public static CursorGetByOperation MoveNextBy(this LightningCursor cur)
8342
{
84-
return CursorMoveBy(cur, cur.MoveNext);
43+
return cur.CursorMoveBy(cur.MoveNext);
8544
}
8645

8746
public static bool MoveNext<TKey, TValue>(this LightningCursor cur, out KeyValuePair<TKey, TValue> pair)
8847
{
89-
return CursorMove<TKey, TValue>(cur, cur.MoveNext, out pair);
48+
return cur.CursorMove<TKey, TValue>(cur.MoveNext, out pair);
9049
}
9150

9251
public static CursorGetByOperation MoveNextDuplicateBy(this LightningCursor cur)
9352
{
94-
return CursorMoveBy(cur, cur.MoveNextDuplicate);
53+
return cur.CursorMoveBy(cur.MoveNextDuplicate);
9554
}
9655

9756
public static bool MoveNextDuplicate<TKey, TValue>(this LightningCursor cur, out KeyValuePair<TKey, TValue> pair)
9857
{
99-
return CursorMove<TKey, TValue>(cur, cur.MoveNextDuplicate, out pair);
58+
return cur.CursorMove<TKey, TValue>(cur.MoveNextDuplicate, out pair);
10059
}
10160

10261
public static CursorGetByOperation MoveNextNoDuplicateBy(this LightningCursor cur)
10362
{
104-
return CursorMoveBy(cur, cur.MoveNextNoDuplicate);
63+
return cur.CursorMoveBy(cur.MoveNextNoDuplicate);
10564
}
10665

10766
public static bool MoveNextNoDuplicate<TKey, TValue>(this LightningCursor cur, out KeyValuePair<TKey, TValue> pair)
10867
{
109-
return CursorMove<TKey, TValue>(cur, cur.MoveNextNoDuplicate, out pair);
68+
return cur.CursorMove<TKey, TValue>(cur.MoveNextNoDuplicate, out pair);
11069
}
11170

11271
public static CursorGetByOperation MovePrevBy(this LightningCursor cur)
11372
{
114-
return CursorMoveBy(cur, cur.MovePrev);
73+
return cur.CursorMoveBy(cur.MovePrev);
11574
}
11675

11776
public static bool MovePrev<TKey, TValue>(this LightningCursor cur, out KeyValuePair<TKey, TValue> pair)
11877
{
119-
return CursorMove<TKey, TValue>(cur, cur.MovePrev, out pair);
78+
return cur.CursorMove<TKey, TValue>(cur.MovePrev, out pair);
12079
}
12180

12281
public static CursorGetByOperation MovePrevDuplicateBy(this LightningCursor cur)
12382
{
124-
return CursorMoveBy(cur, cur.MovePrevDuplicate);
83+
return cur.CursorMoveBy(cur.MovePrevDuplicate);
12584
}
12685

12786
public static bool MovePrevDuplicate<TKey, TValue>(this LightningCursor cur, out KeyValuePair<TKey, TValue> pair)
12887
{
129-
return CursorMove<TKey, TValue>(cur, cur.MovePrevDuplicate, out pair);
88+
return cur.CursorMove<TKey, TValue>(cur.MovePrevDuplicate, out pair);
13089
}
13190

13291
public static CursorGetByOperation MovePrevNoDuplicateBy(this LightningCursor cur)
13392
{
134-
return CursorMoveBy(cur, cur.MovePrevNoDuplicate);
93+
return cur.CursorMoveBy(cur.MovePrevNoDuplicate);
13594
}
13695

13796
public static bool MovePrevNoDuplicate<TKey, TValue>(this LightningCursor cur, out KeyValuePair<TKey, TValue> pair)
13897
{
139-
return CursorMove<TKey, TValue>(cur, cur.MovePrevNoDuplicate, out pair);
98+
return cur.CursorMove<TKey, TValue>(cur.MovePrevNoDuplicate, out pair);
14099
}
141100

142101
public static GetByOperation MoveToFirstDuplicateBy(this LightningCursor cur)
143102
{
144-
return CursorMoveValueBy(cur, cur.MoveToFirstDuplicate);
103+
return cur.CursorMoveValueBy(cur.MoveToFirstDuplicate);
145104
}
146105

147106
public static bool MoveToFirstDuplicate<TValue>(this LightningCursor cur, out TValue value)
148107
{
149-
return CursorMoveValue<TValue>(cur, cur.MoveToFirstDuplicate, out value);
108+
return cur.CursorMoveValue<TValue>(cur.MoveToFirstDuplicate, out value);
150109
}
151110

152111
public static GetByOperation MoveToLastDuplicateBy(this LightningCursor cur)
153112
{
154-
return CursorMoveValueBy(cur, cur.MoveToLastDuplicate);
113+
return cur.CursorMoveValueBy(cur.MoveToLastDuplicate);
155114
}
156115

157116
public static bool MoveToLastDuplicate<TValue>(this LightningCursor cur, out TValue value)
158117
{
159-
return CursorMoveValue<TValue>(cur, cur.MoveToLastDuplicate, out value);
118+
return cur.CursorMoveValue<TValue>(cur.MoveToLastDuplicate, out value);
160119
}
161120

162121
}

src/LightningDB/LightningCursorMoveExtensions.tt

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,60 +29,19 @@ namespace LightningDB
2929
{
3030
public static class LightningCursorMoveExtensions
3131
{
32-
private static CursorGetByOperation CursorMoveBy(LightningCursor cur, Func<KeyValuePair<byte[], byte[]>?> mover)
33-
{
34-
return new CursorGetByOperation(cur, mover.Invoke());
35-
}
36-
37-
private static GetByOperation CursorMoveValueBy(LightningCursor cur, Func<byte[]> mover)
38-
{
39-
return new GetByOperation(cur.Database, mover.Invoke());
40-
}
41-
42-
private static bool CursorMove<TKey, TValue>(LightningCursor cur, Func<KeyValuePair<byte[], byte[]>?> mover, out KeyValuePair<TKey, TValue> pair)
43-
{
44-
var op = CursorMoveBy(cur, mover);
45-
46-
if (!op.PairExists)
47-
{
48-
pair = default(KeyValuePair<TKey, TValue>);
49-
return false;
50-
}
51-
else
52-
{
53-
pair = op.Pair<TKey, TValue>();
54-
return true;
55-
}
56-
}
57-
58-
private static bool CursorMoveValue<TValue>(LightningCursor cur, Func<byte[]> mover, out TValue value)
59-
{
60-
var op = CursorMoveValueBy(cur, mover);
61-
62-
if (op == null)
63-
{
64-
value = default(TValue);
65-
return false;
66-
}
67-
else
68-
{
69-
value = op.Value< TValue>();
70-
return true;
71-
}
72-
}
7332
<#
7433
foreach(var name in cursorPairMethods)
7534
{
7635
#>
7736

7837
public static CursorGetByOperation <#= name #>By(this LightningCursor cur)
7938
{
80-
return CursorMoveBy(cur, cur.<#= name #>);
39+
return cur.CursorMoveBy(cur.<#= name #>);
8140
}
8241

8342
public static bool <#= name #><TKey, TValue>(this LightningCursor cur, out KeyValuePair<TKey, TValue> pair)
8443
{
85-
return CursorMove<TKey, TValue>(cur, cur.<#= name #>, out pair);
44+
return cur.CursorMove<TKey, TValue>(cur.<#= name #>, out pair);
8645
}
8746
<#
8847
}
@@ -94,12 +53,12 @@ namespace LightningDB
9453

9554
public static GetByOperation <#= name #>By(this LightningCursor cur)
9655
{
97-
return CursorMoveValueBy(cur, cur.<#= name #>);
56+
return cur.CursorMoveValueBy(cur.<#= name #>);
9857
}
9958

10059
public static bool <#= name #><TValue>(this LightningCursor cur, out TValue value)
10160
{
102-
return CursorMoveValue<TValue>(cur, cur.<#= name #>, out value);
61+
return cur.CursorMoveValue<TValue>(cur.<#= name #>, out value);
10362
}
10463
<#
10564
}

0 commit comments

Comments
 (0)