dbTalk Databases Forums  

Problem with DB->put

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


Discuss Problem with DB->put in the comp.databases.berkeley-db forum.



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

Default Problem with DB->put - 09-13-2004 , 08:00 PM






I am trying to write a simple program using BDB , similar to the
programs given in the reference document. Both the key and data are
integers. For some reason, the DB->put function is failing (Invalid
Argument). I am not able to figure out what could fail with a simple
statement as that.
If I try to display the error using DB->err, I get a segmentation
fault.
And if I try to set the page (1K) and cache size (32K), those methods
fail as well.
Am I missing something important over here?
Thanks very much in advance.
Regards,
Maitreyee

Reply With Quote
  #2  
Old   
Ron Cohen
 
Posts: n/a

Default Re: Problem with DB->put - 09-14-2004 , 09:28 AM






rmaitreyee (AT) gmail (DOT) com (Maitreyee) wrote in message news:<db53bce6.0409131700.72d6c963 (AT) posting (DOT) google.com>...
Quote:
I am trying to write a simple program using BDB , similar to the
programs given in the reference document. Both the key and data are
integers. For some reason, the DB->put function is failing (Invalid
Argument). I am not able to figure out what could fail with a simple
statement as that.
If I try to display the error using DB->err, I get a segmentation
fault.
And if I try to set the page (1K) and cache size (32K), those methods
fail as well.
Am I missing something important over here?
Thanks very much in advance.
Regards,
Maitreyee
Please post your code that will demonstrate the problem.

Regards,
Ron

Sleepycat Software


Reply With Quote
  #3  
Old   
Maitreyee
 
Posts: n/a

Default Re: Problem with DB->put - 09-14-2004 , 02:36 PM



ron (AT) sleepycat (DOT) com (Ron Cohen) wrote in message news:<6906cc78.0409140628.c1695ec (AT) posting (DOT) google.com>...
Quote:
rmaitreyee (AT) gmail (DOT) com (Maitreyee) wrote in message news:<db53bce6.0409131700.72d6c963 (AT) posting (DOT) google.com>...
I am trying to write a simple program using BDB , similar to the
programs given in the reference document. Both the key and data are
integers. For some reason, the DB->put function is failing (Invalid
Argument). I am not able to figure out what could fail with a simple
statement as that.
If I try to display the error using DB->err, I get a segmentation
fault.
And if I try to set the page (1K) and cache size (32K), those methods
fail as well.
Am I missing something important over here?
Thanks very much in advance.
Regards,
Maitreyee

Please post your code that will demonstrate the problem.

Regards,
Ron

Sleepycat Software
Here is the piece of code:

#define SOMEDB "somedb.db"

int main(void)
{
DB *dbp;
DBT key,data;
int i;
int *pInt;
int *pData;
int ret;

if ( db_create( &dbp,NULL,0) != 0)
{
printf("Error creating database\n");
exit(0);
}
dbp->set_errfile(dbp, stderr);

if ((ret = dbp->set_pagesize(dbp, 1024)) != 0)
{
dbp->err(dbp, ret, "set_pagesize");
exit(0);
}
if ((ret = dbp->set_cachesize(dbp, 0, 32 * 1024, 0)) != 0)
{
dbp->err(dbp, ret, "set_cachesize");
exit(0);
}

if ( dbp->open ( dbp , NULL , SOMEDB , DB_RECNO ,DB_CREATE,
0664 ) != 0)
{
printf("Error opening client database\n");
exit(0);
}

if ( (pInt = (int*) malloc (sizeof(int))) == NULL )
{
printf("Error doing malloc for pInt\n");
exit(0);
}

for ( i=0 ; i<10 ; i++ )
{
memset ( &key , 0 , sizeof(key) );
memset ( &data , 0 , sizeof(data) );
*pInt = i+1;
key.data = pInt;
key.size = sizeof(int);
data.data = pInt;
data.size = sizeof(int);

if((ret = dbp->put(dbp,NULL,&key,&data,DB_APPEND)) !=
0 )
{
printf("Error putting record in the database for record :%d\n",i);
exit(0);
}
}
for (i=0;i<10;i+2)
{
*pInt = i;
key.data = pInt;
key.size = sizeof(int);

if ( dbp->get(dbp,NULL,&key,&data,0) != 0 )
{
printf("Error retrieving record\n");
exit(0);
}
pData = data.data;
printf("data is:%d",*pData);
}
free(pInt);
}

Running this as it is gives a segmentation fault which goes away with
the removal of dbp->err.
Thanks for your help.
Regards,
Maitreyee


Reply With Quote
  #4  
Old   
Ron Cohen
 
Posts: n/a

Default Re: Problem with DB->put - 09-21-2004 , 09:35 AM



rmaitreyee (AT) gmail (DOT) com (Maitreyee) wrote in message news:<db53bce6.0409141136.3b8c5734 (AT) posting (DOT) google.com>...
Quote:
ron (AT) sleepycat (DOT) com (Ron Cohen) wrote in message news:<6906cc78.0409140628.c1695ec (AT) posting (DOT) google.com>...
rmaitreyee (AT) gmail (DOT) com (Maitreyee) wrote in message news:<db53bce6.0409131700.72d6c963 (AT) posting (DOT) google.com>...
I am trying to write a simple program using BDB , similar to the
programs given in the reference document. Both the key and data are
integers. For some reason, the DB->put function is failing (Invalid
Argument).
The DB->put function is due to a coding error. I modified your code
below.

Also, this program would not compile as it was missing an argument for
the get method.

I am not able to figure out what could fail with a simple
Quote:
statement as that.


Please post your code that will demonstrate the problem.

Regards,
Ron

Sleepycat Software

Here is the piece of code:

#define SOMEDB "somedb.db"

int main(void)
{
DB *dbp;
DBT key,data;
int i;
int *pInt;
int *pData;
int ret;

if ( db_create( &dbp,NULL,0) != 0)
{
printf("Error creating database\n");
exit(0);
}
dbp->set_errfile(dbp, stderr);

if ((ret = dbp->set_pagesize(dbp, 1024)) != 0)
{
dbp->err(dbp, ret, "set_pagesize");
exit(0);
}
if ((ret = dbp->set_cachesize(dbp, 0, 32 * 1024, 0)) != 0)
{
dbp->err(dbp, ret, "set_cachesize");
exit(0);
}

if ( dbp->open ( dbp , NULL , SOMEDB , DB_RECNO ,DB_CREATE,
0664 ) != 0)
I had to change this:

if ( dbp->open ( dbp , NULL , SOMEDB , NULL, DB_RECNO ,DB_CREATE,
0664 ) != 0)



Quote:
{
printf("Error opening client database\n");
exit(0);
}

if ( (pInt = (int*) malloc (sizeof(int))) == NULL )
{
printf("Error doing malloc for pInt\n");
exit(0);
}

for ( i=0 ; i<10 ; i++ )
{
memset ( &key , 0 , sizeof(key) );
memset ( &data , 0 , sizeof(data) );
*pInt = i+1;
key.data = pInt;
key.size = sizeof(int);
data.data = pInt;
data.size = sizeof(int);

if((ret = dbp->put(dbp,NULL,&key,&data,DB_APPEND)) !=
0 )
{
printf("Error putting record in the database for record :%d\n",i);
exit(0);
}
}
for (i=0;i<10;i+2)
I also changed the line above
for (i=1;i<10;i++)




Quote:
{
*pInt = i;
key.data = pInt;
key.size = sizeof(int);
May want to use:


key.flags = DB_DBT_USERMEM;

Quote:
if ( dbp->get(dbp,NULL,&key,&data,0) != 0 )
{
printf("Error retrieving record\n");
exit(0);
}
pData = data.data;
printf("data is:%d",*pData);
}
free(pInt);
}

Running this as it is gives a segmentation fault which goes away with
the removal of dbp->err.
Thanks for your help.
Regards,
Maitreyee


You are welcome.


Ron


Reply With Quote
  #5  
Old   
Maitreyee
 
Posts: n/a

Default Re: Problem with DB->put - 09-28-2004 , 06:13 PM



Thanks Ron for your help.
Unfortunately, db->put is giving me trouble again.
I have a database and a secondary index associated with it.
Whenever I try to "put" to the primary, I get an "Unknown error". The
error number is a wierd (similar to -10056378200)negative value. If I
try to "put" to a database which does not have a secondary index
associated with it,it works super fine.Does any special arrangement
need to be done when the database is associated with a secondary?
Once again,Thanks in advance.
Regards,
Maitreyee

ron (AT) sleepycat (DOT) com (Ron Cohen) wrote in message news:<6906cc78.0409210635.6b20ac55 (AT) posting (DOT) google.com>...
Quote:
rmaitreyee (AT) gmail (DOT) com (Maitreyee) wrote in message news:<db53bce6.0409141136.3b8c5734 (AT) posting (DOT) google.com>...
ron (AT) sleepycat (DOT) com (Ron Cohen) wrote in message news:<6906cc78.0409140628.c1695ec (AT) posting (DOT) google.com>...
rmaitreyee (AT) gmail (DOT) com (Maitreyee) wrote in message news:<db53bce6.0409131700.72d6c963 (AT) posting (DOT) google.com>...
I am trying to write a simple program using BDB , similar to the
programs given in the reference document. Both the key and data are
integers. For some reason, the DB->put function is failing (Invalid
Argument).

The DB->put function is due to a coding error. I modified your code
below.

Also, this program would not compile as it was missing an argument for
the get method.

I am not able to figure out what could fail with a simple
statement as that.


Please post your code that will demonstrate the problem.

Regards,
Ron

Sleepycat Software

Here is the piece of code:

#define SOMEDB "somedb.db"

int main(void)
{
DB *dbp;
DBT key,data;
int i;
int *pInt;
int *pData;
int ret;

if ( db_create( &dbp,NULL,0) != 0)
{
printf("Error creating database\n");
exit(0);
}
dbp->set_errfile(dbp, stderr);

if ((ret = dbp->set_pagesize(dbp, 1024)) != 0)
{
dbp->err(dbp, ret, "set_pagesize");
exit(0);
}
if ((ret = dbp->set_cachesize(dbp, 0, 32 * 1024, 0)) != 0)
{
dbp->err(dbp, ret, "set_cachesize");
exit(0);
}

if ( dbp->open ( dbp , NULL , SOMEDB , DB_RECNO ,DB_CREATE,
0664 ) != 0)
I had to change this:

if ( dbp->open ( dbp , NULL , SOMEDB , NULL, DB_RECNO ,DB_CREATE,
0664 ) != 0)



{
printf("Error opening client database\n");
exit(0);
}

if ( (pInt = (int*) malloc (sizeof(int))) == NULL )
{
printf("Error doing malloc for pInt\n");
exit(0);
}

for ( i=0 ; i<10 ; i++ )
{
memset ( &key , 0 , sizeof(key) );
memset ( &data , 0 , sizeof(data) );
*pInt = i+1;
key.data = pInt;
key.size = sizeof(int);
data.data = pInt;
data.size = sizeof(int);

if((ret = dbp->put(dbp,NULL,&key,&data,DB_APPEND)) !=
0 )
{
printf("Error putting record in the database for record :%d\n",i);
exit(0);
}
}
for (i=0;i<10;i+2)

I also changed the line above
for (i=1;i<10;i++)




{
*pInt = i;
key.data = pInt;
key.size = sizeof(int);

May want to use:


key.flags = DB_DBT_USERMEM;


if ( dbp->get(dbp,NULL,&key,&data,0) != 0 )
{
printf("Error retrieving record\n");
exit(0);
}
pData = data.data;
printf("data is:%d",*pData);
}
free(pInt);
}

Running this as it is gives a segmentation fault which goes away with
the removal of dbp->err.
Thanks for your help.
Regards,
Maitreyee



You are welcome.


Ron

Reply With Quote
  #6  
Old   
Ron Cohen
 
Posts: n/a

Default Re: Problem with DB->put - 09-29-2004 , 10:00 PM



Here is an example of using a secondary index in C. I hope this helps.

Ron
Sleepycat Software



#include <sys/types.h>

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "db.h"

int getname(DB *, const DBT *, const DBT *, DBT *);
void insert(DB *, int, char *);

int
getname(dbp, pkey, pdata, skey)
DB *dbp;
const DBT *pkey, *pdata;
DBT *skey;
{
memset(skey, 0, sizeof(DBT));
skey->data = strchr(pdata->data, '.') + 1;
skey->size = strlen(skey->data);
return (0);
}

void
insert(dbp, id, name)
DB *dbp;
int id;
char *name;
{
DBT key, data;
char idbuf[10];

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

sprintf(idbuf, "%05d", id);
key.data = idbuf;
key.size = strlen(idbuf);

data.data = name;
data.size = strlen(name);

printf("insert: %s\n", name);
assert(dbp->put(dbp, NULL, &key, &data, 0) == 0);
}

int
main()
{
DB_ENV *dbenv;
DB *dbp, *sdbp;

(void)system("rm -rf TESTDIR; mkdir TESTDIR");

assert(db_env_create(&dbenv, 0) == 0);
dbenv->set_errfile(dbenv, stderr);
assert(dbenv->open(dbenv,
"TESTDIR", DB_CREATE | DB_INIT_MPOOL, 0666) == 0);

assert(db_create(&dbp, dbenv, 0) == 0);
assert(dbp->open(dbp, NULL,
"a.db", "primary", DB_BTREE, DB_CREATE, 0) == 0);

assert(db_create(&sdbp, dbenv, 0) == 0);
assert(sdbp->set_flags(sdbp, DB_DUP) == 0);
assert(sdbp->open(sdbp, NULL,
"a.db", "secondary", DB_BTREE, DB_CREATE, 0) == 0);

assert(dbp->associate(dbp, NULL, sdbp, getname, 0) == 0);

insert(dbp, 1, "Alice.AAAA");
insert(dbp, 2, "Bob.BBBB");
insert(dbp, 3, "Carl.CCCC");

assert(sdbp->close(sdbp, 0) == 0);
assert(dbp->close(dbp, 0) == 0);
assert(dbenv->close(dbenv, 0) == 0);

return (0);
}

Reply With Quote
  #7  
Old   
Philip Guenther
 
Posts: n/a

Default Re: Problem with DB->put - 09-30-2004 , 04:11 AM



rmaitreyee (AT) gmail (DOT) com (Maitreyee) writes:
Quote:
I have a database and a secondary index associated with it.
Whenever I try to "put" to the primary, I get an "Unknown error". The
error number is a wierd (similar to -10056378200)negative value.
Are you sure that the callback that you passed to DB->associate() is
*always* returning zero or a valid error number? DB itself doesn't use
such a large negative value for its errors, so it would seem that either
some C library call did so (doubtful) or your associate callback
returned that value.


Philip Guenther


Reply With Quote
  #8  
Old   
Maitreyee
 
Posts: n/a

Default Re: Problem with DB->put - 09-30-2004 , 02:22 PM



Thank You both for your replies. I was not returning any value from
the callback routine.
I also realized that I was having a primary with duplicates , which as
the documentation says is not allowed when a secondary is to be
associated with it.
Would the following way be a potential workaround for the situation
where the primary has duplicates and yet a secondary is needed?
Step 1:Locate the first available key/data pair in the primary by
doing a pget on the secondary.
Step 2:Traverse the primary using a cursor to get the remaining
key/data pairs?

I am sorry if this is confusing.
Thanks again.
Regards,
Maitreyee


Philip Guenther <guenther (AT) lab (DOT) smi.sendmail.com> wrote

Quote:
rmaitreyee (AT) gmail (DOT) com (Maitreyee) writes:
I have a database and a secondary index associated with it.
Whenever I try to "put" to the primary, I get an "Unknown error". The
error number is a wierd (similar to -10056378200)negative value.

Are you sure that the callback that you passed to DB->associate() is
*always* returning zero or a valid error number? DB itself doesn't use
such a large negative value for its errors, so it would seem that either
some C library call did so (doubtful) or your associate callback
returned that value.


Philip Guenther

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 - 2013, Jelsoft Enterprises Ltd.