leveldb基本知识
来源:互联网 发布:数据库 数据仓库 编辑:程序博客网 时间:2024/06/13 22:22
levelDB是google开源的一个key-value存储引擎库,类似于开源的Lucene索引库一样。其他的软件开发者可以利用该库做二次开发,来满足定制需求。LevelDB采用日志式的写方式来提高写性能,但是牺牲了部分读性能。为了弥补牺牲了的读性能,一些人提议使用SSD作为存储介质。
对于本地化的Key-value存储引擎来说,简单的使用一般都分成三个基本的步骤:(1)打开一个数据库实例;(2)对这个数据库实例进行插入,修改和查询操作;(3)最后在使用完成之后,关闭该数据库。
Opening A Database
A leveldb database has a name which corresponds to a file system directory. All of the contents of database are stored in this directory. The following example shows how to open a database, creating it if necessary:#include <assert>#include "leveldb/db.h"leveldb::DB* db;leveldb::Options options;options.create_if_missing = true;leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);assert(status.ok());...If you want to raise an error if the database already exists, add the following line before the leveldb::DB::Open call:
options.error_if_exists = true;
Status
You may have noticed the leveldb::Status type above. Values of this type are returned by most functions in leveldb that may encounter an error. You can check if such a result is ok, and also print an associated error message:leveldb::Status s = ...;if (!s.ok()) cerr << s.ToString() << endl;
Closing A Database
When you are done with a database, just delete the database object. Example:... open the db as described above ...... do something with db ...delete db;
Reads And Writes
The database provides Put, Delete, and Get methods to modify/query the database. For example, the following code moves the value stored under key1 to key2.std::string value;leveldb::Status s = db->Get(leveldb::ReadOptions(), key1, &value);if (s.ok()) s = db->Put(leveldb::WriteOptions(), key2, value);if (s.ok()) s = db->Delete(leveldb::WriteOptions(), key1);
测试程序示例
#include <assert.h>#include <iostream>#include "leveldb/db.h"using namespace std;using namespace leveldb;int main(){DB* db;Options options;options.create_if_missing = true;//options.error_if_exists = true;Status status = DB::Open(options, "/tmp/testdb", &db);assert(status.ok());cout << status.ToString() << endl;string key1 = "hdu1", value1 = "123";string key2 = "hdu2", value2 = "456";string value;db->Put(WriteOptions(), key1, value1);db->Put(WriteOptions(), key2, value2);status = db->Get(ReadOptions(), key1, &value);if (status.ok())status = db->Put(WriteOptions(), key2, value);//if (status.ok())//status = db->Delete(WriteOptions(), key1);status = db->Get(ReadOptions(), key1, &value);if (status.ok())cout << key1 << ": " << value << endl;status = db->Get(ReadOptions(), key2, &value);if (status.ok())cout << key2 << ": " << value << endl;delete db;return 0;}
编译程序
命令:g++ -o test test.cpp /home/luox28/leveldb/libleveldb.a -I/home/luox28/leveldb/include -lpthread
当然,在下载好了leveldb源码后,进入到leveldb主目录,编译源码。 cd leveldb && make all参考资料
1、leveldb自带doc文档
2、leveldb入门知识
0 0
- leveldb基本知识
- LevelDb
- LevelDb
- LevelDb
- leveldb
- leveldb
- leveldb
- leveldb
- LevelDb
- LevelDb
- Leveldb
- Leveldb
- levelDB
- LevelDb
- leveldb
- LevelDb
- LevelDb
- LevelDb
- C++中的空指针和野指针
- Win7与Ubuntu双系统时卸载Ubuntu的方法
- 布局下拉刷新效果(仿大众点评吃包子)
- 黑马程序员-Java的面向对象(异常和包)
- C语言编译过程
- leveldb基本知识
- 【郝斌数据结构自学笔记】66-69_森林的存储_二叉树的先序遍历_二叉树的中序遍历【中间访问根节点】_二叉树的后序遍历【最后访问根节点】
- 分布式ID生成器
- Java中的锁
- PAT练习基础编程题目之 判断奇偶性
- Parquet_6. 在Impala表中使用 Parquet 格式
- Telerik Grid 设置合计
- Oracle XE http端口8080的修改
- linux中的trap命令 | set