开发者

How to store an array as the value in Tokyo Cabinet?

Is there any way I can store an array of numbers in a Tokyo Cabinet db? For example, I have predictable arrays of values such as

1 => [1, 2, 444, 0.987],
2 => [2, 23, 123, -0.234],
3 => [3, 1, 34, 1.456]

I would like to store the above in a 开发者_JAVA百科TC fixed length db. Is there a way to store the above as arrays instead of as strings?


Tokyo Cabinet allows arbitrary byte sequences as both key and value, so the schema is really up to you. The first step is to decide how to store each number. This could be float, double, or fixed point (e.g. BigDecimal).

Then, you decide how to serialize the array. This could be contiguous:

num => 1 2 444 0.987

The TC value is simply all the numeric values concatenated together. E.g. using 32-bit floats:

num => 0x 3f 80 00 00 40 00 00 00 43 de 00 00 3f 7c ac 08

Another possibility is a linked list:

key => num next_key

1 => 1.1 2
2 => 2 3
3 => 444 4
4 => 0.987 0

You concatenate the current value and the next key in the array

This provides the traditional benefits of a linked list, including inserting in the middle easily.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜