1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | * |
16 | * \author guonix |
17 | * \date Nov 2020 |
18 | * \brief |
19 | */ |
20 | |
21 | #pragma once |
22 | |
23 | #include "executor/executor.h" |
24 | #include "context.h" |
25 | #include "forward_serializer.h" |
26 | #include "meta_wrapper.h" |
27 | #include "query.h" |
28 | |
29 | namespace proxima { |
30 | namespace be { |
31 | namespace query { |
32 | |
33 | /*! |
34 | * Implementation of Context |
35 | */ |
36 | class ContextImpl : public Query, public CollectionQueryContext { |
37 | public: |
38 | //! Constructor |
39 | ContextImpl(uint64_t traceID, index::IndexServicePtr index_service, |
40 | MetaWrapperPtr meta, ProfilerPtr profiler, ExecutorPtr executor); |
41 | |
42 | //! Destructor |
43 | ~ContextImpl() override = default; |
44 | |
45 | public: |
46 | //! Unique request id, using trace all relevant information |
47 | uint64_t id() const override; |
48 | |
49 | //! Validate query object, 0 for valid, otherwise non zero returned |
50 | int validate() const override; |
51 | |
52 | //! Get Profiler |
53 | ProfilerPtr profiler() const override; |
54 | |
55 | protected: |
56 | //! Retrieve meta handler |
57 | MetaWrapperPtr meta() const; |
58 | |
59 | //! Retrieve executor |
60 | ExecutorPtr executor() const; |
61 | |
62 | //! List segment under current collection |
63 | int list_segments(index::SegmentPtrList *segments); |
64 | |
65 | //! Fill forward field |
66 | int fill_forward(const index::QueryResult &forward, proto::Document *doc); |
67 | |
68 | //! Get forward columns |
69 | ColumnNameList *get_forward_columns(const index::QueryResult &forward); |
70 | |
71 | //! Check valid executor |
72 | bool valid_executor() const { |
73 | return executor_ != nullptr; |
74 | } |
75 | |
76 | private: |
77 | //! Trace ID |
78 | uint64_t trace_id_{0}; |
79 | |
80 | //! Index Service Handler |
81 | index::IndexServicePtr index_service_{nullptr}; |
82 | |
83 | //! Schema Validator Handler |
84 | MetaWrapperPtr meta_{nullptr}; |
85 | |
86 | //! Executor |
87 | ExecutorPtr executor_{nullptr}; |
88 | |
89 | //! Profiler handler |
90 | ProfilerPtr profiler_{nullptr}; |
91 | |
92 | //! revision map |
93 | std::unordered_map<uint64_t, ColumnNameList> revision_to_forward_columns_; |
94 | }; |
95 | |
96 | |
97 | /*! |
98 | * Implementation of CollectionQuery |
99 | */ |
100 | class CollectionQuery : public ContextImpl, public QueryContext { |
101 | public: |
102 | //! Constructor |
103 | CollectionQuery(uint64_t traceID, const proto::QueryRequest *request, |
104 | index::IndexServicePtr index_service, MetaWrapperPtr meta, |
105 | ExecutorPtr executor, ProfilerPtr profiler, |
106 | proto::QueryResponse *response); |
107 | |
108 | //! Destructor |
109 | ~CollectionQuery() override; |
110 | |
111 | public: |
112 | //! Validate query object, 0 for valid, otherwise non zero returned |
113 | int validate() const override; |
114 | |
115 | //! Retrieve the PB request of query |
116 | const proto::QueryRequest *request() const override; |
117 | |
118 | //! Retrieve the PB response of query |
119 | const proto::QueryResponse *response() const override; |
120 | |
121 | //! Retrieve the mutable PB response of query |
122 | proto::QueryResponse *mutable_response() override; |
123 | |
124 | //! Retrieve collection name |
125 | const std::string &collection() const override; |
126 | |
127 | protected: |
128 | //! Check valid response |
129 | bool valid_response() const { |
130 | return response_; |
131 | } |
132 | |
133 | private: |
134 | //! Readonly QueryRequest Handler |
135 | const proto::QueryRequest *request_{nullptr}; |
136 | |
137 | //! Response handler |
138 | proto::QueryResponse *response_{nullptr}; |
139 | }; |
140 | |
141 | |
142 | } // namespace query |
143 | } // namespace be |
144 | } // namespace proxima |
145 | |