ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libcpjit/NOTES
Revision: 1.1
Committed: Tue Oct 11 21:13:39 2005 UTC (18 years, 9 months ago) by root
Branch: MAIN
CVS Tags: HEAD
Log Message:
temporary notes

File Contents

# User Rev Content
1 root 1.1 Berkeley database handling examples:
2    
3     DB *open_db (const char *f, bool dup = false) {
4     DB *dbp = 0;
5     int r = db_create (&dbp, NULL, 0);
6    
7     if (r != 0)
8     {
9     printf ("db_create: %s\n", db_strerror (r));
10     exit (1);
11     }
12    
13     if (dup)
14     if (dbp->set_flags (dbp, DB_DUPSORT) != 0)
15     {
16     cout << "Couldn't set duplicate records for database" << endl;
17     exit (1);
18     }
19    
20     if ((r = dbp->open (dbp, NULL, f, NULL, DB_BTREE, DB_CREATE, 0664)) != 0)
21     {
22     dbp->err (dbp, r, f);
23     dbp->close (dbp, 0);
24     exit (1);
25     }
26     return dbp;
27     }
28    
29     void stor_string (DB *dbp, int32_t id, const dynstr *s)
30     {
31     DBT dkey, data;
32     memset (&dkey, 0, sizeof (DBT));
33     memset (&data, 0, sizeof (DBT));
34    
35     dkey.data = &id;
36     dkey.size = sizeof (int32_t);
37    
38     data.data = s->str;
39     data.size = s->len + 1;
40    
41     int r = dbp->put (dbp, NULL, &dkey, &data, 0);
42     if (r != 0) {
43     cerr << "Database put failed: " << id << " => " << s << endl;
44     exit (1);
45     }
46     }
47    
48     void del_entry (DB *dbp, int32_t id)
49     {
50     DBT dkey;
51     memset (&dkey, 0, sizeof (DBT));
52     dkey.data = &id;
53     dkey.size = sizeof (int32_t);
54     dbp->del (dbp, NULL, &dkey, 0);
55     // FIXME: check returnvalue
56     }
57    
58     bool retr_string (DB *dbp, int32_t id, dynstr *&s)
59     {
60     DBT dkey, data;
61    
62     memset (&dkey, 0, sizeof (DBT));
63     memset (&data, 0, sizeof (DBT));
64    
65     dkey.data = &id;
66     dkey.size = sizeof (int32_t);
67    
68     int r = dbp->get (dbp, NULL, &dkey, &data, 0);
69     if (r != DB_NOTFOUND) {
70     s = new dynstr ((char *)data.data, data.size - 1);
71     return true;
72     }
73     return false;
74     }
75