• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H03-May-2022-1,5521,294

AUTHORSH A D21-Nov-200149 41

COPYINGH A D19-Nov-20011.5 KiB3123

ChangeLogH A D08-Mar-20021.1 KiB3326

FORMAT_DESCRH A D04-Dec-20013.6 KiB10871

INSTALLH A D26-Aug-20017.6 KiB183143

Makefile.amH A D04-Dec-200148 74

Makefile.inH A D08-Mar-200212.9 KiB436352

NEWSH A D08-Mar-2002696 2720

READMEH A D21-Nov-20018.9 KiB321186

aclocal.m4H A D08-Mar-2002143.8 KiB4,3483,839

config.guessH A D08-Mar-200237.5 KiB1,3221,141

config.h.inH A D08-Mar-20024.3 KiB159110

config.subH A D08-Mar-200228.3 KiB1,4441,303

configureH A D08-Mar-2002323.1 KiB10,8558,808

configure.acH A D08-Mar-20021.1 KiB5644

depcompH A D08-Mar-200212.2 KiB437298

install-shH A D08-Mar-20025.4 KiB252153

ltmain.shH A D16-Sep-2001135.7 KiB4,9853,988

missingH A D08-Mar-200210 KiB337263

mkinstalldirsH A D08-Mar-20021.8 KiB10272

README

1
2
3
4                              .:. PUREDB 2 .:.
5
6
7
8           ------------------------ BLURB ------------------------
9
10
11PureDB is a portable and tiny set of libraries for creating and reading
12constant databases. It manages data files that contains text or binary
13key/data pairs of arbitrary sizes. Lookups are very fast (normally only one
14disk access to match a hash value), overhead is low (a database is 1028
15bytes plus only 16 extra bytes per record), multiple concurrent read access
16are supported, and databases can be up to 4 Gb long, and they are portable
17accross architectures.
18
19
20        ------------------------ COMPILATION ------------------------
21
22
23Compiling PureDB is a simple matter of :
24
25./configure
26make install
27
28Static libraries, shared libraries and links are installed in
29/usr/local/lib/libpuredb_read* and /usr/local/lib/libpuredb_write* .
30
31Header files are installed as /usr/local/include/puredb_read.h and
32/usr/local/include/puredb_write.h .
33
34The library that creates databases is different from the library that reads
35them, because these different tasks are usually handled by separate
36applications. However, you can safely link both libraries together.
37
38
39           ------------------------ USAGE ------------------------
40
41
42To compile a program with puredb, you have to include the following headers :
43
44#include <puredb_read.h>
45
46and/or
47
48#include <puredb_write.h>
49
50If your application only reads PureDB databases, just include the first
51header. If it only writes databases, just include the second one. It it does
52both, include both.
53
54The same thing goes for linker options : you have to link against
55libpuredb_read and/or libpuredb_write :
56
57cc -o myapp1 myapp1.c -lpuredb_read
58cc -o myapp2 myapp2.c -lpuredb_write
59cc -o myapp3 myapp3.c -lpuredb_read -lpuredb_write
60
61
62------------------------ API FOR CREATING DATABASES ------------------------
63
64
65Creating a new database is usually a 4-step operation :
66
671) Create the database files and initialize the internal structures with
68puredbw_open() .
69
702) Feed key/data pairs with puredbw_add() or puredbw_add_s() .
71
723) Complete and close the database files with puredbw_close() .
73
744) Free the internal structures with puredbw_free() .
75
76Here are the functions :
77
78
79int puredbw_open(PureDBW * const dbw,
80                 const char * const file_index,
81                 const char * const file_data,
82                 const char * const file_final);
83
84This function takes a point to an already allocated PureDBW structure, and
85three file names. file_index and file_data are temporary files, needed to
86create the database. They will be automatically deleted, and the final
87database will atomically be stored in file_final.
88
89Return value: 0 if everything is ok, a negative value if something went wrong.
90
91
92
93int puredbw_add(PureDBW * const dbw,
94                const char * const key, const size_t key_len,
95                const char * const content, const size_t content_len);
96
97This function stores a new key/data pair in the database. key is a pointer
98to the key, that is key_len long. Same thing for content and content_len.
99These buffers can handle binary data, and can have any size up to 4 Gb.
100
101Return value: 0 if everything is ok, a negative value if something went wrong.
102
103
104
105int puredbw_add_s(PureDBW * const dbw,
106                  const char * const key, const char * const content);
107
108This function is a shortcut to puredbw_add(), designed to store 0-terminated
109strings. It's equivalent to call puredbw_add() with strlen(key) and
110strlen(content) as parameters 3 and 5.
111
112Return value: 0 if everything is ok, a negative value if something went wrong.
113
114
115
116int puredbw_close(PureDBW * const dbw);
117
118This function performs a quick sort of the hashed values, writes them to the
119disk, merges index and data files, rename the result to the final file name
120and delete the old files. You must call this after having inserted all
121values in the database.
122
123Return value: 0 if everything is ok, a negative value if something went wrong.
124
125
126
127void puredbw_free(PureDBW * const dbw);
128
129This function frees all memory chunks allocated by puredbw_open(),
130puredbw_add() and puredbw_add_s() . You must call this either after a
131puredbw_close(), or after something went wrong if you decide to abort.
132
133
134Here's an example, that creates a new database, and inserts three key/data
135pairs into it.
136
137
138#include <stdio.h>
139#include <stdlib.h>
140#include <puredb_write.h>
141
142int main(void)
143{
144    PureDBW dbw;
145
146    if (puredbw_open(&dbw, "puredb.index", "puredb.data", "puredb.pdb") != 0) {
147        perror("Can't create the database");
148        goto end;
149    }
150    if (puredbw_add_s(&dbw, "key", "content") != 0 ||
151        puredbw_add_s(&dbw, "key2", "content2") != 0 ||
152        puredbw_add_s(&dbw, "key42", "content42") != 0) {
153        perror("Error while inserting key/data pairs");
154        goto end;
155    }
156    if (puredbw_close(&dbw) != 0) {
157        perror("Error while closing the database");
158    }
159
160    end:
161    puredbw_free(&dbw);
162
163    return 0;
164}
165
166
167 ------------------------ API FOR READING DATABASES ------------------------
168
169
170Reading the content of a database is usually a 5-step operation :
171
1721) Open the database files and initialize the internal structures with
173puredb_open() .
174
1752) Perform a lookup for a key with puredb_find() or puredb_find_s() .
176
1773) If the key is found, read the associated data with puredb_read() .
178
179[process the data]
180
1814) Free the data with puredb_read_free() .
182
183[repeat steps 2, 3 and 4 to read more key/data pairs]
184
1855) Close the database and free the internal structures with puredb_close() .
186
187Here are the functions :
188
189
190int puredb_open(PureDB * const db, const char *dbfile);
191
192This function opens an existing database, stored in a file named dbfile, and
193initializes a preallocated PureDB structure.
194
195Return value: 0 if everything is ok, a negative value if something went wrong.
196
197
198
199int puredb_find(PureDB * const db, const char * const tofind,
200                const size_t tofind_len, off_t * const retpos,
201                size_t * const retlen);
202
203This function looks the database for a key matching tofind, whoose length is
204tofind_len. After a successful match, retpos contains the offset to the
205first byte of the matching data, and retlen is the length of the data.
206
207Return value: 0 if the key was found.
208             -1 if the key was not found.
209             -2 if the database is corrupted.
210             -3 if a system error occured.
211
212
213
214int puredb_find_s(PureDB * const db, const char * const tofind,
215                  off_t * const retpos, size_t * const retlen);
216
217This function is a shortcut to puredb_find() for text keys, which computes
218strlen(tofind) as a key length.
219
220Return value: 0 if the key was found.
221             -1 if the key was not found.
222             -2 if the database is corrupted.
223             -3 if a system error occured.
224
225
226
227void *puredb_read(PureDB * const db, const off_t offset, const size_t len);
228
229This function reads len bytes in the database file, starting at offset. A
230large enough buffer is allocated, filled and returned. It is guaranteed to
231be terminated by an extra \0, so it's safe to process C-strings returned by
232that function.
233
234Return value: the address of a buffer with the data, or NULL if something
235went wrong (no memory, corrupted file, no permission, etc) .
236
237
238
239void puredb_read_free(void *data);
240
241Frees a buffer allocated by puredb_read() .
242
243
244
245int puredb_getfd(PureDB * const db);
246
247Returns the file descriptor opened for the database, just in case you want
248to read the data by yourself. It can be interesting if the data is too large
249to be stored in memory.
250
251Return value: a file descriptor. or -1 if none was allocated (error).
252
253
254
255off_t puredb_getsize(PureDB * const db);
256
257This function returns the size of the file handling the database, in bytes.
258
259Return value: the size of the file.
260
261
262
263int puredb_close(PureDB * const db);
264
265This function closes the database and frees all related internal structures.
266Don't forget to call this even if something went wrong, and you decide to
267abort.
268
269
270Here's an example, that reads a previously created database.
271
272
273#include <stdio.h>
274#include <stdlib.h>
275#include <puredb_read.h>
276
277int main(void)
278{
279    PureDB db;
280    off_t retpos;
281    size_t retlen;
282    char *data;
283
284    if (puredb_open(&db, "puredb.pdb") != 0) {
285        perror("Can't open the database");
286        goto end;
287    }
288    if (puredb_find_s(&db, "key42", &retpos, &retlen) != 0) {
289        fprintf(stderr, "The key wasn't found\n");
290        goto end;
291    }
292    if ((data = puredb_read(&db, retpos, retlen)) != NULL) {
293        printf("The maching data is : [%s]\n", data);
294        puredb_read_free(data);
295    }
296    end:
297    if (puredb_close(&db) != 0) {
298        perror("The database couldn't be properly closed");
299    }
300
301    return 0;
302}
303
304
305    ------------------------ DOWNLOADING PUREDB ------------------------
306
307
308PureDB home page is : http://www.pureftpd.org/puredb/ .
309
310If you have question, suggestions or patches, feel free to get in touch with
311me. Newbies and silly ideas are welcome.
312
313
314Thank you,
315
316                        -Frank DENIS "Jedi/Sector One" <j@pureftpd.org> .
317
318
319
320
321