Skip to content

Commit cd0cffa

Browse files
committed
handle non-existent keys in db.Get(key, buffer, ...), fixing #44
1 parent 4f29997 commit cd0cffa

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

RocksDbSharp/RocksDb.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,42 @@ public byte[] Get(byte[] key, long keyLength, ColumnFamilyHandle cf = null, Read
119119
return Native.Instance.rocksdb_get(Handle, (readOptions ?? DefaultReadOptions).Handle, key, keyLength, cf);
120120
}
121121

122+
/// <summary>
123+
/// Reads the contents of the database value associated with <paramref name="key"/>, if present, into the supplied
124+
/// <paramref name="buffer"/> at <paramref name="offset"/> up to <paramref name="length"/> bytes, returning the
125+
/// length of the value in the database, or -1 if the key is not present.
126+
/// </summary>
127+
/// <param name="key"></param>
128+
/// <param name="buffer"></param>
129+
/// <param name="offset"></param>
130+
/// <param name="length"></param>
131+
/// <param name="cf"></param>
132+
/// <param name="readOptions"></param>
133+
/// <returns>The actual length of the database field if it exists, otherwise -1</returns>
122134
public long Get(byte[] key, byte[] buffer, long offset, long length, ColumnFamilyHandle cf = null, ReadOptions readOptions = null)
123135
{
124136
return Get(key, key.GetLongLength(0), buffer, offset, length, cf, readOptions);
125137
}
126138

139+
/// <summary>
140+
/// Reads the contents of the database value associated with <paramref name="key"/>, if present, into the supplied
141+
/// <paramref name="buffer"/> at <paramref name="offset"/> up to <paramref name="length"/> bytes, returning the
142+
/// length of the value in the database, or -1 if the key is not present.
143+
/// </summary>
144+
/// <param name="key"></param>
145+
/// <param name="buffer"></param>
146+
/// <param name="offset"></param>
147+
/// <param name="length"></param>
148+
/// <param name="cf"></param>
149+
/// <param name="readOptions"></param>
150+
/// <returns>The actual length of the database field if it exists, otherwise -1</returns>
127151
public long Get(byte[] key, long keyLength, byte[] buffer, long offset, long length, ColumnFamilyHandle cf = null, ReadOptions readOptions = null)
128152
{
129153
unsafe
130154
{
131155
var ptr = Native.Instance.rocksdb_get(Handle, (readOptions ?? DefaultReadOptions).Handle, key, keyLength, out long valLength, cf);
156+
if (ptr == IntPtr.Zero)
157+
return -1;
132158
var copyLength = Math.Min(length, valLength);
133159
Marshal.Copy(ptr, buffer, (int)offset, (int)copyLength);
134160
Native.Instance.rocksdb_free(ptr);

tests/RocksDbSharpTest/FunctionalTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public void FunctionalTest()
6161
Assert.Equal(8, length);
6262
Assert.Equal(new byte[] { 0, 1, 2, 3, 4 }, buffer.Take((int)Math.Min(buffer.Length, length)));
6363

64+
length = db.Get(Encoding.UTF8.GetBytes("bogus"), buffer, 0, buffer.Length);
65+
Assert.Equal(-1, length);
66+
6467
// Write batches
6568
// With strings
6669
using (WriteBatch batch = new WriteBatch()

0 commit comments

Comments
 (0)