curiosity-ai / rocksdb-sharp

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

Add Try* methods for operations that may fail #2

Closed devhawk closed 3 years ago

devhawk commented 3 years ago

Suggestions:

(Note, I'd be willing to contribute these implementations if the project is interested in accepting them)

// TryListColumnFamilies can be implemented by RocksDbSharp users, but it would be nice if it was built in
private static bool TryListColumnFamilies(DbOptions options, string name, [MaybeNullWhen(false)] out string[] strings)
{
    var result = Native.Instance.rocksdb_list_column_families(options.Handle, name, out UIntPtr lencf, out IntPtr errptr);
    if (errptr != IntPtr.Zero)
    {
        strings = null!;
        return false;
    }

    try
    {
        IntPtr[] ptrs = new IntPtr[(ulong)lencf];
        Marshal.Copy(result, ptrs, 0, (int)lencf);
        strings = new string[(ulong)lencf];
        for (ulong i = 0; i < (ulong)lencf; i++)
            strings[i] = Marshal.PtrToStringAnsi(ptrs[i]) ?? throw new Exception("Marshal.PtrToStringAnsi failed");

        return true;
    }
    finally
    {
        Native.Instance.rocksdb_list_column_families_destroy(result, lencf);
    }
}

Other

theolivenbaum commented 3 years ago

Hi @devhawk, happy to take the contribution! Could you open a pull request for it?

devhawk commented 3 years ago

Happy to make it...about 10 days from now. Heading out for a short trip, I doubt I'll have time to submit the PR until I get back.