aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/DB.cpp21
-rw-r--r--src/crepe/facade/DB.h43
2 files changed, 53 insertions, 11 deletions
diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp
index bd2f089..c885560 100644
--- a/src/crepe/facade/DB.cpp
+++ b/src/crepe/facade/DB.cpp
@@ -1,6 +1,7 @@
#include <cstring>
#include "util/log.h"
+#include "Exception.h"
#include "DB.h"
@@ -14,24 +15,22 @@ DB::DB(const string & path) {
// init database struct
libdb::DB * db;
if ((ret = libdb::db_create(&db, NULL, 0)) != 0)
- throw nullptr; // TODO: exception
+ throw Exception("db_create: %s", libdb::db_strerror(ret));
this->db = { db, [] (libdb::DB * db) { db->close(db, 0); } };
// load or create database file
- if ((ret = this->db->open(this->db.get(), NULL, path.c_str(), NULL, libdb::DB_BTREE, DB_CREATE, 0)) != 0) {
- throw nullptr;
- }
+ if ((ret = this->db->open(this->db.get(), NULL, path.c_str(), NULL, libdb::DB_BTREE, DB_CREATE, 0)) != 0)
+ throw Exception("db->open: %s", libdb::db_strerror(ret));
// create cursor
libdb::DBC * cursor;
- if ((ret = this->db->cursor(this->db.get(), NULL, &cursor, 0)) != 0) {
- throw nullptr;
- }
+ if ((ret = this->db->cursor(this->db.get(), NULL, &cursor, 0)) != 0)
+ throw Exception("db->cursor: %s", libdb::db_strerror(ret));
this->cursor = { cursor, [] (libdb::DBC * cursor) { cursor->close(cursor); } };
}
-libdb::DBT DB::to_thing(const string & thing) const {
+libdb::DBT DB::to_thing(const string & thing) const noexcept {
libdb::DBT thang;
memset(&thang, 0, sizeof(libdb::DBT));
thang.data = (void *) thing.data();
@@ -45,7 +44,8 @@ string DB::get(const string & key) {
memset(&db_val, 0, sizeof(libdb::DBT));
int ret = this->cursor->get(this->cursor.get(), &db_key, &db_val, DB_FIRST);
- if (ret != 0) throw nullptr; // TODO: proper exception
+ if (ret != 0)
+ throw Exception("cursor->get: %s", libdb::db_strerror(ret));
return { static_cast<char *>(db_val.data), db_val.size };
}
@@ -53,7 +53,8 @@ void DB::set(const string & key, const string & value) {
libdb::DBT db_key = this->to_thing(key);
libdb::DBT db_val = this->to_thing(value);
int ret = this->db->put(this->db.get(), NULL, &db_key, &db_val, 0);
- if (ret != 0) throw nullptr; // TODO: proper exception
+ if (ret != 0)
+ throw Exception("cursor->get: %s", libdb::db_strerror(ret));
}
bool DB::has(const std::string & key) noexcept {
diff --git a/src/crepe/facade/DB.h b/src/crepe/facade/DB.h
index 06442ad..b62a974 100644
--- a/src/crepe/facade/DB.h
+++ b/src/crepe/facade/DB.h
@@ -12,22 +12,63 @@ extern "C" {
namespace crepe {
+/**
+ * \brief Berkeley DB facade
+ *
+ * Berkeley DB is a simple key-value database that stores arbitrary data as
+ * both key and value. This facade uses STL strings as keys/values.
+ */
class DB {
public:
+ /**
+ * \param path The path of the database (created if nonexistant)
+ */
DB(const std::string & path);
virtual ~DB() = default;
public:
+ /**
+ * \brief Get a value from the database, or throw an exception
+ *
+ * \param key The value key
+ *
+ * \return The value
+ *
+ * \throws Exception if value is not found in DB or other error occurs
+ */
std::string get(const std::string & key);
+ /**
+ * \brief Set (create or overwrite) a value in the database
+ *
+ * \param key The value key
+ * \param value The value to store
+ *
+ * \throws Exception if an error occurs
+ */
void set(const std::string & key, const std::string & value);
+ /**
+ * \brief Check if a key exists in the database
+ *
+ * \param key The value key
+ *
+ * \returns True if the key exists, or false if it does not
+ */
bool has(const std::string & key) noexcept;
private:
+ //! RAII wrapper around \c DB struct
std::unique_ptr<libdb::DB, std::function<void(libdb::DB *)>> db;
+ //! RAII wrapper around \c DBC struct
std::unique_ptr<libdb::DBC, std::function<void(libdb::DBC *)>> cursor;
private:
- libdb::DBT to_thing(const std::string & thing) const;
+ /**
+ * \brief Convert an STL string to DBT (data base thang)
+ *
+ * \param thing Input data
+ * \return \c DBT with the same data as input \c thing
+ */
+ libdb::DBT to_thing(const std::string & thing) const noexcept;
};
}