berkeleydb_datastore.h 2.85 KB
Newer Older
1 2 3 4 5 6
// Copyright (c) 2017, Los Alamos National Security, LLC.
// All rights reserved.
#ifndef bdb_datastore_h
#define bdb_datastore_h

#include "kv-config.h"
7
#include "datastore/datastore.h"
8 9
#include <db_cxx.h>
#include <dbstl_map.h>
10
#include "sdskv-common.h"
11 12 13

// may want to implement some caching for persistent stores like BerkeleyDB
class BerkeleyDBDataStore : public AbstractDataStore {
14 15 16 17 18 19 20 21 22 23 24

    private:
        struct DbWrapper {
            Db _db;
            AbstractDataStore::comparator_fn _less;

            template<typename ... T>
            DbWrapper(T&&... args) :
            _db(std::forward<T>(args)...), _less(nullptr) {}
        };

25
        static int compkeys(Db *db, const Dbt *dbt1, const Dbt *dbt2, hg_size_t *locp);
26 27 28

    public:
        BerkeleyDBDataStore();
29
        BerkeleyDBDataStore(bool eraseOnGet, bool debug);
30
        virtual ~BerkeleyDBDataStore();
31
        virtual bool openDatabase(const std::string& db_name, const std::string& path) override;
32 33
        virtual int put(const void* key, hg_size_t ksize, const void* value, hg_size_t vsize) override;
        virtual int put_multi(hg_size_t num_items,
34
                               const void* const* keys,
35
                               const hg_size_t* ksizes,
36
                               const void* const* values,
37
                               const hg_size_t* vsizes) override;
38 39
        virtual bool get(const ds_bulk_t &key, ds_bulk_t &data) override;
        virtual bool get(const ds_bulk_t &key, std::vector<ds_bulk_t> &data) override;
40
        virtual bool exists(const void* key, hg_size_t ksize) const override;
41 42 43 44
        virtual bool erase(const ds_bulk_t &key) override;
        virtual void set_in_memory(bool enable) override; // enable/disable in-memory mode
        virtual void set_comparison_function(const std::string& name, comparator_fn less) override;
        virtual void set_no_overwrite() override {
45 46
            _no_overwrite = true;
        }
47
        virtual void sync() override;
48
#ifdef USE_REMI
49
        virtual remi_fileset_t create_and_populate_fileset() const override;
50
#endif
51
    protected:
52
        virtual std::vector<ds_bulk_t> vlist_keys(
53
                const ds_bulk_t &start, hg_size_t count, const ds_bulk_t &prefix) const override;
54
        virtual std::vector<std::pair<ds_bulk_t,ds_bulk_t>> vlist_keyvals(
55
                const ds_bulk_t &start_key, hg_size_t count, const ds_bulk_t &) const override;
56
        virtual std::vector<ds_bulk_t> vlist_key_range(
57
                const ds_bulk_t &lower_bound, const ds_bulk_t &upper_bound, hg_size_t max_keys) const override;
58
        virtual std::vector<std::pair<ds_bulk_t,ds_bulk_t>> vlist_keyval_range(
59
                const ds_bulk_t &lower_bound, const ds_bulk_t& upper_bound, hg_size_t max_keys) const override;
60 61 62
        DbEnv *_dbenv = nullptr;
        Db *_dbm = nullptr;
        DbWrapper* _wrapper = nullptr;
63 64 65
};

#endif // bdb_datastore_h