Commit 2cd8da31 authored by Giorgio Azzinnaro's avatar Giorgio Azzinnaro

basic key generation

parent 87ccc7bc
......@@ -30,21 +30,21 @@ void profanedb::server::Server::HandleRpcs()
server->Wait();
}
grpc::Status profanedb::server::Server::DbServiceImpl::Get(grpc::ServerContext* context, const profanedb::protobuf::GetReq* request, profanedb::protobuf::GetResp* response)
grpc::Status profanedb::server::Server::DbServiceImpl::Get(grpc::ServerContext * context, const profanedb::protobuf::GetReq * request, profanedb::protobuf::GetResp * response)
{
db.Get(*request);
return grpc::Status::OK;
}
grpc::Status profanedb::server::Server::DbServiceImpl::Put(grpc::ServerContext* context, const profanedb::protobuf::PutReq* request, profanedb::protobuf::PutResp* response)
grpc::Status profanedb::server::Server::DbServiceImpl::Put(grpc::ServerContext * context, const profanedb::protobuf::PutReq * request, profanedb::protobuf::PutResp * response)
{
db.Put(*request);
return grpc::Status::OK;
}
grpc::Status profanedb::server::Server::DbServiceImpl::Delete(grpc::ServerContext* context, const profanedb::protobuf::DelReq* request, profanedb::protobuf::DelResp* response)
grpc::Status profanedb::server::Server::DbServiceImpl::Delete(grpc::ServerContext * context, const profanedb::protobuf::DelReq * request, profanedb::protobuf::DelResp * response)
{
db.Delete(*request);
......
......@@ -9,11 +9,10 @@
#include <profanedb/protobuf/db.pb.h>
#include <profanedb/protobuf/db.grpc.pb.h>
using profanedb::protobuf::Db;
namespace profanedb {
namespace server {
// This is a thin layer to use ProfaneDB with gRPC
class Server
{
public:
......@@ -27,7 +26,7 @@ private:
std::unique_ptr<grpc::Server> server;
class DbServiceImpl : public Db::Service {
class DbServiceImpl : public profanedb::protobuf::Db::Service {
public:
grpc::Status Get(grpc::ServerContext * context, const profanedb::protobuf::GetReq * request, profanedb::protobuf::GetResp* response) override;
......
......@@ -7,13 +7,17 @@ profanedb::storage::Db::Db()
rocksdb::DB::Open(options, "/tmp/profanedb", &db);
}
profanedb::storage::Db::~Db()
{
}
profanedb::protobuf::GetResp profanedb::storage::Db::Get(const profanedb::protobuf::GetReq & request)
{
}
profanedb::protobuf::PutResp profanedb::storage::Db::Put(const profanedb::protobuf::PutReq & request)
{
parser.ParseMessage(request.serializable());
std::cout << parser.ParseMessage(request.serializable()) << std::endl;
}
profanedb::protobuf::DelResp profanedb::storage::Db::Delete(const profanedb::protobuf::DelReq & request)
......
#ifndef DB_H
#define DB_H
#include <iostream>
#include "parser.h"
#include <rocksdb/db.h>
......@@ -10,10 +12,12 @@
namespace profanedb {
namespace storage {
// Db should be the main interface when embedding ProfaneDB
class Db
{
public:
Db();
~Db();
protobuf::GetResp Get(const protobuf::GetReq & request);
protobuf::PutResp Put(const protobuf::PutReq & request);
......
......@@ -24,34 +24,74 @@ profanedb::storage::Parser::~Parser()
{
}
void profanedb::storage::Parser::ParseMessage(const Any& serializable)
std::string profanedb::storage::Parser::ParseMessage(const Any & serializable)
{
std::string type = serializable.type_url();
const Descriptor* definition = pool->FindMessageTypeByName(type.substr(type.rfind('/')+1, string::npos));
const FieldDescriptor* fd;
const Descriptor * definition = pool->FindMessageTypeByName(type.substr(type.rfind('/')+1, string::npos));
const FieldDescriptor * fd;
// TODO Should check for multiple keys (might be supported later), for now throw error
for (int idx = 0; idx < definition->field_count(); idx++) {
fd = definition->field(idx);
if (fd->options().GetExtension(profanedb::protobuf::options).key()) {
std::cout << fd->full_name() << " is key" << std::endl;
break;
}
}
Message* container = messageFactory.GetPrototype(definition)->New();
Message * container = messageFactory.GetPrototype(definition)->New();
serializable.UnpackTo(container);
std::cout << container->GetReflection()->GetInt32(*container, fd) << std::endl;
return definition->full_name() + "$" + FieldToString(container, fd);
}
std::string profanedb::storage::Parser::FieldToString(const google::protobuf::Message * container, const google::protobuf::FieldDescriptor * fd)
{
const Reflection * reflection = container->GetReflection();
std::string field_bytes;
switch (fd->cpp_type()) {
case FieldDescriptor::CPPTYPE_INT32:
field_bytes = std::to_string(reflection->GetInt32(*container, fd));
break;
case FieldDescriptor::CPPTYPE_INT64:
field_bytes = std::to_string(reflection->GetInt64(*container, fd));
break;
case FieldDescriptor::CPPTYPE_UINT32:
field_bytes = std::to_string(reflection->GetUInt32(*container, fd));
break;
case FieldDescriptor::CPPTYPE_UINT64:
field_bytes = std::to_string(reflection->GetUInt64(*container, fd));
break;
case FieldDescriptor::CPPTYPE_DOUBLE:
field_bytes = std::to_string(reflection->GetDouble(*container, fd));
break;
case FieldDescriptor::CPPTYPE_FLOAT:
field_bytes = std::to_string(reflection->GetFloat(*container, fd));
break;
case FieldDescriptor::CPPTYPE_BOOL:
field_bytes = std::to_string(reflection->GetBool(*container, fd));
break;
case FieldDescriptor::CPPTYPE_ENUM:
field_bytes = std::to_string(reflection->GetEnum(*container, fd)->index());
break;
case FieldDescriptor::CPPTYPE_STRING:
field_bytes = reflection->GetString(*container, fd);
break;
case FieldDescriptor::CPPTYPE_MESSAGE:
// TODO Normalize data here
break;
}
return field_bytes;
}
profanedb::storage::Parser::ErrorCollector::ErrorCollector()
{
}
void profanedb::storage::Parser::ErrorCollector::AddError(const string& filename, int line, int column, const string& message)
void profanedb::storage::Parser::ErrorCollector::AddError(const string & filename, int line, int column, const string & message)
{
if (line == -1) { // Entire file error
std::cerr << filename << " error: " << message << "\n";
......@@ -60,7 +100,7 @@ void profanedb::storage::Parser::ErrorCollector::AddError(const string& filename
}
}
void profanedb::storage::Parser::ErrorCollector::AddWarning(const string& filename, int line, int column, const string& message)
void profanedb::storage::Parser::ErrorCollector::AddWarning(const string & filename, int line, int column, const string & message)
{
std::cerr << filename << " " << line+1 << ":" << column+1 << " warning: " << message << "\n";
}
......@@ -23,8 +23,7 @@ public:
Parser();
~Parser();
void ParseMessage(const Any & serializable);
std::string ParseMessage(const Any & serializable);
private:
io::ZeroCopyInputStream * inputStream;
compiler::DiskSourceTree sourceTree;
......@@ -34,6 +33,8 @@ private:
DynamicMessageFactory messageFactory;
std::string FieldToString(const Message * container, const FieldDescriptor * fd);
class ErrorCollector : public compiler::MultiFileErrorCollector {
public:
ErrorCollector();
......
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