Commit e4be45de authored by Giorgio Azzinnaro's avatar Giorgio Azzinnaro

added error collector

parent ff64556f
......@@ -33,10 +33,15 @@ using google::protobuf::DescriptorProto;
using google::protobuf::FieldDescriptor;
using google::protobuf::FieldDescriptorProto_Type;
using google::protobuf::DescriptorPool;
using google::protobuf::Message;
using profanedb::protobuf::Key;
profanedb::format::protobuf::Loader::RootSourceTree::RootSourceTree(std::initializer_list<path> paths)
namespace profanedb {
namespace format {
namespace protobuf {
Loader::RootSourceTree::RootSourceTree(std::initializer_list<path> paths)
: paths(paths)
{
if (paths.size() == 0)
......@@ -53,24 +58,16 @@ profanedb::format::protobuf::Loader::RootSourceTree::RootSourceTree(std::initial
throw std::runtime_error(this->GetLastErrorMessage());
}
profanedb::format::protobuf::Loader::Loader(
Loader::Loader(
std::unique_ptr<RootSourceTree> include,
std::unique_ptr<RootSourceTree> schema)
: includeSourceTree(std::move(include))
, schemaSourceTree(std::move(schema))
, includeDb(includeSourceTree.get())
, schemaDb(schemaSourceTree.get())
, schemaPool(new MergedDescriptorDatabase(&includeDb, &schemaDb))
, normalizedPool(&normalizedDescriptorDb)
, schemaPool(new MergedDescriptorDatabase(&includeDb, &schemaDb), &errorCollector)
, normalizedPool(new MergedDescriptorDatabase(&includeDb, &normalizedDescriptorDb), &errorCollector)
{
// profanedb.protobuf.options.key is defined in here
// It is used to mark the primary key on Messages
const FileDescriptor * optionsFile = schemaPool.FindFileByName("profanedb/protobuf/options.proto");
FileDescriptorProto optionsProto;
optionsFile->CopyTo(&optionsProto);
this->normalizedDescriptorDb.AddAndOwn(&optionsProto);
BOOST_LOG_TRIVIAL(debug) << "Loading profanedb/protobuf/options.proto and copying to normalized descriptor db";
// Just in case schema is defined in more than one place
for (const auto & path: schemaSourceTree->paths) {
......@@ -89,13 +86,13 @@ profanedb::format::protobuf::Loader::Loader(
BOOST_LOG_TRIVIAL(debug) << "Adding normalized proto " << normalizedProto.name();
// The normalizedDescriptorDb keeps these new Descriptors
this->normalizedDescriptorDb.AddAndOwn(&normalizedProto);
normalizedDescriptorDb.AddAndOwn(&normalizedProto);
}
}
}
}
FileDescriptorProto profanedb::format::protobuf::Loader::ParseFile(
FileDescriptorProto Loader::ParseFile(
const FileDescriptor * fileDescriptor)
{
BOOST_LOG_TRIVIAL(debug) << "Parsing file " << fileDescriptor->name();
......@@ -114,7 +111,7 @@ FileDescriptorProto profanedb::format::protobuf::Loader::ParseFile(
return normFileDescProto;
}
DescriptorProto profanedb::format::protobuf::Loader::ParseAndNormalizeDescriptor(
DescriptorProto Loader::ParseAndNormalizeDescriptor(
const Descriptor * descriptor)
{
BOOST_LOG_TRIVIAL(debug) << "Parsing descriptor " << descriptor->full_name();
......@@ -153,7 +150,7 @@ DescriptorProto profanedb::format::protobuf::Loader::ParseAndNormalizeDescriptor
return normDescProto;
}
bool profanedb::format::protobuf::Loader::IsKeyable(const Descriptor * descriptor) const
bool Loader::IsKeyable(const Descriptor * descriptor) const
{
for (int i = 0; i < descriptor->field_count(); i++) {
// If any field in message has profanedb::protobuf::options::key set
......@@ -163,12 +160,36 @@ bool profanedb::format::protobuf::Loader::IsKeyable(const Descriptor * descripto
return false;
}
const DescriptorPool & profanedb::format::protobuf::Loader::GetSchemaPool() const
const DescriptorPool & Loader::GetSchemaPool() const
{
return this->schemaPool;
}
const DescriptorPool & profanedb::format::protobuf::Loader::GetNormalizedPool() const
const DescriptorPool & Loader::GetNormalizedPool() const
{
return this->normalizedPool;
}
void Loader::BoostLogErrorCollector::AddError(
const std::string & filename,
const std::string & element_name,
const Message * descriptor,
DescriptorPool::ErrorCollector::ErrorLocation location,
const std::string & message)
{
BOOST_LOG_TRIVIAL(error) << message;
}
void Loader::BoostLogErrorCollector::AddWarning(
const std::string & filename,
const std::string & element_name,
const Message * descriptor,
DescriptorPool::ErrorCollector::ErrorLocation location,
const std::string & message)
{
BOOST_LOG_TRIVIAL(warning) << message;
}
}
}
}
......@@ -43,7 +43,7 @@ public:
// mapping all the paths provided to the root ("/") path for easier import.
// Paths are available for Loader to populate its Pool;
class RootSourceTree : public google::protobuf::compiler::DiskSourceTree {
friend Loader;
friend class Loader;
public:
RootSourceTree(std::initializer_list<boost::filesystem::path> paths);
......@@ -76,6 +76,23 @@ private:
// Check whether a Descriptor has a field with key option set
bool IsKeyable(const google::protobuf::Descriptor * descriptor) const;
class BoostLogErrorCollector
: public google::protobuf::DescriptorPool::ErrorCollector {
virtual void AddError(const std::string & filename,
const std::string & element_name,
const google::protobuf::Message * descriptor,
google::protobuf::DescriptorPool::ErrorCollector::ErrorLocation location,
const std::string & message) override;
virtual void AddWarning(const std::string & filename,
const std::string & element_name,
const google::protobuf::Message * descriptor,
google::protobuf::DescriptorPool::ErrorCollector::ErrorLocation location,
const std::string & message) override;
};
// google::protobuf::DescriptorPool::ErrorCollector & errorCollector;
BoostLogErrorCollector errorCollector;
std::unique_ptr<RootSourceTree> includeSourceTree;
std::unique_ptr<RootSourceTree> schemaSourceTree;
......
......@@ -149,14 +149,24 @@ const Message & Marshaller::Unmarshal(const StorableMessage & storable)
Message * Marshaller::CreateMessage(Marshaller::MessagePool pool, std::string type)
{
BOOST_LOG_TRIVIAL(debug) << "Creating prototype message "
<< type
<< " from "
<< ((pool == SCHEMA) ? "SCHEMA" : "NORMALIZED")
<< " pool";
// DescriptorPool is either from Schema or Normalized
const DescriptorPool & descriptorPool =
(pool == SCHEMA)
? loader->GetSchemaPool()
: loader->GetNormalizedPool();
const Descriptor * descriptor = descriptorPool.FindMessageTypeByName(type);
if (descriptor == nullptr)
throw std::runtime_error(type + " doesn't exist");
// TODO Check whether type exists
return this->messageFactory.GetPrototype(descriptorPool.FindMessageTypeByName(type))->New();
return this->messageFactory.GetPrototype(descriptor)->New();
}
void Marshaller::CopyField(
......
......@@ -29,6 +29,8 @@
#include <google/protobuf/descriptor.h>
#include <google/protobuf/dynamic_message.h>
#include <boost/log/trivial.hpp>
#include <profanedb/format/marshaller.h>
#include "loader.h"
......
......@@ -31,9 +31,9 @@ struct Format
auto storage = std::make_shared<RocksStorage>(std::unique_ptr<rocksdb::DB>(rocks));
auto includeSourceTree = new Loader::RootSourceTree{
"/usr/include", "/home/giorgio/Documents/ProfaneDB/ProfaneDBLib/src"};
"/usr/include", "/home/giorgio/Documents/ProfaneDB/ProfaneDB/src"};
auto schemaSourceTree = new Loader::RootSourceTree{"/home/giorgio/Documents/ProfaneDB/ProfaneDBLib/test/profanedb/test/protobuf/schema"};
auto schemaSourceTree = new Loader::RootSourceTree{"/home/giorgio/Documents/ProfaneDB/ProfaneDB/test/profanedb/test/protobuf/schema"};
auto loader = std::make_shared<Loader>(
std::unique_ptr<Loader::RootSourceTree>(includeSourceTree),
......
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