Leveldb

Mac OSX 10.8

install

wget http://leveldb.googlecode.com/files/leveldb-1.9.0.tar.gz
tar -zxvf leveldb-1.9.0.tar.gz 
cd leveldb-1.9.0/
make check

cp lib* /usr/local/lib/
cp include/ /usr/local/include/


put

#include <cassert>
#include <iostream>
#include <leveldb/db.h>
int main()
{

  leveldb::DB* db=NULL;
  leveldb::Options options;
  options.create_if_missing = true;
  leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
  assert(status.ok());

  leveldb::Slice key = "Hello,";
  std::string val = "World!";
  status = db->Put(leveldb::WriteOptions(), key, val);
  if(!status.ok()) std::cout<<status.ToString()<<std::endl;

  delete db;
  return(0);
}

get

#include <cassert>
#include <iostream>
#include <leveldb/db.h>
int main()
{
  leveldb::DB* db=NULL;
  leveldb::Options options;
  options.create_if_missing = true;
  leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
  assert(status.ok());

  leveldb::Slice key = "Hello,";
  std::string val;
  status = db->Get(leveldb::ReadOptions(), key, &val);
  if(!status.ok()) std::cout<<status.ToString()<<std::endl;

  std::cout<<key.ToString()<<val<<std::endl;

  delete db;
  return(0);
}


delete

#include <cassert>
#include <iostream>
#include <leveldb/db.h>
int main()
{
  leveldb::DB* db=NULL;
  leveldb::Options options;
  options.create_if_missing = true;
  leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
  assert(status.ok());

  leveldb::Slice key = "Hello,";
  status = db->Delete(leveldb::WriteOptions(), key);
  if(!status.ok()) std::cout<<status.ToString()<<std::endl;

  delete db;
  return(0);
}

complie

g++  hoge.cpp -lleveldb
Remove all ads