leveldb安装及第一个程序

os: ubuntu 14.04 lts

leveldb version=1.19


安装

cd leveldb

make

此时leveldb/下多出out-shared和out-static目录,其中out-shared/下有:

db  db_bench  helpers  libleveldb.so  libleveldb.so.1  libleveldb.so.1.19  port  table  util

然后sudo cp out-shared/libleveldb.so* /usr/local/lib & sudo cp -R include/* /usr/local/include

完成。


第一个程序

test_leveldb.cc

#include 
#include 
#include 
#include 

#include 

int main()
{
	leveldb::DB *db;
	leveldb::Options options;
	leveldb::Status status;

	std::string key1 = "key1";
	std::string val1 = "val1", val;

	options.create_if_missing = true;
	status = leveldb::DB::Open(options, "/tmp/testdb", &db);
	if (!status.ok())
	{
		std::cout << status.ToString() << std::endl;
		exit(1);
	}

	status = db->Put(leveldb::WriteOptions(), key1, val1);
	if (!status.ok())
	{
		std::cout << status.ToString() << std::endl;
		exit(2);
	}

	status = db->Get(leveldb::ReadOptions(), key1, &val);
	if (!status.ok())
	{
		std::cout << status.ToString() << std::endl;
		exit(3);
	}
	std::cout << "Get val: " << val << std::endl;

	status = db->Delete(leveldb::WriteOptions(), key1);
	if (!status.ok())
	{
		std::cout << status.ToString() << std::endl;
		exit(4);
	}

	status = db->Get(leveldb::ReadOptions(), key1, &val);
	if (!status.ok())
	{
		std::cout << status.ToString() << std::endl;
		exit(5);
	}
	std::cout << "Get val: " << val << std::endl;

	return 0;
}

编译:g++ -o dbtest test_leveldb.cc -lleveldb

运行:./dbtest

output:

Get val: val1
NotFound:

你可能感兴趣的:(leveldb)