![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
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 |
#5
| |||
| |||
|
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |