#include <iostream>
#include <sstream>
#include "define.h"
#include "leveldb.h"
static vector<CLevelDB *> g_vecLevelDB;
CLevelDB::CLevelDB()
{
m_pLevelDB = NULL;
m_bInit = false;
}
int CLevelDB::Init( const string &strPath )
{
if ( m_bInit ) return SUCCESS;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open( options, strPath, &m_pLevelDB );
if ( !status.ok() ) {
return FAILURE;
}
m_bInit = true;
return SUCCESS;
}
int CLevelDB::Set ( string &key, string &data )
{
if ( m_pLevelDB == NULL ) return FAILURE;
leveldb::Status status;
status = m_pLevelDB->Put( leveldb::WriteOptions(), key, data );
if ( status.ok() ) {
return SUCCESS;
}
if ( status.IsIOError() ) {
return ERRNO_DB_IO;
}
return FAILURE;
}
int CLevelDB::Get ( string &key, string &data )
{
if ( m_pLevelDB == NULL ) {
return FAILURE;
}
leveldb::Status status;
status = m_pLevelDB->Get( leveldb::ReadOptions(), key, &data );
if ( status.ok() ) {
return SUCCESS;
}
if ( status.IsNotFound() ) {
return ERRNO_ELEMENT_NOT_FOUND;
}
if ( status.IsIOError() ) {
return ERRNO_DB_IO;
}
return FAILURE;
}
int CLevelDB::Del ( string &key )
{
if ( m_pLevelDB == NULL ) {
return FAILURE;
}
leveldb::Status status;
status = m_pLevelDB->Delete( leveldb::WriteOptions(), key );
if ( status.ok() ) {
return SUCCESS;
}
if ( status.IsNotFound() ) {
return ERRNO_ELEMENT_NOT_FOUND;
}
if ( status.IsIOError() ) {
return ERRNO_DB_IO;
}
return FAILURE;
}
uint32_t CLevelDB::Walk ( uint32_t counter, pcbFunc cbf )
{
map<uint32_t,string> m;
uint32_t todo = 0;
uint32_t cnt = 0;
uint32_t uin = 0;
string data;
leveldb::Iterator* it = m_pLevelDB->NewIterator( leveldb::ReadOptions() );
for ( it->SeekToFirst(); it->Valid(); it->Next() ) {
todo++;
data = it->value().ToString();
it->key().ToString().copy( (char*)&uin, sizeof(uint32_t) );
m.insert( pair<uint32_t,string>(uin,data) );
cnt++;
if ( cnt == counter ) {
usleep( 1000 ); // leveldb 非常消耗CPU,循环counter次让进程休眠1ms可降低CPU
cbf( m );
cnt = 0;
m.clear();
}
}
return todo;
}
int CLevelDBManager::Init( const string &strPathPrefix, const uint32_t dbNum )
{
for ( uint32_t i = 0; i < dbNum; i++ ) {
CLevelDB *db = NULL;
db = new CLevelDB;
if ( db == NULL ) {
exit(1);
}
stringstream ss;
ss<<strPathPrefix<<"/"<<i;
string path = ss.str();
if ( db->Init( path ) == SUCCESS ) {
g_vecLevelDB.push_back( db );
}
}
return g_vecLevelDB.size();
}
CLevelDB * CLevelDBManager::GetDB ( uint32_t i )
{
if ( i >= g_vecLevelDB.size() ) {
return NULL;
}
return g_vecLevelDB[i];
}
int CLevelDBManager::Fini ()
{
int cnt = 0;
vector<CLevelDB*>::iterator it;
for ( it = g_vecLevelDB.begin(); it != g_vecLevelDB.end(); it++ ) {
if ( *it != NULL ) {
delete *it;
*it = NULL;
cnt++;
}
}
return cnt;
}
海量数据处理的初探--亿级数据的离线计算--LevelDB简单封装
3星 · 超过75%的资源 需积分: 50 130 浏览量
2013-06-18
11:38:06
上传
评论
收藏 3KB RAR 举报
战歌IT
- 粉丝: 122
- 资源: 2409
最新资源
- 基于JSP水产品销售系统源码.zip
- 基于JSP手机商城管理系统源码.zip
- 5.3.1_1二叉树的先中后序遍历.mp420240404-134540.png
- 基于JSP实现一个C语言教学网站平台源码.zip
- 抖音快手-课程网盘链接提取码下载 .txt
- 934742083249391XGOBOTV1.2.4.apk
- 课程设计基于OpenCV的材料缺陷检测程序python源码(含超详细注释).zip
- 基于python和百度EsayDL实现自动驾驶算法+基于ESP32开发板作为智能车主控芯片的自动驾驶智能车项目+源码(高分项目)
- Centos7-离线安装-MySQL5-7-31-tar包安装
- 蓝海平台带货-课程网盘链接提取码下载 .txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈