持久化的 map ,使用 BerkeleyDB
使用前面介绍的序列化框架,可以非常简单地将Bekeley DB作为存储层,实现一个易于使用的,强类型的,持久化的map。
这个设计的的基本原则就是:模板作为一个薄的、类型安全的包装层,实现层的代码可以多个模板实例来公用,这样不但加快了编译时间,也减小了生成的代码尺寸。
这个实现相当于std::map<Key,Data>,但接口上也不完全相同,主要是基于易实现和性能考虑。
下一篇介绍std::map<Key1,std::map<Key2,Data> >的BerkeleyDB实现。
多的不说,贴上代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
#ifndef __febird_bdb_dbmap_h__ #define __febird_bdb_dbmap_h__ #if defined(_MSC_VER) && (_MSC_VER >= 1020) # pragma once #endif #include <db_cxx.h> #include "native_compare.hpp" #include "../io/DataIO.hpp" #include "../io/MemStream.hpp" #include "../util/refcount.hpp" #include <febird/num_to_str.hpp> #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 6) #else #error BerkelyDB version too low, please update #endif namespace febird { class FEBIRD_DLL_EXPORT dbmap_iterator_impl_base : public RefCounter { public: class dbmap_base* m_owner; DBC* m_curp; int m_ret; public: dbmap_iterator_impl_base(class dbmap_base* owner); void init(DB* dbp, DB_TXN* txn, const char* func); virtual ~dbmap_iterator_impl_base(); virtual void load_key(void* data, size_t size) = 0; virtual void load_val(void* data, size_t size) = 0; virtual void save_key(PortableDataOutput<AutoGrownMemIO>& oKey) = 0; void advance(u_int32_t direction_flag, const char* func); void update(const void* d, const char* func); void remove(const char* func); }; class FEBIRD_DLL_EXPORT dbmap_base { DECLARE_NONE_COPYABLE_CLASS(dbmap_base) public: DB* m_db; size_t m_bulkSize; bt_compare_fcn_type m_bt_comp; dbmap_base(DB_ENV* env, const char* dbname , u_int32_t flags , DB_TXN* txn , bt_compare_fcn_type bt_comp , const char* func ); virtual ~dbmap_base(); virtual void save_key(PortableDataOutput<AutoGrownMemIO>& dio, const void* key) const = 0; virtual void save_val(PortableDataOutput<AutoGrownMemIO>& dio, const void* data) const = 0; virtual dbmap_iterator_impl_base* make_iter() = 0; dbmap_iterator_impl_base* begin_impl(DB_TXN* txn, const char* func); dbmap_iterator_impl_base* end_impl(DB_TXN* txn, const char* func); dbmap_iterator_impl_base* find_impl(const void* k, DB_TXN* txn, u_int32_t flags, const char* func); bool insert_impl(const void* k, const void* d, u_int32_t flags, DB_TXN* txn, const char* func); bool remove_impl(const void* k, DB_TXN* txn, const char* func); void clear_impl(DB_TXN* txn, const char* func); }; template<class Key, class Val, class Value, class Impl> class dbmap_iterator : public std::iterator<std::bidirectional_iterator_tag, Value, ptrdiff_t, const Value*, const Value&> { boost::intrusive_ptr<dbmap_iterator_impl_base> m_impl; void copy_on_write() { if (m_impl->get_refcount() > 1) { Impl* q = static_cast<Impl*>(m_impl.get()); Impl* p = new Impl(q->m_owner); q->m_ret = q->m_curp->dup(q->m_curp, &p->m_curp, DB_POSITION); FEBIRD_RT_assert(0 == q->m_ret, std::runtime_error); m_impl.reset(p); } } private: #ifdef _MSC_VER //# pragma warning(disable: 4661) // declaration but not definition //! MSVC will warning C4661 "declaration but not definition" void operator++(int) { assert(0); } void operator--(int) { assert(0); } #else //! disable, because clone iterator will cause very much time and resource void operator++(int);// { assert(0); } void operator--(int);// { assert(0); } #endif public: dbmap_iterator() {} explicit dbmap_iterator(dbmap_iterator_impl_base* impl) : m_impl(impl) { assert(impl); assert(dynamic_cast<Impl*>(impl)); } // bool exist() const { return DB_NOTFOUND != m_impl->m_ret && DB_KEYEMPTY != m_impl->m_ret; } bool exist() const { return 0 == m_impl->m_ret; } void update(const Val& val) { m_impl->update(&val, BOOST_CURRENT_FUNCTION); } void remove() { m_impl->remove(BOOST_CURRENT_FUNCTION); } dbmap_iterator& operator++() { assert(0 == m_impl->m_ret); copy_on_write(); m_impl->advance(DB_NEXT, BOOST_CURRENT_FUNCTION); return *this; } dbmap_iterator& operator--() { assert(0 == m_impl->m_ret); copy_on_write(); m_impl->advance(DB_PREV, BOOST_CURRENT_FUNCTION); return *this; } const Value& operator*() const { assert(0 == m_impl->m_ret); return static_cast<Impl*>(m_impl.get())->m_kv; } const Value* operator->() const { assert(0 == m_impl->m_ret); return &static_cast<Impl*>(m_impl.get())->m_kv; } Value& get_mutable() const { assert(0 == m_impl->m_ret); return static_cast<Impl*>(m_impl.get())->m_kv; } }; template<class Key, class Val> class dbmap : protected dbmap_base { DECLARE_NONE_COPYABLE_CLASS(dbmap) public: typedef Key key_type; typedef std::pair<Key, Val> value_type; protected: class dbmap_iterator_impl : public dbmap_iterator_impl_base { public: value_type m_kv; dbmap_iterator_impl(dbmap_base* owner) : dbmap_iterator_impl_base(owner) {} virtual void load_key(void* data, size_t size) { PortableDataInput<MemIO> iKey; iKey.set(data, size); iKey >> m_kv.first; FEBIRD_RT_assert(iKey.eof(), std::logic_error); } virtual void load_val(void* data, size_t size) { PortableDataInput<MinMemIO> iVal; iVal.set(data); iVal >> m_kv.second; FEBIRD_RT_assert(iVal.diff(data) == size, std::logic_error); } virtual void save_key(PortableDataOutput<AutoGrownMemIO>& oKey1) { oKey1 << m_kv.first; } }; //! overrides void save_key(PortableDataOutput<AutoGrownMemIO>& dio, const void* key) const { dio << *(const Key*)key; } void save_val(PortableDataOutput<AutoGrownMemIO>& dio, const void* val) const { dio << *(const Val*)val; } dbmap_iterator_impl_base* make_iter() { return new dbmap_iterator_impl(this); } public: typedef dbmap_iterator<Key, Val, value_type, dbmap_iterator_impl> iterator, const_iterator; //! constructor //! @param bt_comp //! Key 比较函数,默认情况下自动推导 //! -# 如果 Key 是预定义类型,会推导至相应的比较函数 //! -# 如果是自定义类型,会推导至 Key::bdb_bt_compare, 如果未定义此函数或原型不匹配,都会报错 //! -# 也可以直接指定,从而禁止自动推导 dbmap(DB_ENV* env, const char* dbname , u_int32_t flags = DB_CREATE , DB_TXN* txn = NULL , bt_compare_fcn_type bt_comp = bdb_auto_bt_compare((Key*)(0)) ) : dbmap_base(env, dbname, txn, bt_comp, BOOST_CURRENT_FUNCTION) { } dbmap(DbEnv* env, const char* dbname , u_int32_t flags = DB_CREATE , DbTxn* txn = NULL , bt_compare_fcn_type bt_comp = bdb_auto_bt_compare((Key*)(0)) ) : dbmap_base(env->get_DB_ENV(), dbname, flags, txn ? txn->get_DB_TXN() : NULL, bt_comp, BOOST_CURRENT_FUNCTION) { } iterator begin(DB_TXN* txn = NULL) { return iterator(begin_impl(txn, BOOST_CURRENT_FUNCTION)); } iterator end (DB_TXN* txn = NULL) { return iterator(end_impl (txn, BOOST_CURRENT_FUNCTION)); } iterator begin(DbTxn* txn) { return iterator(begin_impl(txn ? txn->get_DB_TXN() : NULL, BOOST_CURRENT_FUNCTION)); } iterator end (DbTxn* txn) { return iterator(end_impl (txn ? txn->get_DB_TXN() : NULL, BOOST_CURRENT_FUNCTION)); } value_type back() { iterator iter = this->end(); --iter; if (iter.exist()) return *iter; throw std::runtime_error(BOOST_CURRENT_FUNCTION); } value_type front() { iterator iter = this->begin(); if (iter.exist()) return *iter; throw std::runtime_error(BOOST_CURRENT_FUNCTION); } iterator find(const Key& k, DB_TXN* txn = NULL) { return iterator(find_impl(&k, txn, DB_SET, BOOST_CURRENT_FUNCTION)); } iterator find(const Key& k, DbTxn* txn) { return iterator(find_impl(&k, txn ? txn->get_DB_TXN() : NULL, DB_SET, BOOST_CURRENT_FUNCTION)); } iterator lower_bound(const Key& k, DB_TXN* txn = NULL) { return iterator(find_impl(&k, txn, DB_SET_RANGE, BOOST_CURRENT_FUNCTION)); } iterator lower_bound(const Key& k, DbTxn* txn) { return iterator(find_impl(&k, txn ? txn->get_DB_TXN() : NULL, DB_SET_RANGE, BOOST_CURRENT_FUNCTION)); } bool insert(const std::pair<Key,Val>& kv, DB_TXN* txn = NULL) { return insert_impl(&kv.first, &kv.second, DB_NOOVERWRITE, txn, BOOST_CURRENT_FUNCTION); } bool insert(const std::pair<Key,Val>& kv, DbTxn* txn) { return insert_impl(&kv.first, &kv.second, DB_NOOVERWRITE, txn ? txn->get_DB_TXN() : NULL, BOOST_CURRENT_FUNCTION); } bool insert(const Key& k, const Val& d, DB_TXN* txn = NULL) { return insert_impl(&k, &d, DB_NOOVERWRITE, txn, BOOST_CURRENT_FUNCTION); } bool insert(const Key& k, const Val& d, DbTxn* txn) { return insert_impl(&k, &d, DB_NOOVERWRITE, txn ? txn->get_DB_TXN() : NULL, BOOST_CURRENT_FUNCTION); } void replace(const std::pair<Key,Val>& kv, DB_TXN* txn = NULL) { insert_impl(&kv.first, &kv.second, 0, txn, BOOST_CURRENT_FUNCTION); } void replace(const std::pair<Key,Val>& kv, DbTxn* txn) { insert_impl(&kv.first, &kv.second, 0, txn ? txn->get_DB_TXN() : NULL, BOOST_CURRENT_FUNCTION); } void replace(const Key& k, const Val& d, DB_TXN* txn = NULL) { insert_impl(&k, &d, 0, txn, BOOST_CURRENT_FUNCTION); } void replace(const Key& k, const Val& d, DbTxn* txn) { insert_impl(&k, &d, 0, txn ? txn->get_DB_TXN() : NULL, BOOST_CURRENT_FUNCTION); } bool remove(const Key& k, DB_TXN* txn = NULL) { return remove_impl(&k, txn, BOOST_CURRENT_FUNCTION); } bool remove(const Key& k, DbTxn* txn) { return remove_impl(&k, txn ? txn->get_DB_TXN() : NULL, BOOST_CURRENT_FUNCTION); } bool erase(iterator& iter) { return iter.remove(); } void clear(DB_TXN* txn = NULL) { clear_impl(txn, BOOST_CURRENT_FUNCTION); } void clear(DbTxn* txn) { return clear_impl(txn ? txn->get_DB_TXN() : NULL, BOOST_CURRENT_FUNCTION); } DB* getDB() { return m_db; } const DB* getDB() const { return m_db; } }; } // namespace febird #endif // __febird_bdb_dbmap_h__ |
楼主贴源码怎么推广使用啊? 应该贴如何调用的代码吧?