curiosity-ai / rocksdb-sharp

.net bindings for the rocksdb by facebook
BSD 2-Clause "Simplified" License
155 stars 38 forks source link

WriteBatch.PutV support #22

Closed devhawk closed 1 year ago

devhawk commented 2 years ago

possible implementation:

public unsafe static void PutV(this WriteBatch writeBatch, IReadOnlyList<ReadOnlyMemory<byte>> keys, IReadOnlyList<ReadOnlyMemory<byte>> values, ColumnFamilyHandle? columnFamily)
{
    var memoryHandles = new List<MemoryHandle>(keys.Count + values.Count);
    try
    {
        var keysList = stackalloc byte*[keys.Count];
        var keysListSizes = stackalloc int[keys.Count];
        for (var i = 0; i < keys.Count; i++)
        {
            var handle = keys[i].Pin();
            memoryHandles.Add(handle);
            keysList[i] = (byte*)handle.Pointer;
            keysListSizes[i] = keys[i].Length;
        }

        var valuesList = stackalloc byte*[values.Count];
        var valuesListSizes = stackalloc int[values.Count];
        for (var i = 0; i < values.Count; i++)
        {
            var handle = values[i].Pin();
            memoryHandles.Add(handle);
            keysList[i] = (byte*)handle.Pointer;
            keysListSizes[i] = values[i].Length;
        }

        if (columnFamily is null)
        {
            writeBatch.Putv(keys.Count, (IntPtr)keysList, (IntPtr)keysListSizes, values.Count, (IntPtr)valuesList, (IntPtr)valuesListSizes);
        }
        else
        {
            writeBatch.PutvCf(columnFamily.Handle, keys.Count, (IntPtr)keysList, (IntPtr)keysListSizes, values.Count, (IntPtr)valuesList, (IntPtr)valuesListSizes);
        }
    }
    finally
    {
        for (int i = 0; i < memoryHandles.Count; i++)
        {
            memoryHandles[i].Dispose();
        }
    }
}
theolivenbaum commented 2 years ago

Hi @devhawk, that's a nice addition - would you mind sending a PR with it? Probably good to handle the case where values.Count > 256 and not stackalloc in that case.