leveldb 尝鲜


今天刷知乎看到一个问题的答案推荐了
https://github.com/google/leveldb,我好奇的点开了链接,看了下代码量,然后就入坑了。这是要转行做IT咩?。。。

以前用过Oracle的Pro*C,用过MySQL的C语言API,今天我又体验了下leveldb,感觉飞起来了!难道是因为Jeff Dean大神写的?

首先zypper se leveldb有,然后zypper in zypper in leveldb-devel leveldb-devel-static顺便装上了依赖snappy

然后码点代码尝尝鲜

/*********************************************************
          File Name:level.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Thu 17 Mar 2016 06:47:50 PM CST
**********************************************************/

#include <iostream>
#include <string>
#include <leveldb/db.h>

using std::cout;
using std::cerr;
using std::endl;
using std::string;

class DB
{
    private:
        leveldb::DB *db;
        leveldb::Options options;
        leveldb::ReadOptions rdoption;
        leveldb::WriteOptions wroption;
        leveldb::Status status;
    public:
        DB() = delete;
        DB(const string &dbname)
        {
            options.create_if_missing = true;
            status = leveldb::DB::Open(options, dbname, &db);
        }
        ~DB()
        {
            delete db;
        }

        void setreadoptions(bool checksum, bool fill_cache = true, leveldb::Snapshot *snapshot = nullptr)
        {
            rdoption.verify_checksums = checksum;
            rdoption.fill_cache = fill_cache;
            rdoption.snapshot = snapshot;
        }

        void setwriteoption(bool sync = true)
        {
            wroption.sync = sync;
        }

        bool ok()
        {
            return status.ok();
        }

        bool put(const string &key, const string &value)
        {
            status = db->Put(wroption, key, value);
            return status.ok();
        }

        bool get(const string &key, string &value)
        {
            status = db->Get(rdoption, key, &value);
            return status.ok();
        }

        bool del(const string &key)
        {
            status = db->Delete(wroption, key);
            return status.ok();
        }

        bool all()
        {
            leveldb::Iterator *it = db->NewIterator(rdoption);
            for(it->SeekToFirst(); it->Valid(); it->Next())
                cout << it->key().ToString() << " => " << it->value().ToString() << endl;
            status = it->status();
            delete it;
            return status.ok();
        }

        string msg()
        {
            return status.ToString();
        }
};

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        cerr << argv[0] << " database\n";
        return 1;
    }

    DB db(argv[1]);

    if(!db.ok())
    {
        cerr << "error!\n";
        return 1;
    }
    std::string cmd, key, value, res;
    cerr << "help for commands, Ctrl+D exit.\n";
    while(std::getline(std::cin, cmd))
    {
        if(cmd == "write")
        {
            cerr << "Key: ";
            std::getline(std::cin, key);
            cerr << "value: ";
            std::getline(std::cin, value);
            db.put(key, value);
            cerr << db.msg() << endl;
        }
        else if(cmd == "read")
        {
            cerr << "Key: ";
            std::getline(std::cin, key);
            bool stat = db.get(key, value);
            if(stat)
                cerr << "=> " << value << endl;
            cerr << db.msg() << endl;
        }
        else if(cmd == "delete")
        {
            cerr << "Key: ";
            std::getline(std::cin, key);
            db.del(key);
            cerr << db.msg() << endl;
        }
        else if(cmd == "show")
        {
            db.all();
            cerr << db.msg() << endl;
        }
        else 
        {
            system("clear");
            cerr << "commands: write, read, delete, show, clear, help\n";
        }
    }
}

然而,就这么点代码也出问题了。。

clang++ -std=c++11 level.cpp -lleveldb
/tmp/level-726e50.o: In function `DB::msg()':
level.cpp:(.text._ZN2DB3msgEv[_ZN2DB3msgEv]+0x1c): undefined reference to `leveldb::Status::ToString() const'
clang-3.7: error: linker command failed with exit code 1 (use -v to see invocation)

Google了半天没找到办法,最后想起了gcc,就用gcc编译过了。。。

leveldbtest

真好用,我要看源码!我要看源码!我要看源码!

2016-04-06 16:44更新
openSUSE Leap42 上的leveldb

Information for package libleveldb1:
------------------------------------
Repository: openSUSE-Leap-42.1-Oss
Name: libleveldb1
Version: 1.18-2.3
Arch: x86_64
Vendor: openSUSE
Installed: Yes
Status: up-to-date
Installed Size: 361.3 Ki

clang版本

clang version 3.7.0 (tags/RELEASE_370/final 246586)
Target: x86_64-suse-linux
Thread model: posix
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/4.8
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/5
Found candidate GCC installation: /usr/lib64/gcc/x86_64-suse-linux/4.8
Found candidate GCC installation: /usr/lib64/gcc/x86_64-suse-linux/5
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/5
Candidate multilib: .;@m64
Selected multilib: .;@m64

在这样的平台上能用clang编译通过


然而在openSUSE Tumbleweed上

Information for package libleveldb1:
------------------------------------
Repository: oss
Name: libleveldb1
Version: 1.18-2.6
Arch: x86_64
Vendor: openSUSE
Installed: Yes
Status: up-to-date
Installed Size: 393.3 KiB

clang版本已经升级成3.8了

clang version 3.8.0 (tags/RELEASE_380/final 262553)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/5
Found candidate GCC installation: /usr/lib64/gcc/x86_64-suse-linux/5
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/5
Candidate multilib: .;@m64
Selected multilib: .;@m64

仍然编译不过。。。



转载请注明:Serenity » leveldb 尝鲜

上一篇

下一篇