curiosity-ai / rocksdb-sharp

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

Seems like HasKey doesn't work well? #34

Closed danielkornev closed 1 year ago

danielkornev commented 1 year ago

I've posted a question on StackOverflow yesterday regarding this:

https://stackoverflow.com/questions/74257148/how-to-use-rocksdb-to-make-many-concurrent-writes-and-reads-without-database-loo

In general, HasKey only allows one to work with Span while Get and Put methods operate on good ol' strings as well. In any case, checks for HasKey always return "false". Yet if I call Get in try...catch I can retrieve most if not all keys. Why is that?

theolivenbaum commented 1 year ago

Hi @danielkornev, from a quick check on your SO question, I think you're missing encoding the key: StringKey will use by default UTF8 encoding, while your code sample seems to be casting an in-memory string to a byte array and then calling the HasKey method:

            var charSpan = storageKey.AsSpan();
            var byteSpan = MemoryMarshal.Cast<char, byte>(charSpan);     <------- this is not converting it to UTF8

Can you try instead to use Encoding.UTF8.GetBytes(key) when reading?