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 Hechong.xyf |
17 | * \date Oct 2019 |
18 | * \brief Interface of AiTheta Index Converter |
19 | */ |
20 | |
21 | #ifndef __AITHETA2_INDEX_CONVERTER_H__ |
22 | #define __AITHETA2_INDEX_CONVERTER_H__ |
23 | |
24 | #include "index_dumper.h" |
25 | #include "index_holder.h" |
26 | #include "index_meta.h" |
27 | #include "index_stats.h" |
28 | |
29 | namespace aitheta2 { |
30 | |
31 | /*! Index Converter |
32 | */ |
33 | class IndexConverter : public IndexModule { |
34 | public: |
35 | //! Index Converter Pointer |
36 | typedef std::shared_ptr<IndexConverter> Pointer; |
37 | |
38 | /*! Index Converter Stats |
39 | */ |
40 | class Stats : public IndexStats { |
41 | public: |
42 | //! Set count of documents trained |
43 | void set_trained_count(size_t count) { |
44 | trained_count_ = count; |
45 | } |
46 | |
47 | //! Set count of documents transformed |
48 | void set_transformed_count(size_t count) { |
49 | transformed_count_ = count; |
50 | } |
51 | |
52 | //! Set size of documents dumped |
53 | void set_dumped_size(size_t size) { |
54 | dumped_size_ = size; |
55 | } |
56 | |
57 | //! Set count of documents discarded |
58 | void set_discarded_count(size_t count) { |
59 | discarded_count_ = count; |
60 | } |
61 | |
62 | //! Set time cost of documents trained |
63 | void set_trained_costtime(uint64_t cost) { |
64 | trained_costtime_ = cost; |
65 | } |
66 | |
67 | //! Set time cost of documents transformed |
68 | void set_transformed_costtime(uint64_t cost) { |
69 | transformed_costtime_ = cost; |
70 | } |
71 | |
72 | //! Set time cost of documents dumped |
73 | void set_dumped_costtime(uint64_t cost) { |
74 | dumped_costtime_ = cost; |
75 | } |
76 | |
77 | //! Retrieve count of documents trained |
78 | size_t trained_count(void) const { |
79 | return trained_count_; |
80 | } |
81 | |
82 | //! Retrieve count of documents transformed |
83 | size_t transformed_count(void) const { |
84 | return transformed_count_; |
85 | } |
86 | |
87 | //! Retrieve size of documents dumped |
88 | size_t dumped_size(void) const { |
89 | return dumped_size_; |
90 | } |
91 | |
92 | //! Retrieve count of documents discarded |
93 | size_t discarded_count(void) const { |
94 | return discarded_count_; |
95 | } |
96 | |
97 | //! Retrieve time cost of documents trained |
98 | uint64_t trained_costtime(void) const { |
99 | return trained_costtime_; |
100 | } |
101 | |
102 | //! Retrieve time cost of documents transformed |
103 | uint64_t transformed_costtime(void) const { |
104 | return transformed_costtime_; |
105 | } |
106 | |
107 | //! Retrieve time cost of documents dumped |
108 | uint64_t dumped_costtime(void) const { |
109 | return dumped_costtime_; |
110 | } |
111 | |
112 | //! Retrieve count of documents trained (mutable) |
113 | size_t *mutable_trained_count(void) { |
114 | return &trained_count_; |
115 | } |
116 | |
117 | //! Retrieve count of documents transformed (mutable) |
118 | size_t *mutable_transformed_count(void) { |
119 | return &transformed_count_; |
120 | } |
121 | |
122 | //! Retrieve size of documents dumped (mutable) |
123 | size_t *mutable_dumped_size(void) { |
124 | return &dumped_size_; |
125 | } |
126 | |
127 | //! Retrieve count of documents discarded (mutable) |
128 | size_t *mutable_discarded_count(void) { |
129 | return &discarded_count_; |
130 | } |
131 | |
132 | //! Retrieve time cost of documents trained (mutable) |
133 | uint64_t *mutable_trained_costtime(void) { |
134 | return &trained_costtime_; |
135 | } |
136 | |
137 | //! Retrieve time cost of documents transformed (mutable) |
138 | uint64_t *mutable_transformed_costtime(void) { |
139 | return &transformed_costtime_; |
140 | } |
141 | |
142 | //! Retrieve time cost of documents dumped (mutable) |
143 | uint64_t *mutable_dumped_costtime(void) { |
144 | return &dumped_costtime_; |
145 | } |
146 | |
147 | private: |
148 | //! Members |
149 | size_t trained_count_{0u}; |
150 | size_t transformed_count_{0u}; |
151 | size_t dumped_size_{0u}; |
152 | size_t discarded_count_{0u}; |
153 | uint64_t trained_costtime_{0u}; |
154 | uint64_t transformed_costtime_{0u}; |
155 | uint64_t dumped_costtime_{0u}; |
156 | }; |
157 | |
158 | //! Destructor |
159 | virtual ~IndexConverter(void) {} |
160 | |
161 | //! Initialize Converter |
162 | virtual int init(const IndexMeta &meta, const IndexParams ¶ms) = 0; |
163 | |
164 | //! Cleanup Converter |
165 | virtual int cleanup(void) = 0; |
166 | |
167 | //! Train the data |
168 | virtual int train(IndexHolder::Pointer holder) = 0; |
169 | |
170 | //! Transform the data |
171 | virtual int transform(IndexHolder::Pointer holder) = 0; |
172 | |
173 | //! Dump index into storage |
174 | virtual int dump(const IndexDumper::Pointer &dumper) = 0; |
175 | |
176 | //! Retrieve statistics |
177 | virtual const Stats &stats(void) const = 0; |
178 | |
179 | //! Retrieve a holder as result |
180 | virtual IndexHolder::Pointer result(void) const = 0; |
181 | |
182 | //! Retrieve Index Meta |
183 | virtual const IndexMeta &meta(void) const = 0; |
184 | |
185 | //! Train and transform the index |
186 | static int TrainAndTransform(const IndexConverter::Pointer &converter, |
187 | IndexHolder::Pointer holder); |
188 | |
189 | //! Train, transform and dump the index |
190 | static int TrainTransformAndDump(const IndexConverter::Pointer &converter, |
191 | IndexHolder::Pointer holder, |
192 | const IndexDumper::Pointer &dumper); |
193 | }; |
194 | |
195 | } // namespace aitheta2 |
196 | |
197 | #endif // __AITHETA2_INDEX_CONVERTER_H__ |
198 | |