Skip to content

Commit 2d7f1e1

Browse files
committed
All public members covered with docs
Also fixed options for the wrapper for mdb_cursor_put operation flags
1 parent 2f4b77c commit 2d7f1e1

12 files changed

+298
-29
lines changed

src/LightningDB.Tests/CursorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private void PopulateCursorValues()
5454
//act
5555
foreach (var k in keys)
5656
{
57-
cur.Put(k, k, PutOptions.None);
57+
cur.Put(k, k, CursorPutOptions.None);
5858
}
5959
}
6060
}

src/LightningDB/Converters/ConverterExtensions.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,39 @@ namespace LightningDB.Converters
88
/// </summary>
99
public static class ConverterExtensions
1010
{
11+
/// <summary>
12+
/// Register a converter into bytes.
13+
/// </summary>
14+
/// <typeparam name="TFrom">Converter's source convertion type</typeparam>
15+
/// <param name="store">Target sotre.</param>
16+
/// <param name="convert">Convertion lambda</param>
1117
public static void AddConvertToBytes<TFrom>(this ConverterStore store, Func<LightningDatabase, TFrom, byte[]> convert)
1218
{
1319
var converter = new ConvertToBytesInstance<TFrom>(convert);
1420
store.AddConvertToBytes(converter);
1521
}
16-
22+
23+
/// <summary>
24+
/// Register a converter from bytes
25+
/// </summary>
26+
/// <typeparam name="TTo">Converter's target convertion type</typeparam>
27+
/// <param name="store">Target store.</param>
28+
/// <param name="convert">Convertion lambda.</param>
1729
public static void AddConvertFromBytes<TTo>(this ConverterStore store, Func<LightningDatabase, byte[], TTo> convert)
1830
{
1931
var converter = new ConvertFromBytesInstance<TTo>(convert);
2032
store.AddConvertFromBytes(converter);
2133
}
2234

23-
public static Func<LightningDatabase, byte[], TTo> EnsureCorrectSize<TTo>(this Func<LightningDatabase, byte[], TTo> convert, int? size = null) where TTo : struct
35+
/// <summary>
36+
/// Return a convertion delegate that first ensures if a size in bytes is correct.
37+
/// </summary>
38+
/// <typeparam name="TTo">Convertion target type.</typeparam>
39+
/// <param name="convert">Basic convertion lambda without size ensurance.</param>
40+
/// <param name="size">Explicit size or autodetect if null.</param>
41+
/// <returns>A convertion delegate that first ensures if a size in bytes is correct</returns>
42+
public static Func<LightningDatabase, byte[], TTo> EnsureCorrectSize<TTo>(this Func<LightningDatabase, byte[], TTo> convert, int? size = null)
43+
where TTo : struct
2444
{
2545
return (db, x) =>
2646
{

src/LightningDB/Converters/ConverterStore.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,51 @@ public class ConverterStore
1212
private readonly IDictionary<Type, object> _convertToBytes = new ConcurrentDictionary<Type, object>();
1313
private readonly IDictionary<Type, object> _convertFromBytes = new ConcurrentDictionary<Type, object>();
1414

15+
/// <summary>
16+
/// Register a converter into bytes.
17+
/// </summary>
18+
/// <typeparam name="TConvertFrom">Converter's source convertion type</typeparam>
19+
/// <param name="converter">A converter</param>
1520
public void AddConvertToBytes<TConvertFrom>(IConvertToBytes<TConvertFrom> converter)
1621
{
1722
_convertToBytes.Add(converter.ConvertFromType, converter);
1823
}
1924

25+
/// <summary>
26+
/// Register a converter from bytes
27+
/// </summary>
28+
/// <typeparam name="TConvertTo">Converter's target convertion type</typeparam>
29+
/// <param name="converter">A converter</param>
2030
public void AddConvertFromBytes<TConvertTo>(IConvertFromBytes<TConvertTo> converter)
2131
{
2232
_convertFromBytes.Add(converter.ConvertFromType, converter);
2333
}
2434

35+
/// <summary>
36+
/// Gets a converter into bytes for the specified type
37+
/// </summary>
38+
/// <typeparam name="TConvertFrom">Type to convert from.</typeparam>
39+
/// <returns>Corresponsding converter.</returns>
2540
public IConvertToBytes<TConvertFrom> GetToBytes<TConvertFrom>()
2641
{
2742
return (IConvertToBytes<TConvertFrom>) GetToBytes(typeof(TConvertFrom));
2843
}
2944

45+
/// <summary>
46+
/// Gets a converter from bytes into the specified type.
47+
/// </summary>
48+
/// <typeparam name="TConvertTo">Type to convert to.</typeparam>
49+
/// <returns>Corresponding converter.</returns>
3050
public IConvertFromBytes<TConvertTo> GetFromBytes<TConvertTo>()
3151
{
3252
return (IConvertFromBytes<TConvertTo>) GetFromBytes(typeof(TConvertTo));
3353
}
3454

55+
/// <summary>
56+
/// Gets a converter into bytes for the specified type
57+
/// </summary>
58+
/// <param name="fromType">Type to convert from</param>
59+
/// <returns>Corresponsding converter</returns>
3560
public object GetToBytes(Type fromType)
3661
{
3762
if (!_convertToBytes.ContainsKey(fromType))
@@ -40,6 +65,11 @@ public object GetToBytes(Type fromType)
4065
return _convertToBytes[fromType];
4166
}
4267

68+
/// <summary>
69+
/// Gets a converter from bytes into the specified type.
70+
/// </summary>
71+
/// <param name="toType">Type to convert to</param>
72+
/// <returns>Corresponding converter.</returns>
4373
public object GetFromBytes(Type toType)
4474
{
4575
if (!_convertToBytes.ContainsKey(toType))

src/LightningDB/CursorPutOptions.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
3+
namespace LightningDB
4+
{
5+
/// <summary>
6+
/// Special options for cursor put operation.
7+
/// </summary>
8+
[Flags]
9+
public enum CursorPutOptions
10+
{
11+
/// <summary>
12+
/// No special behavior.
13+
/// </summary>
14+
None = 0,
15+
16+
/// <summary>
17+
/// Overwrite the current key/data pair
18+
/// </summary>
19+
Current = 0x40,
20+
21+
/// <summary>
22+
/// Only for MDB_DUPSORT
23+
/// For put: don't write if the key and data pair already exist.
24+
/// For mdb_cursor_del: remove all duplicate data items.
25+
/// </summary>
26+
NoDuplicateData = 0x20,
27+
28+
/// <summary>
29+
/// use sorted duplicates
30+
/// </summary>
31+
DuplicateSort = 0x04,
32+
33+
/// <summary>
34+
/// For put: Don't write if the key already exists.
35+
/// </summary>
36+
NoOverwrite = 0x10,
37+
38+
/// <summary>
39+
/// For put: Just reserve space for data, don't copy it. Return a pointer to the reserved space.
40+
/// </summary>
41+
ReserveSpace = 0x10000,
42+
43+
/// <summary>
44+
/// Data is being appended, don't split full pages.
45+
/// </summary>
46+
AppendData = 0x20000,
47+
48+
/// <summary>
49+
/// Duplicate data is being appended, don't split full pages.
50+
/// </summary>
51+
AppendDuplicateData = 0x40000
52+
53+
}
54+
}

src/LightningDB/LightningCursor.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,33 +76,55 @@ private void DetachClosingHandler()
7676
/// </summary>
7777
public LightningTransaction Transaction { get; private set; }
7878

79+
/// <summary>
80+
/// Position at specified key
81+
/// </summary>
82+
/// <param name="key">Key</param>
83+
/// <returns>Key-value pair for the specified key</returns>
7984
public KeyValuePair<byte[], byte[]>? MoveTo(byte[] key)
8085
{
8186
using (var marshalKey = new MarshalValueStructure(key))
8287
return this.Get(CursorOperation.Set, marshalKey.ValueStructure);
8388
}
8489

90+
/// <summary>
91+
/// Position at key/data pair. Only for MDB_DUPSORT
92+
/// </summary>
93+
/// <param name="key">Key.</param>
94+
/// <param name="value">Value</param>
95+
/// <returns>Current key/data pair.</returns>
8596
public KeyValuePair<byte[], byte[]>? MoveTo(byte[] key, byte[] value)
8697
{
8798
using (var marshalKey = new MarshalValueStructure(key))
8899
using (var marshalValue = new MarshalValueStructure(value))
89100
return this.Get(CursorOperation.GetBoth, marshalKey.ValueStructure, marshalValue.ValueStructure);
90101
}
91102

103+
/// <summary>
104+
/// position at key, nearest data. Only for MDB_DUPSORT
105+
/// </summary>
106+
/// <param name="key">Key</param>
107+
/// <param name="value">Value</param>
108+
/// <returns>Nearest value and corresponding key</returns>
92109
public KeyValuePair<byte[], byte[]>? MoveToFirstValueAfter(byte[] key, byte[] value)
93110
{
94111
using (var marshalKey = new MarshalValueStructure(key))
95112
using (var marshalValue = new MarshalValueStructure(value))
96113
return this.Get(CursorOperation.GetBothRange, marshalKey.ValueStructure, marshalValue.ValueStructure);
97114
}
98115

116+
/// <summary>
117+
/// Position at first key greater than or equal to specified key.
118+
/// </summary>
119+
/// <param name="key">Key</param>
120+
/// <returns>First key-value pair with a key greater than or equal to specified key.</returns>
99121
public KeyValuePair<byte[], byte[]>? MoveToFirstAfter(byte[] key)
100122
{
101123
using(var marshalKey = new MarshalValueStructure(key))
102124
return this.Get(CursorOperation.SetRange, marshalKey.ValueStructure);
103125
}
104126

105-
//What difference from CursorOperation.Set
127+
//What is the difference from CursorOperation.Set?
106128
/*public KeyValuePair<byte[], byte[]> MoveTo(byte[] key)
107129
{
108130
using (var marshalKey = new MarshalValueStructure(key))
@@ -250,7 +272,24 @@ public byte[] MoveNextMultiple()
250272
: new KeyValuePair<byte[], byte[]>(keyStruct.ToByteArray(res), valueStruct.ToByteArray(res));
251273
}
252274

253-
public void Put(byte[] key, byte[] value, PutOptions options)
275+
/// <summary>
276+
/// Store by cursor.
277+
/// This function stores key/data pairs into the database.
278+
/// If the function fails for any reason, the state of the cursor will be unchanged.
279+
/// If the function succeeds and an item is inserted into the database, the cursor is always positioned to refer to the newly inserted item.
280+
/// </summary>
281+
/// <param name="key">The key operated on.</param>
282+
/// <param name="value">The data operated on.</param>
283+
/// <param name="options">
284+
/// Options for this operation. This parameter must be set to 0 or one of the values described here.
285+
/// CursorPutOptions.Current - overwrite the data of the key/data pair to which the cursor refers with the specified data item. The key parameter is ignored.
286+
/// CursorPutOptions.NoDuplicateData - enter the new key/data pair only if it does not already appear in the database. This flag may only be specified if the database was opened with MDB_DUPSORT. The function will return MDB_KEYEXIST if the key/data pair already appears in the database.
287+
/// CursorPutOptions.NoOverwrite - enter the new key/data pair only if the key does not already appear in the database. The function will return MDB_KEYEXIST if the key already appears in the database, even if the database supports duplicates (MDB_DUPSORT).
288+
/// CursorPutOptions.ReserveSpace - reserve space for data of the given size, but don't copy the given data. Instead, return a pointer to the reserved space, which the caller can fill in later. This saves an extra memcpy if the data is being generated later.
289+
/// CursorPutOptions.AppendData - append the given key/data pair to the end of the database. No key comparisons are performed. This option allows fast bulk loading when keys are already known to be in the correct order. Loading unsorted keys with this flag will cause data corruption.
290+
/// CursorPutOptions.AppendDuplicateData - as above, but for sorted dup data.
291+
/// </param>
292+
public void Put(byte[] key, byte[] value, CursorPutOptions options)
254293
{
255294
using(var keyMarshalStruct = new MarshalValueStructure(key))
256295
using (var valueMarshalStruct = new MarshalValueStructure(value))

src/LightningDB/LightningCursorExtensions.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,37 @@ namespace LightningDB
1010
/// </summary>
1111
public static class LightningCursorExtensions
1212
{
13+
/// <summary>
14+
/// Delete current key/data pair.
15+
/// This function deletes the key/data pair to which the cursor refers.
16+
/// </summary>
17+
/// <param name="cur">A cursor.</param>
18+
/// <param name="removeAllDuplicateData">if true, delete all of the data items for the current key. This flag may only be specified if the database was opened with MDB_DUPSORT.</param>
1319
public static void Delete(this LightningCursor cur, bool removeAllDuplicateData = true)
1420
{
1521
cur.Delete(
1622
removeAllDuplicateData ? CursorDeleteOption.NoDuplicateData : CursorDeleteOption.None);
1723
}
1824

19-
public static void Put<TKey, TValue>(this LightningCursor cur, TKey key, TValue value, PutOptions options = PutOptions.None)
25+
/// <summary>
26+
/// Store by cursor.
27+
/// This function stores key/data pairs into the database.
28+
/// If the function fails for any reason, the state of the cursor will be unchanged.
29+
/// If the function succeeds and an item is inserted into the database, the cursor is always positioned to refer to the newly inserted item.
30+
/// </summary>
31+
/// <param name="cur">A cursor.</param>
32+
/// <param name="key">The key operated on.</param>
33+
/// <param name="value">The data operated on.</param>
34+
/// <param name="options">
35+
/// Options for this operation. This parameter must be set to 0 or one of the values described here. (optional)
36+
/// CursorPutOptions.Current - overwrite the data of the key/data pair to which the cursor refers with the specified data item. The key parameter is ignored.
37+
/// CursorPutOptions.NoDuplicateData - enter the new key/data pair only if it does not already appear in the database. This flag may only be specified if the database was opened with MDB_DUPSORT. The function will return MDB_KEYEXIST if the key/data pair already appears in the database.
38+
/// CursorPutOptions.NoOverwrite - enter the new key/data pair only if the key does not already appear in the database. The function will return MDB_KEYEXIST if the key already appears in the database, even if the database supports duplicates (MDB_DUPSORT).
39+
/// CursorPutOptions.ReserveSpace - reserve space for data of the given size, but don't copy the given data. Instead, return a pointer to the reserved space, which the caller can fill in later. This saves an extra memcpy if the data is being generated later.
40+
/// CursorPutOptions.AppendData - append the given key/data pair to the end of the database. No key comparisons are performed. This option allows fast bulk loading when keys are already known to be in the correct order. Loading unsorted keys with this flag will cause data corruption.
41+
/// CursorPutOptions.AppendDuplicateData - as above, but for sorted dup data.
42+
/// </param>
43+
public static void Put<TKey, TValue>(this LightningCursor cur, TKey key, TValue value, CursorPutOptions options = CursorPutOptions.None)
2044
{
2145
var keyBytes = cur.ToBytes(key);
2246
var valueBytes = cur.ToBytes(value);

0 commit comments

Comments
 (0)