Skip to content
Snippets Groups Projects

ENH: Allow insertion and deletion of single entities

Merged Florian Spreckelsen requested to merge f-insert into dev
All threads resolved!
Compare and Show latest version
2 files
+ 4
0
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 204
0
/*
* This file is a part of the CaosDB Project.
*
* Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
* Copyright (C) 2021 Florian Spreckelsen <f.spreckelsen@indiscale.com>
* Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "caosdb/entity.h"
#include "caosdb/entity/v1alpha1/main.pb.h" // for Parent, Arena::CreateMay...
#include "caosdb/protobuf_helper.h" // for get_arena
#include "google/protobuf/arena.h" // for Arena
namespace caosdb::entity {
using caosdb::entity::v1alpha1::IdResponse;
using ProtoParent = caosdb::entity::v1alpha1::Parent;
using ProtoProperty = caosdb::entity::v1alpha1::Property;
using ProtoEntity = caosdb::entity::v1alpha1::Entity;
using caosdb::utility::get_arena;
Parent::Parent() : wrapped(Parent::CreateProtoParent()) {
// TODO(fspreck) Re-enable once we have decided how to attach
// messages to parents.
// errors.wrapped = this->wrapped->mutable_errors();
// warnings.wrapped = this->wrapped->mutable_warnings();
// infos.wrapped = this->wrapped->mutable_infos();
}
auto Parent::CreateProtoParent() -> ProtoParent * {
return google::protobuf::Arena::CreateMessage<ProtoParent>(get_arena());
}
auto Parent::SetName(const std::string &name) -> void {
this->wrapped->set_name(name);
}
auto Parent::SetId(const std::string &id) -> void { this->wrapped->set_id(id); }
[[nodiscard]] auto Parent::GetId() const -> const std::string & {
return this->wrapped->id();
}
[[nodiscard]] auto Parent::GetName() const -> const std::string & {
return this->wrapped->name();
}
auto Parents::Append(const Parent &parent) -> void {
auto *destination = this->wrapped->Add();
destination->Swap(parent.wrapped);
// Clear the originally wrapped object and return it to the Arena
parent.wrapped->Clear();
// set the pointer to the new object which is owned by the RepeatedPtrField
parent.wrapped = destination;
}
Property::Property() : wrapped(Property::CreateProtoProperty()) {}
auto Property::CreateProtoProperty() -> ProtoProperty * {
return google::protobuf::Arena::CreateMessage<ProtoProperty>(get_arena());
}
[[nodiscard]] auto Property::GetId() const -> const std::string & {
return this->wrapped->id();
}
[[nodiscard]] auto Property::GetName() const -> const std::string & {
return this->wrapped->name();
}
[[nodiscard]] auto Property::GetDescription() const -> const std::string & {
return this->wrapped->description();
}
[[nodiscard]] auto Property::GetImportance() const -> const std::string & {
return this->wrapped->importance();
}
[[nodiscard]] auto Property::GetValue() const -> const std::string & {
return this->wrapped->value();
}
[[nodiscard]] auto Property::GetUnit() const -> const std::string & {
return this->wrapped->unit();
}
[[nodiscard]] auto Property::GetDatatype() const -> const std::string & {
return this->wrapped->datatype();
}
auto Property::SetId(const std::string &id) -> void {
this->wrapped->set_id(id);
}
auto Property::SetName(const std::string &name) -> void {
this->wrapped->set_name(name);
}
auto Property::SetImportance(const std::string &importance) -> void {
this->wrapped->set_importance(importance);
}
auto Property::SetValue(const std::string &value) -> void {
this->wrapped->set_value(value);
}
auto Property::SetUnit(const std::string &unit) -> void {
this->wrapped->set_unit(unit);
}
auto Property::SetDatatype(const std::string &datatype) -> void {
this->wrapped->set_datatype(datatype);
}
auto Properties::Append(const Property &property) -> void {
auto *destination = this->wrapped->Add();
destination->Swap(property.wrapped);
property.wrapped->Clear();
property.wrapped = destination;
}
[[nodiscard]] auto Entity::GetParents() const -> const Parents & {
return parents;
}
auto Entity::AppendParent(const Parent &parent) -> void {
this->parents.Append(parent);
}
[[nodiscard]] auto Entity::GetProperties() const -> const Properties & {
return properties;
}
auto Entity::AppendProperty(const Property &property) -> void {
this->properties.Append(property);
}
auto Entity::CreateProtoEntity() -> ProtoEntity * {
return google::protobuf::Arena::CreateMessage<ProtoEntity>(get_arena());
}
Entity::Entity() : wrapped(Entity::CreateProtoEntity()) {
properties.wrapped = this->wrapped->mutable_properties();
parents.wrapped = this->wrapped->mutable_parents();
errors.wrapped = this->wrapped->mutable_errors();
warnings.wrapped = this->wrapped->mutable_warnings();
infos.wrapped = this->wrapped->mutable_infos();
}
Entity::Entity(IdResponse *idResponse) : Entity() {
this->wrapped->set_id(idResponse->id());
this->wrapped->mutable_errors()->Swap(idResponse->mutable_entity_errors());
this->wrapped->mutable_warnings()->Swap(
idResponse->mutable_entity_warnings());
this->wrapped->mutable_infos()->Swap(idResponse->mutable_entity_infos());
}
auto Entity::SetId(const std::string &id) -> void { this->wrapped->set_id(id); }
auto Entity::SetVersionId(const std::string &id) -> void {
this->wrapped->mutable_version()->set_id(id);
}
auto Entity::CopyTo(ProtoEntity *target) -> void {
target->CopyFrom(*(this->wrapped));
}
auto Entity::SetRole(const std::string &role) -> void {
this->wrapped->set_role(role);
}
auto Entity::SetName(const std::string &name) -> void {
this->wrapped->set_name(name);
}
auto Entity::SetValue(const std::string &value) -> void {
this->wrapped->set_value(value);
}
auto Entity::SetUnit(const std::string &unit) -> void {
this->wrapped->set_unit(unit);
}
auto Entity::SetDatatype(const std::string &datatype) -> void {
this->wrapped->set_datatype(datatype);
}
} // namespace caosdb::entity
Loading