1: #include <stdio.h>
2: #include <sys/types.h>
3: #include <limits.h>
4: #include <sys/stat.h>
5: #include <fcntl.h>
6: #include <db1/db.h>
7: #include <errno.h>
8: #include <string.h>
9: #include <assert.h>
10: #include <stdlib.h>
11:
12:
13: /**
14: * Shows the usage of the Berkely DB library ( only db1 ).
15: *
16: * tested fuctions are:
17: * -> dbopen
18: * -> put
19: * -> get
20: * -> sync
21: * -> del
22: * -> seq
23: * -> close
24: * @date 2002-05-23
25: * @author Arno Wilhelm <a.w(ad)quirxi.com>
26: */
27: int main( void )
28: {
29: int ret = 0;
30: char str_key[8] = { 0 };
31: char str_dat[8] = { 0 };
32:
33: DBT key;
34: DBT data;
35:
36: ///////////////////
37: // Open database //
38: ///////////////////
39: printf("\nCreating/opening database:\n");
40: DB * database = dbopen( "DATABASE", O_CREAT|O_RDWR, S_IWUSR|S_IRUSR|S_IRGRP, DB_BTREE, 0 );
41: if ( 0 != database ) printf("\tOk\n\n");
42: else
43: {
44: printf( "\tError: Cannot open database: ( %d: %s ).\n\n", errno, strerror( errno ) );
45: exit( 1 );
46: }
47:
48: printf( "Adding values to database:\n\n" );
49: for ( char c = 65; c < 85; ++c )
50: {
51: str_key[0] = c;
52: str_key[1] = c + 1;
53: str_key[2] = c + 2;
54: str_key[3] = c + 3;
55: str_key[4] = c + 4;
56: str_key[5] = c + 5;
57: str_key[6] = c + 6;
58: str_key[7] = 0x0;
59:
60: str_dat[7] = 0x0;
61: for ( int i = 6; i >= 0; --i )
62: {
63: str_dat[i] = str_key[6 - i];
64: }
65:
66: printf( "\tKEY: %s:\tVALUE: %s\n", str_key, str_dat );
67:
68: key.data = static_cast<void *> ( str_key );
69: key.size = strlen( str_key ) + 1;
70: data.data = static_cast<void *> ( str_dat );
71: data.size = strlen( str_dat ) + 1;
72:
73: ///////////////////
74: // Fill database //
75: ///////////////////
76: ret = database->put( database, &key, &data, 0 );
77: if ( 0 != ret )
78: {
79: printf( "\tError %d: Cannot write to database ( %d: %s ).\n\n", ret, errno, strerror( errno ) );
80: exit( 1 );
81: }
82: }
83:
84: ///////////////////////
85: // Get single values //
86: ///////////////////////
87: char * search_key = "JKLMNOP";
88: key.data = (void *) search_key;
89: key.size = strlen( search_key ) + 1;
90:
91: ret = database->get( database, &key, &data, 0 );
92:
93: printf("\nGetting single value for key \"%s\":\n", search_key);
94: if ( 0 == ret )
95: {
96: char * value = static_cast<char *> (data.data);
97: assert( 0 != value );
98: printf( "\tValue for key \"%s\" is \"%s\".\n", search_key, value );
99: }
100: else
101: {
102: if ( 1 == ret )
103: {
104: printf( "\tError: No value for key %s found in database!\n", search_key );
105: }
106: else
107: {
108: printf( "\tError %d: Cannot read from database ( %d: %s ).\n", ret, errno, strerror( errno ) );
109: exit( 1 );
110: }
111: }
112:
113: //////////////////
114: // Delete value //
115: //////////////////
116: char * delete_key = "ABCDEFG";
117: key.data = (void *) delete_key;
118: key.size = strlen( delete_key ) + 1;
119:
120: ret = database->del( database, &key, 0 );
121:
122: printf("\nDeleting database entry with key \"%s\":\n", delete_key);
123: if ( 0 == ret )
124: {
125: printf( "\tDeleted database entry with key \"%s\" successfully.\n", delete_key );
126: }
127: else if ( 1 == ret )
128: {
129: printf( "\Error: Could not delete value with key \"%s\":\n\tKey does not exist in database!\n",
130: delete_key );
131: }
132: else if ( -1 == ret )
133: {
134: printf( "\tError %d: Cannot delete value with key \"%s\": ( %d: %s ).\n",
135: ret, delete_key, errno, strerror( errno ) );
136: exit( 1 );
137: }
138:
139: ///////////////////////
140: // Get single values //
141: ///////////////////////
142: search_key = delete_key;
143: key.data = (void *) search_key;
144: key.size = strlen( search_key ) + 1;
145:
146: ret = database->get( database, &key, &data, 0 );
147:
148: printf("\nTrying to get value for deleted key \"%s\":\n", search_key);
149: if ( 0 == ret )
150: {
151: char * value = static_cast<char *> (data.data);
152: assert( 0 != value );
153: printf( "\tValue for key \"%s\" is \"%s\".\n", search_key, value );
154: }
155: else
156: {
157: if ( 1 == ret )
158: {
159: printf( "\tError: No value for key %s found in database!\n", search_key );
160: }
161: else
162: {
163: printf( "\tError %d: Cannot read from database ( %d: %s ).\n", ret, errno, strerror( errno ) );
164: exit( 1 );
165: }
166: }
167:
168: ////////////////////
169: // Sync database //
170: ///////////////////
171: printf("\nTrying to sync database:\n");
172: ret = database->sync( database, 0 );
173: if ( 0 == ret ) printf("\tOk\n");
174: else
175: {
176: printf( "\tError %d: Cannot sync database ( %d: %s ).\n", ret, errno, strerror( errno ) );
177: }
178:
179: /////////////////////////////////////////////////////
180: // Cycle through database and print out all values //
181: /////////////////////////////////////////////////////
182: printf( "\nReading sequential from database:\n\n" );
183: ret = database->seq( database, &key, &data, R_FIRST );
184: do
185: {
186: char * sz_key = static_cast<char *> (key.data);
187: char * sz_dat = static_cast<char *> (data.data);
188: printf( "\tKEY: %s:\tVALUE: %s\n", sz_key, sz_dat );
189: ret = database->seq( database, &key, &data, R_NEXT );
190: } while ( 0 == ret );
191:
192: if ( -1 == ret )
193: {
194: printf( "\tError %d: Cannot read sequential from database ( %d: %s ).\n\n", ret, errno, strerror( errno ) );
195: exit( 1 );
196: }
197:
198: ////////////////////
199: // Close database //
200: ////////////////////
201: printf("\nClosing database:\n");
202: if ( -1 != database->close( database ) ) printf("\tOk\n\n");
203: else
204: {
205: printf( "\tError closing database: ( %d: %s ).\n\n", errno, strerror( errno ) );
206: exit( 1 );
207: }
208:
209: return 0;
210: }
211:
212: /**
213: * Output:
214:
215: > ./DB
216:
217: Creating/opening database:
218: Ok
219:
220: Adding values to database:
221:
222: KEY: ABCDEFG: VALUE: GFEDCBA
223: KEY: BCDEFGH: VALUE: HGFEDCB
224: KEY: CDEFGHI: VALUE: IHGFEDC
225: KEY: DEFGHIJ: VALUE: JIHGFED
226: KEY: EFGHIJK: VALUE: KJIHGFE
227: KEY: FGHIJKL: VALUE: LKJIHGF
228: KEY: GHIJKLM: VALUE: MLKJIHG
229: KEY: HIJKLMN: VALUE: NMLKJIH
230: KEY: IJKLMNO: VALUE: ONMLKJI
231: KEY: JKLMNOP: VALUE: PONMLKJ
232: KEY: KLMNOPQ: VALUE: QPONMLK
233: KEY: LMNOPQR: VALUE: RQPONML
234: KEY: MNOPQRS: VALUE: SRQPONM
235: KEY: NOPQRST: VALUE: TSRQPON
236: KEY: OPQRSTU: VALUE: UTSRQPO
237: KEY: PQRSTUV: VALUE: VUTSRQP
238: KEY: QRSTUVW: VALUE: WVUTSRQ
239: KEY: RSTUVWX: VALUE: XWVUTSR
240: KEY: STUVWXY: VALUE: YXWVUTS
241: KEY: TUVWXYZ: VALUE: ZYXWVUT
242:
243: Getting single value for key "JKLMNOP":
244: Value for key "JKLMNOP" is "PONMLKJ".
245:
246: Deleting database entry with key "ABCDEFG":
247: Deleted database entry with key "ABCDEFG" successfully.
248:
249: Trying to get value for deleted key "ABCDEFG":
250: Error: No value for key ABCDEFG found in database!
251:
252: Trying to sync database:
253: Ok
254:
255: Reading sequential from database:
256:
257: KEY: BCDEFGH: VALUE: HGFEDCB
258: KEY: CDEFGHI: VALUE: IHGFEDC
259: KEY: DEFGHIJ: VALUE: JIHGFED
260: KEY: EFGHIJK: VALUE: KJIHGFE
261: KEY: FGHIJKL: VALUE: LKJIHGF
262: KEY: GHIJKLM: VALUE: MLKJIHG
263: KEY: HIJKLMN: VALUE: NMLKJIH
264: KEY: IJKLMNO: VALUE: ONMLKJI
265: KEY: JKLMNOP: VALUE: PONMLKJ
266: KEY: KLMNOPQ: VALUE: QPONMLK
267: KEY: LMNOPQR: VALUE: RQPONML
268: KEY: MNOPQRS: VALUE: SRQPONM
269: KEY: NOPQRST: VALUE: TSRQPON
270: KEY: OPQRSTU: VALUE: UTSRQPO
271: KEY: PQRSTUV: VALUE: VUTSRQP
272: KEY: QRSTUVW: VALUE: WVUTSRQ
273: KEY: RSTUVWX: VALUE: XWVUTSR
274: KEY: STUVWXY: VALUE: YXWVUTS
275: KEY: TUVWXYZ: VALUE: ZYXWVUT
276:
277: Closing database:
278: Ok
279: */
280:
281: