Spreads / Spreads.LMDB

Low-level zero-overhead and the fastest LMDB .NET wrapper with some additional native methods useful for Spreads
http://docs.dataspreads.io/spreads/libs/lmdb/api/README.html
Mozilla Public License 2.0
80 stars 9 forks source link

No support for string keys? #21

Closed aliostad closed 5 years ago

aliostad commented 5 years ago

On the same topic, I need to store string for keys. From my understanding, keys and values are bytes in LMDB but in this library they are meant to be struct.

Is it not possible from your library? I understand that the struct makes the use of Spans possible but can be limiting. Happy also to open an issue for it so the answer stays.

It is certainly possible to create a struct to represent a fixed-length string with implicit conversion as below but a bit clunky.

struct TenCharAsciiString
{
    public byte char0;
    public byte char1;
    public byte char2;
    public byte char3;
    public byte char4;
    public byte char5;
    public byte char6;
    public byte char7;
    public byte char8;
    public byte char9;

    public static implicit operator string(TenCharAsciiString s)
    {
        //...
    }
    public static implicit operator TenCharAsciiString(string s)
    {
        //...
    }
}
buybackoff commented 5 years ago

I answered in #19.

Also you may use Symbol structs from here: https://github.com/Spreads/Spreads/blob/master/src/Spreads.Core/DataTypes/Symbol.cs

if you need fixed-size structs for strings.

It is all not particularly important how you get a byte pointer and its length.

Generic methods in this library are just helper utilities for blittable structs that convert them to DirectBuffer anyway. See the implementation of methods that accept generics - they just redirect to overloads with DirectBuffer and do the fast conversion from structs to DirectBuffer on the stack.

buybackoff commented 5 years ago

I could create an overload for string keys, but then need to decide on default encoding and add this as a parameter. This should be out of scope of this library. Example is already in #19.