直接使用Berkeley DB的Memory Pool 功能

 

 


 

本文是一個示例,展示瞭如何直接使用 Berkeley DB memory pool功能。

BerkeleyDB對外開放了它的 memory pool, logging, mutex lock子系統,應用程序開發者可以使用 mpool來管理其他文件,按頁對那些文件做讀寫操作,還可以同時使用 lock子系統實現互斥。同時你還可以註冊 你自己的日誌讀寫和恢複函數並且使用 logging API寫日誌或者操縱日誌文件和記錄,並且使用 mutex實現多進程 /多線程互斥。有了這幾塊功能,你就可以使用 Berkeley DB的功能寫一個 Access method,比如你可以寫另外一個 btree或者 list,或者其他的數據訪問方法。有興趣的讀者可以探索一下。 這塊的功能我會在後文當中依次 demo一下。


//
// Create an environment object and initialize it for error
// reporting.
//
DbEnv *dbenv = new DbEnv(0);
int pagesize = 1024;
int cachesize = 1024 * 20;
int hits = 50;
int npages = 30;
db_pgno_t pageno, pgno1 = 1, pgno2 = 2, pgno3 = 3;
int cnt, ret;
void *p, *pg1, *pg2, *pg3;
DbMpoolFile *mfp;

dbenv->set_error_stream(&err_stream);
dbenv->set_errpfx("Mpool");
dbenv->set_shm_key(123);
dbenv->set_flags(DB_NOMMAP, 0);
dbenv->set_cachesize(0, 20 * 1024, 0);

// Databases are in a subdirectory.
dbenv->open(home, DB_CREATE | DB_INIT_MPOOL | DB_SYSTEM_MEM, 0);
try
{
if ((ret = dbenv->memp_fcreate(&mfp, 0)) != 0) {
cerr << "Mpool: memp_fcreate failed: "
<< strerror(ret) << "/n";
return;
}

int i = mfp->open("MyMPOOLFile", DB_CREATE, 0644, pagesize);
if (mfp->get(&pgno1, NULL, DB_MPOOL_CREATE, &pg1) != 0 ||
mfp->get(&pgno3, NULL, DB_MPOOL_CREATE, &pg3) != 0)
std::cout<<"/nFailed to create pages in MyMPOOLFile";
/* Now we have pg1 and pg3 to read from. */
else {
mfp->sync();/* MyMPOOLFile is enlarged here to 3 pages. */
std::cout << "retrieve " << hits << " random pages... /n";
/* Put back pg1 and pg3. */
mfp->put(pg1, DB_PRIORITY_UNCHANGED, 0);
mfp->put(pg3, DB_PRIORITY_UNCHANGED, 0);

/*
* So far pg2 is never used, so it's not in mpool, and we should use DB_MPOOL_CREATE. If we
* directly specify DB_MPOOL_DIRTY here, we can't get the page.
*/
mfp->get(&pgno2, NULL, DB_MPOOL_CREATE, &pg2);

/*
* Must put before dirty it otherwise we are self-blocked by our own shared latch.
* We can't OR any flags here, any of the 5 flags should be specified separately in db5.0, thus
* we have to CREATE pg2, put it back to mpool, then DIRTY it, modify it and put it back again.
*/
mfp->put(pg2, DB_PRIORITY_UNCHANGED, 0);

/*
* We want to write pg2, so must specify DIRTY flag. Without the DIRTY flag, we got read only pages,
* our changes won't be synced to disk when evicted.
*/
mfp->get(&pgno2, NULL, DB_MPOOL_DIRTY, &pg2);
/* Write bytes to pg2. */
strcpy((char *)pg2 + 60, "hello world!");
/* Put back pg2. */
mfp->put(pg2, DB_PRIORITY_UNCHANGED, 0);
mfp->close(0);
}
}
catch(DbException &e)
{
std::cerr<< "Exception occur: "
<<e.what()<<std::endl;
}

// Close the handle.
dbenv->close(0);
delete dbenv;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章