dbTalk Databases Forums  

Need sugestion.

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


Discuss Need sugestion. in the comp.databases.berkeley-db forum.



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

Default Need sugestion. - 07-24-2006 , 08:36 AM






I have folowing problem:
(also,sorry on my awkward english)

need to save and read some data to/from db

for key i use next struct:
//KEY
struct key{
int key_part1;
int key_part2;
};

// I am ok with Key but have problem with Data

//DATA
struct subitem{
int num;
float sub2;
float sub3;
};

struct data{ //data struct
bool flag;
int num_item_in_vector;// number of items in vector
std::vector<subitem> vec;// vector thta hold ? subitem structs
};

my problem come to this:
how to put (and get) "struct data" to(from) database since that struct
conatains vector ?

can i just use:
//simplify function store data

void func(struct key kkey, struct data ddata){

Dbt dbKey(&kkey,sizeof(struct key));
Dbt dbData(&ddata,sizeof(struct data));

db>put(NULL,&dbKey,&dbData,0); // to store item in db

}

i have no idea how to retrive that data from db? is it just enough to
call:

Dbt dbKey(&kkey,sizeof(struct key);
Dbt dbData(&ddata,sizeof(struct data);
Dbc * tcur;

....
while(..){
tcur->get(&dbKey,&dbData,DB_NEXT))==0)
// will dbData contain valid data??? i(f key is valid)
....
....
....}
any suggestion are most welcome

tnx in advance


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

Default Re: Need sugestion. - 07-25-2006 , 08:59 AM






Of course, this is not working as I thought .

problem is in vector... need to marshall data from vector in
contiguous memory...

sugestion are still most wellcome




sinisa wrote:
Quote:
I have folowing problem:
(also,sorry on my awkward english)

need to save and read some data to/from db

for key i use next struct:
//KEY
struct key{
int key_part1;
int key_part2;
};

// I am ok with Key but have problem with Data

//DATA
struct subitem{
int num;
float sub2;
float sub3;
};

struct data{ //data struct
bool flag;
int num_item_in_vector;// number of items in vector
std::vector<subitem> vec;// vector thta hold ? subitem structs
};

my problem come to this:
how to put (and get) "struct data" to(from) database since that struct
conatains vector ?

can i just use:
//simplify function store data

void func(struct key kkey, struct data ddata){

Dbt dbKey(&kkey,sizeof(struct key));
Dbt dbData(&ddata,sizeof(struct data));

db>put(NULL,&dbKey,&dbData,0); // to store item in db

}

i have no idea how to retrive that data from db? is it just enough to
call:

Dbt dbKey(&kkey,sizeof(struct key);
Dbt dbData(&ddata,sizeof(struct data);
Dbc * tcur;

....
while(..){
tcur->get(&dbKey,&dbData,DB_NEXT))==0)
// will dbData contain valid data??? i(f key is valid)
....
....
....}
any suggestion are most welcome

tnx in advance


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

Default Re: Need sugestion. - 07-25-2006 , 12:14 PM



Since BDB supports variable-sized records, just copy all the vector's
elements to a contiguous array and point a Dbt at it. To "unserialize"
after a get(), just reverse the process. The following struct might be
handy:

struct serial_data {
bool flag;
int num_item_in_vector;
struct subitems[]; // points to the memory just beyond this struct,
where the subitems are stored
};

Using this struct the serialize process would be (pass those structs by
reference!!!):

void put(struct key &kkey, struct data &ddata) {
// allocate enough space to hold everything
size_t size = ...;
char tmp[size]; // use new if size might ever be bigger than ~100

// cast it to a serial_data* to make life easier
// sizeof(tmp) > sizeof(serial_data) on purpose --
// you need space for the subitems
struct serial_data* tmp_data = (struct serial_data*) tmp;

// copy the flags and item count across

// use an STL algorithm like copy_n to copy the vector's elements
// into tmp_data->subitems[]

// point a Dbt at tmp (not ddata!) and call put()
}

The unserialize process would be similar:

void get(struct key &kkey, struct data &ddata) {
Dbt dbData, dbKey;

// call get()

struct serial_data* tmp_data = (struct serial_data*)
dbData.get_data();

// copy the flags and size across

// use an appropriate STL algorithm to fill the vector
// use the size you just read to avoid going out of bounds

// free the Dbt's memory (see the API ref)
}

Just watch out for alignment issues if your machine cares.

Ryan


Reply With Quote
  #4  
Old   
sinisa
 
Posts: n/a

Default Re: Need sugestion. - 07-25-2006 , 02:36 PM



tnx Ryan

Ryan wrote:
Quote:
Since BDB supports variable-sized records, just copy all the vector's
elements to a contiguous array and point a Dbt at it. To "unserialize"
after a get(), just reverse the process. The following struct might be
handy:

struct serial_data {
bool flag;
int num_item_in_vector;
struct subitems[]; // points to the memory just beyond this struct,
where the subitems are stored
};

Using this struct the serialize process would be (pass those structs by
reference!!!):

void put(struct key &kkey, struct data &ddata) {
// allocate enough space to hold everything
size_t size = ...;
char tmp[size]; // use new if size might ever be bigger than ~100

// cast it to a serial_data* to make life easier
// sizeof(tmp) > sizeof(serial_data) on purpose --
// you need space for the subitems
struct serial_data* tmp_data = (struct serial_data*) tmp;

// copy the flags and item count across

// use an STL algorithm like copy_n to copy the vector's elements
// into tmp_data->subitems[]

// point a Dbt at tmp (not ddata!) and call put()
}

The unserialize process would be similar:

void get(struct key &kkey, struct data &ddata) {
Dbt dbData, dbKey;

// call get()

struct serial_data* tmp_data = (struct serial_data*)
dbData.get_data();

// copy the flags and size across

// use an appropriate STL algorithm to fill the vector
// use the size you just read to avoid going out of bounds

// free the Dbt's memory (see the API ref)
}

Just watch out for alignment issues if your machine cares.

Ryan


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

Default Re: Need sugestion. - 07-25-2006 , 02:38 PM



tnx Ryan

Ryan wrote:
Quote:
Since BDB supports variable-sized records, just copy all the vector's
elements to a contiguous array and point a Dbt at it. To "unserialize"
after a get(), just reverse the process. The following struct might be
handy:

struct serial_data {
bool flag;
int num_item_in_vector;
struct subitems[]; // points to the memory just beyond this struct,
where the subitems are stored
};

Using this struct the serialize process would be (pass those structs by
reference!!!):

void put(struct key &kkey, struct data &ddata) {
// allocate enough space to hold everything
size_t size = ...;
char tmp[size]; // use new if size might ever be bigger than ~100

// cast it to a serial_data* to make life easier
// sizeof(tmp) > sizeof(serial_data) on purpose --
// you need space for the subitems
struct serial_data* tmp_data = (struct serial_data*) tmp;

// copy the flags and item count across

// use an STL algorithm like copy_n to copy the vector's elements
// into tmp_data->subitems[]

// point a Dbt at tmp (not ddata!) and call put()
}

The unserialize process would be similar:

void get(struct key &kkey, struct data &ddata) {
Dbt dbData, dbKey;

// call get()

struct serial_data* tmp_data = (struct serial_data*)
dbData.get_data();

// copy the flags and size across

// use an appropriate STL algorithm to fill the vector
// use the size you just read to avoid going out of bounds

// free the Dbt's memory (see the API ref)
}

Just watch out for alignment issues if your machine cares.

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.