Skip to content
Snippets Groups Projects
Verified Commit 89f9959c authored by Timm Fitschen's avatar Timm Fitschen
Browse files

Merge branch 'f-multi-retrieve' into f-update

parents b5cd8f46 2fc9ec2b
Branches
No related tags found
1 merge request!6F update
Pipeline #11419 failed
This commit is part of merge request !6. Comments created here will be created in the context of that merge request.
...@@ -299,7 +299,7 @@ if(_LINTING) ...@@ -299,7 +299,7 @@ if(_LINTING)
else() else()
message(STATUS "clang-tidy: ${clang_tidy}") message(STATUS "clang-tidy: ${clang_tidy}")
set(_CMAKE_CXX_CLANG_TIDY_CHECKS set(_CMAKE_CXX_CLANG_TIDY_CHECKS
"--checks=*,-fuchsia-*,-llvmlibc-*,-readability-convert-member-functions-to-static,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-hicpp-no-array-decay,-llvm-else-after-return,-readability-else-after-return,-modernize-use-trailing-return-type") "--checks=*,-fuchsia-*,-llvmlibc-*,-readability-convert-member-functions-to-static,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-hicpp-no-array-decay,-llvm-else-after-return,-readability-else-after-return,-modernize-use-trailing-return-type,-bugprone-branch-clone")
set(_CMAKE_C_CLANG_TIDY_CHECKS "${_CMAKE_CXX_CLANG_TIDY_CHECKS}") set(_CMAKE_C_CLANG_TIDY_CHECKS "${_CMAKE_CXX_CLANG_TIDY_CHECKS}")
set(_CMAKE_CXX_CLANG_TIDY "${clang_tidy}" set(_CMAKE_CXX_CLANG_TIDY "${clang_tidy}"
"--header-filter=caosdb/.*[^\(\.pb\.h\)]$" "--header-filter=caosdb/.*[^\(\.pb\.h\)]$"
......
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
#include <string> #include <string>
namespace caosdb {
/** /**
* StatusCodes represent the status of this client, it's connections, * StatusCodes represent the status of this client, it's connections,
* configuration and so on. * configuration and so on.
...@@ -33,9 +35,6 @@ ...@@ -33,9 +35,6 @@
* GENERIC_TRANSACTION_ERROR indicates that *there are* errors in a * GENERIC_TRANSACTION_ERROR indicates that *there are* errors in a
* transaction). * transaction).
*/ */
namespace caosdb {
enum StatusCode { enum StatusCode {
READY = -4, READY = -4,
GO_ON = -3, GO_ON = -3,
......
...@@ -18,12 +18,8 @@ ...@@ -18,12 +18,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
* *
*/ */
#ifndef CAOSDB_TRANSACTION_H #ifndef CAOSDB_TRANSACTION_H
#define CAOSDB_TRANSACTION_H #define CAOSDB_TRANSACTION_H
/**
* @brief Creation and execution of transactions.
*/
#include "boost/log/core/record.hpp" // for record #include "boost/log/core/record.hpp" // for record
#include "boost/log/sources/record_ostream.hpp" // for basic_record_o... #include "boost/log/sources/record_ostream.hpp" // for basic_record_o...
#include "boost/preprocessor/seq/limits/enum_256.hpp" // for BOOST_PP_SEQ_E... #include "boost/preprocessor/seq/limits/enum_256.hpp" // for BOOST_PP_SEQ_E...
...@@ -37,11 +33,19 @@ ...@@ -37,11 +33,19 @@
#include "google/protobuf/util/json_util.h" // for MessageToJsonString, Jso... #include "google/protobuf/util/json_util.h" // for MessageToJsonString, Jso...
#include <stdexcept> #include <stdexcept>
#include <iterator> #include <iterator>
// IWYU pragma: no_include <ext/alloc_traits.h>
#include <memory> // for shared_ptr, unique_ptr #include <memory> // for shared_ptr, unique_ptr
#include <string> // for string #include <string> // for string
#include <vector> // for vector #include <vector> // for vector
/* /**
* @brief Creation and execution of transactions.
* @author Timm Fitschen
* @date 2021-08-05
*/
namespace caosdb::transaction {
/**
* Do all necessary checks and assure that another retrieval (by id or by * Do all necessary checks and assure that another retrieval (by id or by
* query) can be added as a sub-request to a transaction. * query) can be added as a sub-request to a transaction.
*/ */
...@@ -62,7 +66,7 @@ ...@@ -62,7 +66,7 @@
"wrong TransactionType.") \ "wrong TransactionType.") \
} }
/* /**
* Do all necessary checks and assure that another deletion can be added as a * Do all necessary checks and assure that another deletion can be added as a
* sub-request to a transaction. * sub-request to a transaction.
*/ */
...@@ -84,7 +88,7 @@ ...@@ -84,7 +88,7 @@
"wrong TransactionType.") \ "wrong TransactionType.") \
} }
/* /**
* Do all necessary checks and assure that another insertion can be added as a * Do all necessary checks and assure that another insertion can be added as a
* sub-request to a transaction. * sub-request to a transaction.
*/ */
...@@ -146,8 +150,13 @@ using caosdb::transaction::TransactionStatus; ...@@ -146,8 +150,13 @@ using caosdb::transaction::TransactionStatus;
using WrappedResponseCase = using WrappedResponseCase =
caosdb::entity::v1alpha1::TransactionResponse::WrappedResponseCase; caosdb::entity::v1alpha1::TransactionResponse::WrappedResponseCase;
class Transaction;
static const std::string logger_name = "caosdb::transaction"; static const std::string logger_name = "caosdb::transaction";
/**
* Abstract base class for the results of a Transaction.
*/
class ResultSet { class ResultSet {
public: public:
virtual ~ResultSet() = default; virtual ~ResultSet() = default;
...@@ -155,34 +164,16 @@ public: ...@@ -155,34 +164,16 @@ public:
[[nodiscard]] virtual auto At(const int index) const -> const Entity & = 0; [[nodiscard]] virtual auto At(const int index) const -> const Entity & = 0;
}; };
/**
* Container with results of a transaction.
*
* In contrast to UniqueResult, this one can also hold multiple entities or zero
* entities.
*/
class MultiResultSet : public ResultSet { class MultiResultSet : public ResultSet {
public: public:
~MultiResultSet() = default; ~MultiResultSet() = default;
explicit inline MultiResultSet(MultiTransactionResponse *response) { explicit MultiResultSet(MultiTransactionResponse *response);
auto responses = response->mutable_responses();
Entity *entity = nullptr;
for (auto sub_response : *responses) {
switch (sub_response.wrapped_response_case()) {
case WrappedResponseCase::kRetrieveResponse:
entity = new Entity(
sub_response.mutable_retrieve_response()->release_entity());
break;
case WrappedResponseCase::kInsertResponse:
entity = new Entity(sub_response.release_insert_response());
break;
case WrappedResponseCase::kDeleteResponse:
entity = new Entity(sub_response.release_delete_response());
break;
default:
// TODO(tf) Updates
break;
}
if (entity) {
this->entities.push_back(std::unique_ptr<Entity>(entity));
}
}
}
[[nodiscard]] inline auto Size() const noexcept -> int override { [[nodiscard]] inline auto Size() const noexcept -> int override {
return this->entities.size(); return this->entities.size();
} }
...@@ -193,6 +184,12 @@ public: ...@@ -193,6 +184,12 @@ public:
std::vector<std::unique_ptr<Entity>> entities; std::vector<std::unique_ptr<Entity>> entities;
}; };
/**
* Container with the single result of a transaction.
*
* In contrast to MultiResultSet, this one guarantees to hold exactly one
* entity.
*/
class UniqueResult : public ResultSet { class UniqueResult : public ResultSet {
public: public:
~UniqueResult() = default; ~UniqueResult() = default;
...@@ -219,14 +216,21 @@ private: ...@@ -219,14 +216,21 @@ private:
*/ */
class Transaction { class Transaction {
public: public:
/**
* The transaction type restricts the kind of sub-transaction which may be
* added to a transaction (insertion, update, deletion, retrieval).
*
* @note MIXED_READ_AND_WRITE and MIXED_WRITE transaction are not supported
* yet.
*/
enum TransactionType { enum TransactionType {
NONE, NONE, /// Unspecified or not specified yet.
READ_ONLY, READ_ONLY, /// Only retrievals (by id, by query)
INSERT, INSERT, /// Only insertions
UPDATE, UPDATE, /// Only updates
DELETE, DELETE, /// Only deletions
MIXED_WRITE, MIXED_WRITE, /// Only insertions, deletions, updates
MIXED_READ_AND_WRITE MIXED_READ_AND_WRITE /// all kind of transaction.
}; };
Transaction(std::shared_ptr<EntityTransactionService::Stub> service_stub); Transaction(std::shared_ptr<EntityTransactionService::Stub> service_stub);
......
...@@ -22,15 +22,6 @@ ...@@ -22,15 +22,6 @@
#ifndef CAOSDB_TRANSACTION_STATUS_H #ifndef CAOSDB_TRANSACTION_STATUS_H
#define CAOSDB_TRANSACTION_STATUS_H #define CAOSDB_TRANSACTION_STATUS_H
/**
* TransactionStatus indicates the current status of a transaction and, when it
* has already terminated, whether the transaction has been successful or not.
*
* A status code of 0 denotes a generic success state, positive values indicate
* errors, and negative values indicate other states, such as different stages
* of a transaction in process.
*/
#include "caosdb/status_code.h" #include "caosdb/status_code.h"
#include "caosdb/exceptions.h" #include "caosdb/exceptions.h"
#include <memory> // for shared_ptr, unique_ptr #include <memory> // for shared_ptr, unique_ptr
...@@ -44,7 +35,12 @@ using caosdb::exceptions::Exception; ...@@ -44,7 +35,12 @@ using caosdb::exceptions::Exception;
using caosdb::exceptions::TransactionError; using caosdb::exceptions::TransactionError;
/** /**
* Status of a Request or Transaction. * TransactionStatus indicates the current status of a transaction and, when it
* has already terminated, whether the transaction has been successful or not.
*
* A status code of 0 denotes a generic success state, positive values indicate
* errors, and negative values indicate other states, such as different stages
* of a transaction in process.
*/ */
class TransactionStatus { class TransactionStatus {
public: public:
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include <cstdlib> // for getenv #include <cstdlib> // for getenv
#include <cstring> // for strcmp #include <cstring> // for strcmp
#include <exception> // IWYU pragma: keep #include <exception> // IWYU pragma: keep
// IWYU pragma: no_include <bits/exception.h>
#include <grpcpp/security/credentials.h> // for SslCredentials #include <grpcpp/security/credentials.h> // for SslCredentials
#include <iterator> // for next #include <iterator> // for next
#include <map> // for map #include <map> // for map
......
...@@ -100,6 +100,29 @@ using grpc::ClientAsyncResponseReader; ...@@ -100,6 +100,29 @@ using grpc::ClientAsyncResponseReader;
using ProtoEntity = caosdb::entity::v1alpha1::Entity; using ProtoEntity = caosdb::entity::v1alpha1::Entity;
using grpc::CompletionQueue; using grpc::CompletionQueue;
MultiResultSet::MultiResultSet(MultiTransactionResponse *response) {
auto *responses = response->mutable_responses();
for (auto sub_response : *responses) {
switch (sub_response.wrapped_response_case()) {
case WrappedResponseCase::kRetrieveResponse:
this->entities.push_back(std::make_unique<Entity>(
sub_response.mutable_retrieve_response()->release_entity()));
break;
case WrappedResponseCase::kInsertResponse:
this->entities.push_back(
std::make_unique<Entity>(sub_response.release_insert_response()));
break;
case WrappedResponseCase::kDeleteResponse:
this->entities.push_back(
std::make_unique<Entity>(sub_response.release_insert_response()));
break;
default:
// TODO(tf) Updates
break;
}
}
}
[[nodiscard]] auto UniqueResult::GetEntity() const -> const Entity & { [[nodiscard]] auto UniqueResult::GetEntity() const -> const Entity & {
const Entity *result = this->entity.get(); const Entity *result = this->entity.get();
return *result; return *result;
...@@ -268,7 +291,7 @@ auto Transaction::WaitForIt() const noexcept -> TransactionStatus { ...@@ -268,7 +291,7 @@ auto Transaction::WaitForIt() const noexcept -> TransactionStatus {
this->result_set = std::make_unique<UniqueResult>(deletedIdResponse); this->result_set = std::make_unique<UniqueResult>(deletedIdResponse);
} break; } break;
default: default:
// TODO(tf) // TODO(tf) Error and Update
break; break;
} }
} else { } else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment