dbTalk Databases Forums  

partial search using secondary database not working

comp.databases.berkeley-db comp.databases.berkeley-db


Discuss partial search using secondary database not working in the comp.databases.berkeley-db forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default partial search using secondary database not working - 08-10-2006 , 07:00 AM






Hi all,
I am using Linux fedora-4 c compiler gcc-4.0.2-8.fc4 with Kdevelop IDE.

I am working on BERKELEY DB since last month I have created a primary
database , opened it after that a secondary database i have created and
open.
In order to link secondary database I associate seconday database to
primary database as in assocation I have provided a key extractor ,
but problem is that search return -30989 which means no data in the
database , i m attaching my code please any one help me ...

#include <sys/types.h>
#include <stdio.h>
#include <db.h>
#include <db_cxx.h>
#include <unistd.h>
#include <iostream.h>
#include <string.h>

#define DATABASE "mydb.db"
#define SECDATABASE "mysecdb.db"

struct contactData
{
char *contactName;
char *contactDesig;
char *contactNum;
char *contactQuickDialNum;
} dataIn , dataOut;

size_t len;
char *p;
char data_buffer[100];
DBC *dbcurpointer;

int getName( DB *sdbp, const DBT *pkey, const DBT *pdata,DBT *skey)
{
contactData *conData;
conData =reinterpret_cast<contactData *>(pdata->data);
memset(skey, 0, sizeof(DBT));
skey->data = conData->contactNum;
skey->size = strlen(conData->contactName) + 1;
return (0);
}

int main()
{
DB *dbp,*sdbp;
u_int32_t open_flags;
DBC *dbcp;
DBC *cursorp;
DBT key, data;
int ret;
DB *my_secondary_database;

DBT pkey, pdata; /* Used to return the primary key and data */

char *search_name = "ALI";
if ((ret = db_create(&dbp, NULL, 0)) != 0)
{
fprintf(stderr, "db_create: %s\n", db_strerror(ret));
}
if(ret==0)
ret = dbp->open(dbp,NULL,DATABASE,NULL,DB_BTREE, DB_CREATE, 0664);
if (ret != 0)
{
cout<<"Error opening primary database"<<endl;
}
ret = db_create(&sdbp, NULL, 0);
if (ret != 0)
{
cout<<"Error creating secondary databse"<<endl;
}
ret = sdbp->set_flags(sdbp, DB_DUPSORT);
if (ret != 0)
{
cout<<"Error setting flags"<<endl;
}
ret = sdbp->open(sdbp,NULL,SECDATABASE,NULL,DB_BTREE,DB_CREAT E, 0);
if (ret != 0)
{
cout<<"Error opening secondary database"<<endl;
}
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
dataIn.contactName="ZULQARNAIN";
dataIn.contactDesig="software";
dataIn.contactNum="4232454545";
dataIn.contactQuickDialNum="625";
memset(data_buffer,100,0);
strcpy(data_buffer,dataIn.contactDesig);

strcat(data_buffer,",");
strcat(data_buffer,dataIn.contactNum);
strcat(data_buffer,",");
strcat(data_buffer,dataIn.contactQuickDialNum);

memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));


key.data = dataIn.contactName;
key.size = 25;
data.data = data_buffer;
data.size = 25;

if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) == 0)
printf("db: %s: key stored.\n", (char *)key.data);
else
{
sdbp->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");
}

dataIn.contactName="ALI_MUHAMMAD";

dataIn.contactDesig="software";
dataIn.contactNum="4232454545";

dataIn.contactQuickDialNum="625";

memset(data_buffer,100,0);

strcpy(data_buffer,dataIn.contactDesig);

strcat(data_buffer,",");
strcat(data_buffer,dataIn.contactNum);
strcat(data_buffer,",");
strcat(data_buffer,dataIn.contactQuickDialNum);


memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));


key.data = dataIn.contactName;
key.size = 25;//sizeof(key.data);
data.data = data_buffer;
data.size = 25;//sizeof(data_buffer);

if ((ret = dbp->put(dbp, NULL, &key, &data, 0/*DB_NOOVERWRITE*/)) == 0)

printf("db: %s: key stored.\n", (char *)key.data);
else
{
sdbp->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");
}

ret=dbp->associate(dbp,NULL,sdbp,getName,0);
if(ret==0)
cout<<"\n\n secodary database associated "<<endl;

if ((ret = dbp->cursor(dbp, NULL, &dbcurpointer, 0)) != 0)
{
dbp->err(dbp, ret, "DB->cursor");
}
if (ret != DB_NOTFOUND)
{
dbp->err(dbp, ret, "DBcursor->get");
}

while((ret = dbcurpointer->c_get(dbcurpointer, &key, &data, DB_NEXT))
== 0)
{
printf ("%s\n",key.data);
printf ("%s\n",data.data);
}


memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
key.data = search_name;
key.size = strlen(search_name) + 1;

sdbp->cursor(sdbp, NULL, &cursorp, 0);
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
key.data = search_name;
key.size = strlen(search_name) + 1;


if ((ret = sdbp->cursor(sdbp, NULL, &dbcurpointer, 0)) != 0)
{
sdbp->err(sdbp, ret, "DB->cursor");
}

if (ret != DB_NOTFOUND)
{
sdbp->err(sdbp, ret, "DBcursor->get");
}

while((ret = dbcurpointer->c_get(dbcurpointer, &key, &data, DB_NEXT))
== 0)
{
printf ("%s\n",key.data);
printf ("%s\n",data.data);
}

cout<<"key.data ="<<(char *)key.data<<endl;
cout<<"key.size =="<<(int *)key.size<<endl;// = strlen(search_name) +
1;

if (sdbp != NULL)
sdbp->close(sdbp, 0);
if (dbp != NULL)
dbp->close(dbp, 0);

cout<<"Closed database"<<endl;
return 0;
}


Reply With Quote
  #2  
Old   
Ryan
 
Posts: n/a

Default Re: partial search using secondary database not working - 08-10-2006 , 12:32 PM






amohammad (AT) ibnkhaldun (DOT) com.pk wrote:
Quote:
In order to link secondary database I associate seconday database to
primary database as in assocation I have provided a key extractor ,
but problem is that search return -30989 which means no data in the
database , i m attaching my code please any one help me ...
Two bugs:

The db stores (char[], char[]) key/data pairs but your key extractor
expects (char[], contactData) pairs. You'll either have to change
contactData to use char[] instead of char* so it can be stored directly
or properly deserialize it in the key extractor.

The key and data lengths are both hard coded to 25, which will either
truncate your data in the database or store junk that messes up your
key comparisons. BDB either needs the real length or fixed-length data
with zeroed-out padding.

Unless saving space is super important you could solve both problems
easily by declaring your contactData struct to use char[] instead of
char* (include space for the '\0' terminator):
Quote:
struct contactData
{
char contactName[MAX_NAME_LEN+1];
char contactDesig[MAX_DESIG_LEN+1];
char contactNum[10+1];
char contactQuickDialNum[3+1];
} dataIn , dataOut;
Then this code would work like you expect:
Quote:
int getName( DB *sdbp, const DBT *pkey, const DBT *pdata,DBT *skey)
{
contactData *conData;
conData =reinterpret_cast<contactData *>(pdata->data);
memset(skey, 0, sizeof(DBT));
skey->data = conData->contactNum;
skey->size = strlen(conData->contactName) + 1;
return (0);
}
And this code:
Quote:
memset(data_buffer,100,0);
strcpy(data_buffer,dataIn.contactDesig);

strcat(data_buffer,",");
strcat(data_buffer,dataIn.contactNum);
strcat(data_buffer,",");
strcat(data_buffer,dataIn.contactQuickDialNum);

memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));

key.data = dataIn.contactName;
key.size = 25;
data.data = data_buffer;
data.size = 25;
would change to:
Quote:
key.data = dataIn.contactName
key.size = sizeof(dataIn.contactName);
data.data = &dataIn;
data.size = sizeof(dataIn)
You'd have to change this code to use strcpy or something similar to
populate the data, but that's not very hard:
Quote:
dataIn.contactName="ZULQARNAIN";
dataIn.contactDesig="software";
dataIn.contactNum="4232454545";
dataIn.contactQuickDialNum="625";
BTW, to avoid leaking memory be sure to either free the key/data pairs
returned by cursors or allocate the space yourself first and set the
DBT_USERMEM flag.

Also, if you're going to use C++ go all the way -- the BDB classes are
much easier to use than the structs, and the methods all throw
exceptions instead of forcing you to check error codes all the time.

Ryan



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.