author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Fri, 19 Nov 2010 16:32:43 +0100 | |
changeset 160 | 26414889dded |
parent 147 | f9015072361f |
permissions | -rw-r--r-- |
147
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
1 |
/** |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
2 |
* berkdb.c (C) 2010 Markus Bröker |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
3 |
* |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
4 |
*/ |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
5 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
6 |
#include <stdio.h> |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
7 |
#include <stdlib.h> |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
8 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
9 |
#include <sys/types.h> |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
10 |
#include <limits.h> |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
11 |
#include <db.h> |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
12 |
#include <fcntl.h> |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
13 |
#include <errno.h> |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
14 |
#include <string.h> |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
15 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
16 |
DBT key; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
17 |
DBT data; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
18 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
19 |
/* initialize the db library and open a db file */ |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
20 |
DB *dbinit (DB * db, char *filename) |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
21 |
{ |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
22 |
int ret; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
23 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
24 |
printf ("Initializing berkley db...\n"); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
25 |
if ((ret = db_create (&db, NULL, 0)) != 0) { |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
26 |
perror ("db_create"); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
27 |
return NULL; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
28 |
} |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
29 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
30 |
bzero (&key, sizeof (key)); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
31 |
bzero (&data, sizeof (data)); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
32 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
33 |
printf ("Opening berkdb.db flat database file...\n"); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
34 |
if ((ret = db->open (db, NULL, filename, NULL, DB_BTREE, DB_CREATE, 0)) != 0) { |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
35 |
perror ("db->open"); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
36 |
return NULL; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
37 |
} |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
38 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
39 |
return db; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
40 |
} |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
41 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
42 |
/* insert a key/value pair into the database */ |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
43 |
int dbinsert (DB * db, int id, char *description) |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
44 |
{ |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
45 |
key.data = &id; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
46 |
key.size = sizeof (int); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
47 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
48 |
data.data = description; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
49 |
data.size = strlen (description) + 1; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
50 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
51 |
return db->put (db, NULL, &key, &data, DB_NOOVERWRITE); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
52 |
} |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
53 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
54 |
/* get the associated value from the database for the key 'id' */ |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
55 |
char *dbget (DB * db, int id) |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
56 |
{ |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
57 |
key.data = &id; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
58 |
key.size = sizeof (int); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
59 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
60 |
if (db->get (db, NULL, &key, &data, 0) == 0) |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
61 |
return (char *)data.data; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
62 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
63 |
return NULL; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
64 |
} |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
65 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
66 |
/* iterate over the db and print all values */ |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
67 |
void dblist (DB * db) |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
68 |
{ |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
69 |
int ret; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
70 |
DBC *dbc; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
71 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
72 |
if (db->cursor (db, NULL, &dbc, 0) != 0) { |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
73 |
perror ("db->cursor"); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
74 |
return; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
75 |
} |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
76 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
77 |
bzero (&key, sizeof (key)); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
78 |
while ((ret = dbc->get (dbc, &key, &data, DB_NEXT)) == 0) { |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
79 |
printf ("KEY[%02d] => %s\n", *(int *)key.data, (char *)data.data); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
80 |
} |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
81 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
82 |
if (dbc != NULL) { |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
83 |
dbc->close (dbc); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
84 |
} else |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
85 |
perror ("dbc"); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
86 |
} |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
87 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
88 |
/* close the db */ |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
89 |
int dbclose (DB * db) |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
90 |
{ |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
91 |
if (db != NULL) { |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
92 |
printf ("Closing berkley db file...\n"); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
93 |
return db->close (db, 0); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
94 |
} |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
95 |
return EXIT_FAILURE; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
96 |
} |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
97 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
98 |
int main (int argc, char **argv) |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
99 |
{ |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
100 |
char description[80]; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
101 |
int id; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
102 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
103 |
DB *db = NULL; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
104 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
105 |
if ((db = dbinit (db, "berkdb.db")) == NULL) |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
106 |
return EXIT_FAILURE; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
107 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
108 |
for (id = 0; id < 100; id++) { |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
109 |
sprintf (description, "This is value %d", id); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
110 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
111 |
switch (dbinsert (db, id, description)) { |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
112 |
case 0: |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
113 |
printf ("KEY ADDED\n"); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
114 |
break; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
115 |
case DB_KEYEXIST: |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
116 |
printf ("DUPLICATE KEY %d\n", id); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
117 |
break; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
118 |
default: |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
119 |
printf ("Error while storing primary key %d\n", id); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
120 |
} |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
121 |
} |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
122 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
123 |
char *value; |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
124 |
for (id = 0; id < 10; id++) { |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
125 |
if ((value = dbget (db, id)) != NULL) |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
126 |
printf ("DB: %s\n", value); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
127 |
} |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
128 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
129 |
dblist (db); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
130 |
|
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
131 |
return dbclose (db); |
f9015072361f
simple berkdb usage demo
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff
changeset
|
132 |
} |