tags:

views:

53

answers:

1

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 TC fixed length db. Is there a way to store the above as arrays instead of as strings?

+1  A: 

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.

Matthew Flaschen
> The TC value is simply all the numeric values concatenated togetherWhat does that mean? What is the "concatenated value"? Is it stored as a string?My intent behind this question is to save space -- in a SQL db such as Postgres, the number 1948 can be stored as a SMALLINT, that is, 2 bytes. But in TC, it will take 4 bytes. A few bytes here, a few bytes there, but a few hundred million times, will be a lot of wasted space.
punkish
No, I meant concatenating the IEEE values. I added a real example, using hexadecimal notation. You can store shorts in TC too (though your examples aren't all integers); again, both the key and value can be arbitrary byte sequences. Which language API are you using?
Matthew Flaschen