日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

直接使用Berkeley DB的Memory Pool 功能

發布時間:2025/3/20 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 直接使用Berkeley DB的Memory Pool 功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文是一個示例,展示了如何直接使用BerkeleyDBmemory 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 << " randompages... /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 useDB_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 sharedlatch.
* We can't OR any flags here, any of the 5 flags should be specified separatelyin db5.0, thus
* we have to CREATE pg2, put it back to mpool, then DIRTY it, modify it and putit back again.
*/
mfp->put(pg2, DB_PRIORITY_UNCHANGED, 0);

/*
* We want to write pg2, so must specify DIRTY flag. Without the DIRTY flag, wegot 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;

轉載于:https://blog.51cto.com/davidzhao/1225805

總結

以上是生活随笔為你收集整理的直接使用Berkeley DB的Memory Pool 功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。