README 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. .:. PUREDB 2 .:.
  2. ------------------------ BLURB ------------------------
  3. PureDB is a portable and tiny set of libraries for creating and reading
  4. constant databases. It manages data files that contains text or binary
  5. key/data pairs of arbitrary sizes. Lookups are very fast (normally only one
  6. disk access to match a hash value), overhead is low (a database is 1028
  7. bytes plus only 16 extra bytes per record), multiple concurrent read access
  8. are supported, databases can be up to 4 Gb long, and they are portable
  9. across architectures.
  10. ------------------------ COMPILATION ------------------------
  11. Compiling PureDB is a simple matter of:
  12. ./configure
  13. make install
  14. Static libraries, shared libraries and links are installed in
  15. /usr/local/lib/libpuredb_read* and /usr/local/lib/libpuredb_write* .
  16. Header files are installed as /usr/local/include/puredb_read.h and
  17. /usr/local/include/puredb_write.h .
  18. The library that creates databases is different from the library that reads
  19. them, because these different tasks are usually handled by separate
  20. applications. However, you can safely link both libraries together.
  21. ------------------------ USAGE ------------------------
  22. To compile a program with puredb, you have to include the following headers:
  23. #include <puredb_read.h>
  24. and/or
  25. #include <puredb_write.h>
  26. If your application only reads PureDB databases, just include the first
  27. header. If it only writes databases, just include the second one. It it does
  28. both, include both.
  29. The same thing goes for linker options: you have to link against
  30. libpuredb_read and/or libpuredb_write:
  31. cc -o myapp1 myapp1.c -lpuredb_read
  32. cc -o myapp2 myapp2.c -lpuredb_write
  33. cc -o myapp3 myapp3.c -lpuredb_read -lpuredb_write
  34. ------------------------ API FOR CREATING DATABASES ------------------------
  35. Creating a new database is usually a 4-step operation:
  36. 1) Create the database files and initialize the internal structures with
  37. puredbw_open() .
  38. 2) Feed key/data pairs with puredbw_add() or puredbw_add_s() .
  39. 3) Complete and close the database files with puredbw_close() .
  40. 4) Free the internal structures with puredbw_free() .
  41. Here are the functions:
  42. int puredbw_open(PureDBW * const dbw,
  43. const char * const file_index,
  44. const char * const file_data,
  45. const char * const file_final);
  46. This function takes a point to an already allocated PureDBW structure, and
  47. three file names. file_index and file_data are temporary files, needed to
  48. create the database. They will be automatically deleted, and the final
  49. database will atomically be stored in file_final.
  50. Return value: 0 if everything is ok, a negative value if something went wrong.
  51. int puredbw_add(PureDBW * const dbw,
  52. const char * const key, const size_t key_len,
  53. const char * const content, const size_t content_len);
  54. This function stores a new key/data pair in the database. key is a pointer
  55. to the key, that is key_len long. Same thing for content and content_len.
  56. These buffers can handle binary data, and can have any size up to 4 Gb.
  57. Return value: 0 if everything is ok, a negative value if something went wrong.
  58. int puredbw_add_s(PureDBW * const dbw,
  59. const char * const key, const char * const content);
  60. This function is a shortcut to puredbw_add(), designed to store 0-terminated
  61. strings. It's equivalent to call puredbw_add() with strlen(key) and
  62. strlen(content) as parameters 3 and 5.
  63. Return value: 0 if everything is ok, a negative value if something went wrong.
  64. int puredbw_close(PureDBW * const dbw);
  65. This function performs a quick sort of the hashed values, writes them to the
  66. disk, merges index and data files, rename the result to the final file name
  67. and delete the old files. You must call this after having inserted all
  68. values in the database.
  69. Return value: 0 if everything is ok, a negative value if something went wrong.
  70. void puredbw_free(PureDBW * const dbw);
  71. This function frees all memory chunks allocated by puredbw_open(),
  72. puredbw_add() and puredbw_add_s() . You must call this either after a
  73. puredbw_close(), or after something went wrong if you decide to abort.
  74. Here's an example, that creates a new database, and inserts three key/data
  75. pairs into it.
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78. #include <puredb_write.h>
  79. int main(void)
  80. {
  81. PureDBW dbw;
  82. if (puredbw_open(&dbw, "puredb.index", "puredb.data", "puredb.pdb") != 0) {
  83. perror("Can't create the database");
  84. goto end;
  85. }
  86. if (puredbw_add_s(&dbw, "key", "content") != 0 ||
  87. puredbw_add_s(&dbw, "key2", "content2") != 0 ||
  88. puredbw_add_s(&dbw, "key42", "content42") != 0) {
  89. perror("Error while inserting key/data pairs");
  90. goto end;
  91. }
  92. if (puredbw_close(&dbw) != 0) {
  93. perror("Error while closing the database");
  94. }
  95. end:
  96. puredbw_free(&dbw);
  97. return 0;
  98. }
  99. ------------------------ API FOR READING DATABASES ------------------------
  100. Reading the content of a database is usually a 5-step operation:
  101. 1) Open the database files and initialize the internal structures with
  102. puredb_open() .
  103. 2) Perform a lookup for a key with puredb_find() or puredb_find_s() .
  104. 3) If the key is found, read the associated data with puredb_read() .
  105. [process the data]
  106. 4) Free the data with puredb_read_free() .
  107. [repeat steps 2, 3 and 4 to read more key/data pairs]
  108. 5) Close the database and free the internal structures with puredb_close() .
  109. Here are the functions:
  110. int puredb_open(PureDB * const db, const char *dbfile);
  111. This function opens an existing database, stored in a file named dbfile, and
  112. initializes a preallocated PureDB structure.
  113. Return value: 0 if everything is ok, a negative value if something went wrong.
  114. int puredb_find(PureDB * const db, const char * const tofind,
  115. const size_t tofind_len, off_t * const retpos,
  116. size_t * const retlen);
  117. This function looks the database for a key matching tofind, whose length is
  118. tofind_len. After a successful match, retpos contains the offset to the
  119. first byte of the matching data, and retlen is the length of the data.
  120. Return value: 0 if the key was found.
  121. -1 if the key was not found.
  122. -2 if the database is corrupted.
  123. -3 if a system error occurred.
  124. int puredb_find_s(PureDB * const db, const char * const tofind,
  125. off_t * const retpos, size_t * const retlen);
  126. This function is a shortcut to puredb_find() for text keys, which computes
  127. strlen(tofind) as a key length.
  128. Return value: 0 if the key was found.
  129. -1 if the key was not found.
  130. -2 if the database is corrupted.
  131. -3 if a system error occurred.
  132. void *puredb_read(PureDB * const db, const off_t offset, const size_t len);
  133. This function reads len bytes in the database file, starting at offset. A
  134. large enough buffer is allocated, filled and returned. It is guaranteed to
  135. be terminated by an extra \0, so it's safe to process C-strings returned by
  136. that function.
  137. Return value: the address of a buffer with the data, or NULL if something
  138. went wrong (no memory, corrupted file, no permission, etc) .
  139. void puredb_read_free(void *data);
  140. Frees a buffer allocated by puredb_read() .
  141. int puredb_getfd(PureDB * const db);
  142. Returns the file descriptor opened for the database, just in case you want
  143. to read the data by yourself. It can be interesting if the data is too large
  144. to be stored in memory.
  145. Return value: a file descriptor. or -1 if none was allocated (error).
  146. off_t puredb_getsize(PureDB * const db);
  147. This function returns the size of the file handling the database, in bytes.
  148. Return value: the size of the file.
  149. int puredb_close(PureDB * const db);
  150. This function closes the database and frees all related internal structures.
  151. Don't forget to call this even if something went wrong, and you decide to
  152. abort.
  153. Here's an example, that reads a previously created database.
  154. #include <stdio.h>
  155. #include <stdlib.h>
  156. #include <puredb_read.h>
  157. int main(void)
  158. {
  159. PureDB db;
  160. off_t retpos;
  161. size_t retlen;
  162. char *data;
  163. if (puredb_open(&db, "puredb.pdb") != 0) {
  164. perror("Can't open the database");
  165. goto end;
  166. }
  167. if (puredb_find_s(&db, "key42", &retpos, &retlen) != 0) {
  168. fprintf(stderr, "The key wasn't found\n");
  169. goto end;
  170. }
  171. if ((data = puredb_read(&db, retpos, retlen)) != NULL) {
  172. printf("The maching data is: [%s]\n", data);
  173. puredb_read_free(data);
  174. }
  175. end:
  176. if (puredb_close(&db) != 0) {
  177. perror("The database couldn't be properly closed");
  178. }
  179. return 0;
  180. }
  181. If you have question, suggestions or patches, feel free to get in touch with
  182. me. Newbies and silly ideas are welcome.
  183. Thank you,
  184. -Frank DENIS "Jedi/Sector One" <j at pureftpd dot org> .