Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions RocksDbSharp/Native.Marshaled.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/*
/*
The functions in this file provide some wrappers around the lowest level C API to aid in marshalling.
This is kept separate so that the lowest level imports can be kept as close as possible to c.h from rocksdb.
See Native.Raw.cs for more information.
*/
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using Transitional;
Expand Down Expand Up @@ -155,7 +156,37 @@ public byte[] rocksdb_get(
rocksdb_free(resultPtr);
return result;
}


public unsafe Span<byte> rocksdb_get_span(
IntPtr db,
IntPtr read_options,
byte[] key,
long keyLength,
out IntPtr errptr,
ColumnFamilyHandle cf = null)
{
UIntPtr skLength = (UIntPtr)keyLength;
var resultPtr = cf == null
? rocksdb_get(db, read_options, key, skLength, out UIntPtr valueLength, out errptr)
: rocksdb_get_cf(db, read_options, cf.Handle, key, skLength, out valueLength, out errptr);
if (errptr != IntPtr.Zero)
return null;
if (resultPtr == IntPtr.Zero)
return null;
Span<byte> span = new Span<byte>((void*)resultPtr, (int)valueLength);
//MemoryMarshal.GetReference(span);


return span;
}

public unsafe void rocksdb_release_span(in Span<byte> span)
{
ref byte ptr = ref MemoryMarshal.GetReference(span);
IntPtr intPtr = new IntPtr(Unsafe.AsPointer(ref ptr));
rocksdb_free(intPtr);
}

/// <summary>
/// Executes a multi_get with automatic marshalling
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions RocksDbSharp/Native.Wrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ public byte[] rocksdb_get(
throw new RocksDbException(errptr);
return result;
}

public Span<byte> rocksdb_get_span(
IntPtr db,
IntPtr read_options,
byte[] key,
long keyLength = 0,
ColumnFamilyHandle cf = null)
{
var result = rocksdb_get_span(db, read_options, key, keyLength == 0 ? key.Length : keyLength, out IntPtr errptr, cf);
if (errptr != IntPtr.Zero)
throw new RocksDbException(errptr);
return result;
}

public KeyValuePair<string, string>[] rocksdb_multi_get(
IntPtr db,
Expand Down
17 changes: 16 additions & 1 deletion RocksDbSharp/RocksDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,22 @@ public byte[] Get(byte[] key, ColumnFamilyHandle cf = null, ReadOptions readOpti
{
return Get(key, key.GetLongLength(0), cf, readOptions);
}

public byte[] Get(byte[] key, long keyLength, ColumnFamilyHandle cf = null, ReadOptions readOptions = null)
{
return Native.Instance.rocksdb_get(Handle, (readOptions ?? DefaultReadOptions).Handle, key, keyLength, cf);
}

public Span<byte> GetSpan(byte[] key, ColumnFamilyHandle cf = null, ReadOptions readOptions = null)
{
return GetSpan(key, key.GetLongLength(0), cf, readOptions);
}

public Span<byte> GetSpan(byte[] key, long keyLength, ColumnFamilyHandle cf = null, ReadOptions readOptions = null)
{
return Native.Instance.rocksdb_get_span(Handle, (readOptions ?? DefaultReadOptions).Handle, key, keyLength, cf);
}

/// <summary>
/// Reads the contents of the database value associated with <paramref name="key"/>, if present, into the supplied
/// <paramref name="buffer"/> at <paramref name="offset"/> up to <paramref name="length"/> bytes, returning the
Expand Down Expand Up @@ -306,5 +316,10 @@ public void CompactRange(string start, string limit, ColumnFamilyHandle cf = nul
encoding = Encoding.UTF8;
CompactRange(encoding.GetBytes(start), encoding.GetBytes(limit), cf);
}

public void DangerousReleaseMemory(in Span<byte> span)
{
Native.Instance.rocksdb_release_span(in span);
}
}
}
3 changes: 2 additions & 1 deletion RocksDbSharp/RocksDbSharp.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\Versions.targets.include"/>
<Import Project="..\Versions.targets.include" />
<PropertyGroup>
<Title>RocksDbSharp</Title>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
Expand Down Expand Up @@ -40,5 +40,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
</ItemGroup>
</Project>