Created
June 24, 2010 14:38
-
-
Save kumagi/451528 to your computer and use it in GitHub Desktop.
tokyo cabinet C++ wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TCPP_HPP_ | |
#define TCPP_HPP_ | |
#include <tcutil.h> | |
#include <tchdb.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <stdint.h> | |
#include <string> | |
#include <vector> | |
#include <boost/shared_ptr.hpp> | |
namespace tokyo_cabinet{ | |
class buff{ | |
public: | |
const void* ptr; | |
const int len; | |
inline buff(const buff& o):ptr(o.ptr),len(o.len){} | |
inline buff(const std::string& o):ptr(o.data()),len(o.size()){} | |
inline buff(const void* _buff, int _len):ptr(_buff),len(_len){} | |
inline buff(const std::vector<char>& o):ptr(o.data()),len(o.size()){} | |
private: | |
buff(); | |
}; | |
class record{ | |
public: | |
void* const ptr; | |
const int len; | |
inline record(void* _ptr, int _len):ptr(_ptr),len(_len){} | |
~record(){ | |
free(ptr); | |
} | |
private: | |
record(); | |
record(const record&); | |
}; | |
class map{ | |
TCMAP* m; | |
public: | |
inline map():m(tcmapnew()){} | |
inline map(const map& org):m(tcmapdup(org.m)){} | |
inline map* operator=(const map& rhs){ | |
tcmapdel(m); | |
m = tcmapdup(rhs.m); | |
return this; | |
} | |
inline void insert(const buff& k, const buff& v){ | |
tcmapput(m, k.ptr, k.len, v.ptr, v.len); | |
} | |
inline void remove(const buff& k){ | |
tcmapout(m, k.ptr, k.len); | |
} | |
inline buff find(const buff& k){ | |
int length; | |
const void* ptr = tcmapget(m, k.ptr, k.len, &length); | |
return buff(ptr,length); | |
} | |
inline uint64_t size()const{ | |
return tcmaprnum(m); | |
} | |
inline uint64_t datasize()const{ | |
return tcmapmsiz(m); | |
} | |
~map(){ | |
tcmapdel(m); | |
} | |
}; | |
class db_map{ | |
TCHDB* h; | |
public: | |
inline db_map(const char* filename, int backetsize):h(tchdbnew()){ | |
tchdbsetmutex(h); | |
tchdbtune(h,backetsize,6,14,HDBTLARGE); | |
tchdbopen(h,filename,HDBOWRITER | HDBOCREAT | HDBOTRUNC); | |
} | |
inline void insert(const buff& k, const buff& v){ | |
bool result = tchdbput(h, k.ptr, k.len, v.ptr, v.len); | |
if(!result){ | |
int ecode = tchdbecode(h); | |
fprintf(stderr,"hash db error:%s\n",tchdberrmsg(ecode)); | |
} | |
} | |
inline void remove(const buff& k){ | |
tchdbout(h, k.ptr, k.len); | |
} | |
inline const record* find(const buff& k){ | |
int length; | |
void* ptr = tchdbget(h, k.ptr, k.len, &length); | |
fprintf(stderr,"length: %d\n",length); | |
return new record(ptr,length); | |
} | |
~db_map(){ | |
tchdbclose(h); | |
tchdbdel(h); | |
} | |
}; | |
namespace { | |
template<typename T> | |
int cmp(const char* lhs_k, int, const char* rhs_k, int, void*){ | |
const T& lhs = T(*(T*)lhs_k); | |
const T& rhs = T(*(T*)rhs_k); | |
/* | |
if(lhs->operator<(*rhs))return -1; | |
else if(rhs->operator<(*lhs))return 1; | |
else return 0; | |
*/ | |
if(operator<(lhs,rhs)) return -1; | |
else if(operator<(rhs,lhs)) return 1; | |
else return 0; | |
} | |
template<> | |
int cmp<std::string>(const char* lhs_k, int, const char* rhs_k, int, void*){ | |
const std::string& lhs(lhs_k); | |
const std::string& rhs(rhs_k); | |
if(operator<(lhs,rhs)) return -1; | |
else if(operator<(rhs,lhs)) return 1; | |
else return 0; | |
} | |
} | |
template<typename T> | |
class tree{ | |
TCTREE *t; | |
public: | |
inline tree():t(tctreenew2(cmp<T>, NULL)){} | |
inline tree(const tree& org):t(tctreedup(org.t)){} | |
inline tree* operator=(const tree& rhs){ | |
tctreedel(t); | |
t = tctreedup(rhs.t); | |
return this; | |
} | |
inline void insert(const buff& k, const buff& v){ | |
tctreeput(t, k.ptr, k.len, v.ptr, v.len); | |
} | |
inline void remove(const buff& k){ | |
tctreeout(t, k.ptr, k.len); | |
} | |
inline buff find(const buff& k){ | |
int length; | |
const void* ptr = tctreeget(t, k.ptr, k.len, &length); | |
return buff(ptr,length); | |
} | |
inline uint64_t size()const{ | |
return tctreernum(t); | |
} | |
inline uint64_t datasize()const{ | |
return tctreemsiz(t); | |
} | |
~tree(){ | |
tctreedel(t); | |
} | |
}; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment