dbTalk Databases Forums  

db->stat not returning nkeys correctly.

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


Discuss db->stat not returning nkeys correctly. in the comp.databases.berkeley-db forum.



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

Default db->stat not returning nkeys correctly. - 10-25-2005 , 11:19 PM






hi all,

i can't seem to make stat work for me. i can use stat_print and it
gives me the correct number of records in the db, and after inspecting
the source for stat_print i can't work out what's going wrong.

the following code creates a db, adds records and then stat_print's the
db, and finally, stat's the db and the result is different to
stat_print.

help much appreciated,


jack


#include <db.h>
#include <assert.h>
typedef int I;
typedef void*P;
#define DC(ex) do{I RV=(ex);if((RV))printf("DC:%d,
%s\n",RV,db_strerror(RV));\ assert(!(RV));}while(0)
#define DCL(fn,args...) DC(db->fn(db,##args))
DB *db;
create()
{DC(db_create(&db,0,0));db->set_errfile(db,stdout);
DCL(open,0,"rn.db",0,DB_RECNO,DB_TRUNCATE|DB_CREAT E,0);
}
db_recno_t add(void *p,size_t z)
{DBT ts[2],*k=&ts[0],*v=&ts[1];
I i;for(i=0;i<2;i++)memset(&ts[i],0,sizeof(DBT));
v->data=p;v->size=v->ulen=z;v->flags=DB_DBT_USERMEM;
k->flags=DB_DBT_MALLOC;
DCL(put,0,k,v,DB_APPEND);
return *(I*)k->data;
}
stat()
{DCL(sync,0);
DCL(stat_print,0);
DB_BTREE_STAT sp; DCL(stat,0,&sp,0);
printf("\nnkeys: %lu\n\n",(u_long)sp.bt_nkeys);
}
main() {create(); I i;for(i=0;i<4;i++)add(&i,sizeof(I)); stat(); }


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

Default Re: db->stat not returning nkeys correctly. - 10-26-2005 , 12:25 AM






effbiae <effbiae (AT) ivorykite (DOT) com> writes:
Quote:
i can't seem to make stat work for me. i can use stat_print and it
gives me the correct number of records in the db, and after inspecting
the source for stat_print i can't work out what's going wrong.
....
#define DC(ex) do{I RV=(ex);if((RV))printf("DC:%d, %s\n",RV,db_strerror(RV));\
assert(!(RV));}while(0)
#define DCL(fn,args...) DC(db->fn(db,##args))
....
stat()
{DCL(sync,0);
DCL(stat_print,0);
DB_BTREE_STAT sp; DCL(stat,0,&sp,0);
printf("\nnkeys: %lu\n\n",(u_long)sp.bt_nkeys);
}
main() {create(); I i;for(i=0;i<4;i++)add(&i,sizeof(I)); stat(); }
The second argument to DB->stat() should be the address of a _pointer_
to a DB_whatever_STAT structure, not the address of such a structure.
Ergo, your stat() routine should read:

void
stat(void)
{
DCL(sync,0);
DCL(stat_print,0);

DB_BTREE_STAT *sp;
DCL(stat, 0, &sp, 0);
printf("\nnkeys: %lu\n\n", (unsigned long)sp->bt_nkeys);
free(sp);
}


Philip Guenther

(I'll note that your code has undefined behavior because it falls off
the end of functions with 'int' return type. Yep, leaving off the
return type from a function means that it returns 'int'. Or at least it
_did_, as that was deprecated in the C89 standard and made a constraint
violation in the C99 standard. Not even gcc defines the results, so
your program may continue to behave weirdly until you fix that...)


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.