sdskv-server.hpp 5.46 KB
Newer Older
1 2 3 4 5 6 7
#ifndef __SDSKV_SERVER_HPP
#define __SDSKV_SERVER_HPP

#include <sdskv-server.h>
#include <sdskv-common.hpp>

#define _CHECK_RET(__ret) \
8
            if(__ret != SDSKV_SUCCESS) throw exception(__ret)
9 10 11

namespace sdskv {

Matthieu Dorier's avatar
Matthieu Dorier committed
12 13 14
/**
 * @brief The provider class wraps an sdskv_provider_t C handle.
 */
15 16 17
class provider {

    margo_instance_id m_mid     = MARGO_INSTANCE_NULL;
18
    sdskv_provider_t m_provider = NULL;
19

Matthieu Dorier's avatar
Matthieu Dorier committed
20 21 22 23 24 25 26
    /**
     * @brief Constructor is private. Use the create() factory method.
     *
     * @param mid Margo instance.
     * @param provider_id Provider id.
     * @param pool Argobots pool.
     */
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    provider(margo_instance_id mid,
             uint16_t provider_id = 0,
             ABT_pool pool = SDSKV_ABT_POOL_DEFAULT)
    : m_mid(mid)
    {
        int ret = sdskv_provider_register(mid, provider_id, pool, &m_provider);
        _CHECK_RET(ret);
    }

    static void finalize_callback(void* args) {
        auto* p = static_cast<provider*>(args);
        delete p;
    }

    public:

Matthieu Dorier's avatar
Matthieu Dorier committed
43 44 45 46 47 48 49 50 51 52
    /**
     * @brief Create a pointer to a provider. The provider will be automatically
     * deleted when Margo finalizes, unless the user calls delete before.
     *
     * @param mid Margo instance.
     * @param provider_id Provider id.
     * @param pool Argobots pool.
     *
     * @return Pointer to a newly allocated provider.
     */
53 54 55 56 57
    static provider* create(margo_instance_id mid,
                            uint16_t provider_id = 0,
                            ABT_pool pool = SDSKV_ABT_POOL_DEFAULT)
    {
        auto p = new provider(mid, provider_id, pool);
58 59
        margo_provider_push_finalize_callback(mid, p, &finalize_callback, p);
        return p;
60 61
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
62 63 64
    /**
     * @brief Copy constructor is deleted.
     */
65 66
    provider(const provider&) = delete;

Matthieu Dorier's avatar
Matthieu Dorier committed
67 68 69
    /**
     * @brief Move constructor is deleted.
     */
70 71
    provider(provider&& other) = delete;

Matthieu Dorier's avatar
Matthieu Dorier committed
72 73 74
    /**
     * @brief Copy-assignment operator is deleted.
     */
75 76
    provider& operator=(const provider&) = delete;

Matthieu Dorier's avatar
Matthieu Dorier committed
77 78 79
    /**
     * @brief Move-assignment operator is deleted.
     */
80 81
    provider& operator=(provider&&) = delete;

Matthieu Dorier's avatar
Matthieu Dorier committed
82 83 84
    /**
     * @brief Destructor. Deregisters the provider.
     */
85
    ~provider() {
86
        margo_provider_pop_finalize_callback(m_mid, this);
87 88 89
        sdskv_provider_destroy(m_provider);
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
90 91 92 93 94 95
    /**
     * @brief Add a comparison function.
     *
     * @param name Name for the comparison function.
     * @param comp_fn Comparison function pointer.
     */
96 97 98 99 100 101
    void add_comparison_function(const std::string& name,
                                 sdskv_compare_fn comp_fn) {
        int ret = sdskv_provider_add_comparison_function(m_provider, name.c_str(), comp_fn);
        _CHECK_RET(ret);
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
102 103 104 105 106 107 108
    /**
     * @brief Attach a database and returns the database id.
     *
     * @param config Database configuration.
     *
     * @return Database id.
     */
109 110 111 112 113 114 115
    sdskv_database_id_t attach_database(const sdskv_config_t& config) {
        sdskv_database_id_t db_id;
        int ret = sdskv_provider_attach_database(m_provider, &config, &db_id);
        _CHECK_RET(ret);
        return db_id;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
116 117 118 119 120
    /**
     * @brief Remove a database (this will not remove the underlying files).
     *
     * @param db_id Database id of the database to remove.
     */
121 122 123 124 125
    void remove_database(sdskv_database_id_t db_id) {
        int ret = sdskv_provider_remove_database(m_provider, db_id);
        _CHECK_RET(ret);
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
126 127 128 129
    /**
     * @brief Remove all the databases from this provider (this will not
     * remove the underlying files).
     */
130 131 132 133 134
    void remove_all_databases() {
        int ret = sdskv_provider_remove_all_databases(m_provider);
        _CHECK_RET(ret);
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
135 136 137 138 139
    /**
     * @brief Returns the list of databases handled by this provider.
     *
     * @return Vector of database ids.
     */
140 141 142 143 144 145 146 147 148 149
    std::vector<sdskv_database_id_t> databases() const {
        std::vector<sdskv_database_id_t> dbs;
        uint64_t num_dbs;
        int ret = sdskv_provider_count_databases(m_provider, &num_dbs);
        _CHECK_RET(ret);
        dbs.resize(num_dbs);
        ret = sdskv_provider_list_databases(m_provider, dbs.data());
        _CHECK_RET(ret);
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
150 151 152 153 154 155 156 157
    /**
     * @brief Compute the size of a database (combined sizes of all files
     * that make up the database). Requires REMI support.
     *
     * @param db_id Database id.
     *
     * @return Size of the specified database.
     */
158 159 160 161 162 163 164 165 166 167
    size_t compute_database_size(sdskv_database_id_t db_id) const {
        size_t size;
        int ret = sdskv_provider_compute_database_size(
                    m_provider,
                    db_id,
                    &size);
        _CHECK_RET(ret);
        return size;
    }
    
Matthieu Dorier's avatar
Matthieu Dorier committed
168 169 170 171 172 173 174
    /**
     * @brief Registers migration callbacks for REMI to use.
     *
     * @param pre_cb Pre-migration callback.
     * @param post_cb Post-migration callback.
     * @param uargs User arguments.
     */
175 176 177 178 179 180 181 182
    void set_migration_callbacks(sdskv_pre_migration_callback_fn pre_cb,
                                 sdskv_post_migration_callback_fn post_cb,
                                 void* uargs) {
        int ret = sdskv_provider_set_migration_callbacks(m_provider,
                        pre_cb, post_cb, uargs);
        _CHECK_RET(ret);
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
183 184 185 186 187
    /**
     * @brief Set the ABT-IO instance to be used by REMI.
     *
     * @param abtio ABT-IO instance.
     */
188 189 190 191 192 193 194
    void set_abtio_instance(abt_io_instance_id abtio) {
        int ret = sdskv_provider_set_abtio_instance(m_provider, abtio);
        _CHECK_RET(ret);
    }
};

}
195
#undef _CHECK_RET
196
#endif