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

Rob Latham's avatar
Rob Latham committed
6
#include "kv-config.h"
7
#include "bulk.h"
8 9
#include <margo.h>
#ifdef USE_REMI
10
#include "remi/remi-common.h"
11
#endif
12 13

#include <vector>
14

15
class AbstractDataStore {
16 17
    public:

18
        typedef int (*comparator_fn)(const void*, hg_size_t, const void*, hg_size_t);
19 20

        AbstractDataStore();
21
        AbstractDataStore(bool eraseOnGet, bool debug);
22 23
        virtual ~AbstractDataStore();
        virtual bool openDatabase(const std::string& db_name, const std::string& path)=0;
24
        virtual int put(const void* kdata, hg_size_t ksize, const void* vdata, hg_size_t vsize)=0;
25
        virtual int put(const ds_bulk_t &key, const ds_bulk_t &data) {
26 27
            return put(key.data(), key.size(), data.data(), data.size());
        }
28
        virtual int put(ds_bulk_t&& key, ds_bulk_t&& data) {
29 30
            return put(key.data(), key.size(), data.data(), data.size());
        }
31
        virtual int put_multi(hg_size_t num_items,
32
                              const void* const* keys,
33
                              const hg_size_t* ksizes,
34
                              const void* const* values,
35
                              const hg_size_t* vsizes)
36
        {
37
            int ret = 0;
38
            for(hg_size_t i=0; i < num_items; i++) {
39 40
                int r = put(keys[i], ksizes[i], values[i], vsizes[i]);
                ret = ret == 0 ? r : 0;
41
            }
42
            return ret;
43
        }
44 45
        virtual bool get(const ds_bulk_t &key, ds_bulk_t &data)=0;
        virtual bool get(const ds_bulk_t &key, std::vector<ds_bulk_t> &data)=0;
46
        virtual bool exists(const void* key, hg_size_t ksize) const = 0;
47 48 49
        virtual bool exists(const ds_bulk_t &key) const {
            return exists(key.data(), key.size());
        }
50 51
        virtual bool erase(const ds_bulk_t &key) = 0;
        virtual void set_in_memory(bool enable)=0; // enable/disable in-memory mode (where supported)
52
        virtual void set_comparison_function(const std::string& name, comparator_fn less)=0;
53
        virtual void set_no_overwrite()=0;
Matthieu Dorier's avatar
Matthieu Dorier committed
54
        virtual void sync() = 0;
55 56

#ifdef USE_REMI
57
        virtual remi_fileset_t create_and_populate_fileset() const = 0;
58
#endif
59 60 61 62 63 64 65 66 67 68 69 70

        const std::string& get_path() const {
            return _path;
        }

        const std::string& get_name() const {
            return _name;
        }

        const std::string& get_comparison_function_name() const {
            return _comp_fun_name;
        }
71 72

        std::vector<ds_bulk_t> list_keys(
73
                const ds_bulk_t &start_key, hg_size_t count, const ds_bulk_t& prefix=ds_bulk_t()) const {
74 75 76 77
            return vlist_keys(start_key, count, prefix);
        }

        std::vector<std::pair<ds_bulk_t,ds_bulk_t>> list_keyvals(
78
                const ds_bulk_t &start_key, hg_size_t count, const ds_bulk_t& prefix=ds_bulk_t()) const {
79 80 81 82
            return vlist_keyvals(start_key, count, prefix);
        }

        std::vector<ds_bulk_t> list_key_range(
83
                const ds_bulk_t &lower_bound, const ds_bulk_t &upper_bound, hg_size_t max_keys=0) const {
84 85 86 87
            return vlist_key_range(lower_bound, upper_bound, max_keys);
        }

        std::vector<std::pair<ds_bulk_t,ds_bulk_t>> list_keyval_range(
88
                const ds_bulk_t &lower_bound, const ds_bulk_t& upper_bound, hg_size_t max_keys=0) const {
89 90 91 92
            return vlist_keyval_range(lower_bound, upper_bound, max_keys);
        }

    protected:
93 94 95 96
        std::string _path;
        std::string _name;
        std::string _comp_fun_name;
        bool _no_overwrite = false;
97 98 99 100 101
        bool _eraseOnGet;
        bool _debug;
        bool _in_memory;

        virtual std::vector<ds_bulk_t> vlist_keys(
102
                const ds_bulk_t &start_key, hg_size_t count, const ds_bulk_t& prefix) const = 0;
103
        virtual std::vector<std::pair<ds_bulk_t,ds_bulk_t>> vlist_keyvals(
104
                const ds_bulk_t &start_key, hg_size_t count, const ds_bulk_t& prefix) const = 0;
105
        virtual std::vector<ds_bulk_t> vlist_key_range(
106
                const ds_bulk_t &lower_bound, const ds_bulk_t &upper_bound, hg_size_t max_keys) const = 0;
107
        virtual std::vector<std::pair<ds_bulk_t,ds_bulk_t>> vlist_keyval_range(
108
                const ds_bulk_t &lower_bound, const ds_bulk_t& upper_bound, hg_size_t max_keys) const = 0;
109 110 111
};

#endif // datastore_h