Commit c6d704f5 authored by Giorgio Azzinnaro's avatar Giorgio Azzinnaro

decouple storage

parent 826ebdf4
......@@ -30,4 +30,16 @@ option objc_class_prefix = "PDB";
message Key {
string type = 1;
bytes value = 2;
}
\ No newline at end of file
}
// A StorableMessage has a unique Key and a serialized representation of the object
message StorableMessage {
Key key = 1;
bytes payload = 2;
}
// A Message might depend on other messages to be stored first
message MessageTreeNode {
StorableMessage message = 1;
repeated MessageTreeNode children = 2;
}
add_library(profanedb_storage db.cpp parser.cpp config.cpp normalizer.cpp rootsourcetree.cpp)
add_library(profanedb_storage db.cpp parser.cpp config.cpp normalizer.cpp rootsourcetree.cpp rocks.cpp)
target_link_libraries(profanedb_storage profanedb_protobuf ${ROCKSDB_LIBRARIES} ${Boost_LIBRARIES})
......@@ -37,14 +37,8 @@ profanedb::storage::Db::~Db()
profanedb::protobuf::GetResp profanedb::storage::Db::Get(const protobuf::GetReq & request)
{
std::string type = request.key();
// Split at first dollar sign ($), here starts the actual key value
type = type.substr(0, type.find('$'));
// The last dot (.) separates the message type name from the field type name
type = type.substr(0, type.rfind('.'));
std::string * serialized = new std::string();
db->Get(rocksdb::ReadOptions(), request.key(), serialized);
db->Get(rocksdb::ReadOptions(), request.key().SerializeAsString(), serialized);
return *protobuf::GetResp().New();
}
......
......@@ -67,16 +67,6 @@ private:
google::protobuf::DescriptorProto & proto
);
// A simple ErrorCollector for debug, write to stderr
class ErrorCollector : public compiler::MultiFileErrorCollector {
public:
ErrorCollector();
void AddError(const string & filename, int line, int column, const string & message) override;
void AddWarning(const string & filename, int line, int column, const string & message) override;
};
ErrorCollector errCollector;
class NormalizedDescriptor {
public:
NormalizedDescriptor(
......@@ -95,6 +85,16 @@ private:
};
std::map<std::string, NormalizedDescriptor> normalizedDescriptors;
// A simple ErrorCollector for debug, write to stderr
class ErrorCollector : public compiler::MultiFileErrorCollector {
public:
ErrorCollector();
void AddError(const string & filename, int line, int column, const string & message) override;
void AddWarning(const string & filename, int line, int column, const string & message) override;
};
ErrorCollector errCollector;
};
}
}
......
/*
* ProfaneDB - A Protocol Buffers database.
* Copyright (C) 2017 "Giorgio Azzinnaro" <giorgio.azzinnaro@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "rocks.h"
profanedb::storage::Rocks::Rocks(std::shared_ptr<rocksdb::DB> database)
: database(database)
{
}
profanedb::storage::Rocks::~Rocks()
{
}
void profanedb::storage::Rocks::Store(const profanedb::protobuf::Key & key, const std::string & payload)
{
database->Put(rocksdb::WriteOptions(),
key.SerializeAsString(),
payload);
}
std::string profanedb::storage::Rocks::Retrieve(const profanedb::protobuf::Key & key)
{
std::string payload;
database->Get(rocksdb::ReadOptions(),
key.SerializeAsString(),
&payload);
}
/*
* ProfaneDB - A Protocol Buffers database.
* Copyright (C) 2017 "Giorgio Azzinnaro" <giorgio.azzinnaro@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef PROFANEDB_STORAGE_ROCKS_H
#define PROFANEDB_STORAGE_ROCKS_H
#include "storage.h"
#include <rocksdb/db.h>
namespace profanedb {
namespace storage {
// Use RocksDB as storage layer
class Rocks : profanedb::storage::Storage
{
public:
Rocks(std::shared_ptr<rocksdb::DB> database);
~Rocks();
protected:
void Store(const profanedb::protobuf::Key & key, const std::string & payload) override;
std::string Retrieve(const profanedb::protobuf::Key & key) override;
private:
std::shared_ptr<rocksdb::DB> database;
};
}
}
#endif // PROFANEDB_STORAGE_ROCKS_H
/*
* ProfaneDB - A Protocol Buffers database.
* Copyright (C) 2017 "Giorgio Azzinnaro" <giorgio.azzinnaro@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "storage.h"
void profanedb::storage::Storage::Put(const profanedb::protobuf::MessageTreeNode & messageTree)
{
for (auto const & child: messageTree.children()) {
Put(child);
}
const profanedb::protobuf::StorableMessage & current = messageTree.message();
this->Store(current.key(), current.payload());
}
const profanedb::protobuf::MessageTreeNode profanedb::storage::Storage::Get(const profanedb::protobuf::Key & key) const
{
profanedb::protobuf::MessageTreeNode messageTree;
*messageTree.mutable_message()->mutable_key() = key;
*messageTree.mutable_message()->mutable_payload() = this->Retrieve(key);
// TODO Parse message and foreach nested
// messageTree.add_children( Retrieve(nestedKey) )
return messageTree;
}
/*
* ProfaneDB - A Protocol Buffers database.
* Copyright (C) 2017 "Giorgio Azzinnaro" <giorgio.azzinnaro@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef PROFANEDB_STORAGE_STORAGE_H
#define PROFANEDB_STORAGE_STORAGE_H
#include <google/protobuf/message.h>
#include <profanedb/protobuf/storage.pb.h>
namespace profanedb {
namespace storage {
// Handles storage and retrieval of objects,
// subclass to implement a storage layer
class Storage {
public:
virtual ~Storage() = 0;
void Put(const profanedb::protobuf::MessageTreeNode & messageTree);
const profanedb::protobuf::MessageTreeNode Get(const profanedb::protobuf::Key & key) const;
protected:
virtual void Store(const profanedb::protobuf::Key & key, const std::string & payload) = 0;
virtual const std::string Retrieve(const profanedb::protobuf::Key & key) const = 0;
};
}
}
#endif /* PROFANEDB_STORAGE_STORAGE_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment