dbTalk Databases Forums  

read/write problem

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


Discuss read/write problem in the comp.databases.berkeley-db forum.



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

Default read/write problem - 04-03-2006 , 02:39 AM






void PlumeData::loadPlumeDB(MyDb& PlumeDB)
{
// PlumeData my_Plume;

std::string stringBuf;
// getCO (inFile, stringBuf);
// memset (&my_Plume, 0, sizeof(my_Plume));

//// getCO();
//// getCO2();
//// getHC();
//// getNOX();

double buff = (double)getCO();
buff = 0.0;
int size = sizeof(buff);
Dbt key (&buff, size);


double *buff2 = new double[4];
//// buff2 = (double *) getBuffer();
buff2[0] = 1.2;
buff2[1] = 2.2;
buff2[2] = 3.3;
buff2[3] = 4.4;
int size2 = sizeof(buff2)*4;
// size = getBufferSize();
Dbt data (buff2, size2);



PlumeDB.getDb().put(NULL, &key, &data, 0); // stream error
}

I don't know the above code is the pest (this is a guess), so do u
think the code snippet above would lead to any problems? I have written
data into the db using the above code and read it back, I've got
segmentation error when doing read. Thanks in advance
Jack


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

Default Re: read/write problem - 04-03-2006 , 03:03 AM






In the read program, I used the following method, and did not compile

Dbt key (0.0, sizeof(double));
is there a way to get around this?
Thanks
Jack


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

Default Re: read/write problem - 04-03-2006 , 04:32 AM



When I used to string to represent 0.0, then there is runtime error
(segmentation fault)...


Reply With Quote
  #4  
Old   
agorrod@gmail.com
 
Posts: n/a

Default Re: read/write problem - 04-03-2006 , 05:00 PM



I have built an example with very similar data types to your example
above:

#include "db_cxx.h"

int main(int argc, char* argv[])
{
int ret;
Db db(NULL, 0);
u_int32_t oFlags = DB_CREATE;
db.open(NULL, "my_db.db", NULL, DB_BTREE, oFlags, 0);

// begin Plume code
double buff = 0.0;
int size = sizeof(buff);
Dbt key (&buff, size);

double *buff2 = new double[4];
buff2[0] = 1.2;
buff2[1] = 2.2;
buff2[2] = 3.3;
buff2[3] = 4.4;
int size2 = sizeof(buff2)*4; // this size was wrong - need size of
double, not pointer
Dbt data (buff2, size2);

ret = db.put(NULL, &key, &data, 0); // stream error

if(ret != 0) {
printf("Failed to put key/data pair.\n");
exit(ret);
}

Dbt retrdata = Dbt();
ret = db.get(NULL, &key, &retrdata, 0);

if(ret != 0) {
printf("Failed to get key/data pair.\n");
exit(ret);
}

// ensure we got the correct data
if(memcmp(data.get_data(), retrdata.get_data(), size2) != 0) {
printf("The retrieved data is different to the put data!\n");
} else {
printf("The retrieved data matches the put data!\n");
}

return 0;
}

This builds, and runs as expected. One thing that is likely wrong in
your code is this line:
int size2 = sizeof(buff2)*4;
You almost certainly want it to be:
int size2 = sizeof(buff2[0])*4;
Otherwise the size is 4*the size of a pointer, not 4*the size of a
double.

You should read through the c++ getting started guide
(http://www.sleepycat.com/docs/gsg/CXX/index.html) It walks through the
steps of creating an application using examples. The examples generally
show best practice for usage of BDB.


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

Default Re: read/write problem - 04-03-2006 , 08:19 PM



Hi,
Have you got the same results as mine? I've got segmentation error when
reading back from the db. The key was 0.0
Thanks
Jack


Reply With Quote
  #6  
Old   
Jack
 
Posts: n/a

Default Re: read/write problem - 04-03-2006 , 08:24 PM



The program for reading:


#include <iostream.h>
#include <fstream>
#include <cstdlib>

#include "MyDb.h"
#include "db.h"

#ifdef _WIN32
extern "C" {
extern int getopt(int, char * const *, const char *);
extern char *optarg;
}
#else
#include <unistd.h>
#endif

int get_item_name (Db *dbp, const Dbt *pkey, const Dbt *pdata, Dbt
*skey);

// Forward declarations
int show_item(std::string &itemName);
//int show_all_records(MyDb &inventoryDB, MyDb &vendorDB);
//int show_vendor(MyDb &vendorDB, const char *vendor);

int
usage()
{
std::cout << "example_database_read [-i <path to data files>]"
<< " [-h <database home directory>]" << std::endl;

std::cout << "Note: Any path specified to the -h parameter must
end"
<< " with your system's path delimiter (/ or \\)"
<< std::endl;
return (-1);
}

int
main (int argc, char *argv[])
{

char ch, lastChar;

// Initialize the path to the database files
std::string databaseHome("./");
std::string itemName;

std::string iDbName("plumedb.db");



try
{
// Open all databases.
MyDb plumeDB(databaseHome, iDbName);


show_item(itemName);

} catch(DbException &e) {
std::cerr << "Error reading databases. " << std::endl;
return (e.get_errno());
} catch(std::exception &e) {
std::cerr << "Error reading databases. " << std::endl;
std::cerr << e.what() << std::endl;
return (-1);
}

return (0);
} // End main

// Shows the records in the inventory database that
// have a specific item name. For each inventory record
// shown, the appropriate vendor record is also displayed.
//int
//show_item(MyDb &plumeSDB, std::string &itemName)
int show_item(std::string &itemName)
{

// Get a cursor to the itemname secondary db
Dbc *cursorp;

try {
// plumeSDB.getDb().cursor(NULL, &cursorp, 0);

// Get the search key. This is the name on the inventory
// record that we want to examine.
std::cout << "Looking for " << itemName << std::endl;
// Dbt key((void *)itemName.c_str(),
(u_int32_t)itemName.length() + 1);
Dbt key (0.0, sizeof(double));
Dbt data;

// Position the cursor to the first record in the secondary
// database that has the appropriate key.
int ret = cursorp->get(&key, &data, DB_SET);
if (!ret) {
do {
PlumeData plumeItem(data.get_data());
plumeItem.show();

// show_vendor(vendorDB,
inventoryItem.getVendor().c_str());

} while (cursorp->get(&key, &data, DB_NEXT_DUP) == 0);
} else {
std::cerr << "No records found for '" << itemName
<< "'" << std::endl;
}
} catch(DbException &e) {
// plumeSDB.getDb().err(e.get_errno(), "Error in show_item");
// cursorp->close();
throw e;
} catch(std::exception &e) {
// plumeSDB.getDb().errx("Error in show_item: %s", e.what());
// cursorp->close();
throw e;
}

// cursorp->close();
return (0);
}

// Shows all the records in the inventory database.
// For each inventory record shown, the appropriate
// vendor record is also displayed.
#if 0
int
show_all_records(MyDb &inventoryDB)
{

// Get a cursor to the inventory db
Dbc *cursorp;
try {
inventoryDB.getDb().cursor(NULL, &cursorp, 0);

// Iterate over the inventory database, from the first record
// to the last, displaying each in turn
Dbt key, data;
int ret;
while ((ret = cursorp->get(&key, &data, DB_NEXT)) == 0 )
{
InventoryData inventoryItem(data.get_data());
inventoryItem.show();

// show_vendor(vendorDB, inventoryItem.getVendor().c_str());
}
} catch(DbException &e) {
inventoryDB.getDb().err(e.get_errno(), "Error in
show_all_records");
cursorp->close();
throw e;
} catch(std::exception &e) {
cursorp->close();
throw e;
}

cursorp->close();
return (0);
}
#endif

// Shows a vendor record. Each vendor record is an instance of
// a vendor structure. See loadVendorDB() in
// example_database_load for how this structure was originally
// put into the database.
/*int
show_vendor(MyDb &vendorDB, const char *vendor)
{
Dbt data;
VENDOR my_vendor;

try {
// Set the search key to the vendor's name
// vendor is explicitly cast to char * to stop a compiler
// complaint.
Dbt key((char *)vendor, (u_int32_t)strlen(vendor) + 1);

// Make sure we use the memory we set aside for the VENDOR
// structure rather than the memory that DB allocates.
// Some systems may require structures to be aligned in memory
// in a specific way, and DB may not get it right.

data.set_data(&my_vendor);
data.set_ulen(sizeof(VENDOR));
data.set_flags(DB_DBT_USERMEM);

// Get the record
vendorDB.getDb().get(NULL, &key, &data, 0);
std::cout << " " << my_vendor.street << "\n"
<< " " << my_vendor.city << ", "
<< my_vendor.state << "\n"
<< " " << my_vendor.zipcode << "\n"
<< " " << my_vendor.phone_number << "\n"
<< " Contact: " << my_vendor.sales_rep << "\n"
<< " " << my_vendor.sales_rep_phone
<< std::endl;

} catch(DbException &e) {
vendorDB.getDb().err(e.get_errno(), "Error in show_vendor");
throw e;
} catch(std::exception &e) {
throw e;
}
return (0);
}

return 0;
}*/

class Db;
class Dbt;

int get_item_name (Db *dbp, const Dbt *pkey, const Dbt *pdata, Dbt
*skey)
{
u_int32_t offset = 0;
char *itemname = (char *) pdata->get_data() + offset;

(void) pkey;

if (offset > pdata->get_size()) {
dbp->errx("get_item_name: buffer sizes do not match!");
return (-1);
}
skey->set_data(itemname);
skey->set_size((u_int32_t) strlen(itemname) + 1);
return (0);
}


Reply With Quote
  #7  
Old   
Alex
 
Posts: n/a

Default Re: read/write problem - 04-03-2006 , 10:19 PM



My example worked fine - though it was not using cursors.

Your show_item is not going to work. The cursor create call is
commented out:
// plumeSDB.getDb().cursor(NULL, &cursorp, 0);

Without that line, any call on the uninitialized cursorp object will
result in an access violation.

If the cursor exists, your app should get a bit further. I strongly
recommend you try working through the Getting Started Guide.
The relevant section for using cursors is:
http://www.sleepycat.com/docs/gsg/CXX/Positioning.html


Reply With Quote
  #8  
Old   
Jack
 
Posts: n/a

Default Re: read/write problem - 04-04-2006 , 03:47 AM



Fine. Thanks to you guys for helping
Jack


Reply With Quote
  #9  
Old   
Jack
 
Posts: n/a

Default Re: read/write problem - 04-04-2006 , 03:47 AM



Fine. Thanks to you guys for helping
Jack


Reply With Quote
  #10  
Old   
Jack
 
Posts: n/a

Default Re: read/write problem - 04-04-2006 , 04:36 AM



Hi
With plumeSBD which is a secondary index table, when I don't use index,
how do I get thru the cursor stuff?
Thanks
Jack


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 - 2013, Jelsoft Enterprises Ltd.