DataStoreImpl.hpp 23.3 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5
/*
 * (C) 2018 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
6 7 8
#ifndef __HEPNOS_PRIVATE_DATASTORE_IMPL
#define __HEPNOS_PRIVATE_DATASTORE_IMPL

9
#include <vector>
10
#include <unordered_set>
Matthieu Dorier's avatar
Matthieu Dorier committed
11
#include <unordered_map>
12 13 14
#include <functional>
#include <iostream>
#include <yaml-cpp/yaml.h>
15
#include <sdskv-client.hpp>
16 17 18 19
#include <ch-placement.h>
#include "hepnos/Exception.hpp"
#include "hepnos/DataStore.hpp"
#include "hepnos/DataSet.hpp"
Matthieu Dorier's avatar
Matthieu Dorier committed
20
#include "StringHash.hpp"
21
#include "DataSetImpl.hpp"
Matthieu Dorier's avatar
Matthieu Dorier committed
22
#include "ItemImpl.hpp"
Matthieu Dorier's avatar
Matthieu Dorier committed
23
#include "ItemType.hpp"
24 25 26

namespace hepnos {

Matthieu Dorier's avatar
Matthieu Dorier committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
inline void object_resize(hepnos::UUID& uuid, size_t new_size) {
    (void)uuid;
    (void)new_size;
}

inline void* object_data(hepnos::UUID& uuid) {
    return uuid.data;
}

inline const void* object_data(const hepnos::UUID& uuid) {
    return uuid.data;
}

inline size_t object_size(const hepnos::UUID& uuid) {
    return sizeof(uuid);
}

44
////////////////////////////////////////////////////////////////////////////////////////////
45
// DataStoreImpl implementation
46 47
////////////////////////////////////////////////////////////////////////////////////////////

48 49 50 51 52
struct DistributedDBInfo {
    std::vector<sdskv::database>  dbs;
    struct ch_placement_instance* chi = nullptr;
};

53
class DataStoreImpl {
54 55
    public:

Matthieu Dorier's avatar
Matthieu Dorier committed
56 57
    margo_instance_id                         m_mid;          // Margo instance
    std::unordered_map<std::string,hg_addr_t> m_addrs;        // Addresses used by the service
58
    sdskv::client                             m_sdskv_client; // SDSKV client
59
    DistributedDBInfo                         m_dataset_dbs;  // list of SDSKV databases for DataSets
Matthieu Dorier's avatar
Matthieu Dorier committed
60 61 62 63
    DistributedDBInfo                         m_run_dbs;      // list of SDSKV databases for Runs
    DistributedDBInfo                         m_subrun_dbs;   // list of SDSKV databases for Runs
    DistributedDBInfo                         m_event_dbs;    // list of SDSKV databases for Runs
    DistributedDBInfo                         m_product_dbs;  // list of SDSKV databases for Products
64

65
    DataStoreImpl()
66
    : m_mid(MARGO_INSTANCE_NULL)
67
    {}
68

69
    void populateDatabases(DistributedDBInfo& db_info, const YAML::Node& db_config) {
70 71
        int ret;
        hg_return_t hret;
72
        for(YAML::const_iterator address_it = db_config.begin(); address_it != db_config.end(); address_it++) {
73 74 75
            std::string str_addr = address_it->first.as<std::string>();
            YAML::Node providers = address_it->second;
            // lookup the address
76
            hg_addr_t addr;
Matthieu Dorier's avatar
Matthieu Dorier committed
77 78 79 80 81 82 83
            if(m_addrs.count(str_addr) != 0) {
                addr = m_addrs[str_addr];
            } else {
                hret = margo_addr_lookup(m_mid, str_addr.c_str(), &addr);
                if(hret != HG_SUCCESS) {
                    margo_addr_free(m_mid,addr);
                    cleanup();
84
                    throw Exception("margo_addr_lookup failed (MARGO error="+std::to_string(hret)+")");
Matthieu Dorier's avatar
Matthieu Dorier committed
85 86
                }
                m_addrs[str_addr] = addr;
87
            }
88 89 90 91 92 93 94 95 96 97
            // iterate over providers for this address
            for(YAML::const_iterator provider_it = providers.begin(); provider_it != providers.end(); provider_it++) {
                // get the provider id
                uint16_t provider_id = provider_it->first.as<uint16_t>();
                // create provider handle
                sdskv::provider_handle ph(m_sdskv_client, addr, provider_id);
                // get the database ids
                YAML::Node databases = provider_it->second;
                // iterate over databases for this provider
                for(unsigned i=0; i < databases.size(); i++) {
98
                    db_info.dbs.push_back(sdskv::database(ph, databases[i].as<uint64_t>()));
99
                }
100 101
            } // for each provider
        } // for each address
102 103 104 105
        // initialize ch-placement
        db_info.chi = ch_placement_initialize("hash_lookup3", db_info.dbs.size(), 4, 0);
    }

106
    void init(const std::string& configFile, bool use_progress_thread) {
107 108 109 110 111 112 113
        int ret;
        hg_return_t hret;
        YAML::Node config = YAML::LoadFile(configFile);
        checkConfig(config);
        // get protocol
        std::string proto = config["hepnos"]["client"]["protocol"].as<std::string>();
        // initialize Margo
114
        m_mid = margo_init(proto.c_str(), MARGO_CLIENT_MODE, use_progress_thread, 0);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        if(!m_mid) {
            cleanup();
            throw Exception("Could not initialized Margo");
        }
        // initialize SDSKV client
        try {
            m_sdskv_client = sdskv::client(m_mid);
        } catch(sdskv::exception& ex) {
            cleanup();
            throw Exception("Could not create SDSKV client (SDSKV error="+std::to_string(ex.error())+")");
        }
        // populate database info structures for each type of database
        YAML::Node databases = config["hepnos"]["databases"];
        YAML::Node dataset_db = databases["datasets"];
        YAML::Node run_db     = databases["runs"];
        YAML::Node subrun_db  = databases["subruns"];
        YAML::Node event_db   = databases["events"];
        YAML::Node product_db = databases["products"];
        populateDatabases(m_dataset_dbs, dataset_db);
Matthieu Dorier's avatar
Matthieu Dorier committed
134 135 136 137
        populateDatabases(m_run_dbs, run_db);
        populateDatabases(m_subrun_dbs, subrun_db);
        populateDatabases(m_event_dbs, event_db);
        populateDatabases(m_product_dbs, product_db);
138 139 140
    }

    void cleanup() {
Matthieu Dorier's avatar
Matthieu Dorier committed
141 142 143 144 145
        m_dataset_dbs.dbs.clear();
        m_run_dbs.dbs.clear();
        m_subrun_dbs.dbs.clear();
        m_event_dbs.dbs.clear();
        m_product_dbs.dbs.clear();
146
        m_sdskv_client = sdskv::client();
Matthieu Dorier's avatar
Matthieu Dorier committed
147 148 149 150 151
        if(m_dataset_dbs.chi) ch_placement_finalize(m_dataset_dbs.chi);
        if(m_run_dbs.chi)     ch_placement_finalize(m_run_dbs.chi);
        if(m_subrun_dbs.chi)  ch_placement_finalize(m_subrun_dbs.chi);
        if(m_event_dbs.chi)   ch_placement_finalize(m_event_dbs.chi);
        if(m_product_dbs.chi)   ch_placement_finalize(m_product_dbs.chi);
Matthieu Dorier's avatar
Matthieu Dorier committed
152 153 154
        for(auto& addr : m_addrs) {
            margo_addr_free(m_mid, addr.second);
        }
155 156 157 158 159 160
        if(m_mid) margo_finalize(m_mid);
    }

    private:

    static void checkConfig(YAML::Node& config) {
Matthieu Dorier's avatar
Matthieu Dorier committed
161
        // config file starts with hepnos entry
162 163 164 165
        auto hepnosNode = config["hepnos"];
        if(!hepnosNode) {
            throw Exception("\"hepnos\" entry not found in YAML file");
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
166
        // hepnos entry has client entry
167 168 169 170
        auto clientNode = hepnosNode["client"];
        if(!clientNode) {
            throw Exception("\"client\" entry not found in \"hepnos\" section");
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
171
        // client entry has protocol entry
172 173 174 175
        auto protoNode = clientNode["protocol"];
        if(!protoNode) {
            throw Exception("\"protocol\" entry not found in \"client\" section");
        }
176 177 178 179
        // hepnos entry has databases entry
        auto databasesNode = hepnosNode["databases"];
        if(!databasesNode) {
            throw Exception("\"databasess\" entry not found in \"hepnos\" section");
180
        }
181 182
        if(!databasesNode.IsMap()) {
            throw Exception("\"databases\" entry should be a map");
183 184 185 186 187 188 189 190 191 192
        }
        // database entry has keys datasets, runs, subruns, events, and products.
        std::vector<std::string> fields = { "datasets", "runs", "subruns", "events", "products" };
        for(auto& f : fields) {
            auto fieldNode = databasesNode[f];
            if(!fieldNode) {
                throw Exception("\""+f+"\" entry not found in databases section");
            }
            if(!fieldNode.IsMap()) {
                throw Exception("\""+f+"\" entry should be a mapping from addresses to providers");
193
            }
194 195 196 197 198 199 200 201 202 203 204 205
            for(auto addresses_it = fieldNode.begin(); addresses_it != fieldNode.end(); addresses_it++) {
                auto providers = addresses_it->second;
                for(auto provider_it = providers.begin(); provider_it != providers.end(); provider_it++) {
                    // provider entry should be a sequence
                    if(!provider_it->second.IsSequence()) {
                        throw Exception("provider entry should be a sequence");
                    }
                    for(auto db : provider_it->second) {
                        if(!db.IsScalar()) {
                            throw Exception("database id should be a scalar");
                        }
                    }
206
                }
207 208 209 210
            }
        }
    }

211 212
    public:

Matthieu Dorier's avatar
Matthieu Dorier committed
213 214 215 216 217 218 219 220 221
    ///////////////////////////////////////////////////////////////////////////
    // Product access functions
    ///////////////////////////////////////////////////////////////////////////

    static inline ProductID buildProductID(const ItemDescriptor& id, const std::string& productName) {
        ProductID result;
        result.m_key.resize(sizeof(id)+productName.size());
        std::memcpy(const_cast<char*>(result.m_key.data()), &id, sizeof(id));
        std::memcpy(const_cast<char*>(result.m_key.data()+sizeof(id)), productName.data(), productName.size());
222 223 224
        return result;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
225
    const sdskv::database& locateProductDb(const ProductID& productID) const {
226
        // hash the name to get the provider id
Matthieu Dorier's avatar
Matthieu Dorier committed
227 228 229 230 231
        long unsigned db_idx = 0;
        uint64_t hash;
        hash = hashString(productID.m_key);
        ch_placement_find_closest(m_product_dbs.chi, hash, 1, &db_idx);
        return m_product_dbs.dbs[db_idx];
232 233
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
234 235
    bool loadRawProduct(const ProductID& key,
                        std::string& data) const {
236
        // find out which DB to access
Matthieu Dorier's avatar
Matthieu Dorier committed
237
        auto& db =  locateProductDb(key);
238
        // read the value
239 240
        if(data.size() == 0)
            data.resize(2048); // eagerly allocate 2KB
241
        try {
Matthieu Dorier's avatar
Matthieu Dorier committed
242
            db.get(key.m_key, data);
243 244 245 246
        } catch(sdskv::exception& ex) {
            if(ex.error() == SDSKV_ERR_UNKNOWN_KEY)
                return false;
            else
247
                throw Exception("Error occured when calling sdskv::database::get (SDSKV error="+std::to_string(ex.error())+")");
248 249 250 251
        }
        return true;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
252 253 254 255 256 257 258 259 260 261
    bool loadRawProduct(const ItemDescriptor& id,
                        const std::string& productName,
                        std::string& data) const {
        // build product id
        auto key = buildProductID(id, productName);
        return loadRawProduct(key, data);
    }

    bool loadRawProduct(const ProductID& key,
                        char* value, size_t* vsize) const {
262
        // find out which DB to access
Matthieu Dorier's avatar
Matthieu Dorier committed
263
        auto& db =  locateProductDb(key);
264
        try {
Matthieu Dorier's avatar
Matthieu Dorier committed
265
            db.get(key.m_key.data(), key.m_key.size(), value, vsize);
266 267 268 269 270 271 272 273 274 275 276
        } catch(sdskv::exception& ex) {
            if(ex.error() == SDSKV_ERR_UNKNOWN_KEY)
                return false;
            else if(ex.error() == SDSKV_ERR_SIZE)
                return false;
            else
                throw Exception("Error occured when calling sdskv::database::get (SDSKV error="+std::to_string(ex.error())+")");
        }
        return true;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
277 278 279 280 281 282 283 284 285 286 287 288 289
    bool loadRawProduct(const ItemDescriptor& id,
                        const std::string& productName,
                        char* value, size_t* vsize) const {
        // build product id
        auto key = buildProductID(id, productName);
        return loadRawProduct(key, value, vsize);
    }

    ProductID storeRawProduct(const ItemDescriptor& id,
                              const std::string& productName,
                              const char* value, size_t vsize) const {
        // build product id
        auto key = buildProductID(id, productName);
290
        // find out which DB to access
Matthieu Dorier's avatar
Matthieu Dorier committed
291 292
        auto& db =  locateProductDb(key);
        // read the value
293
        try {
Matthieu Dorier's avatar
Matthieu Dorier committed
294
            db.put(key.m_key.data(), key.m_key.size(), value, vsize);
295
        } catch(sdskv::exception& ex) {
Matthieu Dorier's avatar
Matthieu Dorier committed
296 297 298 299 300
            if(ex.error() == SDSKV_ERR_KEYEXISTS) {
                return ProductID();
            } else {
                throw Exception("Error occured when calling sdskv::database::put (SDSKV error=" +std::to_string(ex.error()) + ")");
            }
301
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
302
        return key;
303 304
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
305 306 307 308 309
    ProductID storeRawProduct(const ItemDescriptor& id,
                              const std::string& productName,
                              const std::string& data) const {
        // build product id
        auto key = buildProductID(id, productName);
310
        // find out which DB to access
Matthieu Dorier's avatar
Matthieu Dorier committed
311 312
        auto& db = locateProductDb(key);
        // read the value
313
        try {
Matthieu Dorier's avatar
Matthieu Dorier committed
314
            db.put(key.m_key, data);
315
        } catch(sdskv::exception& ex) {
316 317 318
            if(ex.error() == SDSKV_ERR_KEYEXISTS) {
                return ProductID();
            } else {
319
                throw Exception("Error occured when calling sdskv::database::put (SDSKV error=" +std::to_string(ex.error()) + ")");
320
            }
321
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
322
        return key;
323
    }
324

325 326 327 328 329 330 331
    ///////////////////////////////////////////////////////////////////////////
    // DataSet access functions
    ///////////////////////////////////////////////////////////////////////////

    /**
     * Builds the database key of a particular DataSet.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
332
    static inline std::string buildDataSetKey(uint8_t level, const std::string& containerName, const std::string& objectName) {
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
        size_t c = 1 + objectName.size();
        if(!containerName.empty()) c += containerName.size() + 1;
        std::string result(c,'\0');
        result[0] = level;
        if(!containerName.empty()) {
            std::memcpy(&result[1], containerName.data(), containerName.size());
            size_t x = 1+containerName.size();
            result[x] = '/';
            std::memcpy(&result[x+1], objectName.data(), objectName.size());
        } else {
            std::memcpy(&result[1], objectName.data(), objectName.size());
        }
        return result;
    }

    /**
     * Locates and return the database in charge of the provided DataSet info.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
351
    const sdskv::database& locateDataSetDb(const std::string& containerName) const {
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
        // hash the name to get the provider id
        long unsigned db_idx = 0;
        uint64_t hash;
        hash = hashString(containerName);
        ch_placement_find_closest(m_dataset_dbs.chi, hash, 1, &db_idx);
        return m_dataset_dbs.dbs[db_idx];
    }

    /**
     * @brief Fills the result vector with a sequence of up to
     * maxDataSets shared_ptr to DataSetImpl coming after the
     * current dataset. Returns the number of DataSets read.
     */
    size_t nextDataSets(const std::shared_ptr<DataSetImpl>& current, 
            std::vector<std::shared_ptr<DataSetImpl>>& result,
            size_t maxDataSets) const {
        int ret;
        result.resize(0);
        auto& level = current->m_level;
        auto& containerName = *current->m_container;
        auto& currentName = current->m_name;
        if(current->m_level == 0) return 0; // cannot iterate at object level
Matthieu Dorier's avatar
Matthieu Dorier committed
374
        auto& db = locateDataSetDb(containerName);
375
        // make an entry for the lower bound
Matthieu Dorier's avatar
Matthieu Dorier committed
376
        auto lb_entry = buildDataSetKey(level, containerName, currentName);
377 378 379 380 381 382 383 384 385 386 387
        // ignore keys that don't have the same level or the same prefix
        std::string prefix(2+containerName.size(), '\0');
        prefix[0] = level;
        if(containerName.size() != 0) {
            std::memcpy(&prefix[1], containerName.data(), containerName.size());
            prefix[prefix.size()-1] = '/';
        } else {
            prefix.resize(1);
        }
        // issue an sdskv_list_keys
        std::vector<std::string> entries(maxDataSets, std::string(1024,'\0'));
Matthieu Dorier's avatar
Matthieu Dorier committed
388
        std::vector<UUID> uuids(maxDataSets);
389
        try {
Matthieu Dorier's avatar
Matthieu Dorier committed
390
            db.list_keyvals(lb_entry, prefix, entries, uuids);
391 392 393 394 395 396 397 398 399 400 401 402 403
        } catch(sdskv::exception& ex) {
            throw Exception("Error occured when calling sdskv::database::list_keys (SDSKV error="+std::string(ex.what()) + ")");
        }
        result.resize(0);
        for(const auto& entry : entries) {
            size_t i = entry.find_last_of('/');
            if(i == std::string::npos) i = 1;
            else i += 1;
            result.push_back(
                    std::make_shared<DataSetImpl>(
                        current->m_datastore,
                        level,
                        current->m_container,
Matthieu Dorier's avatar
Matthieu Dorier committed
404 405
                        entry.substr(i),
                        uuids[i]
406 407 408 409 410 411 412 413 414 415 416 417
                    )
                );
        }
        return result.size();
    }

    /**
     * @brief Checks if a particular dataset exists.
     */
    bool dataSetExists(uint8_t level, const std::string& containerName, const std::string& objectName) const {
        int ret;
        // build key
Matthieu Dorier's avatar
Matthieu Dorier committed
418
        auto key = buildDataSetKey(level, containerName, objectName);
419
        // find out which DB to access
Matthieu Dorier's avatar
Matthieu Dorier committed
420
        auto& db = locateDataSetDb(containerName);
421
        try {
422 423
            bool b = db.exists(key);
            return b;
424 425 426 427 428
        } catch(sdskv::exception& ex) {
            throw Exception("Error occured when calling sdskv::database::exists (SDSKV error="+std::to_string(ex.error())+")");
        }
        return false;
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
429 430 431 432 433 434 435
    
    /*
     * @brief Loads a dataset.
     */
    bool loadDataSet(uint8_t level, const std::string& containerName, const std::string& objectName, UUID& uuid) const {
        int ret;
        // build key
Matthieu Dorier's avatar
Matthieu Dorier committed
436
        auto key = buildDataSetKey(level, containerName, objectName);
Matthieu Dorier's avatar
Matthieu Dorier committed
437
        // find out which DB to access
Matthieu Dorier's avatar
Matthieu Dorier committed
438
        auto& db = locateDataSetDb(containerName);
Matthieu Dorier's avatar
Matthieu Dorier committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
        try {
            size_t s = sizeof(uuid);
            db.get(static_cast<const void*>(key.data()), 
                   key.size(),
                   static_cast<void*>(uuid.data),
                   &s);
            return s == sizeof(uuid);
        } catch(sdskv::exception& ex) {
            if(ex.error() == SDSKV_ERR_UNKNOWN_KEY) {
                return false;
            }
            throw Exception("Error occured when calling sdskv::database::get (SDSKV error="+std::to_string(ex.error())+")");
        }
        return false;
    }
454 455 456 457

    /**
     * Creates a DataSet
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
458
    bool createDataSet(uint8_t level, const std::string& containerName, const std::string& objectName, UUID& uuid) {
459
        // build full name
Matthieu Dorier's avatar
Matthieu Dorier committed
460
        auto key = buildDataSetKey(level, containerName, objectName);
461
        // find out which DB to access
Matthieu Dorier's avatar
Matthieu Dorier committed
462
        auto& db = locateDataSetDb(containerName);
Matthieu Dorier's avatar
Matthieu Dorier committed
463
        uuid.randomize();
464
        try {
Matthieu Dorier's avatar
Matthieu Dorier committed
465
            db.put(key.data(), key.size(), uuid.data, sizeof(uuid));
466
        } catch(sdskv::exception& ex) {
Matthieu Dorier's avatar
Matthieu Dorier committed
467 468 469 470 471 472 473 474 475 476 477 478 479
            if(ex.error() == SDSKV_ERR_KEYEXISTS) {
                return false;
            } else {
                throw Exception("Error occured when calling sdskv::database::put (SDSKV error=" +std::to_string(ex.error()) + ")");
            }
        }
        return true;
    }

    ///////////////////////////////////////////////////////////////////////////
    // Access functions for numbered items (Runs, SubRuns, and Events)
    ///////////////////////////////////////////////////////////////////////////

Matthieu Dorier's avatar
Matthieu Dorier committed
480
    const sdskv::database& locateItemDb(const ItemType& type, const ItemDescriptor& id) const {
Matthieu Dorier's avatar
Matthieu Dorier committed
481 482 483 484
        long unsigned db_idx = 0;
        uint64_t hash;
        size_t prime = 1099511628211ULL;
        hash = id.dataset.hash();
Matthieu Dorier's avatar
Matthieu Dorier committed
485
        if(type == ItemType::RUN) { // we are locating a Run
Matthieu Dorier's avatar
Matthieu Dorier committed
486 487
            ch_placement_find_closest(m_run_dbs.chi, hash, 1, &db_idx);
            return m_run_dbs.dbs[db_idx];
Matthieu Dorier's avatar
Matthieu Dorier committed
488
        } else if(type == ItemType::SUBRUN) { // we are locating a SubRun
Matthieu Dorier's avatar
Matthieu Dorier committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
            hash *= prime;
            hash = hash ^ id.run;
            ch_placement_find_closest(m_subrun_dbs.chi, hash, 1, &db_idx);
            return m_subrun_dbs.dbs[db_idx];
        } else { // we are locating an Event
            hash *= prime;
            hash = hash ^ id.subrun;
            ch_placement_find_closest(m_event_dbs.chi, hash, 1, &db_idx);
            return m_event_dbs.dbs[db_idx];
        }
    }

    /**
     * @brief Fills the result vector with a sequence of up to
     * maxRuns shared_ptr to RunImpl coming after the
     * current run. Returns the number of Runs read.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
506 507 508
    size_t nextItems(
            const ItemType& type,
            const std::shared_ptr<ItemImpl>& current, 
Matthieu Dorier's avatar
Matthieu Dorier committed
509 510 511 512 513
            std::vector<std::shared_ptr<ItemImpl>>& result,
            size_t maxItems) const {
        int ret;
        result.resize(0);
        const ItemDescriptor& start_key   = current->m_descriptor;
Matthieu Dorier's avatar
Matthieu Dorier committed
514
        auto& db = locateItemDb(type, start_key);
Matthieu Dorier's avatar
Matthieu Dorier committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
        // ignore keys that don't have the same uuid
        // issue an sdskv_list_keys
        std::vector<ItemDescriptor> descriptors(maxItems);
        std::vector<void*> keys_addr(maxItems);
        std::vector<hg_size_t> keys_sizes(maxItems, sizeof(ItemDescriptor));
        for(auto i=0; i < maxItems; i++) {
            keys_addr[i] = static_cast<void*>(&descriptors[i]);
        }
        try {
            hg_size_t s = maxItems;
            db.list_keys(&start_key, sizeof(start_key),
                         &start_key, current->parentPrefixSize(),
                         keys_addr.data(), keys_sizes.data(), &s);
            maxItems = s;
        } catch(sdskv::exception& ex) {
            throw Exception("Error occured when calling sdskv::database::list_keys (SDSKV error="+std::string(ex.what()) + ")");
        }
        descriptors.resize(maxItems);
        for(const auto& key : descriptors) {
            result.push_back(std::make_shared<ItemImpl>(current->m_datastore, key));
        }
        return result.size();
    }

    /**
     * @brief Checks if a particular Run/SubRun/Event exists.
     */
    bool itemExists(const UUID& containerUUID,
                    const RunNumber& run_number,
                    const SubRunNumber& subrun_number = InvalidSubRunNumber,
                    const EventNumber& event_number = InvalidEventNumber) const {
        // build the key
        ItemDescriptor k;
        k.dataset = containerUUID;
        k.run     = run_number;
        k.subrun  = subrun_number;
        k.event   = event_number;
Matthieu Dorier's avatar
Matthieu Dorier committed
552 553 554 555 556 557
        ItemType type = ItemType::RUN;
        if(subrun_number != InvalidSubRunNumber) {
            type = ItemType::SUBRUN;
            if(event_number != InvalidEventNumber)
                type = ItemType::EVENT;
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
558
        // find out which DB to access
Matthieu Dorier's avatar
Matthieu Dorier committed
559
        auto& db = locateItemDb(type, k);
Matthieu Dorier's avatar
Matthieu Dorier committed
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
        try {
            bool b = db.exists(&k, sizeof(k));
            return b;
        } catch(sdskv::exception& ex) {
            throw Exception("Error occured when calling sdskv::database::exists (SDSKV error="+std::to_string(ex.error())+")");
        }
        return false;
    }
    
    /**
     * Creates a Run, SubRun, or Event
     */
    bool createItem(const UUID& containerUUID,
                    const RunNumber& run_number,
                    const SubRunNumber& subrun_number = InvalidSubRunNumber,
                    const EventNumber& event_number = InvalidEventNumber) {
        // build the key
        ItemDescriptor k;
        k.dataset = containerUUID;
        k.run     = run_number;
        k.subrun  = subrun_number;
        k.event   = event_number;
Matthieu Dorier's avatar
Matthieu Dorier committed
582 583 584 585 586 587
        ItemType type = ItemType::RUN;
        if(subrun_number != InvalidSubRunNumber) {
            type = ItemType::SUBRUN;
            if(event_number != InvalidEventNumber)
                type = ItemType::EVENT;
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
588
        // find out which DB to access
Matthieu Dorier's avatar
Matthieu Dorier committed
589
        auto& db = locateItemDb(type, k);
Matthieu Dorier's avatar
Matthieu Dorier committed
590 591 592 593
        try {
            db.put(&k, sizeof(k), nullptr, 0);
        } catch(sdskv::exception& ex) {
            if(ex.error() == SDSKV_ERR_KEYEXISTS) {
594 595 596 597 598 599 600
                return false;
            } else {
                throw Exception("Error occured when calling sdskv::database::put (SDSKV error=" +std::to_string(ex.error()) + ")");
            }
        }
        return true;
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
601

602 603 604
};

}
605 606

#endif