int DiskBDB::Truncate ( const Vdt *dzname ) { int ret = -1; int numPartition = 0; DbTxn* txn = NULL; Db *DbHandle; numPartition = m_dzManager->GetNumPartition( *dzname ); string s_dzname = ""; uint num_deleted = 0; const char **pathArray; m_DiskEnv->get_data_dirs(&pathArray); string s_datadir_init = pathArray[0]; string s_datadir, s_pathFile; TCUtility::fixDataDirectoryPath( s_datadir_init, s_datadir ); for( int i = 0; i < numPartition; i++ ) { //txn = NULL; //m_DiskEnv->txn_begin(NULL, &txn, 0); s_pathFile.clear(); s_dzname.clear(); //s_dzname = ""; m_dzManager->AppendPartitionID(*dzname, i, s_dzname); s_dzname += TC_TRUCATE_DZONE_MASK; s_pathFile = s_datadir + s_dzname; //ret = m_dzManager->GetHandleForPartition( s_dzname.c_str(), strlen(s_dzname.c_str()), DbHandle ); //ret = m_dzManager->OpenHandleNoEnv( s_dzname.c_str(), strlen(s_dzname.c_str()), DbHandle ); ret = m_dzManager->OpenHandleNoEnv( s_pathFile.c_str(), strlen(s_pathFile.c_str())+1, DbHandle ); //handle ret!=0 //blablabla... try { ret = DbHandle->truncate(NULL, &num_deleted, 0); //ret = DbHandle->truncate( txn, &num_deleted, 0 ); //handle ret!=0 //blablabla... //txn->commit(0); } catch(DbException ex) { ret = ex.get_errno(); cout << ex.what(); cout << ex.get_errno(); cout<<"Error. DiskBDB::Truncate-> Exception encountered while truncating DiskBDB for DZName: " << s_dzname << endl; //txn->abort(); } DbHandle->close(0); } if(ret!=0) { cout<<"The Handle was not retrieved from datazone manager"<<endl; return ret; } return ret; }
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"; } // Now, truncate and make sure that it's really gone. cout << "truncating data...\n"; u_int32_t nrecords; db->truncate(NULL, &nrecords, 0); cout << "truncate returns " << nrecords << "\n"; if ((ret = db->get(NULL, goodkeydbt, resultdbt, 0)) != 0) { // We expect this... cout << "after truncate get: " << DbEnv::strerror(ret) << "\n"; } else { char *result = (char *)resultdbt->get_data(); cout << "got data: " << result << "\n"; } db->close(0); cout << "finished test\n"; } catch (DbException &dbe) { cerr << "Db Exception: " << dbe.what(); } return 0; }