datastore.h 4.36 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 16

enum class Duplicates : int {ALLOW, IGNORE};

17
class AbstractDataStore {
18 19 20 21 22 23 24 25
    public:

        typedef int (*comparator_fn)(const void*, size_t, const void*, size_t);

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

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

        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;
        }
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

        std::vector<ds_bulk_t> list_keys(
                const ds_bulk_t &start_key, size_t count, const ds_bulk_t& prefix=ds_bulk_t()) const {
            return vlist_keys(start_key, count, prefix);
        }

        std::vector<std::pair<ds_bulk_t,ds_bulk_t>> list_keyvals(
                const ds_bulk_t &start_key, size_t count, const ds_bulk_t& prefix=ds_bulk_t()) const {
            return vlist_keyvals(start_key, count, prefix);
        }

        std::vector<ds_bulk_t> list_key_range(
                const ds_bulk_t &lower_bound, const ds_bulk_t &upper_bound, size_t max_keys=0) const {
            return vlist_key_range(lower_bound, upper_bound, max_keys);
        }

        std::vector<std::pair<ds_bulk_t,ds_bulk_t>> list_keyval_range(
                const ds_bulk_t &lower_bound, const ds_bulk_t& upper_bound, size_t max_keys=0) const {
            return vlist_keyval_range(lower_bound, upper_bound, max_keys);
        }

    protected:
95 96 97
        std::string _path;
        std::string _name;
        std::string _comp_fun_name;
98
        Duplicates _duplicates;
99
        bool _no_overwrite = false;
100 101 102 103 104 105 106 107 108 109 110 111
        bool _eraseOnGet;
        bool _debug;
        bool _in_memory;

        virtual std::vector<ds_bulk_t> vlist_keys(
                const ds_bulk_t &start_key, size_t count, const ds_bulk_t& prefix) const = 0;
        virtual std::vector<std::pair<ds_bulk_t,ds_bulk_t>> vlist_keyvals(
                const ds_bulk_t &start_key, size_t count, const ds_bulk_t& prefix) const = 0;
        virtual std::vector<ds_bulk_t> vlist_key_range(
                const ds_bulk_t &lower_bound, const ds_bulk_t &upper_bound, size_t max_keys) const = 0;
        virtual std::vector<std::pair<ds_bulk_t,ds_bulk_t>> vlist_keyval_range(
                const ds_bulk_t &lower_bound, const ds_bulk_t& upper_bound, size_t max_keys) const = 0;
112 113 114
};

#endif // datastore_h