blob: 69ca868eef508e1e9b81cedeadd4bd8e934aeb39 [file]
/*
* Copyright (c) 2019-2022 ExpoLab, UC Davis
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#pragma once
#include <memory>
#include "chain/storage/storage.h"
#include "platform/proto/resdb.pb.h"
namespace resdb {
/*
* TransactionManager is an interface to manager the transactions that have been
* committed by the replicas. ExecuteData will be called inside ExecuteBatch to
* execute each transaction. One of the interface ExecuteBatch or ExecuteData
* should be implemented, but not both.
*/
class TransactionManager {
public:
TransactionManager(bool is_out_of_order = false, bool need_response = true);
virtual ~TransactionManager() = default;
virtual std::unique_ptr<BatchUserResponse> ExecuteBatch(
const BatchUserRequest& request);
virtual std::unique_ptr<
std::vector<std::unique_ptr<google::protobuf::Message>>>
Prepare(const BatchUserRequest& request);
std::vector<std::unique_ptr<std::string>> ExecuteBatchData(
const std::vector<std::unique_ptr<google::protobuf::Message>>& requests);
std::vector<std::unique_ptr<std::string>> ExecuteBatchData(
const BatchUserRequest& request);
std::unique_ptr<BatchUserResponse> ExecutePreparedData(
const std::vector<std::unique_ptr<google::protobuf::Message>>& requests);
virtual std::unique_ptr<BatchUserResponse> ExecutePreparedData(
const BatchUserRequest& batch_request) {
return nullptr;
}
virtual std::unique_ptr<std::string> ExecuteData(const std::string& request);
virtual void SetAsyncCallback(
std::function<void(const BatchUserRequest, std::unique_ptr<Request>,
std::unique_ptr<BatchUserResponse>)>
func);
virtual void Attach(std::unique_ptr<Request> request) {
request_ = std::move(request);
}
virtual std::unique_ptr<Request> FetchRequest() {
return std::move(request_);
}
bool IsOutOfOrder();
bool NeedResponse();
virtual Storage* GetStorage() { return nullptr; };
protected:
virtual std::unique_ptr<google::protobuf::Message> ParseData(
const std::string& data);
virtual std::unique_ptr<std::string> ExecuteRequest(
const google::protobuf::Message& request);
std::function<void(const BatchUserRequest, std::unique_ptr<Request> request,
std::unique_ptr<BatchUserResponse>)>
call_back_;
std::unique_ptr<Request> request_;
private:
bool is_out_of_order_ = false;
bool need_response_ = true;
};
} // namespace resdb