-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathLightningExtensions.cs
More file actions
204 lines (188 loc) · 8.34 KB
/
LightningExtensions.cs
File metadata and controls
204 lines (188 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using LightningDB.Native;
namespace LightningDB;
public static class LightningExtensions
{
/// <summary>
/// Throws a <see cref="LightningException"/> on anything other than NotFound, or Success
/// </summary>
/// <param name="resultCode">The result code to evaluate for errors</param>
/// <returns><see cref="MDBResultCode"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MDBResultCode ThrowOnReadError(this MDBResultCode resultCode)
{
return resultCode == MDBResultCode.NotFound
? resultCode : resultCode.ThrowOnError();
}
/// <summary>
/// Throws a <see cref="LightningException"/> on anything other than Success
/// </summary>
/// <param name="resultCode">The result code to evaluate for errors</param>
/// <returns><see cref="MDBResultCode"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MDBResultCode ThrowOnError(this MDBResultCode resultCode)
{
if (resultCode == MDBResultCode.Success)
return resultCode;
var statusCode = (int) resultCode;
var message = mdb_strerror(statusCode);
throw new LightningException(message, statusCode);
}
/// <summary>
/// Throws a <see cref="LightningException"/> on anything other than NotFound, or Success
/// </summary>
/// <param name="result">A <see cref="ValueTuple"/> representing the get result operation</param>
/// <returns>The provided <see cref="ValueTuple"/> if no error occurs</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static (MDBResultCode resultCode, MDBValue key, MDBValue value) ThrowOnReadError(
this ValueTuple<MDBResultCode, MDBValue, MDBValue> result)
{
result.Item1.ThrowOnReadError();
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string mdb_strerror(int err)
{
var ptr = Lmdb.mdb_strerror(err);
return Marshal.PtrToStringAnsi(ptr) ?? $"Unknown error {err}";
}
/// <summary>
/// Enumerates the key/value pairs of the <see cref="LightningCursor"/> starting at the current position.
/// </summary>
/// <param name="cursor"><see cref="LightningCursor"/></param>
/// <returns><see cref="ValueTuple"/> key/value pairs of <see cref="MDBValue"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<ValueTuple<MDBValue, MDBValue>> AsEnumerable(this LightningCursor cursor)
{
(MDBResultCode, MDBValue, MDBValue) result;
while((result = cursor.Next()).Item1 == MDBResultCode.Success)
{
yield return (result.Item2, result.Item3);
}
result.Item1.ThrowOnReadError();
}
/// <summary>
/// Enumerates the values for a given key. Requires MDB_DUPSORT
/// </summary>
/// <param name="cursor"><see cref="LightningCursor"/></param>
/// <param name="key">The key with multiple values</param>
/// <returns><see cref="MDBValue"/> representing each value for a given key</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<MDBValue> AllValuesFor(this LightningCursor cursor, byte[] key)
{
return cursor.AllValuesFor(key.AsSpan());
}
/// <summary>
/// Enumerates the values for a given key. Requires MDB_DUPSORT
/// </summary>
/// <param name="cursor"><see cref="LightningCursor"/></param>
/// <param name="key">The key with multiple values</param>
/// <returns><see cref="MDBValue"/> representing each value for a given key</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<MDBValue> AllValuesFor(this LightningCursor cursor, Span<byte> key)
{
var result = cursor.Set(key);
result.ThrowOnReadError();
return cursor.AllValuesForImpl();
}
/// <summary>
/// Enumerates the values for a given key. Requires MDB_DUPSORT
/// </summary>
/// <param name="cursor"><see cref="LightningCursor"/></param>
/// <returns><see cref="MDBValue"/> representing each value for a given key</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static IEnumerable<MDBValue> AllValuesForImpl(this LightningCursor cursor)
{
(MDBResultCode, MDBValue, MDBValue) result = cursor.GetCurrent();
do
{
yield return result.Item3;
result = cursor.NextDuplicate();
} while (result.Item1 == MDBResultCode.Success);
result.Item1.ThrowOnReadError();
}
/// <summary>
/// Tries to get a value by its key.
/// </summary>
/// <param name="tx">The transaction.</param>
/// <param name="db">The database to query.</param>
/// <param name="key">A span containing the key to look up.</param>
/// <param name="value">A byte array containing the value found in the database, if it exists.</param>
/// <returns>True if key exists, false if not.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryGet(this LightningTransaction tx, LightningDatabase db, byte[] key, out byte[]? value)
{
return TryGet(tx, db, key.AsSpan(), out value);
}
/// <summary>
/// Tries to get a value by its key.
/// </summary>
/// <param name="tx">The transaction.</param>
/// <param name="db">The database to query.</param>
/// <param name="key">A span containing the key to look up.</param>
/// <param name="value">A byte array containing the value found in the database, if it exists.</param>
/// <returns>True if key exists, false if not.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryGet(this LightningTransaction tx, LightningDatabase db, ReadOnlySpan<byte> key, out byte[]? value)
{
var (resultCode, _, mdbValue) = tx.Get(db, key);
if (resultCode == MDBResultCode.Success)
{
value = mdbValue.CopyToNewArray();
return true;
}
value = null;
return false;
}
/// <summary>
/// Tries to get a value by its key.
/// </summary>
/// <param name="tx">The transaction.</param>
/// <param name="db">The database to query.</param>
/// <param name="key">A span containing the key to look up.</param>
/// <param name="destinationValueBuffer">
/// A buffer to receive the value data retrieved from the database
/// </param>
/// <returns>True if key exists, false if not.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryGet(this LightningTransaction tx, LightningDatabase db, ReadOnlySpan<byte> key, byte[] destinationValueBuffer)
{
var (resultCode, _, mdbValue) = tx.Get(db, key);
if (resultCode != MDBResultCode.Success)
return false;
var valueSpan = mdbValue.AsSpan();
if (valueSpan.TryCopyTo(destinationValueBuffer))
{
return true;
}
throw new LightningException("Incorrect buffer size given in destinationValueBuffer", (int)MDBResultCode.BadValSize);
}
/// <summary>
/// Check whether data exists in database.
/// </summary>
/// <param name="tx">The transaction.</param>
/// <param name="db">The database to query.</param>
/// <param name="key">A span containing the key to look up.</param>
/// <returns>True if key exists, false if not.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ContainsKey(this LightningTransaction tx, LightningDatabase db, ReadOnlySpan<byte> key)
{
var (resultCode, _, _) = tx.Get(db, key);
return resultCode == MDBResultCode.Success;
}
/// <summary>
/// Check whether data exists in database.
/// </summary>
/// <param name="tx">The transaction.</param>
/// <param name="db">The database to query.</param>
/// <param name="key">A span containing the key to look up.</param>
/// <returns>True if key exists, false if not.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ContainsKey(this LightningTransaction tx, LightningDatabase db, byte[] key)
{
return ContainsKey(tx, db, key.AsSpan());
}
}