datastore.h 4.99 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
        virtual int put_packed(hg_size_t num_items,
                               const char* keys,
                               const hg_size_t* ksizes,
                               const char* values,
                               const hg_size_t* vsizes)
        {
            int ret = 0;
            size_t keys_offset = 0;
            size_t vals_offset = 0;
            for(hg_size_t i=0; i < num_items; i++) {
                int r = put(keys+keys_offset, ksizes[i], values+vals_offset, vsizes[i]);
                ret = ret == 0 ? r : 0;
                keys_offset += ksizes[i];
                vals_offset += vsizes[i];
            }
            return ret;
        }
61 62
        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;
63
        virtual bool exists(const void* key, hg_size_t ksize) const = 0;
64 65 66
        virtual bool exists(const ds_bulk_t &key) const {
            return exists(key.data(), key.size());
        }
67 68
        virtual bool erase(const ds_bulk_t &key) = 0;
        virtual void set_in_memory(bool enable)=0; // enable/disable in-memory mode (where supported)
69
        virtual void set_comparison_function(const std::string& name, comparator_fn less)=0;
70
        virtual void set_no_overwrite()=0;
Matthieu Dorier's avatar
Matthieu Dorier committed
71
        virtual void sync() = 0;
72 73

#ifdef USE_REMI
74
        virtual remi_fileset_t create_and_populate_fileset() const = 0;
75
#endif
76 77 78 79 80 81 82 83 84 85 86 87

        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;
        }
88 89

        std::vector<ds_bulk_t> list_keys(
90
                const ds_bulk_t &start_key, hg_size_t count, const ds_bulk_t& prefix=ds_bulk_t()) const {
91 92 93 94
            return vlist_keys(start_key, count, prefix);
        }

        std::vector<std::pair<ds_bulk_t,ds_bulk_t>> list_keyvals(
95
                const ds_bulk_t &start_key, hg_size_t count, const ds_bulk_t& prefix=ds_bulk_t()) const {
96 97 98 99
            return vlist_keyvals(start_key, count, prefix);
        }

        std::vector<ds_bulk_t> list_key_range(
100
                const ds_bulk_t &lower_bound, const ds_bulk_t &upper_bound, hg_size_t max_keys=0) const {
101 102 103 104
            return vlist_key_range(lower_bound, upper_bound, max_keys);
        }

        std::vector<std::pair<ds_bulk_t,ds_bulk_t>> list_keyval_range(
105
                const ds_bulk_t &lower_bound, const ds_bulk_t& upper_bound, hg_size_t max_keys=0) const {
106 107 108 109
            return vlist_keyval_range(lower_bound, upper_bound, max_keys);
        }

    protected:
110 111 112 113
        std::string _path;
        std::string _name;
        std::string _comp_fun_name;
        bool _no_overwrite = false;
114 115 116 117 118
        bool _eraseOnGet;
        bool _debug;
        bool _in_memory;

        virtual std::vector<ds_bulk_t> vlist_keys(
119
                const ds_bulk_t &start_key, hg_size_t count, const ds_bulk_t& prefix) const = 0;
120
        virtual std::vector<std::pair<ds_bulk_t,ds_bulk_t>> vlist_keyvals(
121
                const ds_bulk_t &start_key, hg_size_t count, const ds_bulk_t& prefix) const = 0;
122
        virtual std::vector<ds_bulk_t> vlist_key_range(
123
                const ds_bulk_t &lower_bound, const ds_bulk_t &upper_bound, hg_size_t max_keys) const = 0;
124
        virtual std::vector<std::pair<ds_bulk_t,ds_bulk_t>> vlist_keyval_range(
125
                const ds_bulk_t &lower_bound, const ds_bulk_t& upper_bound, hg_size_t max_keys) const = 0;
126 127 128
};

#endif // datastore_h