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