BerkeleyDB
www.sleepycat.com
Berkeleydb ist eine lokale Datenbank für C, Java, Perl und einige andere Programmiersprachen.
berkeleydb funktioniert mit keys und datenpayload.. primary key hat hier diesbezüglich eine ganz andere meinung z.b.: create table foobar(id int primary key, name char[10], nachname[10]); -> key ist id payload name, nachname welche in einem struct verpackt sein müssen. demnach kann man das ganze auch nicht so einfach nach einem name bzw nachname durchsuchen (In der Doku gibts noch genauere Informationen wie man 2 Keys verwenden kann). BerkeleyDB ist keine relationale datenbank.
codeschnipsel: #include <stdio.h> #include "geodata.h" #include <db.h> #define DATABASE "geo.db" int main(int argc,char **argv){ DBT key,data; DB *dbp; char keybuffer[3]; char countrybuffer[4]; int i,d,ret; int offset=1; if((ret=db_create(&dbp,NULL,0))!=0){ fprintf(stderr,"db_create: %s\n",db_strerror(ret)); exit(1); } dbp->set_flags(dbp,DB_DUP); if((ret=dbp->open(dbp,NULL,DATABASE,NULL,DB_BTREE,DB_CREATE,0644))!=0){ dbp->err(dbp,ret,"%s",DATABASE); } memset(&key,0,sizeof(key)); memset(&data,0,sizeof(data)); //printf("%s\n",geoname); for(i=0;geoname[i]!=0;i++){ if(i%3==0&&i!=0){ //printf(":%c%c",geoname[i-3],geoname[i-2]); sprintf(keybuffer,"%c%c",geoname[i-3],geoname[i-2]); // printf(" geooffset: %d - %d\n",geooffset[offset-1],geooffset[offset]); for(d=geooffset[offset-1];d<geooffset[offset];d++){ printf("%s\n",geodata[d]); printf("geokey: %s-%s\n",keybuffer,geodata[d]); key.data=keybuffer; key.size=sizeof(keybuffer); data.data=geodata[d]; data.size=sizeof(geodata[d]); if((ret=dbp->put(dbp,NULL,&key,&data,0))==0){ printf("DB: %s: key stored\n",(char*)key.data); } else { printf("key error!\n"); } } offset++; } } dbp->close(dbp,0); return(0); }
#include <sys/types.h> #include <stdio.h> #include <stdlib.h> #include <db.h> #define DATABASE "geo.db" int main(int argv,char **argc){ DBT key,data; DBC *dbcp; DB *dbp; int ret; if((ret=db_create(&dbp,NULL,0))!=0){ fprintf(stderr,"db_create: %s\n",db_strerror(ret)); exit(1); } dbp->set_flags(dbp,DB_DUP); // we set that so that we can store multiple keys! if((ret = dbp->open(dbp,NULL,DATABASE,NULL,DB_BTREE,DB_CREATE,0644))!=0){ dbp->err(dbp,ret,"%s",DATABASE); } memset(&key,0,sizeof(key)); memset(&data,0,sizeof(data)); /* key.data="AT"; key.size=sizeof("AT"); data.data="VIE"; data.size=sizeof("VIE"); */ // switch(ret=dbp->put(dbp,NULL,&key,&data,DB_NOOVERWRITE)){ /*switch(ret=dbp->put(dbp,NULL,&key,&data,0)){ case 0: printf("db: %s: key stored.\n",(char*)key.data); break; case DB_KEYEXIST: printf("db: %s: key previously stored.\n",(char*)key.data); break; default: dbp->err(dbp,ret,"DB->put"); }*/ /* if((ret=dbp->get(dbp,NULL,&key,&data,0))==0){ //printf("db: %s: key retrieved: data was %s\n",(char*)key.data,(char*)data.data); } else { dbp->err(dbp,ret,"DB->get"); }*/ if((ret=dbp->cursor(dbp,NULL,&dbcp,0))!=0){ dbp->err(dbp,ret,"DB->cursor"); return(1); } while((ret=dbcp->c_get(dbcp,&key,&data,DB_NEXT))==0){ printf("%s\n",(char*)data.data); /* if(ret!=DB_NOTFOUND){ dbp->err(dbp,ret,"DBcursor->get"); } */ } dbcp->c_close(dbcp); dbp->close(dbp,0); return(0); }
Timing (countrycodes + airportlists) resolving all american airport IATA codes 1000 times Using hardcoded arrays: retrieved: 1931000 real 0m0.035s user 0m0.022s sys 0m0.002s Using Sleepycat DB(Btree access): retrieved: 1931000 real 0m8.211s user 0m7.377s sys 0m0.202s