dbTalk Databases Forums  

user defined btree comparison function error on solaris10

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


Discuss user defined btree comparison function error on solaris10 in the comp.databases.berkeley-db forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
likun.navipal@gmail.com
 
Posts: n/a

Default user defined btree comparison function error on solaris10 - 10-28-2006 , 09:54 AM






I compiled the berkeleydb 4.4.20 on sparc solaris10, only using
--enable-debug configure parameter, and then wrote a btree comparison
function (i will call it ubcf later) for my a little complicate key
structure. But the app failed at the ubcf, for simplicity, I show a
test code here:

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

#include "db.h"

#define item_count 10

static char* db_name = "test_db_name";
static char* db_home = "/home/user/aatest/db";
static char* table_name = "aa_table";
static char* table_file_name = "aa_table_file.db";

typedef struct _NODE_
{
int nn;
char ss[32];
} NODE;

int default_btree_compare_func(DB* pdb, const DBT* dbt1, const DBT*
dbt2)
{
NODE *p = (NODE *)dbt1->data;
NODE *q = (NODE *)dbt2->data;

if (p->nn != q->nn) return p->nn - q->nn;
else
{
return strcmp(p->ss, q->ss);
}
}

int main()
{
int rc;
DB_ENV* dbenv;
rc = db_env_create(&dbenv, 0);
dbenv->set_errfile(dbenv, stderr);

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

char sz_key[64];
char sz_data[64];
int size;

unsigned int flags = DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL |
DB_THREAD;
int mode = 0;
rc = dbenv->open(dbenv, db_home, flags, mode);
if (0 == rc)
{
DB* pdb;
rc = db_create(&pdb, dbenv, 0);
rc = pdb->set_bt_compare(pdb,
default_btree_compare_func);
flags = DB_CREATE | DB_EXCL | DB_THREAD;
mode = 0;
rc = pdb->open(pdb, NULL,
table_file_name, table_name,
DB_BTREE, flags, mode);

fprintf(stdout, "write data...\n");
for (int i=0; i<item_count; i++)
{
NODE *p = new NODE;
p->nn = i;
sprintf(p->ss, "key%d", i);
key.data = p;
key.size = strlen(sz_key) + 1;

sprintf(sz_data, "data%d", i);
data.data = sz_data;
data.size = strlen(sz_data) + 1;

rc = pdb->put(pdb, NULL, &key, &data, 0);
fprintf(stdout, "%d : key is %s, data is %s. retcode =
%d\n",
i, (char*)p->ss, (char*)data.data, rc);

delete p;
}

rc = pdb->close(pdb, NULL);
}

rc = dbenv->close(dbenv, NULL);

return 0;
}

It always failed at the seconde db->put, in default_btree_compare_func:
if (p->nn != q->nn) return p->nn - q->nn;

The stack trace is:
#0 default_btree_compare_func (pdb=0x24030, dbt1=0xffbffc98,
dbt2=0xffbff480) at aatest.cpp:21
#1 0xff2177b4 in __bam_cmp (dbp=0x24030, dbt=0xffbffc98,
h=0xfee09af8, indx=0,
func=0x10af4 <default_btree_compare_func(__db*, __db_dbt const*,
__db_dbt const*)>, cmpp=0xffbff5cc)
at ../dist/../btree/bt_compare.c:99
#2 0xff23823c in __bam_search (dbc=0x243c8, root_pgno=3,
key=0xffbffc98, flags=12802, slevel=1, recnop=0x0,
exactp=0xffbff834) at ../dist/../btree/bt_search.c:247
#3 0xff222c2c in __bam_c_search (dbc=0x243c8, root_pgno=3,
key=0xffbffc98, flags=16, exactp=0xffbff834)
at ../dist/../btree/bt_cursor.c:2480
#4 0xff220318 in __bam_c_put (dbc=0x243c8, key=0xffbffc98,
data=0xffbffc80, flags=16, pgnop=0xffbff93c)
at ../dist/../btree/bt_cursor.c:1860
#5 0xff2cd258 in __db_c_put (dbc_arg=0x243c8, key=0xffbffc98,
data=0xffbffc80, flags=16) at ../dist/../db/db_cam.c:1430
#6 0xff2c0b50 in __db_put (dbp=0x24030, txn=0x0,
key=0xffbffc98, data=0xffbffc80, flags=0)
at ../dist/../db/db_am.c:404
#7 0xff2dac44 in __db_put_pp (dbp=0x24030, txn=0x0,
key=0xffbffc98, data=0xffbffc80, flags=0)
at ../dist/../db/db_iface.c:1449
#8 0x0001116c in main (argc=2, argv=0xffbffda4)


dbt1 is the new one to be put into berkeleydb, dbt2 is the old one that
has already been put into berkeleydb. I have debug the app using
gdb6.3, and found that the member of p and q in udcf can be printed!
More test showed that "q->nn" result in the bus error.

What's more, I test it in Fedaro Core 4, all is ok.


Reply With Quote
  #2  
Old   
Michael Cahill
 
Posts: n/a

Default Re: user defined btree comparison function error on solaris10 - 10-29-2006 , 06:55 PM






Quote:
More test showed that "q->nn" result in the bus error.
Bus errors are caused by accessing data that is not aligned as expected
by the CPU. Sparc requires alignment but the x86 family does not,
which explains why you are seeing different results there.

See this page for more information:


http://www.oracle.com/technology/doc...t_compare.html

In particular:

Note that the data must first be copied into memory that is
appropriately aligned, as Berkeley DB does not guarantee any kind of
alignment of the underlying data, including for comparison routines.

Regards,
Michael.



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.