About LibMaDB
This library provides a very simple in memory database manager. It also store database on disk at closing.
Usage
Function madb_open_db opens, or creates, a DB from file. Function madb_close_db closes a DB by writing all data into file and deleting its memory blocks. Function madb_get_item get an item value corresponding to a certain key and return a freshly allocated memory block containing the corresponding item value, or NULL if key can't be found. Function madb_delete_item deletes an item corresponding to a certain key and returns 1 if the item has been correctly deleted, or 0 if the key can't be found. Function madb_update_item updates the item value corresponding to a certain key. If the key does not exist, a new item is created. It returns 1 if the item has been updated, 2 if the item has been created or 0 on error.
Function madb_new_iterator creates a new iterator and set it at the list beginning. It returns a freshly allocated iterator structure or NULL on error. Function madb_iterator_goto_begin set an iterator at the beginning of a list. Function madb_iterator_goto_end set an iterator at the end of a list. Function madb_iterator_valid checks if the iterator position is valid, i.e. pointing to an existing item and returns 0 if current position is out of list, else 1. Function madb_iterator_next moves index of iterator to the next element of the list and returns 0 if the next item can't be found in list, else 1. Function madb_iterator_previous moves index of iterator to the previous element of the list and returns 0 if the next item can't be found in list, else 1. Function madb_iterator_key get the key value of the current indexed item of iterator and return a pointer to a newly allocated memory. Block length is returned by key_length. If the current indexed item is invalid, return value is NULL and key_length is set to 0. Function madb_iterator_item get the item value of the current indexed item of iterator and return a pointer to a newly allocated memory. Block length is returned by item_length. If the current indexed item is invalid, return value is NULL and item_length is set to 0. Function madb_delete_iterator frees iterator structure and returns 1 on success else 0.
Examples
The following example shows how to open, to use and to close a database.
#include#include #include #include int main (int argc, char *argv[]) { int rc = 1; if (argc < 3) { printf ("usage: %s \n", argv[0]); return 1; } struct timeval tv; gettimeofday (&tv, 0); srand (tv.tv_sec ^ tv.tv_usec); madb_t *db = madb_open_db (argv[1]); int nb_new_items = atoi (argv[2]); while ((nb_new_items-- > 0) && (rc)) { short key = rand (); long value = rand (); rc = madb_update_item (db, (char *)&key, sizeof (key), (char *)&value, sizeof (value)); } int i = 0; madb_iterator_t *it = madb_new_iterator (db); for (madb_iterator_goto_begin (it); madb_iterator_valid (it); madb_iterator_next (it)) { size_t s; short *key = (short *) madb_iterator_key (it, &s); long *item = (long *) madb_iterator_item (it, &s); printf ("%d: (%d, %ld)\n", i++, *key, *item); free (key); free (item); } return madb_close_db (db); }
Screen-shot
Shell view
Downloads
Source packages
Debian packages
- LibMaDB version 0.8.1 (i386)
- LibMaDB version 0.8.1 (amd64)
- LibMaDB version 0.8.1 (dev i386)
- LibMaDB version 0.8.1 (dev amd64)
Requirements
This library only need a C compiler to be build.
To install, one only needs to
- unpack the archive,
- do a 'make',
- and a 'make install'.
Changelogs
LibMaDB 0.8.1 (Mon, 11 Aug 2014 00:10:08 +0200)
- Adapt for 64 bit arch
- Fix a memory leak
LibMaDB 0.8 (Sat, 19 Jul 2014 07:23:02 +0200)
- Full Documentation (README, manpage and webpage)
- Full coverage of unit test (> 97%)
- Environment variable VERBOSE_MADB_ set the verbose level
- Improve code readness
- Debian packaging
LibMaDB 0.7 (Wed, 04 Jun 2014 09:37:10 +0200)
- Initial version