D_store misc

overview | tutorial | download | API | misc | faq | license

.csv files

Currently d_store supports reading and writing comma separated value files. It can only store and read D_VALUE_INT and D_VALUE_STRING from there. Support for D_VALUE_DOUBLE and D_VALUE_UINT is upcoming.

It can however import these files from many flavours, like tab separated, with or without quotes, etc.

So to 'simulate' a persistant store, just make sure to open a .csv file, and save it at regular intervals, preferably save to a new temp file, then on success, move the new file over the old file. That should be more then sufficient data persistant for most uses.

Use d_store_to_csv(file) to save and d_store_from_csv(store, file) to load. Both functions read or write to a file.


UTF support

D_store uses native c-strings as storage, when using D_TYPE_STRING and therefor does support UTF-8, but not UTF-16 or UTF-32 (due to null chars).

But d_store imposes no real meaning on strings, it will fully support any data type without null chars. And you can always use D_TYPE_DATA for storing any other encoded string. However you cannot save D_TYPE_DATA to .csv files, which is the only format d_store currently can read or write.


tagging

D_store is exceptionally good for tagging. The way to organise your data is:

d_row * item = d_insert(store, D_UINT(0), "item", "test item", 0);
unsigned int item_index = d_row_get_index(item);
d_row_set(item, 1, D_UINT(item_index));

d_insert(store, D_INT(item_index), "tag", "test tag", 0);

Then you can create sets of items that are tagged with things like this:

d_store * all_tags = d_select(store, 2, "tag"); // get all tags

d_store * tag_store = d_select(all_tags, 3, "test tag");
d_store * item_store = d_store_create_empty(tag_store);

d_row * row;
for (row = d_first(tag_store); row; row = d_next(tag_store)) {
    d_store_add_row_with_index(item_store,
        D_TOUINT(d_row_get(row, 1)));
}
d_store_free(tag_store);
d_store_free(all_tags);

This is how you translate from a tag, to the items that are tagged. These items will be in one d_store, you can create many of these and do logical operations on them, like d_and, to select all items that belong to all the selected tags ...


reverse tagging

Doing the reverse of tagging: getting all the tags that a set of items still have in common. This is a bit more tricky, and there may be better ways.

Create a store representing all the tags in the system, and create a new database that contains one tag per row.

Create a d_store representing all rows in the new tag database (our temporal result set). Then per item in your set, translate them in to the rows in the new database, and d_and the item tags and the temporal result store together. You can stop if the temporal result store becomes empty.

After d_and-ing the last item, the tags still left in the temporal result set are shared among the items in the original set.

Alternatively, you can create the new tag database and count the tags you put in there, and only put them there if they are tagged in your result set. Any tag that does not count up to the amount of items in the sub set is not shared.

Last modified: 2007-11-19 20:17 GMT