Unverified Commit f5441bd0 authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

MetadataDB: start using exceptions as error propagation method

The metadataDB class was using integer to return errors.
New exceptions classes has been introduced in order to be used as error
propagation method.

In this commit the MetadataDB::get function has been modified in order
to make use of exceptions.
parent a92831d2
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -11,15 +11,15 @@ int create_metadentry(const std::string& path, mode_t mode);

int db_val_to_stat(const std::string& path, std::string db_val, struct stat& attr);

int get_metadentry(const std::string& path, std::string& val);
std::string get_metadentry_str(const std::string& path);

int get_metadentry(const std::string& path, Metadata& md);
Metadata get_metadentry(const std::string& path);

int remove_metadentry(const std::string& path);

int remove_node(const std::string& path);

int get_metadentry_size(const std::string& path, size_t& ret_size);
size_t get_metadentry_size(const std::string& path);

int update_metadentry_size(const std::string& path, size_t io_size, off_t offset, bool append, size_t& read_size);

+17 −0
Original line number Diff line number Diff line
#ifndef IFS_DB_EXCEPTIONS_HPP
#define IFS_DB_EXCEPTIONS_HPP

#include <string>
#include <stdexcept>

class DBException: public std::runtime_error {
    public:
        DBException(const std::string & s) : std::runtime_error(s) {};
};

class NotFoundException: public DBException {
    public:
        NotFoundException(const std::string & s) : DBException(s) {};
};

#endif //IFS_DB_EXCEPTIONS_HPP
+4 −3
Original line number Diff line number Diff line
@@ -3,10 +3,10 @@

#include <memory>
#include "rocksdb/db.h"
#include "daemon/backend/exceptions.hpp"

namespace rdb = rocksdb;


class MetadataDB {
    private:
        std::unique_ptr<rdb::DB> db;
@@ -16,9 +16,11 @@ class MetadataDB {
        static void optimize_rocksdb_options(rdb::Options& options);

    public:
        static inline void throw_rdb_status_excpt(const rdb::Status& s);

        MetadataDB(const std::string& path);

        bool get(const std::string& key, std::string& val);
        std::string get(const std::string& key) const;
        bool put(const std::string& key, const std::string& val);
        bool remove(const std::string& key);
        bool exists(const std::string& key);
@@ -27,5 +29,4 @@ class MetadataDB {
        void iterate_all();
};


#endif //IFS_METADATA_DB_HPP
+11 −27
Original line number Diff line number Diff line
@@ -42,12 +42,8 @@ int create_metadentry(const std::string& path, mode_t mode) {
    return ADAFS_DATA->mdb()->put(path, md.serialize()) ? 0 : -1;
}

int get_metadentry(const std::string& path, std::string& val) {
    auto ok = ADAFS_DATA->mdb()->get(path, val);
    if (!ok || val.size() == 0) {
        return -1;
    }
    return 0;
std::string get_metadentry_str(const std::string& path) {
        return ADAFS_DATA->mdb()->get(path);
}

/**
@@ -56,15 +52,8 @@ int get_metadentry(const std::string& path, std::string& val) {
 * @param attr
 * @return
 */
int get_metadentry(const std::string& path, Metadata& md) {
    string val;
    auto err = get_metadentry(path, val);
    if (err) {
        return -1;
    }
    Metadata mdi{path, val};
    md = mdi;
    return 0;
Metadata get_metadentry(const std::string& path) {
    return {path, get_metadentry_str(path)};
}

/**
@@ -98,15 +87,8 @@ int remove_node(const string& path) {
 * @param ret_size (return val)
 * @return err
 */
int get_metadentry_size(const string& path, size_t& ret_size) {
    string val;
    auto err = ADAFS_DATA->mdb()->get(path, val);
    if (!err || val.empty()) {
        return ENOENT;
    }
    Metadata md{path, val};
    ret_size = md.size();
    return 0;
size_t get_metadentry_size(const string& path) {
    return get_metadentry(path).size();
}

/**
@@ -141,10 +123,12 @@ int update_metadentry(const string& path, Metadata& md) {
 * @return errno
 */
int check_access_mask(const string& path, const int mask) {
    Metadata md{};
    auto err = get_metadentry(path, md);
    if (err == -1)  // metadentry not found
    Metadata md;
    try {
        md = get_metadentry(path);
    } catch (const NotFoundException& e) {
        return ENOENT;
    }

    /*
     * if only check if file exists is wanted, return success.
+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ add_library(metadata_db STATIC)
target_sources(metadata_db
    PUBLIC
    ${INCLUDE_DIR}/daemon/backend/metadata/db.hpp
    ${INCLUDE_DIR}/daemon/backend/exceptions.hpp
    PRIVATE
    ${INCLUDE_DIR}/daemon/backend/metadata/merge.hpp
    ${CMAKE_CURRENT_LIST_DIR}/merge.cpp
Loading