curiosity-ai/rocksdb-sharp

WriteBatch.PutV support

devhawk opened this issue · 1 comments

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();
        }
    }
}

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.