LevelDBをiPhone実機で試す。

LevelDBいいね。さすが。
パフォーマンスが相当優秀みたいですが、それよりもシンプルに纏まっているドキュメントに魅力を感じる。
iPhone用のDBで色々迷ってたが、これに確定。

で、実機で動かしてみる。
ここのページからlipoでマージされたlibrary、inludeファイルがゲット可能。

試したコードは以下。

#include <CoreFoundation/CoreFoundation.h>
#include <Foundation/Foundation.h>

#include <iostream>
#include <sstream>
#include <string>

#include <leveldb/db.h>

using namespace std;
class LevelDBTest {
    private :
    string getDocumentsDirectory(){
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];

        return string([documentsDirectory cStringUsingEncoding:NSASCIIStringEncoding]);
    }
    
    public :
    
    bool doTest() {
        // Set up database connection information and open database
        leveldb::DB* db;
        leveldb::Options options;
        options.create_if_missing = true;
        
        leveldb::Status status = leveldb::DB::Open(options, getDocumentsDirectory() + "/LevelDB", &db);
        
        if (false == status.ok()) {
            cerr << "Unable to open/create database" << endl;
            cerr << status.ToString() << endl;
            return -1;
        }
        
        // Add 256 values to the database
        leveldb::WriteOptions writeOptions;
        for (unsigned int i = 0; i < 256; ++i) {
            ostringstream keyStream;
            keyStream << "Key" << i;
            
            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()) {
            cout << it->key().ToString() << " : " << it->value().ToString() << endl;
        }
        
        if (false == it->status().ok()) {
            cerr << "An error was found during the scan" << endl;
            cerr << it->status().ToString() << endl;
        }
        
        delete it;
        
        // Close the database
        delete db;
        return true;
    }
};

バッチリ動いた。

最近は Open GL ES2 にべったりやったけど、やっぱDB系の技術はグッとくる。
・・・で、どうやって使っていこうかね。