1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ namespace LightningDB
4
+ {
5
+ /// <summary>
6
+ /// Converter class for pairs obtained via cursor
7
+ /// </summary>
8
+ public class CursorMultipleGetByOperation
9
+ {
10
+ private readonly LightningCursor _cur ;
11
+ private readonly KeyValuePair < GetByOperation , MultipleGetByOperation > ? _pair ;
12
+
13
+ /// <summary>
14
+ /// Creates new instance of CursorGetByOperation.
15
+ /// </summary>
16
+ /// <param name="cur">Cursor.</param>
17
+ /// <param name="pair">Key-value byte arrays pair</param>
18
+ public CursorMultipleGetByOperation ( LightningCursor cur , KeyValuePair < byte [ ] , byte [ ] > ? pair )
19
+ {
20
+ _cur = cur ;
21
+
22
+ if ( pair . HasValue )
23
+ {
24
+ _pair = new KeyValuePair < GetByOperation , MultipleGetByOperation > (
25
+ new GetByOperation ( cur . Database , pair . Value . Key ) ,
26
+ new MultipleGetByOperation ( cur . Database , pair . Value . Value ) ) ;
27
+ }
28
+ }
29
+
30
+ /// <summary>
31
+ /// Does key-value pair exist in database.
32
+ /// </summary>
33
+ public bool PairExists { get { return _pair . HasValue ; } }
34
+
35
+ private void EnsurePairExists ( )
36
+ {
37
+ if ( ! this . PairExists )
38
+ throw new InvalidOperationException ( "Pair doesn't exist" ) ;
39
+ }
40
+
41
+ /// <summary>
42
+ /// Converts database key to a concrete type
43
+ /// </summary>
44
+ /// <typeparam name="TKey">Type to convert key to</typeparam>
45
+ /// <returns>Converted key value.</returns>
46
+ public TKey Key < TKey > ( )
47
+ {
48
+ this . EnsurePairExists ( ) ;
49
+
50
+ return _pair . Value . Key . Value < TKey > ( ) ;
51
+ }
52
+
53
+ /// <summary>
54
+ /// Converts database value to a concrete type.
55
+ /// </summary>
56
+ /// <typeparam name="TValue">Type to convert value to.</typeparam>
57
+ /// <returns>Converted value.</returns>
58
+ public TValue [ ] Values < TValue > ( )
59
+ {
60
+ this . EnsurePairExists ( ) ;
61
+
62
+ return _pair . Value . Value . Values < TValue > ( ) ;
63
+ }
64
+
65
+ /// <summary>
66
+ /// Convert key-value pair to concrete types
67
+ /// </summary>
68
+ /// <typeparam name="TKey">Key type.</typeparam>
69
+ /// <typeparam name="TValue">Value type.</typeparam>
70
+ /// <returns>Pair of converted key and value.</returns>
71
+ public KeyValuePair < TKey , TValue [ ] > Pair < TKey , TValue > ( )
72
+ {
73
+ return new KeyValuePair < TKey , TValue [ ] > ( this . Key < TKey > ( ) , this . Values < TValue > ( ) ) ;
74
+ }
75
+ }
76
+ }
0 commit comments