FreeBSDã§ï¼Google leveldbã使ã£ã¦ã¿ãï¼
ã¾ã æ§è½æ¤è¨¼ã¯ãã¦ãã¾ãããï¼è¨è¿°ãè¦ãã ãã§åãããªäºæãããGoogle謹製ã®æ°NoSQLï¼
GitHub - google/leveldb: LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.
ã³ã¼ããèªãéãï¼æ³å以ä¸ã«åãæãã§ããï¼ããã¥ã¡ã³ãæåã§ã¯ããã¾ãããï¼valueã«ããªãèªç±åº¦ãããã®ã§ï¼ã¢ã¹ãã¼ã³ã¼ãã®é表示ã³ã¼ããå ¥ã£ã¦ãã¦ãè¯ãï¼ãã¤ãé·ããåãã£ã¦ãã¦æå®ããã°ï¼ä»»æã®ãã¤ããªãã¼ã¿ãæ ¼ç´å¯è½ï¼ï¼ãªãã§ãã§ãããã§ãï¼
ã¾ãã¯ï¼FreeBSDã«ã¤ã³ã¹ãã¼ã«ãã¦ã¡ããã¨åãããã«ããã¾ã§3æ¥ããã£ãï¼ï¼ï¼
ã¾ã¨ã¾ã£ãã½ã¼ã¹ã¯ä»ã®ã¨ããé å¸ããã¦ããªãã®ã§ï¼subversionã§ãªãã¸ããªãè½ã¨ãã¾ãï¼
> svn co http://leveldb.googlecode.com/svn/trunk/ leveldb-read-only
HTTPã¢ã¯ã»ã¹ã«proxyè¨å®ãå¿ è¦ãªå ´åã¯ï¼ãããªæãï¼
> svn co --config-option servers:global:http-proxy-host=proxy.example.com --config-option servers:global:http-proxy-port=123456 http://leveldb.googlecode.com/svn/trunk/ leveldb-read-only
configureãç¡ãã¦Makefileããç¡ãã®ã§ï¼ã¾ãã¯æ®éã«makeï¼
> gmake g++ -c -I. -I./include -DLEVELDB_PLATFORM_POSIX -std=c++0x -g2 db/db_bench.cc -o db/db_bench.o cc1plus: error: unrecognized command line option "-std=c++0x" gmake: *** [db/db_bench.o]ãã¨ã©ã¼ 1
ããã©ã«ãã®g++ã¯c++0xã«å¯¾å¿ãã¦ããªãï¼ï¼ï¼
portsã³ã¬ã¯ã·ã§ã³ããï¼ãã¨ãã¨ã試ãã¦ã¿ã¾ããï¼
FreeBSD8ã§ã¯ï¼portsã§ã¯gcc-4.4ããgcc-4.7ã¾ã§å
¥ãããã¨ãã§ãã¾ãï¼
ææ°ã®4.6ã§ãã£ã¦ã¿ã¾ããï¼
ï¼4.7ã¯ã¾ã ä¸å®å®ï¼
Makefileãæ¸ãæãã¾ãï¼
//CC = g++ CC=g++46
makeãã¾ãï¼
g++46 -c -DLEVELDB_PLATFORM_POSIX -I. -I./include -std=c++0x -g2 db/db_bench.cc -o db/db_bench.o In file included from ./port/port.h:14:0, from ./util/coding.h:17, from ./db/dbformat.h:13, from ./db/db_impl.h:9, from db/db_bench.cc:8: ./port/port_posix.h:10:20: fatal error: endian.h: No such file or directory compilation terminated. *** Error code 1 Stop in /home/hoge/leveldb-read-only.
FreeBSDã§ã¯å¾®å¦ã«ãã¹ãéããã§ãï¼ï¼ï¼port_posix.hãæ¸ãæãã¾ãï¼
//#include <endian.h> #include <sys/endian.h>
ã¾ãmakeï¼
g++46 -c -DLEVELDB_PLATFORM_POSIX -I. -I./include -std=c++0x -g2 db/db_bench.cc -o db/db_bench.o In file included from ./port/port.h:14:0, from ./util/coding.h:17, from ./db/dbformat.h:13, from ./db/db_impl.h:9, from db/db_bench.cc:8: ./port/port_posix.h:15:22: fatal error: cstdatomic: No such file or directory compilation terminated. *** Error code 1 Stop in /home/hoge/leveldb-read-only.
ããã¯ããåãããªãã£ãã®ã§ããï¼includeãã¡ã¤ã«ã調ã¹ãã¨ãã¡ã¤ã«åãéãã£ã½ãã®ã§ï¼ã¾ãã½ã¼ã¹port_posix.hãæ¸ãæãã¾ãï¼
//#include <cstdatomic> #include <atomic>
ã¾ãã¾ãmake.
> gmake g++46 -c -DLEVELDB_PLATFORM_POSIX -I. -I./include -std=c++0x -g2 db/db_bench.cc -o db/db_bench.o In file included from ./port/port.h:14, from ./util/coding.h:17, from ./db/dbformat.h:13, from ./db/db_impl.h:9, from db/db_bench.cc:8: ./port/port_posix.h:21: error: '__BYTE_ORDER' was not declared in this scope ./port/port_posix.h:21: error: '__LITTLE_ENDIAN' was not declared in this scope *** Error code 1 Stop in /home/hoge/leveldb-read-only.
ããã¯ã¼ï¼ï¼ï¼å®æ°ã®å®ç¾©ãFreeBSDã¯ã¡ãã£ã¨éãã®ã§ãï¼ã½ã¼ã¹ãæ¸ãæãã¾ãï¼
//static const bool kLittleEndian = (__BYTE_ORDER == __LITTLE_ENDIAN); static const bool kLittleEndian = (_BYTE_ORDER == _LITTLE_ENDIAN);
ã¾ãã¾ãã¾ãmakeï¼
g++46 -c -DLEVELDB_PLATFORM_POSIX -I. -I./include -std=c++0x -g2 ./util/cache.cc -o ./util/cache.o ./util/cache.cc: In member function 'void leveldb::<unnamed>::LRUCache::Unref(leveldb::<unnamed>::LRUHandle*)': ./util/cache.cc:148: error: 'free' was not declared in this scope ./util/cache.cc: In member function 'virtual leveldb::Cache::Handle* leveldb::<unnamed>::LRUCache::Insert(const leveldb::Slice&, void*, size_t, void (*)(const leveldb::Slice&, void*))': ./util/cache.cc:197: error: 'malloc' was not declared in this scope *** Error code 1 Stop in /home/hoge/leveldb-read-only.
ããã¯ãã°ãªããããï¼ã£ã¦æãã§ãï¼mallocãfree使ãã®ã«ããããã¡ã¤ã«ãincludeããã¦ãªãï¼ï¼ï¼ã½ã¼ã¹ãä¿®æ£ãã¾ãï¼
#include <stdlib.h>
ã¾ãã¾ãã¾ãã¾ãmakeï¼
g++46 -c -DLEVELDB_PLATFORM_POSIX -I. -I./include -std=c++0x -g2 ./util/env_posix.cc -o ./util/env_posix.o ./util/env_posix.cc: In member function 'virtual leveldb::Status leveldb::<unnamed>::PosixSequentialFile::Read(size_t, leveldb::Slice*, char*)': ./util/env_posix.cc:43: error: 'fread_unlocked' was not declared in this scope ./util/env_posix.cc: In member function 'virtual leveldb::Status leveldb::<unnamed>::PosixMmapFile::Sync()': ./util/env_posix.cc:230: error: 'fdatasync' was not declared in this scope *** Error code 1 Stop in /home/hoge/leveldb-read-only.
ããã¯å³ããï¼fdatasyncãfread_unlockedã¯FreeBSDã¯ã¾ã ãµãã¼ããã¦ããªããã§ãï¼
ä»æ¹ãç¡ãã®ã§ï¼é
ãé¢æ°åã«æ¸ãæãã¾ãï¼
// size_t r = fread_unlocked(scratch, 1, n, file_); size_t r = fread(scratch, 1, n, file_); // if (fdatasync(fd_) < 0) { if (fsync(fd_) < 0) {
ã¾ãã¾ãã¾ãã¾ãã¾ãmakeï¼
ãªãã¸ããªã®æåã®é ã¯éãã¾ãããï¼ç¾å¨ã¯ã¢ã¼ã«ã¤ãã©ã¤ãã©ãªãä½ãããã«ãªã£ã¦ããã®ã§ï¼*.oãããªãã¦*.aããªã³ã¯ããã°è¯ãããã«ãªã£ã¦ãã¾ãï¼
ãã¹ãããã°ã©ã ãèµ°ãããã¨ï¼
> ./arena_test /libexec/ld-elf.so.1: /usr/lib/libstdc++.so.6: version GLIBCXX_3.4.10 required by /home/hoge/leveldb-read-only/arena_test not found
ãã£ã¨ï¼ããã¯gccã®ãã¼ã¸ã§ã³ãæ··å¨ããã¦ãããããªã®ã§ï¼
> env LD_LIBRARY_PATH=/usr/local/lib/gcc46/ ./arena_test ==== Test ArenaTest.Empty ==== Test ArenaTest.Simple ==== PASSED 2 tests
OK!
次ï¼èå¿ãªãã¼ã¿ãã¼ã¹ã®ãã¹ããèµ°ãããã¨
> env LD_LIBRARY_PATH=/usr/local/lib/gcc46/ ./db_test ==== Test DBTest.Empty Assertion failed: (internal_key.size() >= 8), function ExtractUserKey, file ./db/dbformat.h, line 87. Abort (core dumped)
ããï¼
æ£ã
æ©ãã ãããï¼ã¾ãã³ã³ãã¤ã©ã®ãã¼ã¸ã§ã³ãå¤ãã¦ã¿ãã¨
CC = g++45
ã¤ã±ãï¼
> env LD_LIBRARY_PATH=/usr/local/lib/gcc45/ ./db_test ==== Test DBTest.Empty ==== Test DBTest.ReadWrite ==== Test DBTest.PutDeleteGet ==== Test DBTest.IterEmpty ==== Test DBTest.IterSingle ==== Test DBTest.IterMulti ==== Test DBTest.IterSmallAndLargeMix ==== Test DBTest.Recover ==== Test DBTest.RecoveryWithEmptyLog ==== Test DBTest.MinorCompactionsHappen ==== Test DBTest.RecoverWithLargeLog ==== Test DBTest.CompactionsGenerateMultipleFiles ==== Test DBTest.SparseMerge Step 9900 of 10000 2705 entries compared: ok=1 2684 entries compared: ok=1 2705 entries compared: ok=1 ==== PASSED 24 tests Ok!
putã¨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); }
ãã¼ã¿ã®èªã¿è¾¼ã¿ï¼
#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); }
å®è¡ãã¦ã¿ã¾ãï¼
> env LD_LIBRARY_PATH=/usr/local/lib/gcc45/ ./put_leveldb
ç¡è¨ã§çµäºï¼èªã¿ã ãã¦ã¿ã¾ãï¼
> env LD_LIBRARY_PATH=/usr/local/lib/gcc45/ ./get_leveldb Hello,World
ãã¼ã¿ãã¼ã¹ã®ãã¡ã¤ã«ã¯ãããªæãã«æ ¼ç´ããã¦ãã¾ãï¼
> ls -l /tmp/testdb/ total 18 -rw-r--r-- 1 hoge wheel 123 5æ 31 23:15 000005.sst -rw-r--r-- 1 hoge wheel 123 5æ 31 23:15 000008.sst -rw-r--r-- 1 hoge wheel 123 5æ 31 23:15 000011.sst -rw-r--r-- 1 hoge wheel 123 5æ 31 23:15 000014.sst -rw-r--r-- 1 hoge wheel 123 5æ 31 23:19 000019.sst -rw-r--r-- 1 hoge wheel 0 5æ 31 23:19 000020.log -rw-r--r-- 1 hoge wheel 0 5æ 31 23:19 000021.sst -rw-r--r-- 1 hoge wheel 16 5æ 31 23:19 CURRENT -rw-r--r-- 1 hoge wheel 0 5æ 31 23:19 LOCK -rw-r--r-- 1 hoge wheel 484 5æ 31 23:19 LOG -rw-r--r-- 1 hoge wheel 353 5æ 31 23:19 LOG.old -rw-r--r-- 1 hoge wheel 220 5æ 31 23:19 MANIFEST-000018
å§ç¸®ããã¦ããã ããã£ã¦ãã¡ã¤ã«ãµã¤ãºå°ããã§ããï¼ã¾ã ãã¼ã¿ãå°ãªãããï¼
次ã¯ï¼ã¾ãã¯ãã³ããã¼ã¯ï¼Tokyo/Kyoto Cabinetãmemcachedã¨ã®é度æ¯è¼ã§ããï¼ããã¨åå¾ãã¦ï¼Linuxã§ã®ã¤ã³ã¹ãã¼ã«ãæåããããã§ãï¼