Can integer keys / values be stored in LevelDB?

前端 未结 4 1504
执笔经年
执笔经年 2021-01-01 02:12

I have searched for key value stores that support integer keys and integer values. LevelDB seems a good option, though I can\'t find any information on whether integer value

4条回答
  •  情歌与酒
    2021-01-01 02:21

    To enlarge on Link's answer, partly because I've just been playing with this exact thing as part of the book I'm writing, you can see the BytewiseComparator results he/she talks about below.

    Another approach is to flip your binary integers to big endian format so they will sort OK with the default comparator. This makes it easier to compose keys. long flippedI = htonl(i);

    Note that LevelDB is very fast. I've done tests on an iPhone4 with 50,000 text-keyed records with secondary keys, so about 100,000 key/value pairs and it screams along.

    It is very easy to write a custom Comparator which is used by your database forevermore and still uses ByteWiseComparator for keys other than your numbers. The biggest issue is deciding which keys are covered by your custom rules or not.

    A trivial way would be to say that all non-integer keys are more than 4 characters long so you assume a 4 byte key is an integer. That means you just need to ensure you add trailing spaces or something else to push that. It's all very arbitrary and up to you but remember the only two pieces of information you have are the key content and its length. There's no other metadata for a given key.

    Part of the results from a sample for standard comparator with int keys starting at 1 and going up by 1 to 1000, using a database with standard BytewiseComparator

    Listing the keys in decimal and hex
     256 ( 100)
     512 ( 200)
     768 ( 300)
       1 (   1)
     257 ( 101)
     513 ( 201)
     769 ( 301)
       2 (   2)
     258 ( 102)
     514 ( 202)
     770 ( 302)
       3 (   3)
     259 ( 103)
     515 ( 203)
     771 ( 303)
    ...
     254 (  fe)
     510 ( 1fe)
     766 ( 2fe)
     255 (  ff)
     511 ( 1ff)
     767 ( 2ff)
    

提交回复
热议问题