int main(int argc, char *argv[]) { try { Db *db = new Db(NULL, 0); db->open(NULL, "my.db", NULL, DB_BTREE, DB_CREATE, 0644); // populate our massive database. // all our strings include null for convenience. // Note we have to cast for idiomatic // usage, since newer gcc requires it. Dbt *keydbt = new Dbt((char *)"key", 4); Dbt *datadbt = new Dbt((char *)"data", 5); db->put(NULL, keydbt, datadbt, 0); // Now, retrieve. We could use keydbt over again, // but that wouldn't be typical in an application. Dbt *goodkeydbt = new Dbt((char *)"key", 4); Dbt *badkeydbt = new Dbt((char *)"badkey", 7); Dbt *resultdbt = new Dbt(); resultdbt->set_flags(DB_DBT_MALLOC); int ret; if ((ret = db->get(NULL, goodkeydbt, resultdbt, 0)) != 0) { cout << "get: " << DbEnv::strerror(ret) << "\n"; } else { char *result = (char *)resultdbt->get_data(); cout << "got data: " << result << "\n"; } if ((ret = db->get(NULL, badkeydbt, resultdbt, 0)) != 0) { // We expect this... cout << "get using bad key: " << DbEnv::strerror(ret) << "\n"; } else { char *result = (char *)resultdbt->get_data(); cout << "*** got data using bad key!!: " << result << "\n"; } cout << "finished test\n"; } catch (DbException &dbe) { cerr << "Db Exception: " << dbe.what(); } return 0; }
HeaderGroup* BulkHeaderGroup::getGroup(NewsGroup* ng, QString& articleIndex) { HeaderGroup *hg = 0; int ret; Dbt groupkey; Dbt groupdata; memset(&groupkey, 0, sizeof(groupkey)); memset(&groupdata, 0, sizeof(groupdata)); groupdata.set_flags(DB_DBT_MALLOC); QByteArray ba = articleIndex.toLocal8Bit(); const char *k= ba.constData(); groupkey.set_data((void*)k); groupkey.set_size(articleIndex.length()); Db* groupsDb = ng->getGroupingDb(); ret=groupsDb->get(NULL, &groupkey, &groupdata, 0); if (ret != 0) //key not found { qDebug() << "Failed to find group with key " << articleIndex; } else { qDebug() << "Found group with key " << articleIndex; hg=new HeaderGroup(articleIndex.length(), (char*)k, (char*)groupdata.get_data()); void* ptr = groupdata.get_data(); Q_FREE(ptr); } return hg; }
int DiskBDB::Get (const Vdt& dzname, const Vdt& Key, Vdt *Value,stats *stats_update) { Db *DbHandle; Dbt dbt_value; int ret=0; //ret=m_dzManager->GetHandleForPartition("B0",2,DbHandle); ret=m_dzManager->Get(dzname,Key,DbHandle); if(ret!=0) { cout<<"Datazone handle not retrieved from datazone manager"<<endl; return ret; } Dbt key(Key.get_data(),Key.get_size()); dbt_value.set_data((*Value).get_data()); dbt_value.set_size((*Value).get_size()); dbt_value.set_ulen((*Value).get_size() ); dbt_value.set_flags( DB_DBT_USERMEM ); bool keepTrying=true; int numTries=0; while(keepTrying && numTries < m_maxDeadlockRetries) { numTries++; try { ret=DbHandle->get(NULL,&key,&dbt_value,0); keepTrying=false; if( ret == 0 ) Value->set_size( dbt_value.get_size() ); } catch(DbException &e) { if(numTries==1) printf("DiskBDB Get::%s\n",e.what()); ret=e.get_errno(); if(ret==DB_LOCK_DEADLOCK) { if(stats_update) stats_update->NumDeadlocks++; } else keepTrying=false; if( ret==DB_BUFFER_SMALL ) (*Value).set_size(dbt_value.get_size()); } catch(exception &e) { cout << e.what() << endl; return (-1); } } return ret; }
string read(const char* skey){ Dbt key((void*)skey, ::strlen(skey)); //fetch char* buf = _allocHeap(); //keep as heap mem Dbt data; data.set_data(buf); data.set_ulen(MAX_LEN); data.set_flags(DB_DBT_USERMEM); if( _db->get(nullptr, &key, &data, 0) == DB_NOTFOUND){ return string("F"); }; string res(buf); delete[] buf; return res; };
int main(int argc, const char* argv[]) { if (argc !=3) { cout << "usage: "<< argv[0] << " <filename> <db filename>" << endl; return 1; } const char* kDatabaseName = argv[2]; const char* filename = argv[1]; DbEnv env(0); Db* pdb; string line; bool datamode = false; try { env.set_error_stream(&cerr); env.open("./", DB_CREATE | DB_INIT_MPOOL, 0); pdb = new Db(&env, 0); // If you want to support duplicated records and make duplicated // records sorted by data, you need to call: // pdb->set_flags(DB_DUPSORT); // Note that only Btree-typed database supports sorted duplicated // records // If the database does not exist, create it. If it exists, clear // its content after openning. pdb->set_flags( DB_DUP );// | DB_DUPSORT); pdb->open(NULL, kDatabaseName, NULL, DB_BTREE, DB_CREATE | DB_TRUNCATE, 0); ifstream myfile (filename); if (myfile.is_open()) { int count = 0; while ( myfile.good() ) { //if (count++ > 10) { // break; //} getline (myfile,line); if (!datamode) { if ("[DATA]" == line ) { datamode = true; } continue; } //cout << (int)line[8] << endl; int index = 0; while (32 != (char)line[index]) { index++; } string cjkey = line.substr(0, index); while (32 == line[index]) { index++; } string cjdata = line.substr(index, (line.size()-index)); cout << cjkey << "---" << cjdata << endl; Dbt key(const_cast<char*>(cjkey.data()), cjkey.size()); Dbt value(const_cast<char*>(cjdata.data()), cjdata.size()); pdb->put(NULL, &key, &value, 0); } myfile.close(); } else { cout << "Unable to open file"; } // You need to set ulen and flags=DB_DBT_USERMEM to prevent Dbt // from allocate its own memory but use the memory provided by you. string search("aa"); Dbt key(const_cast<char*>(search.c_str()), search.size()); char buffer[1024]; Dbt data; data.set_data(buffer); data.set_ulen(1024); data.set_flags(DB_DBT_USERMEM); if (pdb->get(NULL, &key, &data, 0) == DB_NOTFOUND) { cerr << "Not found" << endl; } else { cout << "Found: " << "PPPPPP" <<buffer << "PPPPPP" << endl; } if (pdb != NULL) { pdb->close(0); delete pdb; // You have to close and delete an exisiting handle, then create // a new one before you can use it to remove a database (file). pdb = new Db(NULL, 0); //pdb->remove("access.db", NULL, 0); delete pdb; } env.close(0); } catch (DbException& e) { cerr << "DbException: " << e.what() << endl; return -1; } catch (std::exception& e) { cerr << e.what() << endl; return -1; } return 0; }