Hi.
Upon advices some of you guys have given, I've switched to using a simple 'c structure' based data storage where I just dump the data in memory into a file instead of using a database. It works wonders for the most part, but I have a concern regarding certain types of data which can reach very high amounts of entries.
Currently, I commit all of the data as I don't have a way of detecting which entries have changed. I ran a test and it took 160 seconds to commit 5 million entries(each entry being 256 bytes). This is what I have at the moment:
struct PersistentData
{
ItemData item_data[MAX_ITEMS];
unsigned int num_item_data;
unsigned char removed_item_data[MAX_ITEMS];
/* Other types of data in same format */
};
ItemData *addItemData(PersistentData *pd, ItemData *data)
{
if(pd->num_item_data >= MAX_ITEMS)
{
assert(0);
return 0;
}
memcpy(&pd->item_data[pd->num_item_data], data, sizeof(ItemData));
return &pd->item_data[pd->num_item_data++];
}
void getItemData(PersistentData *pd /* List pointer, max list size, num_results pointer, comparison parameters, etc. */)
{
unsigned int num_res = 0;
for (unsigned int i = 0; i < pd->num_item_data; i++)
{
if(0 /* compare */)
{
list[num_res++] = &pd->item_data[i];
/* Break if reached max list size */
}
}
*num_results = num_res;
}
int removeItemData(PersistentData *pd /* comparison parameters */)
{
for (unsigned int i = 0; i < pd->num_item_data; i++)
{
if(0 /* compare */)
{
pd->removed_item_data[i] = 1;
return 1;
}
}
return 0;
}
int commitPersistentData(PersistentData *pd)
{
/* Open file, etc. */
for (unsigned int i = 0; i < pd->num_item_data; i++)
{
if(pd->removed_item_data[i] == 0)
{
/* Write to file */
}
}
/* Close file */
return 1;
}
I believe it takes so long because I'm writing each entry one by one. When I ignore the removed entries and write the data all at once it takes about 15 seconds! If I could move the last entry into the removed entry I could write them all at once but that is not an option either as another part of the program could have a pointer to last entry.
Is there a complicated or even better, a simple, solution which I could implement?