Loading...
Searching...
No Matches
query.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
21
22#include <glog/logging.h>
23#include <unistd.h>
24
25namespace resdb {
26
27Query::Query(const ResDBConfig& config, MessageManager* message_manager,
28 std::unique_ptr<CustomQuery> executor)
29 : config_(config),
30 message_manager_(message_manager),
31 custom_query_executor_(std::move(executor)) {}
32
34
35int Query::ProcessGetReplicaState(std::unique_ptr<Context> context,
36 std::unique_ptr<Request> request) {
37 ReplicaState replica_state;
38 int ret = message_manager_->GetReplicaState(&replica_state);
39 if (ret == 0) {
40 if (context != nullptr && context->client != nullptr) {
41 ret = context->client->SendRawMessage(replica_state);
42 if (ret) {
43 LOG(ERROR) << "send resp" << replica_state.DebugString()
44 << " fail ret:" << ret;
45 }
46 }
47 }
48 return ret;
49}
50
51int Query::ProcessQuery(std::unique_ptr<Context> context,
52 std::unique_ptr<Request> request) {
53 QueryRequest query;
54 if (!query.ParseFromString(request->data())) {
55 LOG(ERROR) << "parse data fail";
56 return -2;
57 }
58 // LOG(ERROR) << "request:" << query.DebugString();
59
60 QueryResponse response;
61 for (uint64_t i = query.min_seq(); i <= query.max_seq(); ++i) {
62 Request* ret_request = message_manager_->GetRequest(i);
63 if (ret_request == nullptr) {
64 break;
65 }
66 Request* txn = response.add_transactions();
67 txn->set_data(ret_request->data());
68 txn->set_hash(ret_request->hash());
69 txn->set_seq(ret_request->seq());
70 txn->set_proxy_id(ret_request->proxy_id());
71 }
72
73 if (context != nullptr && context->client != nullptr) {
74 // LOG(ERROR) << "send response:" << response.DebugString();
75 int ret = context->client->SendRawMessage(response);
76 if (ret) {
77 LOG(ERROR) << "send resp fail ret:" << ret;
78 }
79 }
80 return 0;
81}
82
83int Query::ProcessCustomQuery(std::unique_ptr<Context> context,
84 std::unique_ptr<Request> request) {
85 if (custom_query_executor_ == nullptr) {
86 LOG(ERROR) << "no custom executor";
87 return -1;
88 }
89
90 std::unique_ptr<std::string> resp_str =
91 custom_query_executor_->Query(request->data());
92
93 CustomQueryResponse response;
94 if (resp_str != nullptr) {
95 response.set_resp_str(*resp_str);
96 }
97
98 if (context != nullptr && context->client != nullptr) {
99 int ret = context->client->SendRawMessage(response);
100 if (ret) {
101 LOG(ERROR) << "send resp fail ret:" << ret;
102 }
103 }
104 return 0;
105}
106
107} // namespace resdb
Request * GetRequest(uint64_t seq)
int GetReplicaState(ReplicaState *state)
virtual int ProcessGetReplicaState(std::unique_ptr< Context > context, std::unique_ptr< Request > request)
Definition query.cpp:35
std::unique_ptr< CustomQuery > custom_query_executor_
Definition query.h:45
virtual ~Query()
Definition query.cpp:33
Query(const ResDBConfig &config, MessageManager *message_manager, std::unique_ptr< CustomQuery > executor=nullptr)
Definition query.cpp:27
virtual int ProcessCustomQuery(std::unique_ptr< Context > context, std::unique_ptr< Request > request)
Definition query.cpp:83
virtual int ProcessQuery(std::unique_ptr< Context > context, std::unique_ptr< Request > request)
Definition query.cpp:51
MessageManager * message_manager_
Definition query.h:44
std::unique_ptr< MessageManager > message_manager_
std::unique_ptr< ResDBConfig > config_