Debian Codegraph @

lib/token_column.cpp

raw symbols SCIP
1 /*
2 Copyright(C) 2020 Sutou Kouhei <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <cstring>
20
21 #include "grn.h"
22 #include "grn_str.h"
23 #include "grn_token_column.h"
24 #include "grn_token_cursor.h"
25
26 #ifdef GRN_WITH_APACHE_ARROW
27 # include "grn_arrow.hpp"
28 # include <arrow/util/thread_pool.h>
29 # include <mutex>
30 #endif
31
32 namespace grn {
33 namespace token_column {
34 static uint32_t parallel_chunk_size = 1024;
35 static uint32_t parallel_table_size_threshold = parallel_chunk_size * 10;
36
37 class Builder
38 {
39 public:
40 Builder(grn_ctx *ctx,
41 grn_obj *column)
42 : ctx_(ctx),
43 column_(column),
44 with_weight_(grn_column_get_flags(ctx_, column_) &
45 GRN_OBJ_WITH_WEIGHT),
46 table_(grn_ctx_at(ctx, column->header.domain)),
47 lexicon_(grn_ctx_at(ctx, DB_OBJ(column)->range)),
48 source_(NULL) {
49 grn_id *source_ids = static_cast<grn_id *>(DB_OBJ(column)->source);
50 source_ = grn_ctx_at(ctx, source_ids[0]);
51 }
52
53 ~Builder() {
54 grn_obj_unref(ctx_, source_);
55 grn_obj_unref(ctx_, lexicon_);
56 grn_obj_unref(ctx_, table_);
57 }
58
59 void
60 build() {
61 grn_obj_set_visibility(ctx_, column_, false);
62 if (use_parallel()) {
63 build_parallel();
64 } else {
65 build_sequential();
66 }
67 grn_obj_set_visibility(ctx_, column_, true);
68 }
69
70 private:
71 bool
72 use_parallel() {
73 #ifdef GRN_WITH_APACHE_ARROW
74 return grn_table_size(ctx_, table_) >= parallel_table_size_threshold;
75 #else
76 return false;
77 #endif
78 }
79
80 void
81 build_parallel() {
82 #ifdef GRN_WITH_APACHE_ARROW
83 std::mutex mutex;
84 auto pool = ::arrow::internal::GetCpuThreadPool();
85 uint32_t token_cursor_flags = GRN_TOKEN_CURSOR_PARALLEL;
86 grn_obj *db = grn_ctx_db(ctx_);
87 uint32_t chunk_size = parallel_chunk_size;
88 auto build_chunk = [&](std::vector<grn_id> ids) {
89 grn_ctx ctx;
90 grn_ctx_init(&ctx, 0);
91 grn_ctx_use(&ctx, db);
92 grn_obj tokens;
93 grn_obj value;
94 GRN_RECORD_INIT(&tokens, GRN_OBJ_VECTOR, DB_OBJ(lexicon_)->id);
95 if (with_weight_) {
96 tokens.header.flags |= GRN_OBJ_WITH_WEIGHT;
97 }
98 GRN_VOID_INIT(&value);
99 for (auto id : ids) {
100 GRN_BULK_REWIND(&tokens);
101 GRN_BULK_REWIND(&value);
102 grn_obj_get_value(&ctx, source_, id, &value);
103 if (GRN_TEXT_LEN(&value) > 0) {
104 auto token_cursor = grn_token_cursor_open(&ctx,
105 lexicon_,
106 GRN_BULK_HEAD(&value),
107 GRN_BULK_VSIZE(&value),
108 GRN_TOKEN_ADD,
109 token_cursor_flags);
110 if (token_cursor) {
111 grn_token_cursor_set_source_column(&ctx, token_cursor, source_);
112 grn_token_cursor_set_source_id(&ctx, token_cursor, id);
113 grn_token_cursor_set_index_column(&ctx, token_cursor, column_);
114 while (token_cursor->status == GRN_TOKEN_CURSOR_DOING) {
115 grn_id token_id = grn_token_cursor_next(&ctx, token_cursor);
116 if (token_id == GRN_ID_NIL) {
117 break;
118 }
119 if (with_weight_) {
120 grn_token *token =
121 grn_token_cursor_get_token(&ctx, token_cursor);
122 float weight = grn_token_get_weight(&ctx, token);
123 grn_uvector_add_element_record(&ctx,
124 &tokens,
125 token_id,
126 weight);
127 } else {
128 GRN_RECORD_PUT(&ctx, &tokens, token_id);
129 }
130 }
131 grn_token_cursor_close(&ctx, token_cursor);
132 }
133 }
134 grn_obj_set_value(&ctx, column_, id, &tokens, GRN_OBJ_SET);
135 if (ctx.rc != GRN_SUCCESS) {
136 break;
137 }
138 }
139 GRN_OBJ_FIN(&ctx, &value);
140 GRN_OBJ_FIN(&ctx, &tokens);
141 if (ctx.rc != GRN_SUCCESS) {
142 std::lock_guard<std::mutex> lock(mutex);
143 if (ctx_->rc == GRN_SUCCESS) {
144 ctx_->rc = ctx.rc;
145 ctx_->errlvl = ctx.errlvl;
146 ctx_->errfile = ctx.errfile;
147 ctx_->errline = ctx.errline;
148 ctx_->errfunc = ctx.errfunc;
149 grn_strcpy(ctx_->errbuf, GRN_CTX_MSGSIZE, ctx.errbuf);
150 }
151 }
152 grn_ctx_fin(&ctx);
153 return ::arrow::Status::OK();
154 };
155
156 std::vector<grn_id> ids;
157 std::vector<::arrow::Future<>> futures;
158
159 GRN_TABLE_EACH_BEGIN_FLAGS(ctx_, table_, cursor, id, GRN_CURSOR_BY_ID) {
160 ids.push_back(id);
161 if (ids.size() == chunk_size) {
162 auto future = pool->Submit(build_chunk, ids);
163 if (!grnarrow::check(ctx_,
164 future,
165 "[token-column][build][parallel] "
166 "failed to submit a job")) {
167 break;
168 }
169 futures.push_back(*future);
170 ids.clear();
171 }
172 } GRN_TABLE_EACH_END(ctx_, cursor);
173 if (ctx_->rc == GRN_SUCCESS && !ids.empty()) {
174 auto future = pool->Submit(build_chunk, ids);
175 if (grnarrow::check(ctx_,
176 future,
177 "[token-column][build][parallel] "
178 "failed to submit a job")) {
179 futures.push_back(*future);
180 }
181 }
182 auto status = ::arrow::Status::OK();
183 for (auto& future : futures) {
184 status &= future.status();
185 }
186 grnarrow::check(ctx_,
187 status,
188 "[token-column][build][parallel] "
189 "failed to complete a job");
190 #endif
191 }
192
193 void
194 build_sequential() {
195 grn_obj tokens;
196 grn_obj value;
197 GRN_RECORD_INIT(&tokens, GRN_OBJ_VECTOR, DB_OBJ(lexicon_)->id);
198 if (with_weight_) {
199 tokens.header.flags |= GRN_OBJ_WITH_WEIGHT;
200 }
201 GRN_VOID_INIT(&value);
202 const unsigned int token_flags = 0;
203 GRN_TABLE_EACH_BEGIN_FLAGS(ctx_, table_, cursor, id, GRN_CURSOR_BY_ID) {
204 GRN_BULK_REWIND(&tokens);
205 GRN_BULK_REWIND(&value);
206 grn_obj_get_value(ctx_, source_, id, &value);
207 if (GRN_BULK_VSIZE(&value) > 0) {
208 grn_token_cursor *token_cursor =
209 grn_token_cursor_open(ctx_,
210 lexicon_,
211 GRN_BULK_HEAD(&value),
212 GRN_BULK_VSIZE(&value),
213 GRN_TOKEN_ADD,
214 token_flags);
215 if (token_cursor) {
216 grn_token_cursor_set_source_column(ctx_, token_cursor, source_);
217 grn_token_cursor_set_source_id(ctx_, token_cursor, id);
218 grn_token_cursor_set_index_column(ctx_, token_cursor, column_);
219 while (token_cursor->status == GRN_TOKEN_CURSOR_DOING) {
220 grn_id token_id = grn_token_cursor_next(ctx_, token_cursor);
221 if (token_id == GRN_ID_NIL) {
222 break;
223 }
224 if (with_weight_) {
225 grn_token *token =
226 grn_token_cursor_get_token(ctx_, token_cursor);
227 float weight = grn_token_get_weight(ctx_, token);
228 grn_uvector_add_element_record(ctx_,
229 &tokens,
230 token_id,
231 weight);
232 } else {
233 GRN_RECORD_PUT(ctx_, &tokens, token_id);
234 }
235 }
236 grn_token_cursor_close(ctx_, token_cursor);
237 }
238 }
239 grn_obj_set_value(ctx_, column_, id, &tokens, GRN_OBJ_SET);
240 if (ctx_->rc != GRN_SUCCESS) {
241 break;
242 }
243 } GRN_TABLE_EACH_END(ctx_, cursor);
244 GRN_OBJ_FIN(ctx_, &value);
245 GRN_OBJ_FIN(ctx_, &tokens);
246 }
247
248 grn_ctx *ctx_;
249 grn_obj *column_;
250 bool with_weight_;
251 grn_obj *table_;
252 grn_obj *lexicon_;
253 grn_obj *source_;
254 };
255 }
256 }
257
258 extern "C" {
259 void
260 grn_token_column_init_from_env(void)
261 {
262 {
263 char grn_token_column_parallel_chunk_size_env[GRN_ENV_BUFFER_SIZE];
264 grn_getenv("GRN_TOKEN_COLUMN_PARALLEL_CHUNK_SIZE",
265 grn_token_column_parallel_chunk_size_env,
266 GRN_ENV_BUFFER_SIZE);
267 if (grn_token_column_parallel_chunk_size_env[0]) {
268 size_t env_len = strlen(grn_token_column_parallel_chunk_size_env);
269 uint32_t chunk_size =
270 grn_atoui(grn_token_column_parallel_chunk_size_env,
271 grn_token_column_parallel_chunk_size_env + env_len,
272 NULL);
273 if (chunk_size > 0) {
274 grn::token_column::parallel_chunk_size = chunk_size;
275 }
276 }
277 }
278
279 {
280 char grn_token_column_parallel_table_size_threshold_env[GRN_ENV_BUFFER_SIZE];
281 grn_getenv("GRN_TOKEN_COLUMN_PARALLEL_TABLE_SIZE_THRESHOLD",
282 grn_token_column_parallel_table_size_threshold_env,
283 GRN_ENV_BUFFER_SIZE);
284 if (grn_token_column_parallel_table_size_threshold_env[0]) {
285 size_t env_len =
286 strlen(grn_token_column_parallel_table_size_threshold_env);
287 uint32_t threshold =
288 grn_atoui(grn_token_column_parallel_table_size_threshold_env,
289 grn_token_column_parallel_table_size_threshold_env + env_len,
290 NULL);
291 if (threshold > 0) {
292 grn::token_column::parallel_table_size_threshold = threshold;
293 }
294 }
295 }
296 }
297
298 void
299 grn_token_column_update(grn_ctx *ctx,
300 grn_obj *column,
301 grn_id id,
302 int section,
303 grn_obj *old_value,
304 grn_obj *new_value)
305 {
306 grn_obj *lexicon = grn_ctx_at(ctx, DB_OBJ(column)->range);
307 grn_obj tokens;
308 GRN_RECORD_INIT(&tokens, GRN_OBJ_VECTOR, DB_OBJ(lexicon)->id);
309 if (GRN_TEXT_LEN(new_value) > 0) {
310 unsigned int token_flags = 0;
311 grn_token_cursor *token_cursor =
312 grn_token_cursor_open(ctx,
313 lexicon,
314 GRN_TEXT_VALUE(new_value),
315 GRN_TEXT_LEN(new_value),
316 GRN_TOKEN_ADD,
317 token_flags);
318 if (token_cursor) {
319 while (token_cursor->status == GRN_TOKEN_CURSOR_DOING) {
320 grn_id token_id = grn_token_cursor_next(ctx, token_cursor);
321 if (token_id == GRN_ID_NIL) {
322 break;
323 }
324 GRN_RECORD_PUT(ctx, &tokens, token_id);
325 }
326 grn_token_cursor_close(ctx, token_cursor);
327 }
328 }
329 grn_obj_set_value(ctx, column, id, &tokens, GRN_OBJ_SET);
330 GRN_OBJ_FIN(ctx, &tokens);
331 grn_obj_unref(ctx, lexicon);
332 }
333
334 void
335 grn_token_column_build(grn_ctx *ctx, grn_obj *column)
336 {
337 grn::token_column::Builder builder(ctx, column);
338 builder.build();
339 }
340 }