Commit f01c3623 authored by Giorgio Azzinnaro's avatar Giorgio Azzinnaro

setting key of nested messages

parent 4c073264
......@@ -40,7 +40,7 @@ profanedb::protobuf::PutResp profanedb::storage::Db::Put(const profanedb::protob
auto map = normalizer.NormalizeMessage(request.serializable());
for (auto const & obj: map) {
std::cout << obj.first << ":" << std::endl << obj.second->DebugString() << std::endl;
std::cout << obj.first << ":" << std::endl << obj.second.DebugString() << std::endl;
}
}
......
......@@ -28,7 +28,7 @@ profanedb::storage::Normalizer::Normalizer(Parser & parser)
{
}
std::map<std::string, std::shared_ptr<const google::protobuf::Message>> profanedb::storage::Normalizer::NormalizeMessage(
std::map<std::string, const google::protobuf::Message &> profanedb::storage::Normalizer::NormalizeMessage(
const google::protobuf::Any & serializable)
{
// Any messages have a type url beginning with `type.googleapis.com/`, this is stripped
......@@ -42,18 +42,45 @@ std::map<std::string, std::shared_ptr<const google::protobuf::Message>> profaned
serializable.UnpackTo(container.get());
// The method getting a Message as paramater does the actual normalization of data
return this->NormalizeMessage(container);
return this->NormalizeMessage(*container);
}
std::map<std::string, std::shared_ptr<const google::protobuf::Message>> profanedb::storage::Normalizer::NormalizeMessage(
std::shared_ptr<const google::protobuf::Message> message) const
std::map<std::string, const google::protobuf::Message &> profanedb::storage::Normalizer::NormalizeMessage(
const google::protobuf::Message & message)
{
auto dependencies = std::map<std::string, std::shared_ptr<const google::protobuf::Message>>();
Parser::NormalizedDescriptor & normalizedDesc = parser.normalizedDescriptors.at(message->GetDescriptor()->full_name());
auto dependencies = std::map<std::string, const google::protobuf::Message &>();
Parser::NormalizedDescriptor & normalizedDesc = parser.normalizedDescriptors.at(message.GetTypeName());
dependencies.insert(std::pair<std::string, std::shared_ptr<const google::protobuf::Message>>(
FieldToKey(*message, normalizedDesc.GetKey()),
message));
google::protobuf::Message * normalizedMessage = messageFactory.GetPrototype(normalizedPool->FindMessageTypeByName(message.GetTypeName()))->New();
// normalizedMessage->CopyFrom(*message); // This can't be done as nested keyable messages are now defined as string in normalizedMessage
std::vector< const google::protobuf::FieldDescriptor * > setFields;
message.GetReflection()->ListFields(message, &setFields);
for (const auto & fd: setFields) {
if (normalizedDesc.GetKeyableReferences().find(fd) != normalizedDesc.GetKeyableReferences().end()) {
const google::protobuf::Message & nestedMessage = message.GetReflection()->GetMessage(message, fd, &messageFactory);
auto nestedDependencies = this->NormalizeMessage(nestedMessage);
dependencies.insert(nestedDependencies.begin(), nestedDependencies.end());
const google::protobuf::FieldDescriptor * referenceFieldDescriptor = normalizedMessage->GetDescriptor()->field(fd->index());
Parser::NormalizedDescriptor & nestedNormalizedDesc = parser.normalizedDescriptors.at(nestedMessage.GetTypeName());
normalizedMessage->GetReflection()->SetString(
normalizedMessage,
referenceFieldDescriptor,
FieldToKey(nestedMessage, nestedNormalizedDesc.GetKey()));
// TODO If we made NormalizeMessage return the top level key in a clear way we'd avoid all this
} else {
// TODO Copy field as is from message
}
}
dependencies.insert(std::pair<std::string, const google::protobuf::Message &>(
FieldToKey(message, normalizedDesc.GetKey()),
*normalizedMessage)); // FIXME In here goes the normalized message
return dependencies;
}
......
......@@ -41,12 +41,12 @@ public:
Normalizer(Parser & parser);
// This is just a layer on top of NormalizeMessage(Message) to parse an Any message
std::map<std::string, std::shared_ptr<const google::protobuf::Message>> NormalizeMessage(
std::map<std::string, const google::protobuf::Message &> NormalizeMessage(
const google::protobuf::Any & serializable);
// Unnest keyable messages and assign their key to their parent object
std::map<std::string, std::shared_ptr<const google::protobuf::Message>> NormalizeMessage(
std::shared_ptr<const google::protobuf::Message> message) const;
std::map<std::string, const google::protobuf::Message &> NormalizeMessage(
const google::protobuf::Message & message);
private:
Parser & parser;
......
......@@ -96,7 +96,9 @@ profanedb::storage::Parser::NormalizedDescriptor::NormalizedDescriptor(
// and keyable messages are flagged for normalization
if (nested != NULL && IsKeyable(*nested)) {
proto.mutable_field(k)->set_type(FieldDescriptorProto_Type_TYPE_STRING);
this->keyableMessageReferences.push_back(fd);
proto.mutable_field(k)->clear_type_name();
this->keyableMessageReferences.insert(fd);
} else if (fd->options().GetExtension(profanedb::protobuf::options).key()) {
this->key = fd;
......@@ -119,7 +121,7 @@ const google::protobuf::FieldDescriptor & profanedb::storage::Parser::Normalized
return *this->key;
}
const std::vector<const google::protobuf::FieldDescriptor *> & profanedb::storage::Parser::NormalizedDescriptor::GetKeyableReferences() const
const std::set<const google::protobuf::FieldDescriptor *> & profanedb::storage::Parser::NormalizedDescriptor::GetKeyableReferences() const
{
return this->keyableMessageReferences;
}
......
......@@ -85,10 +85,10 @@ private:
);
const google::protobuf::FieldDescriptor & GetKey() const;
const std::vector< const google::protobuf::FieldDescriptor * > & GetKeyableReferences() const;
const std::set< const google::protobuf::FieldDescriptor * > & GetKeyableReferences() const;
private:
const google::protobuf::FieldDescriptor * key;
std::vector< const google::protobuf::FieldDescriptor * > keyableMessageReferences;
std::set< const google::protobuf::FieldDescriptor * > keyableMessageReferences;
// Check if a message has a key defined in its schema
static bool IsKeyable(const google::protobuf::Descriptor & descriptor);
......
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