Skip to content

Commit 4f29997

Browse files
committed
return db value length from db.Get(key, buffer, ...), fixing #45
1 parent 51ce04d commit 4f29997

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

RocksDbSharp/RocksDb.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ public long Get(byte[] key, long keyLength, byte[] buffer, long offset, long len
129129
unsafe
130130
{
131131
var ptr = Native.Instance.rocksdb_get(Handle, (readOptions ?? DefaultReadOptions).Handle, key, keyLength, out long valLength, cf);
132-
valLength = Math.Min(length, valLength);
133-
Marshal.Copy(ptr, buffer, (int)offset, (int)valLength);
132+
var copyLength = Math.Min(length, valLength);
133+
Marshal.Copy(ptr, buffer, (int)offset, (int)copyLength);
134134
Native.Instance.rocksdb_free(ptr);
135135
return valLength;
136136
}

tests/RocksDbSharpTest/FunctionalTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public void FunctionalTest()
5656
Assert.Equal(8, length);
5757
Assert.Equal(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }, buffer.Take((int)length).ToList());
5858

59+
buffer = new byte[5];
60+
length = db.Get(Encoding.UTF8.GetBytes("key"), buffer, 0, buffer.Length);
61+
Assert.Equal(8, length);
62+
Assert.Equal(new byte[] { 0, 1, 2, 3, 4 }, buffer.Take((int)Math.Min(buffer.Length, length)));
63+
5964
// Write batches
6065
// With strings
6166
using (WriteBatch batch = new WriteBatch()

0 commit comments

Comments
 (0)