Commit 3eae7adb authored by Giorgio Azzinnaro's avatar Giorgio Azzinnaro

Marshaller and Storage to use Any in StorableMessage; Testing

parent 571905a1
......@@ -96,7 +96,7 @@ MessageTreeNode Marshaller::Marshal(const Message & message)
// Now that normalizedMessage has been populated with the correct data,
// actual marshalling (or serialization) occurs
normalizedMessage->SerializeToString(messageTree.mutable_message()->mutable_payload());
messageTree.mutable_message()->mutable_payload()->PackFrom(*normalizedMessage);
return messageTree;
}
......@@ -115,7 +115,7 @@ const Message & Marshaller::Unmarshal(const StorableMessage & storable)
// StorableMessage payload contains the serialized normalized message,
// as previously stored into the DB
normalizedMessage->ParseFromString(storable.payload());
storable.payload().UnpackTo(normalizedMessage);
// Similarly to Marshal, set fields are retrieved
std::vector< const FieldDescriptor * > setFields;
......@@ -227,12 +227,15 @@ Key Marshaller::FieldToKey(
const Reflection * reflection = message.GetReflection();
// TODO If concatenating strings, and they contain the separator, it might mess up,
// we need something much more stable than this
#define REP_SEP "$"
if (fd->is_repeated()) {
for (int y = 0; y < reflection->FieldSize(message, fd); y++) {
switch (fd->cpp_type()) {
#define HANDLE_TYPE(CPPTYPE, METHOD) \
case FieldDescriptor::CPPTYPE_##CPPTYPE: \
*key.mutable_value() += "$" + std::to_string(reflection->GetRepeated##METHOD(message, fd, y)); \
*key.mutable_value() += REP_SEP + std::to_string(reflection->GetRepeated##METHOD(message, fd, y)); \
break;
HANDLE_TYPE(INT32 , Int32 );
......@@ -245,13 +248,13 @@ Key Marshaller::FieldToKey(
#undef HANDLE_TYPE
case FieldDescriptor::CPPTYPE_ENUM:
*key.mutable_value() += "$" + std::to_string(reflection->GetRepeatedEnum(message, fd, y)->index());
*key.mutable_value() += REP_SEP + std::to_string(reflection->GetRepeatedEnum(message, fd, y)->index());
break;
case FieldDescriptor::CPPTYPE_STRING:
*key.mutable_value() += "$" + reflection->GetRepeatedString(message, fd, y);
*key.mutable_value() += REP_SEP + reflection->GetRepeatedString(message, fd, y);
break;
case FieldDescriptor::CPPTYPE_MESSAGE:
*key.mutable_value() += "$" + reflection->GetRepeatedMessage(message, fd, y).SerializeAsString();
*key.mutable_value() += REP_SEP + reflection->GetRepeatedMessage(message, fd, y).SerializeAsString();
break;
}
}
......
......@@ -33,9 +33,14 @@ Storage::Storage(std::unique_ptr<DB> rocksDb)
void Storage::Store(const StorableMessage & storable)
{
// Type is actually duplicated, as it is there in key as well,
// if needed storage space could be reduced by storing only the payload part of Any
std::string serializedPayload;
storable.payload().SerializeToString(&serializedPayload);
this->rocksDb->Put(::rocksdb::WriteOptions(),
storable.key().SerializeAsString(),
storable.payload());
serializedPayload);
}
StorableMessage Storage::Retrieve(const Key & key) const
......@@ -44,9 +49,13 @@ StorableMessage Storage::Retrieve(const Key & key) const
*stored.mutable_key() = key;
std::string serializedPayload;
this->rocksDb->Get(::rocksdb::ReadOptions(),
key.SerializeAsString(),
stored.mutable_payload());
&serializedPayload);
stored.mutable_payload()->ParseFromString(serializedPayload);
return stored;
}
......
......@@ -46,11 +46,48 @@ struct Format
}
};
BOOST_FIXTURE_TEST_CASE(marshal, Format)
#define DEBUG_MESSAGE BOOST_TEST_MESSAGE(message.DebugString()); \
MessageTreeNode tree = marshaller->Marshal(message); \
BOOST_TEST_MESSAGE(tree.DebugString());
BOOST_FIXTURE_TEST_CASE(int_key, Format)
{
schema::Test message;
BOOST_TEST_MESSAGE(message.DebugString());
schema::KeyInt message;
message.set_int_key(17526); // TODO Should be random
MessageTreeNode tree = marshaller->Marshal(message);
BOOST_TEST_MESSAGE(tree.DebugString());
DEBUG_MESSAGE
BOOST_TEST(std::to_string(message.int_key()) == tree.message().key().value());
}
BOOST_FIXTURE_TEST_CASE(string_key, Format)
{
schema::KeyStr message;
message.set_string_key("hp9gh3bv3wgqq"); // TODO Should be random
DEBUG_MESSAGE
BOOST_TEST(message.string_key() == tree.message().key().value());
}
BOOST_FIXTURE_TEST_CASE(repeated_int_key, Format)
{
schema::RepeatedKeyInt message;
message.add_int_key_repeated(29813);
message.add_int_key_repeated(465560);
message.add_int_key_repeated(8746);
DEBUG_MESSAGE
}
BOOST_FIXTURE_TEST_CASE(nonkeyable_nested, Format)
{
schema::NonKeyableNested message;
message.set_int_key(98125489);
message.mutable_nested_nonkeyable_message()->set_boolean(true);
DEBUG_MESSAGE
}
#undef DEBUG_MESSAGE
......@@ -6,29 +6,23 @@ import "profanedb/protobuf/options.proto";
import "nested.proto";
message Test {
int32 field_one_int = 1 [ (profanedb.protobuf.options).key = true ];
string field_two_str = 2;
bool field_three_bool = 3;
Nested field_five_nested = 5;
bytes field_four_bytes = 4;
ExternalNested field_six_externalnested = 6;
Nested.KeyableNestedInNested field_seven_repeated_keyable = 7;
message KeyInt {
int32 int_key = 1 [ (profanedb.protobuf.options).key = true ];
}
message Nested {
string nested_field_one_str = 1 [ (profanedb.protobuf.options).key = true ];
int64 nested_field_two_int = 2;
double nested_field_three_double = 3;
message KeyStr {
string string_key = 1 [ (profanedb.protobuf.options).key = true ];
}
message KeyableNestedInNested {
repeated string nested_in_nested_field_one_repeated_str = 1 [ (profanedb.protobuf.options).key = true ];
}
message RepeatedKeyInt {
repeated int32 int_key_repeated = 1 [ (profanedb.protobuf.options).key = true ];
}
// message NotKeyableNestedInNested {
// int32 nested_in_nested_field_one_int = 1;
// }
message NonKeyableNested {
int32 int_key = 1 [ (profanedb.protobuf.options).key = true ];
message Nested {
bool boolean = 1;
}
Nested nested_nonkeyable_message = 2;
}
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