leveldb学习---0

来源:互联网 发布:烈焰遮天 源码 双端 编辑:程序博客网 时间:2024/06/06 01:44

阅读google c++ style
资料:
https://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/
https://google.github.io/styleguide/cppguide.html

源码:
https://github.com/google/leveldb

编译安装

on ubuntu14.04:

sudo apt-get install git-core libsnappy-dev
git clone https://github.com/google/leveldb.gitcd leveldb/makesudo scp out-static/lib* out-shared/lib* /usr/local/lib/cd include/sudo scp -r leveldb /usr/local/include/sudo ldconfig

参考:
https://github.com/google/leveldb/issues/433

参考:
https://gist.github.com/dustismo/6203329
https://stackoverflow.com/questions/21435690/g-cant-find-headers-but-i-did-include-them

学习样例:
https://github.com/dazfuller/LevelDB-Sample/blob/master/sample.cpp

sample code:

/***      Filename: testdb.cc**        Author: Haibo Hao*        Email : haohaibo@ncic.ac.cn*   Description: ---*        Create: 2017-06-17 19:39:37* Last Modified: 2017-06-17 19:39:37**/#include <iostream>#include <sstream>#include <string>#include "leveldb/db.h"int main(){    leveldb::DB* db;    leveldb::Options options;    options.create_if_missing = true;    leveldb::Status status =  leveldb::DB::Open(options, ".testdb", &db);    if (false == status.ok())    {        std::cerr << "Unable to open/create test database './testdb'"            << std::endl;        std::cerr << status.ToString() << std::endl;        return -1;    }    // Add 256 values to the database    leveldb::WriteOptions writeOptions;    for(unsigned int i = 0; i < 256; ++i)    {        std::ostringstream keyStream;        keyStream << "Key" << i;        std::ostringstream valueStream;        valueStream << "Test data value : " << i;        db->Put(writeOptions, keyStream.str(), valueStream.str());      }    // Iterate over each item in the database and print them    leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());    for(it->SeekToFirst(); it->Valid(); it->Next())    {        std::cout << it->key().ToString()             << " : " << it->value().ToString() << std::endl;    }    if(false == it->status().ok())    {        std::cerr << "An error was found during the scan" << std::endl;    }    delete it;    // Close the databse    delete db;    return 0;}

compile:

g++ -o testdb testdb.cc  -lleveldb