dbTalk Databases Forums  

Who can explain such a ridiculous case?

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


Discuss Who can explain such a ridiculous case? in the comp.databases.berkeley-db forum.



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

Default Who can explain such a ridiculous case? - 08-06-2006 , 04:02 AM






Hi,

I meet a strange case, in which I built a very simple database. Each
key is a
class Edge{
unsigned int id;
unsigned short tag;
}
and each data is just an unsigned data.

When I scan the database, some information is as follows:

key-part data-part
----------------------- ------
edge.id edge.tag data
1 1 3
15 1 21
16 1 15
.... ... ... ...

Consequently, if I want to search the key with edge.id = 15 and
edge.tag=1, I find nothing, such as:

Edge edge;
edge.id = 15;
edge.tag = 1;
Dbt key, data;
int ret;

key.set_data( &edge );
key.set_size( sizeof(Edge) );

ret = cursorp->get( &key, &data, DB_SET ); // the cursorp has been
initialized

if ( ret == 0 )
{
The case is that it cannot find the data on database,
however, I can find data if the id with edge.id = 1 and edge.tag =1
}

Is it strange?

Regards,
Joe


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

Default Re: Who can explain such a ridiculous case? - 08-06-2006 , 07:03 AM






Hi Joe,

Quote:
class Edge{
unsigned int id;
unsigned short tag;
}
It is likely (depending on your compiler settings) that sizeof (Edge)
== 8. That is because the compiler will pad structures to some
alignment. Those padding bytes probably explain what you are seeing
here -- there are some random bytes that you don't see in the fields.

If this is the explanation, you will need to zero all bytes before
setting the fields in the objects (before storing or looking them up).

Regards,
Michael.



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

Default Re: Who can explain such a ridiculous case? - 08-06-2006 , 09:49 AM



Dear Micheal,

For simplicity, I just change the tag from unsigned short to unsigned
int, which keeps the same length.

Another prolbem appears, for example, in one program, I store some tag
value, e.g. 0.
I can correctly read the value by first extract edge from key and then
output edge.id.

However, when I just another test program which just scan the database
and output all key/value, the tag is changed to 3435921408, which is
not 0 any more, why?

Sincerely for your reply,
Joe

However, when I store some tag value

Michael Cahill 写道:

Quote:
Hi Joe,

class Edge{
unsigned int id;
unsigned short tag;
}

It is likely (depending on your compiler settings) that sizeof (Edge)
== 8. That is because the compiler will pad structures to some
alignment. Those padding bytes probably explain what you are seeing
here -- there are some random bytes that you don't see in the fields.

If this is the explanation, you will need to zero all bytes before
setting the fields in the objects (before storing or looking them up).

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.