Berkeley DB在Queue模式下的使用
Queue模式讀數據的一個簡單的示例
在Queue模式下讀數據,記錄(Dbt)要調用set_ulen函數和set_flags函數
#include < time.h >
#include < iostream >
#include < bdb/db_cxx.h >
struct ValueType
{
int _int;
char _char;
char _array[256];
};
void readDB( void )
{
Db bdb( 0, 0 );
bdb.set_re_len( sizeof( ValueType ) ); //用Queue模式一定要調用,而且一定要在open前調用
bdb.set_re_pad( 0x00 ); //為了字符串的填充為0。
bdb.open( 0, "SaveRecodeDB.db", 0, DB_QUEUE, DB_CREATE, 0 );
size_t k;
ValueType v;
Dbt key( &k, sizeof( size_t ) );
key.set_ulen( sizeof( size_t ) );
Dbt value( &v, sizeof( ValueType ) );
value.set_ulen( sizeof( ValueType ) );
value.set_flags( DB_DBT_USERMEM );
//直接用數據庫的讀函數
for( int i=0; i<1000000; ++i )
{
bdb.get( 0, &key, &value, DB_CONSUME );
}
bdb.close( 0 );
}
int main( int argc, char* argv[] )
{
clock_t et1 = clock();
readDB();
clock_t et2 = clock();
std::cout << "work is fine, " << "have times : " << et2 - et1 << std::endl;
return 0;
}
posted on 2007-05-30 13:58
walkspeed 閱讀(1687)
評論(2) 編輯 收藏 引用 所屬分類:
C++語言 、
Berkeley DB