10 // create, modify, list db as set of text lines
11 // ./a.out new /tmp/x.db
12 // ./a.out add /tmp/x.db < /data/add.txt
13 // ./a.out del /tmp/x.db < /data/del.txt
14 // ./a.out get /tmp/x.db < /data/get.txt
15 // ./a.out lst /tmp/x.db > /data/dat.txt
18 ./xsch theDb txs <<eof
20 item_id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
21 data varchar(255) binary NOT NULL
24 CREATE UNIQUE INDEX item_data ON item (data);
27 // then, compile using:
28 // c++ -ggdb -O2 tx.C txs.C tdb.C
31 typedef ItemLoc::ikey_Item_data ItemData;
32 typedef ItemLoc::rkey_Item_data DataItem;
33 class ItemKey : public ItemObj::t_Data {
35 ItemKey(char *key, int len) : ItemObj::t_Data((unsigned char *)key,len) {}
40 int main(int ac, char **av)
42 if( !strcmp(av[1],"new") ) {
51 if( db->open(av[2]) < 0 ) exit(1);
55 if( !strcmp(av[1],"add") ) {
56 while( fgets(line,sizeof(line),stdin) ) {
57 if( !line[0] ) continue;
60 int ret = ItemData(db->item,ItemKey(line,l)).Find();
61 if( !ret ) continue; // duplicate
63 db->item.Data((unsigned char *)line, l);
69 else if( !strcmp(av[1],"del") ) {
70 while( fgets(line,sizeof(line),stdin) ) {
71 if( !line[0] ) continue;
74 int ret = ItemData(db->item,ItemKey(line,l)).Find();
75 if( ret ) continue; // not found
77 db->item.Deallocate();
82 else if( !strcmp(av[1],"get") ) {
83 while( fgets(line,sizeof(line),stdin) ) {
84 if( !line[0] ) continue;
87 int ret = ItemData(db->item,ItemKey(line,l)).Find();
89 printf("%s\n", (char*)db->item.Data());
93 else if( !strcmp(av[1],"lst") ) {
94 if( !DataItem(db->item).First() ) do {
95 printf("%s\n", (char*)db->item.Data());
97 } while( !DataItem(db->item).Next() );
100 fprintf(stderr, "unknown cmd %s\n must be new,add,del,get,lst\n", av[1]);
104 fprintf(stderr, "%jd items input\n", n);
105 fprintf(stderr, "%d items in db\n", db->Item.Count());