Commit fa393bf6 authored by Giorgio Azzinnaro's avatar Giorgio Azzinnaro

changed loader to make use of map to store schema and normalized pools

parent 0578ffb2
......@@ -65,17 +65,15 @@ Loader::Loader(
, schemaSourceTree(std::move(schema))
, includeDb(includeSourceTree.get())
, schemaDb(schemaSourceTree.get())
, 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);
// normalizedDescriptorDb.AddAndOwn(&optionsProto);
// BOOST_LOG_TRIVIAL(debug) << "Loading profanedb/protobuf/options.proto and copying to normalized descriptor db";
auto schemaPool = std::make_shared<DescriptorPool>(
new MergedDescriptorDatabase(&includeDb, &schemaDb), &errorCollector);
auto normalizedPool = std::make_shared<DescriptorPool>(
new MergedDescriptorDatabase(&includeDb, &normalizedDescriptorDb), &errorCollector);
this->pools.emplace(SCHEMA, schemaPool);
this->pools.emplace(NORMALIZED, normalizedPool);
// Just in case schema is defined in more than one place
for (const auto & path: schemaSourceTree->paths) {
......@@ -92,7 +90,7 @@ Loader::Loader(
// The file is now retrieved, and its path for Protobuf must be relative to the mapping
// it's parsed, normalized (nested keyable messages are removed)
FileDescriptorProto * normalizedProto = this->ParseFile(
schemaPool.FindFileByName(file.path().lexically_relative(path).string()));
this->GetPool(SCHEMA).FindFileByName(file.path().lexically_relative(path).string()));
BOOST_LOG_TRIVIAL(debug) << "Adding normalized proto " << normalizedProto->name();
BOOST_LOG_TRIVIAL(trace) << std::endl << normalizedProto->DebugString();
......@@ -177,7 +175,7 @@ bool Loader::IsKeyable(const Descriptor * descriptor) const
const DescriptorPool & Loader::GetPool(PoolType poolType) const
{
return (poolType == SCHEMA) ? this->schemaPool : this->normalizedPool;
return *this->pools.at(poolType);
}
const Descriptor * Loader::GetDescriptor(PoolType poolType, std::string typeName) const
......
......@@ -20,6 +20,8 @@
#ifndef PROFANEDB_FORMAT_PROTOBUF_LOADER_H
#define PROFANEDB_FORMAT_PROTOBUF_LOADER_H
#include <unordered_map>
#include <profanedb/protobuf/options.pb.h>
#include <profanedb/protobuf/storage.pb.h>
......@@ -116,12 +118,10 @@ private:
google::protobuf::SimpleDescriptorDatabase normalizedDescriptorDb;
// schemaPool keeps track of the original messages
google::protobuf::DescriptorPool schemaPool;
// For each keyable message in schema, there is a normalized version
// which has Key in place of nested keyable messages
google::protobuf::DescriptorPool normalizedPool;
// SCHEMA and NORMALIZED pools are stored in here
// SCHEMA is the original DescriptorPool as provided by the user
// NORMALIZED holds references to nested keyable messages
std::unordered_map< PoolType, std::shared_ptr<google::protobuf::DescriptorPool> > pools;
// The message factory is used to create new messages from the pools
google::protobuf::DynamicMessageFactory messageFactory;
......
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