bsl_kv_btree.h 14.0 KB
Newer Older
W
wangguibao 已提交
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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
/* -*- c++ -*-
   copy[write] by dirlt(dirtysalt1987@gmail.com) */
#ifndef _BSL_KV_BTREE_H_
#define _BSL_KV_BTREE_H_

#if defined __x86_64

#define BSL_BTREE_WORKS
//只有在64位下面才能够运行...

#include <functional>
#include "bsl/utils/bsl_construct.h"
//为bsl::string定制了特定的archive特化版本.
#include "bsl/string.h"
#include "df_xz_btree.h"
#include <string>

namespace bsl {
//BTREE错误返回值
enum {
    BTREE_UNKNOWN_ERROR=-1,
    BTREE_OK=0,
    //这个地方设置成为这样的原因
    //是为了和bsl::hashmap的值相同.
    BTREE_EXIST=0xffff,
    BTREE_NOEXIST,
    BTREE_OVERWRITE,
    BTREE_INSERT_SUC,
};

//forward declaration.
template<typename DU,
         unsigned int ROW_SIZE,
         typename BTreeAdapter>
class _bsl_kv_btree_build;

template<typename DU,
         unsigned int ROW_SIZE,
         typename BTreeAdapter>
class _bsl_kv_btree_search;

//archive traits for bsl kv btree.
//序列化的接口..如何进行类型的序列化和反序列化..
#include "bsl_kv_btree_archive_traits.h"

//kv btree data unit.
//数据单元
#include "bsl_kv_btree_data_unit.h"

//kv btree adapter
//现在仅仅提供了xz adapter
#include "bsl_kv_btree_adapter.h"

//kv btree iterator
#include "bsl_kv_btree_iterator.h"

template<typename DU,
         unsigned int ROW_SIZE,
         typename BTreeAdapter>
class _bsl_kv_btree_build:public _bsl_kv_btree_build_iterator_base<DU> {
public:
    typedef typename DU::key_type key_type;
    typedef typename DU::value_type value_type;
    typedef _bsl_kv_btree_build_iterator<DU,
            key_type &,key_type *,
            value_type &,value_type *,
            ROW_SIZE,BTreeAdapter> iterator;
    typedef _bsl_kv_btree_build_iterator<DU,
            const key_type &,const key_type*,
            const value_type &,const value_type *,
            ROW_SIZE,BTreeAdapter> const_iterator;
    typedef _bsl_kv_btree_build_reverse_iterator<DU,
            key_type &,key_type *,
            value_type &,value_type *,
            ROW_SIZE,BTreeAdapter> reverse_iterator;
    typedef _bsl_kv_btree_build_reverse_iterator<DU,
            const key_type &,const key_type*,
            const value_type &,const value_type *,
            ROW_SIZE,BTreeAdapter> const_reverse_iterator;
    typedef BTreeAdapter btree_type;
    typedef _bsl_kv_btree_search<DU,ROW_SIZE,BTreeAdapter> kv_btree_search_type;

private:
    //持有的数据对象
    btree_type _btree;
    //end of iterator...
    iterator _end_iter;
    const_iterator _cst_end_iter;
    reverse_iterator _rend_iter;
    const_reverse_iterator _cst_rend_iter;

public:
    _bsl_kv_btree_build() {
        _end_iter._end=true;
        _cst_end_iter._end=true;
        _rend_iter._end=true;
        _cst_rend_iter._end=true;
    }
    bool get_smallest(DU &du)const {
        return _btree.unify_get_smallest(du);
    }
    bool get_largest(DU &du)const {
        return _btree.unify_get_largest(du);
    }
    bool get_smaller(const DU &pdu,DU &du)const {
        return _btree.unify_get_smaller(pdu,du);
    }
    bool get_larger(const DU &pdu,DU &du)const {
        return _btree.unify_get_larger(pdu,du);
    }
    //insert (key,value) into btree
    //flag==0 如果值存在的话直接返回,否则就直接插入
    //flag!=0 如果值存在的话那么就会进行替换,否则就直接插入
    //返回值
    //-1表示内部错误....
    //BTREE_OVERWRITE 表示覆盖旧节点成功[flag!=0]
    //BTREE_INSERT_SUC 表示插入新节点成功
    //BTREE_EXIST 表示节点存在[flag=0]
    int set(const key_type &k,const value_type &v,int flag=0) {
        DU du(k,v);
        if(flag==0) {
            int ret=_btree.unify_insert(k,du);
            if(ret==0) {
                return BTREE_EXIST;
            } else if(ret==1) {
                return BTREE_INSERT_SUC;
            }
            return BTREE_UNKNOWN_ERROR;
        }
        int ret=_btree.unify_insert_update(k,du);
        if(ret==0) {
            return BTREE_OVERWRITE;
        } else if(ret==1) {
            return BTREE_INSERT_SUC;
        }
        return BTREE_UNKNOWN_ERROR;
    }
    //BTREE_EXIST 项存在
    //BTREE_NOEXIST 项不存在..
    int get(const key_type &k,value_type *val=0) {
        if(_btree.unify_get_batch_mutate_mode()) {
            return BTREE_UNKNOWN_ERROR;
        }
        DU du;
        bool ok=_btree.unify_search(k,&du);
        if(ok) {
            if(val) {
                //做一个copy..
                bsl_construct(val,du._value);
            }
            return BTREE_EXIST;
        }
        return BTREE_NOEXIST;
    }
    int get(const key_type &k,value_type *val=0)const {
        if(_btree.unify_get_batch_mutate_mode()) {
            return BTREE_UNKNOWN_ERROR;
        }
        DU du;
        bool ok=_btree.unify_search(k,&du);
        if(ok) {
            if(val) {
                //做一个copy...
                bsl_construct(val,du._value);
            }
            return BTREE_EXIST;
        }
        return BTREE_NOEXIST;
    }
    //支持从某个KV开始构建iterator.
    //带有参数的begin就是从某个点开始进行构建iterator的方法.
    iterator begin() {
        DU du;
        bool ok=get_smallest(du);
        return iterator(this,du,!ok);
    }
    iterator begin(const key_type &k,const value_type &v) {
        DU du(k,v);
        return iterator(this,du,false);
    }
    const_iterator begin()const {
        DU du;
        bool ok=get_smallest(du);
        return const_iterator(this,du,!ok);
    }
    const_iterator begin(const key_type &k,const value_type &v) const {
        DU du(k,v);
        return const_iterator(this,du,false);
    }
    reverse_iterator rbegin() {
        DU du;
        bool ok=get_largest(du);
        return reverse_iterator(this,du,!ok);
    }
    reverse_iterator rbegin(const key_type &k,const value_type &v) {
        DU du(k,v);
        return reverse_iterator(this,du,false);
    }
    const_reverse_iterator rbegin()const {
        DU du;
        bool ok=get_largest(du);
        return const_reverse_iterator(this,du,!ok);
    }
    const_reverse_iterator rbegin(const key_type &k,const value_type &v)const {
        DU du(k,v);
        return const_reverse_iterator(this,du,false);
    }
    const iterator &end() {
        return _end_iter;
    }
    const const_iterator &end()const {
        return _cst_end_iter;
    }
    const reverse_iterator &rend() {
        return _rend_iter;
    }
    const const_reverse_iterator &rend()const {
        return _cst_rend_iter;
    }
    //返回有多少个key.
    size_t size() const {
        uint64_t key=0,leaf=0,mid=0;
        _btree.unify_get_total_num(key,leaf,mid);
        return key;
    }
    //删除某个元素...
    //BTREE_EXIST 如果key存在
    //BTREE_NOEXIST 如果key不存在.
    int erase(const key_type &k) {
        bool ok=_btree.unify_del(k);
        if(ok) {
            return BTREE_EXIST;
        }
        return BTREE_NOEXIST;
    }
    //清除,始终返回0.
    int clear() {
        int ret=_btree.unify_clear();
        if(ret==0) {
            return BTREE_OK;
        }
        return BTREE_UNKNOWN_ERROR;
    }
    //创建checkpoint
    //其实没有太多用途,因为你已经有了snapshot.
    //0成功,-1失败...
    int make_checkpoint() {
        int ret=_btree.unify_set_checkpoint_state(false);
        if(ret==0) {
            return BTREE_OK;
        }
        return BTREE_UNKNOWN_ERROR;
    }
    int end_checkpoint() {
        int ret=_btree.unify_set_checkpoint_state(true);
        if(ret==0) {
            return BTREE_OK;
        }
        return BTREE_UNKNOWN_ERROR;
    }
    int snapshot(kv_btree_search_type & search) {
        int ret=_btree.unify_snapshot(search._rc_indexer);
        search._btree=&_btree;
        if(ret==0) {
            return BTREE_OK;
        }
        return BTREE_UNKNOWN_ERROR;
    }
    int store(int fd,long long offset) {
        dfs_file_rw_info_t info;
        info.fd=fd;
        info.orig_offset=offset;
        info.reserved=0;//nothing.
        int ret=_btree.unify_store_checkpoint(dfs_write_proc,&info);
        if(ret==0) {
            return BTREE_OK;
        }
        return BTREE_UNKNOWN_ERROR;
    }
    int load(int fd,long long offset) {
        dfs_file_rw_info_t info;
        info.fd=fd;
        info.orig_offset=offset;
        int ret=_btree.unify_load_checkpoint(dfs_read_proc,&info);
        if(ret==0) {
            return BTREE_OK;
        }
        return BTREE_UNKNOWN_ERROR;
    }
    int make_batch_mutate() {
        int ret=_btree.unify_set_batch_mutate_mode(true);
        if(ret==0) {
            return BTREE_OK;
        }
        return BTREE_UNKNOWN_ERROR;
    }
    int end_batch_mutate() {
        int ret=_btree.unify_set_batch_mutate_mode(false);
        if(ret==0) {
            return BTREE_OK;
        }
        return BTREE_UNKNOWN_ERROR;
    }
    //添加下面这两个接口仅仅是为了bsl使用习惯配合.
    //创建肯定成功.
    int create() {
        return 0;
    }
    //销毁的话只是清空.不做任何处理.
    int destroy() {
        int ret=_btree.unify_clear();
        if(ret==0) {
            return BTREE_OK;
        }
        return BTREE_UNKNOWN_ERROR;
    }
};

template<typename DU,
         unsigned int ROW_SIZE,
         typename BTreeAdapter>
class _bsl_kv_btree_search:public _bsl_kv_btree_search_iterator_base<DU> {
public:
    typedef typename DU::key_type key_type;
    typedef typename DU::value_type value_type;
    typedef _bsl_kv_btree_search_iterator<DU,
            key_type &,key_type *,
            value_type &,value_type *,
            ROW_SIZE,BTreeAdapter> iterator;
    typedef _bsl_kv_btree_search_iterator<DU,
            const key_type &,const key_type *,
            const value_type &,const value_type *,
            ROW_SIZE,BTreeAdapter> const_iterator;
    typedef _bsl_kv_btree_search_reverse_iterator<DU,
            key_type &,key_type *,
            value_type &,value_type *,
            ROW_SIZE,BTreeAdapter> reverse_iterator;
    typedef _bsl_kv_btree_search_reverse_iterator<DU,
            const key_type &,const key_type *,
            const value_type &,const value_type *,
            ROW_SIZE,BTreeAdapter> const_reverse_iterator;
    typedef _bsl_kv_btree_build<DU,ROW_SIZE,BTreeAdapter> kv_btree_build_type;
    typedef BTreeAdapter btree_type;

private:
    friend class _bsl_kv_btree_build<DU,ROW_SIZE,BTreeAdapter>;
    //持有的数据对象...
    btree_type *_btree;
    //rc indexer.
    dfs_rc_indexer_t _rc_indexer;
    //end iterator..
    iterator _end_iter;
    const_iterator _cst_end_iter;
    reverse_iterator _rend_iter;
    const_reverse_iterator _cst_rend_iter;

public:
    _bsl_kv_btree_search() {
        _end_iter._end=true;
        _cst_end_iter._end=true;
        _rend_iter._end=true;
        _cst_rend_iter._end=true;
    }
    bool get_smallest(DU **du)const {
        return _btree->unify_get_smallest(du,&_rc_indexer);
    }
    bool get_largest(DU **du)const {
        return _btree->unify_get_largest(du,&_rc_indexer);
    }
    bool get_smaller(const DU &pdu,DU **du,dfs_btree_drill_t *pdrill_info)const {
        return _btree->unify_get_smaller(pdu,du,pdrill_info,&_rc_indexer);
    }
    bool get_larger(const DU &pdu,DU **du,dfs_btree_drill_t *pdrill_info)const {
        return _btree->unify_get_larger(pdu,du,pdrill_info,&_rc_indexer);
    }
    void let_it_go() {
        //释放回收节点..
        _rc_indexer.init();
    }
    ~_bsl_kv_btree_search() {
        let_it_go();
    }
    //BTREE_EXIST 项存在
    //BTREE_NOEXIST 项不存在..
    int get(const key_type &k,value_type *val=0) {
        DU du;
        bool ok=_btree->unify_search(k,&du,&_rc_indexer);
        if(ok) {
            if(val) {
                bsl_construct(val,du._value);
            }
            return BTREE_EXIST;
        }
        return BTREE_NOEXIST;
    }
    int get(const key_type &k,value_type *val=0)const {
        DU du;
        bool ok=_btree->unify_search(k,&du,&_rc_indexer);
        if(ok) {
            if(val) {
                bsl_construct(val,du._value);
            }
            return BTREE_EXIST;
        }
        return BTREE_NOEXIST;
    }
    iterator begin() {
        DU *du;
        bool ok=get_smallest(&du);
        return iterator(this,du,!ok);
    }
    const_iterator begin()const {
        DU *du;
        bool ok=get_smallest(&du);
        return const_iterator(this,du,!ok);
    }
    reverse_iterator rbegin() {
        DU *du;
        bool ok=get_largest(&du);
        return reverse_iterator(this,du,!ok);
    }
    const_reverse_iterator rbegin()const {
        DU *du;
        bool ok=get_largest(&du);
        return const_reverse_iterator(this,du,!ok);
    }
    const iterator &end() {
        return _end_iter;
    }
    const const_iterator &end()const {
        return _cst_end_iter;
    }
    const reverse_iterator &rend() {
        return _rend_iter;
    }
    const const_reverse_iterator &rend()const {
        return _cst_rend_iter;
    }
    //返回有多少个key.
    size_t size() const {
        uint64_t key=0,leaf=0,mid=0;
        _btree->unify_get_total_num(key,leaf,mid,&_rc_indexer);
        return key;
    }
    int store(int fd,unsigned long long offset) {
        dfs_file_rw_info_t info;
        info.fd=fd;
        info.orig_offset=offset;
        int ret=_btree->unify_store_checkpoint(dfs_write_proc,&info,&_rc_indexer);
        if(ret==0) {
            return BTREE_OK;
        }
        return BTREE_UNKNOWN_ERROR;
    }
};

template<typename K,typename V,
         unsigned int ROW_SIZE=dfs_bt_const_t::DEF_ROW_SIZE,
         typename KARCHIVE=_btree_archive_traits<K>,
         typename VARCHIVE=_btree_archive_traits<V>,
         typename LT=std::less<K>,
         typename EQ=std::equal_to<K> >
class bsl_kv_btree_search:
    public _bsl_kv_btree_search< //data unit..
    _bsl_kv_btree_du<K,V,KARCHIVE,VARCHIVE,LT,EQ>,
    ROW_SIZE,
    //adapter...
        _bsl_kv_btree_xz_adapter <_bsl_kv_btree_du<K,V,KARCHIVE,VARCHIVE,LT,EQ>,ROW_SIZE > > {
};
template<typename K,typename V,
         unsigned int ROW_SIZE=dfs_bt_const_t::DEF_ROW_SIZE,
         typename KARCHIVE=_btree_archive_traits<K>,
         typename VARCHIVE=_btree_archive_traits<V>,
         typename LT=std::less<K>,
         typename EQ=std::equal_to<K> >
class bsl_kv_btree_build:
    public _bsl_kv_btree_build<//data unit..
    _bsl_kv_btree_du<K,V,KARCHIVE,VARCHIVE,LT,EQ>,
    ROW_SIZE ,
    //adapter..
        _bsl_kv_btree_xz_adapter <_bsl_kv_btree_du<K,V,KARCHIVE,VARCHIVE,LT,EQ>,ROW_SIZE > > {
};
}
#else
#undef BSL_BTREE_WORKS
#endif

#endif