forked from NethermindEth/nethermind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRocksDbExtensions.cs
More file actions
54 lines (40 loc) · 1.7 KB
/
RocksDbExtensions.cs
File metadata and controls
54 lines (40 loc) · 1.7 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
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using RocksDbSharp;
using RocksDbNative = RocksDbSharp.Native;
namespace Nethermind.Db.Rocks;
internal static class RocksDbExtensions
{
private static readonly ReadOptions _defaultReadOptions = new();
internal static unsafe void DangerousReleaseMemory(this RocksDb _, in ReadOnlySpan<byte> span)
{
ref var ptr = ref MemoryMarshal.GetReference(span);
var intPtr = new IntPtr(Unsafe.AsPointer(ref ptr));
RocksDbNative.Instance.rocksdb_free(intPtr);
}
internal static unsafe Span<byte> GetSpan(this RocksDb db, scoped ReadOnlySpan<byte> key, ColumnFamilyHandle? cf = null, ReadOptions? readOptionObj = null)
{
var readOptions = _defaultReadOptions.Handle;
if (readOptionObj is not null) readOptions = readOptionObj.Handle;
var keyLength = (long)key.Length;
nint result;
nint error;
UIntPtr valueLength;
fixed (byte* ptr = key)
{
var keyLengthPtr = (UIntPtr)keyLength;
result = cf is null
? RocksDbNative.Instance.rocksdb_get(db.Handle, readOptions, ptr, keyLengthPtr, out valueLength, out error)
: RocksDbNative.Instance.rocksdb_get_cf(db.Handle, readOptions, cf.Handle, ptr, keyLengthPtr, out valueLength, out error);
}
if (error != IntPtr.Zero)
throw new RocksDbException(error);
if (result == IntPtr.Zero)
return default;
var span = new Span<byte>((void*)result, (int)valueLength);
return span;
}
}