Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <exception> |
21 | | #include <memory> |
22 | | #include <string> |
23 | | |
24 | | namespace lucene { |
25 | | namespace store { |
26 | | class Directory; |
27 | | } // namespace store |
28 | | |
29 | | namespace index { |
30 | | class Term; |
31 | | class TermDocs; |
32 | | class TermPositions; |
33 | | class IndexReader; |
34 | | } // namespace index |
35 | | } // namespace lucene |
36 | | |
37 | | class CLuceneError; |
38 | | |
39 | | namespace doris { |
40 | | class Status; |
41 | | } // namespace doris |
42 | | |
43 | | namespace doris::segment_v2 { |
44 | | |
45 | | struct DirectoryDeleter { |
46 | | void operator()(lucene::store::Directory* p) const; |
47 | | }; |
48 | | |
49 | | struct TermDeleter { |
50 | | void operator()(lucene::index::Term* p) const; |
51 | | }; |
52 | | using TermPtr = std::unique_ptr<lucene::index::Term, TermDeleter>; |
53 | | |
54 | | template <typename... Args> |
55 | | TermPtr make_term_ptr(Args&&... args); |
56 | | |
57 | | struct CLuceneDeleter { |
58 | | void operator()(lucene::index::TermDocs* p) const; |
59 | | }; |
60 | | using TermDocsPtr = std::unique_ptr<lucene::index::TermDocs, CLuceneDeleter>; |
61 | | using TermPositionsPtr = std::unique_ptr<lucene::index::TermPositions, CLuceneDeleter>; |
62 | | |
63 | | template <typename... Args> |
64 | | TermDocsPtr make_term_doc_ptr(lucene::index::IndexReader* reader, Args&&... args); |
65 | | |
66 | | template <typename... Args> |
67 | | TermPositionsPtr make_term_positions_ptr(lucene::index::IndexReader* reader, Args&&... args); |
68 | | |
69 | | // Converts the current exception at a CLucene boundary after local resources have unwound. |
70 | | Status clucene_error_status(std::string message); |
71 | | |
72 | | struct ErrorContext { |
73 | | std::string err_msg; |
74 | | std::exception_ptr eptr; |
75 | | }; |
76 | | |
77 | | template <typename T> |
78 | | concept HasClose = requires(T t) { |
79 | | { t->close() }; |
80 | | }; |
81 | | |
82 | | template <typename PtrType> |
83 | | requires HasClose<PtrType> |
84 | | void finally_close(PtrType& resource, ErrorContext& error_context); |
85 | | |
86 | | #if defined(__clang__) |
87 | | #pragma clang diagnostic push |
88 | | #pragma clang diagnostic ignored "-Wunused-macros" |
89 | | #endif |
90 | | |
91 | | #define FINALLY_CLOSE(resource) \ |
92 | 90 | { \ |
93 | 90 | static_assert(sizeof(error_context) > 0, \ |
94 | 90 | "error_context must be defined before using FINALLY macro!"); \ |
95 | 90 | finally_close(resource, error_context); \ |
96 | 90 | } |
97 | | |
98 | | // Return ERROR after finally |
99 | | #define FINALLY(finally_block) \ |
100 | 7.46k | { \ |
101 | 7.46k | static_assert(sizeof(error_context) > 0, \ |
102 | 7.46k | "error_context must be defined before using FINALLY macro!"); \ |
103 | 7.46k | finally_block; \ |
104 | 7.46k | if (error_context.eptr) { \ |
105 | 43 | return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(error_context.err_msg); \ |
106 | 43 | } \ |
107 | 3 | } |
108 | | |
109 | | // Re-throw the exception after finally |
110 | | #define FINALLY_EXCEPTION(finally_block) \ |
111 | 1.26k | { \ |
112 | 1.26k | static_assert(sizeof(error_context) > 0, \ |
113 | 1.26k | "error_context must be defined before using FINALLY macro!"); \ |
114 | 1.27k | finally_block; \ |
115 | 1.26k | if (error_context.eptr) { \ |
116 | 4 | std::rethrow_exception(error_context.eptr); \ |
117 | 4 | } \ |
118 | 3 | } |
119 | | |
120 | | #if defined(__clang__) |
121 | | #pragma clang diagnostic pop |
122 | | #endif |
123 | | |
124 | | } // namespace doris::segment_v2 |
125 | | |
126 | | #include "storage/index/index_common_impl.h" |