xref: /aosp_15_r20/external/icing/icing/icing-search-engine.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1*8b6cd535SAndroid Build Coastguard Worker // Copyright (C) 2019 Google LLC
2*8b6cd535SAndroid Build Coastguard Worker //
3*8b6cd535SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*8b6cd535SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*8b6cd535SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*8b6cd535SAndroid Build Coastguard Worker //
7*8b6cd535SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
8*8b6cd535SAndroid Build Coastguard Worker //
9*8b6cd535SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*8b6cd535SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*8b6cd535SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*8b6cd535SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*8b6cd535SAndroid Build Coastguard Worker // limitations under the License.
14*8b6cd535SAndroid Build Coastguard Worker 
15*8b6cd535SAndroid Build Coastguard Worker #ifndef ICING_ICING_SEARCH_ENGINE_H_
16*8b6cd535SAndroid Build Coastguard Worker #define ICING_ICING_SEARCH_ENGINE_H_
17*8b6cd535SAndroid Build Coastguard Worker 
18*8b6cd535SAndroid Build Coastguard Worker #include <cstdint>
19*8b6cd535SAndroid Build Coastguard Worker #include <memory>
20*8b6cd535SAndroid Build Coastguard Worker #include <string>
21*8b6cd535SAndroid Build Coastguard Worker #include <string_view>
22*8b6cd535SAndroid Build Coastguard Worker #include <unordered_set>
23*8b6cd535SAndroid Build Coastguard Worker #include <utility>
24*8b6cd535SAndroid Build Coastguard Worker #include <vector>
25*8b6cd535SAndroid Build Coastguard Worker 
26*8b6cd535SAndroid Build Coastguard Worker #include "icing/text_classifier/lib3/utils/base/status.h"
27*8b6cd535SAndroid Build Coastguard Worker #include "icing/text_classifier/lib3/utils/base/statusor.h"
28*8b6cd535SAndroid Build Coastguard Worker #include "icing/absl_ports/mutex.h"
29*8b6cd535SAndroid Build Coastguard Worker #include "icing/absl_ports/thread_annotations.h"
30*8b6cd535SAndroid Build Coastguard Worker #include "icing/feature-flags.h"
31*8b6cd535SAndroid Build Coastguard Worker #include "icing/file/filesystem.h"
32*8b6cd535SAndroid Build Coastguard Worker #include "icing/file/version-util.h"
33*8b6cd535SAndroid Build Coastguard Worker #include "icing/index/data-indexing-handler.h"
34*8b6cd535SAndroid Build Coastguard Worker #include "icing/index/embed/embedding-index.h"
35*8b6cd535SAndroid Build Coastguard Worker #include "icing/index/index.h"
36*8b6cd535SAndroid Build Coastguard Worker #include "icing/index/numeric/numeric-index.h"
37*8b6cd535SAndroid Build Coastguard Worker #include "icing/jni/jni-cache.h"
38*8b6cd535SAndroid Build Coastguard Worker #include "icing/join/join-children-fetcher.h"
39*8b6cd535SAndroid Build Coastguard Worker #include "icing/join/qualified-id-join-index.h"
40*8b6cd535SAndroid Build Coastguard Worker #include "icing/legacy/index/icing-filesystem.h"
41*8b6cd535SAndroid Build Coastguard Worker #include "icing/performance-configuration.h"
42*8b6cd535SAndroid Build Coastguard Worker #include "icing/proto/blob.pb.h"
43*8b6cd535SAndroid Build Coastguard Worker #include "icing/proto/debug.pb.h"
44*8b6cd535SAndroid Build Coastguard Worker #include "icing/proto/document.pb.h"
45*8b6cd535SAndroid Build Coastguard Worker #include "icing/proto/initialize.pb.h"
46*8b6cd535SAndroid Build Coastguard Worker #include "icing/proto/logging.pb.h"
47*8b6cd535SAndroid Build Coastguard Worker #include "icing/proto/optimize.pb.h"
48*8b6cd535SAndroid Build Coastguard Worker #include "icing/proto/persist.pb.h"
49*8b6cd535SAndroid Build Coastguard Worker #include "icing/proto/reset.pb.h"
50*8b6cd535SAndroid Build Coastguard Worker #include "icing/proto/schema.pb.h"
51*8b6cd535SAndroid Build Coastguard Worker #include "icing/proto/scoring.pb.h"
52*8b6cd535SAndroid Build Coastguard Worker #include "icing/proto/search.pb.h"
53*8b6cd535SAndroid Build Coastguard Worker #include "icing/proto/storage.pb.h"
54*8b6cd535SAndroid Build Coastguard Worker #include "icing/proto/usage.pb.h"
55*8b6cd535SAndroid Build Coastguard Worker #include "icing/query/query-terms.h"
56*8b6cd535SAndroid Build Coastguard Worker #include "icing/result/result-state-manager.h"
57*8b6cd535SAndroid Build Coastguard Worker #include "icing/schema/schema-store.h"
58*8b6cd535SAndroid Build Coastguard Worker #include "icing/scoring/scored-document-hit.h"
59*8b6cd535SAndroid Build Coastguard Worker #include "icing/store/blob-store.h"
60*8b6cd535SAndroid Build Coastguard Worker #include "icing/store/document-id.h"
61*8b6cd535SAndroid Build Coastguard Worker #include "icing/store/document-store.h"
62*8b6cd535SAndroid Build Coastguard Worker #include "icing/tokenization/language-segmenter.h"
63*8b6cd535SAndroid Build Coastguard Worker #include "icing/transform/normalizer.h"
64*8b6cd535SAndroid Build Coastguard Worker #include "icing/util/clock.h"
65*8b6cd535SAndroid Build Coastguard Worker 
66*8b6cd535SAndroid Build Coastguard Worker namespace icing {
67*8b6cd535SAndroid Build Coastguard Worker namespace lib {
68*8b6cd535SAndroid Build Coastguard Worker 
69*8b6cd535SAndroid Build Coastguard Worker // TODO(cassiewang) Top-level comments and links to design-doc.
70*8b6cd535SAndroid Build Coastguard Worker class IcingSearchEngine {
71*8b6cd535SAndroid Build Coastguard Worker  public:
72*8b6cd535SAndroid Build Coastguard Worker   // Note: It is only required to provide a pointer to a valid instance of
73*8b6cd535SAndroid Build Coastguard Worker   // JniCache if this instance needs to perform reverse-jni calls. Users on
74*8b6cd535SAndroid Build Coastguard Worker   // Linux and iOS should always provide a nullptr.
75*8b6cd535SAndroid Build Coastguard Worker   explicit IcingSearchEngine(
76*8b6cd535SAndroid Build Coastguard Worker       const IcingSearchEngineOptions& options,
77*8b6cd535SAndroid Build Coastguard Worker       std::unique_ptr<const JniCache> jni_cache = nullptr);
78*8b6cd535SAndroid Build Coastguard Worker 
79*8b6cd535SAndroid Build Coastguard Worker   // Calculates integrity checks and persists files to disk.
80*8b6cd535SAndroid Build Coastguard Worker   ~IcingSearchEngine();
81*8b6cd535SAndroid Build Coastguard Worker 
82*8b6cd535SAndroid Build Coastguard Worker   // Loads & verifies the contents previously indexed from disk and gets ready
83*8b6cd535SAndroid Build Coastguard Worker   // to handle read/write requests.
84*8b6cd535SAndroid Build Coastguard Worker   //
85*8b6cd535SAndroid Build Coastguard Worker   // WARNING: This is expected to be fast if Icing had a clean shutdown.
86*8b6cd535SAndroid Build Coastguard Worker   // Otherwise, it can take longer as it runs integrity checks and attempts
87*8b6cd535SAndroid Build Coastguard Worker   // to bring the index to a consistent state. If the data on disk is not
88*8b6cd535SAndroid Build Coastguard Worker   // consistent, it restores the state when PersistToDisk() was last called.
89*8b6cd535SAndroid Build Coastguard Worker   //
90*8b6cd535SAndroid Build Coastguard Worker   // TODO(cassiewang): We shouldn't return NOT_FOUND here, this is a symptom
91*8b6cd535SAndroid Build Coastguard Worker   // of some other error. We should return a broader error group, i.e. data
92*8b6cd535SAndroid Build Coastguard Worker   // inconsistency or something
93*8b6cd535SAndroid Build Coastguard Worker   //
94*8b6cd535SAndroid Build Coastguard Worker   // Returns:
95*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
96*8b6cd535SAndroid Build Coastguard Worker   //   DATA_LOSS if encountered any inconsistencies in data and had to restore
97*8b6cd535SAndroid Build Coastguard Worker   //     its state back to the last time PersistToDisk was called. Or if any
98*8b6cd535SAndroid Build Coastguard Worker   //     persisted data was lost and could not be recovered.
99*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL if any internal state was left in an inconsistent. The instance
100*8b6cd535SAndroid Build Coastguard Worker   //     of IcingSearchEngine is unusable if this happens. It's recommended to
101*8b6cd535SAndroid Build Coastguard Worker   //     clear the underlying directory provided in
102*8b6cd535SAndroid Build Coastguard Worker   //     IcingSearchEngineOptions.base_dir and reinitialize.
103*8b6cd535SAndroid Build Coastguard Worker   //   RESOURCE_EXHAUSTED if not enough storage space
104*8b6cd535SAndroid Build Coastguard Worker   //   NOT_FOUND if missing some internal data
105*8b6cd535SAndroid Build Coastguard Worker   InitializeResultProto Initialize() ICING_LOCKS_EXCLUDED(mutex_);
106*8b6cd535SAndroid Build Coastguard Worker 
107*8b6cd535SAndroid Build Coastguard Worker   // Specifies the schema to be applied on all Documents that are already
108*8b6cd535SAndroid Build Coastguard Worker   // stored as well as future documents. A schema can be 'invalid' and/or
109*8b6cd535SAndroid Build Coastguard Worker   // 'incompatible'. These are two independent concepts.
110*8b6cd535SAndroid Build Coastguard Worker   //
111*8b6cd535SAndroid Build Coastguard Worker   // An 'invalid' schema is one that is not constructed properly. For example,
112*8b6cd535SAndroid Build Coastguard Worker   // a PropertyConfigProto is missing the property name field. A schema can be
113*8b6cd535SAndroid Build Coastguard Worker   // 'invalid' even if there is no previously existing schema.
114*8b6cd535SAndroid Build Coastguard Worker   //
115*8b6cd535SAndroid Build Coastguard Worker   // An 'incompatible' schema is one that is incompatible with a previously
116*8b6cd535SAndroid Build Coastguard Worker   // existing schema. If there is no previously existing schema, then a new
117*8b6cd535SAndroid Build Coastguard Worker   // schema cannot be incompatible. An incompatible schema is one that
118*8b6cd535SAndroid Build Coastguard Worker   // invalidates pre-existing data. For example, a previously OPTIONAL field is
119*8b6cd535SAndroid Build Coastguard Worker   // now REQUIRED in the new schema, and pre-existing data is considered invalid
120*8b6cd535SAndroid Build Coastguard Worker   // against the new schema now.
121*8b6cd535SAndroid Build Coastguard Worker   //
122*8b6cd535SAndroid Build Coastguard Worker   // Default behavior will not allow a new schema to be set if it is invalid or
123*8b6cd535SAndroid Build Coastguard Worker   // incompatible.
124*8b6cd535SAndroid Build Coastguard Worker   //
125*8b6cd535SAndroid Build Coastguard Worker   // The argument 'ignore_errors_and_delete_documents' can be set to true to
126*8b6cd535SAndroid Build Coastguard Worker   // force set an incompatible schema. In that case, documents that are
127*8b6cd535SAndroid Build Coastguard Worker   // invalidated by the new schema would be deleted from Icing. This cannot be
128*8b6cd535SAndroid Build Coastguard Worker   // used to force set an invalid schema.
129*8b6cd535SAndroid Build Coastguard Worker   //
130*8b6cd535SAndroid Build Coastguard Worker   // This schema is persisted to disk and used across multiple instances.
131*8b6cd535SAndroid Build Coastguard Worker   // So, callers should only have to call this if the schema changed.
132*8b6cd535SAndroid Build Coastguard Worker   // However, calling it multiple times with the same schema is a no-op.
133*8b6cd535SAndroid Build Coastguard Worker   //
134*8b6cd535SAndroid Build Coastguard Worker   // On some errors, Icing will keep using the older schema, but on
135*8b6cd535SAndroid Build Coastguard Worker   // INTERNAL_ERROR, it is undefined to continue using Icing.
136*8b6cd535SAndroid Build Coastguard Worker   //
137*8b6cd535SAndroid Build Coastguard Worker   // Returns:
138*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
139*8b6cd535SAndroid Build Coastguard Worker   //   ALREADY_EXISTS if 'new_schema' contains multiple definitions of the same
140*8b6cd535SAndroid Build Coastguard Worker   //     type or contains a type that has multiple properties with the same
141*8b6cd535SAndroid Build Coastguard Worker   //     name.
142*8b6cd535SAndroid Build Coastguard Worker   //   INVALID_ARGUMENT if 'new_schema' is invalid
143*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION if 'new_schema' is incompatible, or IcingSearchEngine
144*8b6cd535SAndroid Build Coastguard Worker   //     has not been initialized yet.
145*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR if Icing failed to store the new schema or upgrade
146*8b6cd535SAndroid Build Coastguard Worker   //     existing data based on the new schema. Using Icing beyond this error is
147*8b6cd535SAndroid Build Coastguard Worker   //     undefined and may cause crashes.
148*8b6cd535SAndroid Build Coastguard Worker   //   DATA_LOSS_ERROR if 'new_schema' requires the index to be rebuilt and an
149*8b6cd535SAndroid Build Coastguard Worker   //     IO error leads to some documents being excluded from the index. These
150*8b6cd535SAndroid Build Coastguard Worker   //     documents will still be retrievable via Get, but won't match queries.
151*8b6cd535SAndroid Build Coastguard Worker   //
152*8b6cd535SAndroid Build Coastguard Worker   // TODO(cassiewang) Figure out, document (and maybe even enforce) the best
153*8b6cd535SAndroid Build Coastguard Worker   // way ordering of calls between Initialize() and SetSchema(), both when
154*8b6cd535SAndroid Build Coastguard Worker   // the caller is creating an instance of IcingSearchEngine for the first
155*8b6cd535SAndroid Build Coastguard Worker   // time and when the caller is reinitializing an existing index on disk.
156*8b6cd535SAndroid Build Coastguard Worker   SetSchemaResultProto SetSchema(
157*8b6cd535SAndroid Build Coastguard Worker       SchemaProto&& new_schema, bool ignore_errors_and_delete_documents = false)
158*8b6cd535SAndroid Build Coastguard Worker       ICING_LOCKS_EXCLUDED(mutex_);
159*8b6cd535SAndroid Build Coastguard Worker 
160*8b6cd535SAndroid Build Coastguard Worker   // This function makes a copy of the schema and calls SetSchema(SchemaProto&&
161*8b6cd535SAndroid Build Coastguard Worker   // new_schema, bool ignore_errors_and_delete_documents)
162*8b6cd535SAndroid Build Coastguard Worker   //
163*8b6cd535SAndroid Build Coastguard Worker   // NOTE: It's recommended to call SetSchema(SchemaProto&& new_schema, bool
164*8b6cd535SAndroid Build Coastguard Worker   // ignore_errors_and_delete_documents) directly to avoid a copy if the caller
165*8b6cd535SAndroid Build Coastguard Worker   // can make an rvalue SchemaProto.
166*8b6cd535SAndroid Build Coastguard Worker   SetSchemaResultProto SetSchema(const SchemaProto& new_schema,
167*8b6cd535SAndroid Build Coastguard Worker                                  bool ignore_errors_and_delete_documents =
168*8b6cd535SAndroid Build Coastguard Worker                                      false) ICING_LOCKS_EXCLUDED(mutex_);
169*8b6cd535SAndroid Build Coastguard Worker 
170*8b6cd535SAndroid Build Coastguard Worker   // Get Icing's current copy of the schema.
171*8b6cd535SAndroid Build Coastguard Worker   //
172*8b6cd535SAndroid Build Coastguard Worker   // Returns:
173*8b6cd535SAndroid Build Coastguard Worker   //   SchemaProto on success
174*8b6cd535SAndroid Build Coastguard Worker   //   NOT_FOUND if a schema has not been set yet
175*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION IcingSearchEngine has not been initialized yet.
176*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on IO error
177*8b6cd535SAndroid Build Coastguard Worker   GetSchemaResultProto GetSchema() ICING_LOCKS_EXCLUDED(mutex_);
178*8b6cd535SAndroid Build Coastguard Worker 
179*8b6cd535SAndroid Build Coastguard Worker   // Get Icing's current copy of the schema for the given database.
180*8b6cd535SAndroid Build Coastguard Worker   //
181*8b6cd535SAndroid Build Coastguard Worker   // NOTE: This is an expensive operation. It is recommended to call GetSchema()
182*8b6cd535SAndroid Build Coastguard Worker   // instead if you do not need to filter the schema by database, or if you're
183*8b6cd535SAndroid Build Coastguard Worker   // retrieving the only database in the schema.
184*8b6cd535SAndroid Build Coastguard Worker   //
185*8b6cd535SAndroid Build Coastguard Worker   // Returns:
186*8b6cd535SAndroid Build Coastguard Worker   //   SchemaProto on success
187*8b6cd535SAndroid Build Coastguard Worker   //   NOT_FOUND if a schema has not been set yet, or if the database is not
188*8b6cd535SAndroid Build Coastguard Worker   //     present in the schema
189*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION IcingSearchEngine has not been initialized yet.
190*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on IO error
191*8b6cd535SAndroid Build Coastguard Worker   GetSchemaResultProto GetSchema(std::string_view database)
192*8b6cd535SAndroid Build Coastguard Worker       ICING_LOCKS_EXCLUDED(mutex_);
193*8b6cd535SAndroid Build Coastguard Worker 
194*8b6cd535SAndroid Build Coastguard Worker   // Get Icing's copy of the SchemaTypeConfigProto of name schema_type
195*8b6cd535SAndroid Build Coastguard Worker   //
196*8b6cd535SAndroid Build Coastguard Worker   // Returns:
197*8b6cd535SAndroid Build Coastguard Worker   //   SchemaTypeConfigProto on success
198*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION if a schema has not been set yet, IcingSearchEngine
199*8b6cd535SAndroid Build Coastguard Worker   //     has not been initialized yet.
200*8b6cd535SAndroid Build Coastguard Worker   //   NOT_FOUND if there is no SchemaTypeConfig of schema_type in the
201*8b6cd535SAndroid Build Coastguard Worker   //     SchemaProto
202*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on IO error
203*8b6cd535SAndroid Build Coastguard Worker   GetSchemaTypeResultProto GetSchemaType(std::string_view schema_type)
204*8b6cd535SAndroid Build Coastguard Worker       ICING_LOCKS_EXCLUDED(mutex_);
205*8b6cd535SAndroid Build Coastguard Worker 
206*8b6cd535SAndroid Build Coastguard Worker   // Puts the document into icing search engine so that it's stored and
207*8b6cd535SAndroid Build Coastguard Worker   // indexed. Documents are automatically written to disk, callers can also
208*8b6cd535SAndroid Build Coastguard Worker   // call PersistToDisk() to flush changes immediately.
209*8b6cd535SAndroid Build Coastguard Worker   //
210*8b6cd535SAndroid Build Coastguard Worker   // Returns:
211*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
212*8b6cd535SAndroid Build Coastguard Worker   //   OUT_OF_SPACE if exceeds maximum number of allowed documents
213*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION if a schema has not been set yet, IcingSearchEngine
214*8b6cd535SAndroid Build Coastguard Worker   //     has not been initialized yet.
215*8b6cd535SAndroid Build Coastguard Worker   //   NOT_FOUND if there is no SchemaTypeConfig in the SchemaProto that matches
216*8b6cd535SAndroid Build Coastguard Worker   //     the document's schema
217*8b6cd535SAndroid Build Coastguard Worker   //   DATA_LOSS if an IO error occurs while merging document into the index and
218*8b6cd535SAndroid Build Coastguard Worker   //     the index is lost. These documents will still be retrievable via Get,
219*8b6cd535SAndroid Build Coastguard Worker   //     but won't match queries.
220*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on IO error
221*8b6cd535SAndroid Build Coastguard Worker   PutResultProto Put(DocumentProto&& document) ICING_LOCKS_EXCLUDED(mutex_);
222*8b6cd535SAndroid Build Coastguard Worker 
223*8b6cd535SAndroid Build Coastguard Worker   // This function makes a copy of document and calls Put(DocumentProto&&
224*8b6cd535SAndroid Build Coastguard Worker   // document).
225*8b6cd535SAndroid Build Coastguard Worker   //
226*8b6cd535SAndroid Build Coastguard Worker   // NOTE: It's recommended to call Put(DocumentProto&& document) directly to
227*8b6cd535SAndroid Build Coastguard Worker   // avoid a copy if the caller can make an rvalue DocumentProto.
228*8b6cd535SAndroid Build Coastguard Worker   PutResultProto Put(const DocumentProto& document)
229*8b6cd535SAndroid Build Coastguard Worker       ICING_LOCKS_EXCLUDED(mutex_);
230*8b6cd535SAndroid Build Coastguard Worker 
231*8b6cd535SAndroid Build Coastguard Worker   // Finds and returns the document identified by the given key (namespace +
232*8b6cd535SAndroid Build Coastguard Worker   // uri)
233*8b6cd535SAndroid Build Coastguard Worker   //
234*8b6cd535SAndroid Build Coastguard Worker   // Returns:
235*8b6cd535SAndroid Build Coastguard Worker   //   The document found on success
236*8b6cd535SAndroid Build Coastguard Worker   //   NOT_FOUND if the key doesn't exist or doc has been deleted
237*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION IcingSearchEngine has not been initialized yet
238*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on IO error
239*8b6cd535SAndroid Build Coastguard Worker   GetResultProto Get(std::string_view name_space, std::string_view uri,
240*8b6cd535SAndroid Build Coastguard Worker                      const GetResultSpecProto& result_spec);
241*8b6cd535SAndroid Build Coastguard Worker 
242*8b6cd535SAndroid Build Coastguard Worker   // Reports usage. The corresponding usage scores of the specified document in
243*8b6cd535SAndroid Build Coastguard Worker   // the report will be updated.
244*8b6cd535SAndroid Build Coastguard Worker   //
245*8b6cd535SAndroid Build Coastguard Worker   // Returns:
246*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
247*8b6cd535SAndroid Build Coastguard Worker   //   NOT_FOUND if the [namesapce + uri] key in the report doesn't exist
248*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on I/O errors.
249*8b6cd535SAndroid Build Coastguard Worker   ReportUsageResultProto ReportUsage(const UsageReport& usage_report);
250*8b6cd535SAndroid Build Coastguard Worker 
251*8b6cd535SAndroid Build Coastguard Worker   // Returns all the namespaces that have at least one valid document in it.
252*8b6cd535SAndroid Build Coastguard Worker   //
253*8b6cd535SAndroid Build Coastguard Worker   // Returns:
254*8b6cd535SAndroid Build Coastguard Worker   //   All namespaces on success
255*8b6cd535SAndroid Build Coastguard Worker   GetAllNamespacesResultProto GetAllNamespaces();
256*8b6cd535SAndroid Build Coastguard Worker 
257*8b6cd535SAndroid Build Coastguard Worker   // Deletes the Document specified by the given namespace / uri pair from the
258*8b6cd535SAndroid Build Coastguard Worker   // search engine. Delete changes are automatically applied to disk, callers
259*8b6cd535SAndroid Build Coastguard Worker   // can also call PersistToDisk() to flush changes immediately.
260*8b6cd535SAndroid Build Coastguard Worker   //
261*8b6cd535SAndroid Build Coastguard Worker   // NOTE: Space is not reclaimed for deleted documents until Optimize() is
262*8b6cd535SAndroid Build Coastguard Worker   // called.
263*8b6cd535SAndroid Build Coastguard Worker   //
264*8b6cd535SAndroid Build Coastguard Worker   // Returns:
265*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
266*8b6cd535SAndroid Build Coastguard Worker   //   NOT_FOUND if no document exists with namespace, uri
267*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION IcingSearchEngine has not been initialized yet
268*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on IO error
269*8b6cd535SAndroid Build Coastguard Worker   DeleteResultProto Delete(std::string_view name_space, std::string_view uri)
270*8b6cd535SAndroid Build Coastguard Worker       ICING_LOCKS_EXCLUDED(mutex_);
271*8b6cd535SAndroid Build Coastguard Worker 
272*8b6cd535SAndroid Build Coastguard Worker   // Deletes all Documents belonging to the specified namespace from the search
273*8b6cd535SAndroid Build Coastguard Worker   // engine. Delete changes are automatically applied to disk, callers can also
274*8b6cd535SAndroid Build Coastguard Worker   // call PersistToDisk() to flush changes immediately.
275*8b6cd535SAndroid Build Coastguard Worker   //
276*8b6cd535SAndroid Build Coastguard Worker   // NOTE: Space is not reclaimed for deleted documents until Optimize() is
277*8b6cd535SAndroid Build Coastguard Worker   // called.
278*8b6cd535SAndroid Build Coastguard Worker   //
279*8b6cd535SAndroid Build Coastguard Worker   // Returns:
280*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
281*8b6cd535SAndroid Build Coastguard Worker   //   NOT_FOUND if namespace doesn't exist
282*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION IcingSearchEngine has not been initialized yet
283*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on IO error
284*8b6cd535SAndroid Build Coastguard Worker   DeleteByNamespaceResultProto DeleteByNamespace(std::string_view name_space)
285*8b6cd535SAndroid Build Coastguard Worker       ICING_LOCKS_EXCLUDED(mutex_);
286*8b6cd535SAndroid Build Coastguard Worker 
287*8b6cd535SAndroid Build Coastguard Worker   // Deletes all Documents belonging to the specified type from the search
288*8b6cd535SAndroid Build Coastguard Worker   // engine. Delete changes are automatically applied to disk, callers can also
289*8b6cd535SAndroid Build Coastguard Worker   // call PersistToDisk() to flush changes immediately.
290*8b6cd535SAndroid Build Coastguard Worker   //
291*8b6cd535SAndroid Build Coastguard Worker   // NOTE: Space is not reclaimed for deleted documents until Optimize() is
292*8b6cd535SAndroid Build Coastguard Worker   // called.
293*8b6cd535SAndroid Build Coastguard Worker   //
294*8b6cd535SAndroid Build Coastguard Worker   // Returns:
295*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
296*8b6cd535SAndroid Build Coastguard Worker   //   NOT_FOUND if schema type doesn't exist
297*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION IcingSearchEngine has not been initialized yet
298*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on IO error
299*8b6cd535SAndroid Build Coastguard Worker   DeleteBySchemaTypeResultProto DeleteBySchemaType(std::string_view schema_type)
300*8b6cd535SAndroid Build Coastguard Worker       ICING_LOCKS_EXCLUDED(mutex_);
301*8b6cd535SAndroid Build Coastguard Worker 
302*8b6cd535SAndroid Build Coastguard Worker   // Deletes all Documents that match the query specified in search_spec. Delete
303*8b6cd535SAndroid Build Coastguard Worker   // changes are automatically applied to disk, callers can also call
304*8b6cd535SAndroid Build Coastguard Worker   // PersistToDisk() to flush changes immediately.
305*8b6cd535SAndroid Build Coastguard Worker   //
306*8b6cd535SAndroid Build Coastguard Worker   // NOTE: Space is not reclaimed for deleted documents until Optimize() is
307*8b6cd535SAndroid Build Coastguard Worker   // called.
308*8b6cd535SAndroid Build Coastguard Worker   //
309*8b6cd535SAndroid Build Coastguard Worker   // Returns:
310*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
311*8b6cd535SAndroid Build Coastguard Worker   //   NOT_FOUND if the query doesn't match any documents
312*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION IcingSearchEngine has not been initialized yet
313*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on IO error
314*8b6cd535SAndroid Build Coastguard Worker   DeleteByQueryResultProto DeleteByQuery(
315*8b6cd535SAndroid Build Coastguard Worker       const SearchSpecProto& search_spec,
316*8b6cd535SAndroid Build Coastguard Worker       bool return_deleted_document_info = false) ICING_LOCKS_EXCLUDED(mutex_);
317*8b6cd535SAndroid Build Coastguard Worker 
318*8b6cd535SAndroid Build Coastguard Worker   // Retrieves, scores, ranks, and returns the results according to the specs.
319*8b6cd535SAndroid Build Coastguard Worker   // Results can be empty. If there're multiple pages of results,
320*8b6cd535SAndroid Build Coastguard Worker   // SearchResultProto.next_page_token will be set to a non-zero token and can
321*8b6cd535SAndroid Build Coastguard Worker   // be used to fetch more pages via GetNextPage() method. Clients should call
322*8b6cd535SAndroid Build Coastguard Worker   // InvalidateNextPageToken() after they get the pages they need to release
323*8b6cd535SAndroid Build Coastguard Worker   // result cache in memory. Please refer to each proto file for spec
324*8b6cd535SAndroid Build Coastguard Worker   // definitions.
325*8b6cd535SAndroid Build Coastguard Worker   //
326*8b6cd535SAndroid Build Coastguard Worker   // Returns a SearchResultProto with status:
327*8b6cd535SAndroid Build Coastguard Worker   //   OK with results on success
328*8b6cd535SAndroid Build Coastguard Worker   //   INVALID_ARGUMENT if any of specs is invalid
329*8b6cd535SAndroid Build Coastguard Worker   //   ABORTED if failed to perform search but existing data is not affected
330*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION IcingSearchEngine has not been initialized yet
331*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on any other errors
332*8b6cd535SAndroid Build Coastguard Worker   SearchResultProto Search(const SearchSpecProto& search_spec,
333*8b6cd535SAndroid Build Coastguard Worker                            const ScoringSpecProto& scoring_spec,
334*8b6cd535SAndroid Build Coastguard Worker                            const ResultSpecProto& result_spec)
335*8b6cd535SAndroid Build Coastguard Worker       ICING_LOCKS_EXCLUDED(mutex_);
336*8b6cd535SAndroid Build Coastguard Worker 
337*8b6cd535SAndroid Build Coastguard Worker   // Retrieves, scores, ranks and returns the suggested query string according
338*8b6cd535SAndroid Build Coastguard Worker   // to the specs. Results can be empty.
339*8b6cd535SAndroid Build Coastguard Worker   //
340*8b6cd535SAndroid Build Coastguard Worker   // Returns a SuggestionResponse with status:
341*8b6cd535SAndroid Build Coastguard Worker   //   OK with results on success
342*8b6cd535SAndroid Build Coastguard Worker   //   INVALID_ARGUMENT if any of specs is invalid
343*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION IcingSearchEngine has not been initialized yet
344*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on any other errors
345*8b6cd535SAndroid Build Coastguard Worker   SuggestionResponse SearchSuggestions(
346*8b6cd535SAndroid Build Coastguard Worker       const SuggestionSpecProto& suggestion_spec) ICING_LOCKS_EXCLUDED(mutex_);
347*8b6cd535SAndroid Build Coastguard Worker 
348*8b6cd535SAndroid Build Coastguard Worker   // Fetches the next page of results of a previously executed query. Results
349*8b6cd535SAndroid Build Coastguard Worker   // can be empty if next-page token is invalid. Invalid next page tokens are
350*8b6cd535SAndroid Build Coastguard Worker   // tokens that are either zero or were previously passed to
351*8b6cd535SAndroid Build Coastguard Worker   // InvalidateNextPageToken. If there are pages of results remaining after the
352*8b6cd535SAndroid Build Coastguard Worker   // one retrieved by this call, SearchResultProto.next_page_token will be
353*8b6cd535SAndroid Build Coastguard Worker   // set to a non-zero token and can be used to fetch more pages via
354*8b6cd535SAndroid Build Coastguard Worker   // GetNextPage() method.
355*8b6cd535SAndroid Build Coastguard Worker   //
356*8b6cd535SAndroid Build Coastguard Worker   // Returns a SearchResultProto with status:
357*8b6cd535SAndroid Build Coastguard Worker   //   OK with results on success
358*8b6cd535SAndroid Build Coastguard Worker   //   ABORTED if failed to get results but existing data is not affected
359*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION IcingSearchEngine has not been initialized yet
360*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on any other errors
361*8b6cd535SAndroid Build Coastguard Worker   SearchResultProto GetNextPage(uint64_t next_page_token)
362*8b6cd535SAndroid Build Coastguard Worker       ICING_LOCKS_EXCLUDED(mutex_);
363*8b6cd535SAndroid Build Coastguard Worker 
364*8b6cd535SAndroid Build Coastguard Worker   // Invalidates the next-page token so that no more results of the related
365*8b6cd535SAndroid Build Coastguard Worker   // query can be returned.
366*8b6cd535SAndroid Build Coastguard Worker   void InvalidateNextPageToken(uint64_t next_page_token)
367*8b6cd535SAndroid Build Coastguard Worker       ICING_LOCKS_EXCLUDED(mutex_);
368*8b6cd535SAndroid Build Coastguard Worker 
369*8b6cd535SAndroid Build Coastguard Worker   // Gets or creates a file for write only purpose for the given blob handle.
370*8b6cd535SAndroid Build Coastguard Worker   // To mark the blob is completed written, commitBlob must be called. Once
371*8b6cd535SAndroid Build Coastguard Worker   // commitBlob is called, the blob is sealed and rewrite is not allowed.
372*8b6cd535SAndroid Build Coastguard Worker   //
373*8b6cd535SAndroid Build Coastguard Worker   // Returns:
374*8b6cd535SAndroid Build Coastguard Worker   //   File descriptor on success
375*8b6cd535SAndroid Build Coastguard Worker   //   InvalidArgumentError on invalid blob handle
376*8b6cd535SAndroid Build Coastguard Worker   //   FailedPreconditionError on blob is already opened for write
377*8b6cd535SAndroid Build Coastguard Worker   //   AlreadyExistsError on blob is committed
378*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on IO error
379*8b6cd535SAndroid Build Coastguard Worker   BlobProto OpenWriteBlob(const PropertyProto::BlobHandleProto& blob_handle);
380*8b6cd535SAndroid Build Coastguard Worker 
381*8b6cd535SAndroid Build Coastguard Worker   // Removes a blob file and blob handle from the blob store.
382*8b6cd535SAndroid Build Coastguard Worker   //
383*8b6cd535SAndroid Build Coastguard Worker   // This will remove the blob on any state. No matter it's committed or not or
384*8b6cd535SAndroid Build Coastguard Worker   // it has reference document links or not.
385*8b6cd535SAndroid Build Coastguard Worker   //
386*8b6cd535SAndroid Build Coastguard Worker   // Returns:
387*8b6cd535SAndroid Build Coastguard Worker   //   InvalidArgumentError on invalid blob handle
388*8b6cd535SAndroid Build Coastguard Worker   //   NotFoundError on blob is not found
389*8b6cd535SAndroid Build Coastguard Worker   //   InternalError on IO error
390*8b6cd535SAndroid Build Coastguard Worker   BlobProto RemoveBlob(const PropertyProto::BlobHandleProto& blob_handle);
391*8b6cd535SAndroid Build Coastguard Worker 
392*8b6cd535SAndroid Build Coastguard Worker   // Gets or creates a file for read only purpose for the given blob handle.
393*8b6cd535SAndroid Build Coastguard Worker   // The blob must be committed by calling commitBlob otherwise it is not
394*8b6cd535SAndroid Build Coastguard Worker   // accessible.
395*8b6cd535SAndroid Build Coastguard Worker   //
396*8b6cd535SAndroid Build Coastguard Worker   // Returns:
397*8b6cd535SAndroid Build Coastguard Worker   //   File descriptor on success
398*8b6cd535SAndroid Build Coastguard Worker   //   InvalidArgumentError on invalid blob handle
399*8b6cd535SAndroid Build Coastguard Worker   //   NotFoundError on blob is not found or is not committed
400*8b6cd535SAndroid Build Coastguard Worker   BlobProto OpenReadBlob(const PropertyProto::BlobHandleProto& blob_handle);
401*8b6cd535SAndroid Build Coastguard Worker 
402*8b6cd535SAndroid Build Coastguard Worker   // Commits the given blob, the blob is open to write via openWrite.
403*8b6cd535SAndroid Build Coastguard Worker   // Before the blob is committed, it is not visible to any reader via openRead.
404*8b6cd535SAndroid Build Coastguard Worker   // After the blob is committed, it is not allowed to rewrite or update the
405*8b6cd535SAndroid Build Coastguard Worker   // content.
406*8b6cd535SAndroid Build Coastguard Worker   //
407*8b6cd535SAndroid Build Coastguard Worker   // Returns:
408*8b6cd535SAndroid Build Coastguard Worker   //   True on the blob is successfuly committed.
409*8b6cd535SAndroid Build Coastguard Worker   //   False on the blob is already committed.
410*8b6cd535SAndroid Build Coastguard Worker   //   InvalidArgumentError on invalid blob handle or digest is mismatch with
411*8b6cd535SAndroid Build Coastguard Worker   //     file content NotFoundError on blob is not found.
412*8b6cd535SAndroid Build Coastguard Worker   BlobProto CommitBlob(const PropertyProto::BlobHandleProto& blob_handle);
413*8b6cd535SAndroid Build Coastguard Worker 
414*8b6cd535SAndroid Build Coastguard Worker   // Makes sure that every update/delete received till this point is flushed
415*8b6cd535SAndroid Build Coastguard Worker   // to disk. If the app crashes after a call to PersistToDisk(), Icing
416*8b6cd535SAndroid Build Coastguard Worker   // would be able to fully recover all data written up to this point.
417*8b6cd535SAndroid Build Coastguard Worker   //
418*8b6cd535SAndroid Build Coastguard Worker   // If persist_type is PersistType::LITE, then only the ground truth will be
419*8b6cd535SAndroid Build Coastguard Worker   // synced. This should be relatively lightweight to do (order of microseconds)
420*8b6cd535SAndroid Build Coastguard Worker   // and ensures that there will be no data loss. At worst, Icing may need to
421*8b6cd535SAndroid Build Coastguard Worker   // recover internal data structures by replaying the document log upon the
422*8b6cd535SAndroid Build Coastguard Worker   // next startup. Clients should call PersistToDisk(LITE) after each batch of
423*8b6cd535SAndroid Build Coastguard Worker   // mutations.
424*8b6cd535SAndroid Build Coastguard Worker   //
425*8b6cd535SAndroid Build Coastguard Worker   // If persist_type is PersistType::FULL, then all internal data structures in
426*8b6cd535SAndroid Build Coastguard Worker   // Icing will be synced. This is a heavier operation (order of milliseconds).
427*8b6cd535SAndroid Build Coastguard Worker   // It ensures that Icing will not need to recover internal data structures
428*8b6cd535SAndroid Build Coastguard Worker   // upon the next startup. Clients should call PersistToDisk(FULL) before their
429*8b6cd535SAndroid Build Coastguard Worker   // process dies.
430*8b6cd535SAndroid Build Coastguard Worker   //
431*8b6cd535SAndroid Build Coastguard Worker   // NOTE: It is not necessary to call PersistToDisk() to read back data
432*8b6cd535SAndroid Build Coastguard Worker   // that was recently written. All read APIs will include the most recent
433*8b6cd535SAndroid Build Coastguard Worker   // updates/deletes regardless of the data being flushed to disk.
434*8b6cd535SAndroid Build Coastguard Worker   //
435*8b6cd535SAndroid Build Coastguard Worker   // Returns:
436*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
437*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION IcingSearchEngine has not been initialized yet
438*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL on I/O error
439*8b6cd535SAndroid Build Coastguard Worker   PersistToDiskResultProto PersistToDisk(PersistType::Code persist_type)
440*8b6cd535SAndroid Build Coastguard Worker       ICING_LOCKS_EXCLUDED(mutex_);
441*8b6cd535SAndroid Build Coastguard Worker 
442*8b6cd535SAndroid Build Coastguard Worker   // Allows Icing to run tasks that are too expensive and/or unnecessary to be
443*8b6cd535SAndroid Build Coastguard Worker   // executed in real-time, but are useful to keep it fast and be
444*8b6cd535SAndroid Build Coastguard Worker   // resource-efficient. This method purely optimizes the internal files and
445*8b6cd535SAndroid Build Coastguard Worker   // has no functional impact on what gets accepted/returned.
446*8b6cd535SAndroid Build Coastguard Worker   //
447*8b6cd535SAndroid Build Coastguard Worker   // WARNING: This method is CPU and IO intensive and depending on the
448*8b6cd535SAndroid Build Coastguard Worker   // contents stored, it can take from a few seconds to a few minutes.
449*8b6cd535SAndroid Build Coastguard Worker   // This call also blocks all read/write operations on Icing.
450*8b6cd535SAndroid Build Coastguard Worker   //
451*8b6cd535SAndroid Build Coastguard Worker   // SUGGESTION: Assuming the client has no restrictions on their side, it's
452*8b6cd535SAndroid Build Coastguard Worker   // recommended to call this method about once every 24 hours when the
453*8b6cd535SAndroid Build Coastguard Worker   // device is idle and charging. It can also be called when the system needs
454*8b6cd535SAndroid Build Coastguard Worker   // to free up extra disk-space.
455*8b6cd535SAndroid Build Coastguard Worker   //
456*8b6cd535SAndroid Build Coastguard Worker   // Returns:
457*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
458*8b6cd535SAndroid Build Coastguard Worker   //   ABORTED_ERROR if optimization is aborted due to non-fatal errors before
459*8b6cd535SAndroid Build Coastguard Worker   //                 actual modifications are made.
460*8b6cd535SAndroid Build Coastguard Worker   //   DATA_LOSS_ERROR on errors that could potentially cause data loss,
461*8b6cd535SAndroid Build Coastguard Worker   //                   IcingSearchEngine is still functioning.
462*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on any IO errors or other unrecoverable errors. Continued
463*8b6cd535SAndroid Build Coastguard Worker   //                  use of Icing is undefined.
464*8b6cd535SAndroid Build Coastguard Worker   //                  Clients could clear and reinitialize IcingSearchEngine.
465*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION IcingSearchEngine has not been initialized yet
466*8b6cd535SAndroid Build Coastguard Worker   OptimizeResultProto Optimize() ICING_LOCKS_EXCLUDED(mutex_);
467*8b6cd535SAndroid Build Coastguard Worker 
468*8b6cd535SAndroid Build Coastguard Worker   // Returns potential size and document savings if Optimize were called.
469*8b6cd535SAndroid Build Coastguard Worker   //
470*8b6cd535SAndroid Build Coastguard Worker   // Returns:
471*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
472*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION if IcingSearchEngine has not been initialized yet
473*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on IO error
474*8b6cd535SAndroid Build Coastguard Worker   GetOptimizeInfoResultProto GetOptimizeInfo() ICING_LOCKS_EXCLUDED(mutex_);
475*8b6cd535SAndroid Build Coastguard Worker 
476*8b6cd535SAndroid Build Coastguard Worker   // Calculates the StorageInfo for Icing.
477*8b6cd535SAndroid Build Coastguard Worker   //
478*8b6cd535SAndroid Build Coastguard Worker   // If an IO error occurs while trying to calculate the value for a field, then
479*8b6cd535SAndroid Build Coastguard Worker   // that field will be set to -1.
480*8b6cd535SAndroid Build Coastguard Worker   StorageInfoResultProto GetStorageInfo() ICING_LOCKS_EXCLUDED(mutex_);
481*8b6cd535SAndroid Build Coastguard Worker 
482*8b6cd535SAndroid Build Coastguard Worker   // Get debug information for Icing.
483*8b6cd535SAndroid Build Coastguard Worker   DebugInfoResultProto GetDebugInfo(DebugInfoVerbosity::Code verbosity)
484*8b6cd535SAndroid Build Coastguard Worker       ICING_LOCKS_EXCLUDED(mutex_);
485*8b6cd535SAndroid Build Coastguard Worker 
486*8b6cd535SAndroid Build Coastguard Worker   // Clears all data from Icing and re-initializes. Clients DO NOT need to call
487*8b6cd535SAndroid Build Coastguard Worker   // Initialize again.
488*8b6cd535SAndroid Build Coastguard Worker   //
489*8b6cd535SAndroid Build Coastguard Worker   // Returns:
490*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
491*8b6cd535SAndroid Build Coastguard Worker   //   ABORTED_ERROR if failed to delete underlying files
492*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR if internal state is no longer consistent
493*8b6cd535SAndroid Build Coastguard Worker   ResetResultProto Reset() ICING_LOCKS_EXCLUDED(mutex_);
494*8b6cd535SAndroid Build Coastguard Worker 
495*8b6cd535SAndroid Build Coastguard Worker   // Disallow copy and move.
496*8b6cd535SAndroid Build Coastguard Worker   IcingSearchEngine(const IcingSearchEngine&) = delete;
497*8b6cd535SAndroid Build Coastguard Worker   IcingSearchEngine& operator=(const IcingSearchEngine&) = delete;
498*8b6cd535SAndroid Build Coastguard Worker 
499*8b6cd535SAndroid Build Coastguard Worker  protected:
500*8b6cd535SAndroid Build Coastguard Worker   IcingSearchEngine(IcingSearchEngineOptions options,
501*8b6cd535SAndroid Build Coastguard Worker                     std::unique_ptr<const Filesystem> filesystem,
502*8b6cd535SAndroid Build Coastguard Worker                     std::unique_ptr<const IcingFilesystem> icing_filesystem,
503*8b6cd535SAndroid Build Coastguard Worker                     std::unique_ptr<Clock> clock,
504*8b6cd535SAndroid Build Coastguard Worker                     std::unique_ptr<const JniCache> jni_cache = nullptr);
505*8b6cd535SAndroid Build Coastguard Worker 
506*8b6cd535SAndroid Build Coastguard Worker  private:
507*8b6cd535SAndroid Build Coastguard Worker   const IcingSearchEngineOptions options_;
508*8b6cd535SAndroid Build Coastguard Worker   const FeatureFlags feature_flags_;
509*8b6cd535SAndroid Build Coastguard Worker   const std::unique_ptr<const Filesystem> filesystem_;
510*8b6cd535SAndroid Build Coastguard Worker   const std::unique_ptr<const IcingFilesystem> icing_filesystem_;
511*8b6cd535SAndroid Build Coastguard Worker   bool initialized_ ICING_GUARDED_BY(mutex_) = false;
512*8b6cd535SAndroid Build Coastguard Worker 
513*8b6cd535SAndroid Build Coastguard Worker   // Abstraction for accessing time values.
514*8b6cd535SAndroid Build Coastguard Worker   const std::unique_ptr<const Clock> clock_;
515*8b6cd535SAndroid Build Coastguard Worker 
516*8b6cd535SAndroid Build Coastguard Worker   // Provides key thresholds that affects the running time and memory of major
517*8b6cd535SAndroid Build Coastguard Worker   // components in Icing search engine.
518*8b6cd535SAndroid Build Coastguard Worker   const PerformanceConfiguration performance_configuration_;
519*8b6cd535SAndroid Build Coastguard Worker 
520*8b6cd535SAndroid Build Coastguard Worker   // Used to provide reader and writer locks
521*8b6cd535SAndroid Build Coastguard Worker   absl_ports::shared_mutex mutex_;
522*8b6cd535SAndroid Build Coastguard Worker 
523*8b6cd535SAndroid Build Coastguard Worker   // Stores and processes the schema
524*8b6cd535SAndroid Build Coastguard Worker   std::unique_ptr<SchemaStore> schema_store_ ICING_GUARDED_BY(mutex_);
525*8b6cd535SAndroid Build Coastguard Worker 
526*8b6cd535SAndroid Build Coastguard Worker   // Used to store all valid documents
527*8b6cd535SAndroid Build Coastguard Worker   //
528*8b6cd535SAndroid Build Coastguard Worker   // Dependencies: schema_store_
529*8b6cd535SAndroid Build Coastguard Worker   std::unique_ptr<DocumentStore> document_store_ ICING_GUARDED_BY(mutex_);
530*8b6cd535SAndroid Build Coastguard Worker 
531*8b6cd535SAndroid Build Coastguard Worker   // Used to manage pagination state of query results. Even though
532*8b6cd535SAndroid Build Coastguard Worker   // ResultStateManager has its own reader-writer lock, mutex_ must still be
533*8b6cd535SAndroid Build Coastguard Worker   // acquired first in order to adhere to the global lock ordering:
534*8b6cd535SAndroid Build Coastguard Worker   //   1. mutex_
535*8b6cd535SAndroid Build Coastguard Worker   //   2. result_state_manager_.lock_
536*8b6cd535SAndroid Build Coastguard Worker   //
537*8b6cd535SAndroid Build Coastguard Worker   // Dependencies: document_store_
538*8b6cd535SAndroid Build Coastguard Worker   std::unique_ptr<ResultStateManager> result_state_manager_
539*8b6cd535SAndroid Build Coastguard Worker       ICING_GUARDED_BY(mutex_);
540*8b6cd535SAndroid Build Coastguard Worker 
541*8b6cd535SAndroid Build Coastguard Worker   // Used to store all valid blob data
542*8b6cd535SAndroid Build Coastguard Worker   std::unique_ptr<BlobStore> blob_store_ ICING_GUARDED_BY(mutex_);
543*8b6cd535SAndroid Build Coastguard Worker 
544*8b6cd535SAndroid Build Coastguard Worker   std::unique_ptr<const LanguageSegmenter> language_segmenter_
545*8b6cd535SAndroid Build Coastguard Worker       ICING_GUARDED_BY(mutex_);
546*8b6cd535SAndroid Build Coastguard Worker 
547*8b6cd535SAndroid Build Coastguard Worker   std::unique_ptr<const Normalizer> normalizer_ ICING_GUARDED_BY(mutex_);
548*8b6cd535SAndroid Build Coastguard Worker 
549*8b6cd535SAndroid Build Coastguard Worker   // Storage for all hits of string contents from the document store.
550*8b6cd535SAndroid Build Coastguard Worker   std::unique_ptr<Index> index_ ICING_GUARDED_BY(mutex_);
551*8b6cd535SAndroid Build Coastguard Worker 
552*8b6cd535SAndroid Build Coastguard Worker   // Storage for all hits of numeric contents from the document store.
553*8b6cd535SAndroid Build Coastguard Worker   std::unique_ptr<NumericIndex<int64_t>> integer_index_
554*8b6cd535SAndroid Build Coastguard Worker       ICING_GUARDED_BY(mutex_);
555*8b6cd535SAndroid Build Coastguard Worker 
556*8b6cd535SAndroid Build Coastguard Worker   // Storage for all join qualified ids from the document store.
557*8b6cd535SAndroid Build Coastguard Worker   std::unique_ptr<QualifiedIdJoinIndex> qualified_id_join_index_
558*8b6cd535SAndroid Build Coastguard Worker       ICING_GUARDED_BY(mutex_);
559*8b6cd535SAndroid Build Coastguard Worker 
560*8b6cd535SAndroid Build Coastguard Worker   // Storage for all hits of embedding contents from the document store.
561*8b6cd535SAndroid Build Coastguard Worker   std::unique_ptr<EmbeddingIndex> embedding_index_ ICING_GUARDED_BY(mutex_);
562*8b6cd535SAndroid Build Coastguard Worker 
563*8b6cd535SAndroid Build Coastguard Worker   // Pointer to JNI class references
564*8b6cd535SAndroid Build Coastguard Worker   const std::unique_ptr<const JniCache> jni_cache_;
565*8b6cd535SAndroid Build Coastguard Worker 
566*8b6cd535SAndroid Build Coastguard Worker   // Resets all members that are created during Initialize.
567*8b6cd535SAndroid Build Coastguard Worker   void ResetMembers() ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
568*8b6cd535SAndroid Build Coastguard Worker 
569*8b6cd535SAndroid Build Coastguard Worker   // Resets all members that are created during Initialize, deletes all
570*8b6cd535SAndroid Build Coastguard Worker   // underlying files and initializes a fresh index.
571*8b6cd535SAndroid Build Coastguard Worker   ResetResultProto ResetInternal() ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
572*8b6cd535SAndroid Build Coastguard Worker 
573*8b6cd535SAndroid Build Coastguard Worker   // Checks for the existence of the init marker file. If the failed init count
574*8b6cd535SAndroid Build Coastguard Worker   // exceeds kMaxUnsuccessfulInitAttempts, all data is deleted and the index is
575*8b6cd535SAndroid Build Coastguard Worker   // initialized from scratch. The updated count (original failed init count + 1
576*8b6cd535SAndroid Build Coastguard Worker   // ) is written to the marker file.
577*8b6cd535SAndroid Build Coastguard Worker   //
578*8b6cd535SAndroid Build Coastguard Worker   // RETURNS
579*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
580*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL if an IO error occurs while trying to update the marker file.
581*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::Status CheckInitMarkerFile(
582*8b6cd535SAndroid Build Coastguard Worker       InitializeStatsProto* initialize_stats)
583*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
584*8b6cd535SAndroid Build Coastguard Worker 
585*8b6cd535SAndroid Build Coastguard Worker   // Helper method to do the actual work to persist data to disk. We need this
586*8b6cd535SAndroid Build Coastguard Worker   // separate method so that other public methods don't need to call
587*8b6cd535SAndroid Build Coastguard Worker   // PersistToDisk(). Public methods calling each other may cause deadlock
588*8b6cd535SAndroid Build Coastguard Worker   // issues.
589*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::Status InternalPersistToDisk(
590*8b6cd535SAndroid Build Coastguard Worker       PersistType::Code persist_type) ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
591*8b6cd535SAndroid Build Coastguard Worker 
592*8b6cd535SAndroid Build Coastguard Worker   // Helper method to the actual work to Initialize. We need this separate
593*8b6cd535SAndroid Build Coastguard Worker   // method so that other public methods don't need to call Initialize(). Public
594*8b6cd535SAndroid Build Coastguard Worker   // methods calling each other may cause deadlock issues.
595*8b6cd535SAndroid Build Coastguard Worker   InitializeResultProto InternalInitialize()
596*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
597*8b6cd535SAndroid Build Coastguard Worker 
598*8b6cd535SAndroid Build Coastguard Worker   // Helper method to initialize member variables.
599*8b6cd535SAndroid Build Coastguard Worker   //
600*8b6cd535SAndroid Build Coastguard Worker   // Returns:
601*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
602*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION if initialize_stats is null
603*8b6cd535SAndroid Build Coastguard Worker   //   RESOURCE_EXHAUSTED if the index runs out of storage
604*8b6cd535SAndroid Build Coastguard Worker   //   NOT_FOUND if some Document's schema type is not in the SchemaStore
605*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL on any I/O errors
606*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::Status InitializeMembers(
607*8b6cd535SAndroid Build Coastguard Worker       InitializeStatsProto* initialize_stats)
608*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
609*8b6cd535SAndroid Build Coastguard Worker 
610*8b6cd535SAndroid Build Coastguard Worker   // Do any initialization/recovery necessary to create a SchemaStore instance.
611*8b6cd535SAndroid Build Coastguard Worker   //
612*8b6cd535SAndroid Build Coastguard Worker   // Returns:
613*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
614*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION if initialize_stats is null
615*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL on I/O error
616*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::Status InitializeSchemaStore(
617*8b6cd535SAndroid Build Coastguard Worker       InitializeStatsProto* initialize_stats)
618*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
619*8b6cd535SAndroid Build Coastguard Worker 
620*8b6cd535SAndroid Build Coastguard Worker   // Do any initialization/recovery necessary to create a DocumentStore
621*8b6cd535SAndroid Build Coastguard Worker   // instance.
622*8b6cd535SAndroid Build Coastguard Worker   //
623*8b6cd535SAndroid Build Coastguard Worker   // See comments on DocumentStore::Create for explanation of
624*8b6cd535SAndroid Build Coastguard Worker   // force_recovery_and_revalidate_documents.
625*8b6cd535SAndroid Build Coastguard Worker   //
626*8b6cd535SAndroid Build Coastguard Worker   // Returns:
627*8b6cd535SAndroid Build Coastguard Worker   //   On success, a boolean flag indicating whether derived files of the
628*8b6cd535SAndroid Build Coastguard Worker   //     document store have been regenerated or not. If true, any other
629*8b6cd535SAndroid Build Coastguard Worker   //     components depending on them should also be rebuilt if true.
630*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION if initialize_stats is null
631*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL on I/O error
632*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::StatusOr<bool> InitializeDocumentStore(
633*8b6cd535SAndroid Build Coastguard Worker       bool force_recovery_and_revalidate_documents,
634*8b6cd535SAndroid Build Coastguard Worker       InitializeStatsProto* initialize_stats)
635*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
636*8b6cd535SAndroid Build Coastguard Worker 
637*8b6cd535SAndroid Build Coastguard Worker   // Do any initialization necessary to create a BlobStore instance.
638*8b6cd535SAndroid Build Coastguard Worker   //
639*8b6cd535SAndroid Build Coastguard Worker   // Returns:
640*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
641*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION if initialize_stats is null
642*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::Status InitializeBlobStore(
643*8b6cd535SAndroid Build Coastguard Worker       int32_t orphan_blob_time_to_live_ms, int32_t compression_level)
644*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
645*8b6cd535SAndroid Build Coastguard Worker 
646*8b6cd535SAndroid Build Coastguard Worker   // Do any initialization/recovery necessary to create term index, integer
647*8b6cd535SAndroid Build Coastguard Worker   // index, and qualified id join index instances.
648*8b6cd535SAndroid Build Coastguard Worker   //
649*8b6cd535SAndroid Build Coastguard Worker   // If document_store_derived_files_regenerated is true, then we have to
650*8b6cd535SAndroid Build Coastguard Worker   // rebuild qualified id join index since NamespaceIds were reassigned.
651*8b6cd535SAndroid Build Coastguard Worker   //
652*8b6cd535SAndroid Build Coastguard Worker   // Returns:
653*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
654*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION if initialize_stats is null
655*8b6cd535SAndroid Build Coastguard Worker   //   RESOURCE_EXHAUSTED if the index runs out of storage
656*8b6cd535SAndroid Build Coastguard Worker   //   NOT_FOUND if some Document's schema type is not in the SchemaStore
657*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL on I/O error
658*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::Status InitializeIndex(
659*8b6cd535SAndroid Build Coastguard Worker       bool document_store_derived_files_regenerated,
660*8b6cd535SAndroid Build Coastguard Worker       InitializeStatsProto* initialize_stats)
661*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
662*8b6cd535SAndroid Build Coastguard Worker 
663*8b6cd535SAndroid Build Coastguard Worker   // Implementation of IcingSearchEngine::Search that only grabs the overall
664*8b6cd535SAndroid Build Coastguard Worker   // read-lock, allowing for parallel non-exclusive operations.
665*8b6cd535SAndroid Build Coastguard Worker   // This implementation is used if search_spec.use_read_only_search is true.
666*8b6cd535SAndroid Build Coastguard Worker   SearchResultProto SearchLockedShared(const SearchSpecProto& search_spec,
667*8b6cd535SAndroid Build Coastguard Worker                                        const ScoringSpecProto& scoring_spec,
668*8b6cd535SAndroid Build Coastguard Worker                                        const ResultSpecProto& result_spec)
669*8b6cd535SAndroid Build Coastguard Worker       ICING_LOCKS_EXCLUDED(mutex_);
670*8b6cd535SAndroid Build Coastguard Worker 
671*8b6cd535SAndroid Build Coastguard Worker   // Implementation of IcingSearchEngine::Search that requires the overall
672*8b6cd535SAndroid Build Coastguard Worker   // write lock. No other operations of any kind can be executed in parallel if
673*8b6cd535SAndroid Build Coastguard Worker   // this version is used.
674*8b6cd535SAndroid Build Coastguard Worker   // This implementation is used if search_spec.use_read_only_search is false.
675*8b6cd535SAndroid Build Coastguard Worker   SearchResultProto SearchLockedExclusive(const SearchSpecProto& search_spec,
676*8b6cd535SAndroid Build Coastguard Worker                                           const ScoringSpecProto& scoring_spec,
677*8b6cd535SAndroid Build Coastguard Worker                                           const ResultSpecProto& result_spec)
678*8b6cd535SAndroid Build Coastguard Worker       ICING_LOCKS_EXCLUDED(mutex_);
679*8b6cd535SAndroid Build Coastguard Worker 
680*8b6cd535SAndroid Build Coastguard Worker   // Helper method for the actual work to Search. We need this separate
681*8b6cd535SAndroid Build Coastguard Worker   // method to manage locking for Search.
682*8b6cd535SAndroid Build Coastguard Worker   SearchResultProto InternalSearch(const SearchSpecProto& search_spec,
683*8b6cd535SAndroid Build Coastguard Worker                                    const ScoringSpecProto& scoring_spec,
684*8b6cd535SAndroid Build Coastguard Worker                                    const ResultSpecProto& result_spec)
685*8b6cd535SAndroid Build Coastguard Worker       ICING_SHARED_LOCKS_REQUIRED(mutex_);
686*8b6cd535SAndroid Build Coastguard Worker 
687*8b6cd535SAndroid Build Coastguard Worker   // Processes query and scores according to the specs. It is a helper function
688*8b6cd535SAndroid Build Coastguard Worker   // (called by Search) to process and score normal query and the nested child
689*8b6cd535SAndroid Build Coastguard Worker   // query for join search.
690*8b6cd535SAndroid Build Coastguard Worker   //
691*8b6cd535SAndroid Build Coastguard Worker   // Returns a QueryScoringResults
692*8b6cd535SAndroid Build Coastguard Worker   //   OK on success with a vector of ScoredDocumentHits,
693*8b6cd535SAndroid Build Coastguard Worker   //      SectionRestrictQueryTermsMap, and other stats fields for logging.
694*8b6cd535SAndroid Build Coastguard Worker   //   Any other errors when processing the query or scoring
695*8b6cd535SAndroid Build Coastguard Worker   struct QueryScoringResults {
696*8b6cd535SAndroid Build Coastguard Worker     libtextclassifier3::Status status;
697*8b6cd535SAndroid Build Coastguard Worker     SectionRestrictQueryTermsMap query_terms;
698*8b6cd535SAndroid Build Coastguard Worker     std::vector<ScoredDocumentHit> scored_document_hits;
699*8b6cd535SAndroid Build Coastguard Worker 
QueryScoringResultsQueryScoringResults700*8b6cd535SAndroid Build Coastguard Worker     explicit QueryScoringResults(
701*8b6cd535SAndroid Build Coastguard Worker         libtextclassifier3::Status status_in,
702*8b6cd535SAndroid Build Coastguard Worker         SectionRestrictQueryTermsMap&& query_terms_in,
703*8b6cd535SAndroid Build Coastguard Worker         std::vector<ScoredDocumentHit>&& scored_document_hits_in)
704*8b6cd535SAndroid Build Coastguard Worker         : status(std::move(status_in)),
705*8b6cd535SAndroid Build Coastguard Worker           query_terms(std::move(query_terms_in)),
706*8b6cd535SAndroid Build Coastguard Worker           scored_document_hits(std::move(scored_document_hits_in)) {}
707*8b6cd535SAndroid Build Coastguard Worker   };
708*8b6cd535SAndroid Build Coastguard Worker   QueryScoringResults ProcessQueryAndScore(
709*8b6cd535SAndroid Build Coastguard Worker       const SearchSpecProto& search_spec, const ScoringSpecProto& scoring_spec,
710*8b6cd535SAndroid Build Coastguard Worker       const ResultSpecProto& result_spec,
711*8b6cd535SAndroid Build Coastguard Worker       const JoinChildrenFetcher* join_children_fetcher, int64_t current_time_ms,
712*8b6cd535SAndroid Build Coastguard Worker       QueryStatsProto::SearchStats* search_stats)
713*8b6cd535SAndroid Build Coastguard Worker       ICING_SHARED_LOCKS_REQUIRED(mutex_);
714*8b6cd535SAndroid Build Coastguard Worker 
715*8b6cd535SAndroid Build Coastguard Worker   // Deletes documents propagated from the given deleted document ids via
716*8b6cd535SAndroid Build Coastguard Worker   // joinable properties with delete propagation enabled.
717*8b6cd535SAndroid Build Coastguard Worker   //
718*8b6cd535SAndroid Build Coastguard Worker   // Returns:
719*8b6cd535SAndroid Build Coastguard Worker   //   Number of propagated documents deleted on success
720*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on any I/O errors
721*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::StatusOr<int> PropagateDelete(
722*8b6cd535SAndroid Build Coastguard Worker       const std::unordered_set<DocumentId>& deleted_document_ids,
723*8b6cd535SAndroid Build Coastguard Worker       int64_t current_time_ms) ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
724*8b6cd535SAndroid Build Coastguard Worker 
725*8b6cd535SAndroid Build Coastguard Worker   // Discards derived data that requires rebuild based on rebuild_result.
726*8b6cd535SAndroid Build Coastguard Worker   //
727*8b6cd535SAndroid Build Coastguard Worker   // Returns:
728*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
729*8b6cd535SAndroid Build Coastguard Worker   //   FAILED_PRECONDITION_ERROR if those instances are valid (non nullptr)
730*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on any I/O errors
731*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::Status DiscardDerivedFiles(
732*8b6cd535SAndroid Build Coastguard Worker       const version_util::DerivedFilesRebuildResult& rebuild_result)
733*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
734*8b6cd535SAndroid Build Coastguard Worker 
735*8b6cd535SAndroid Build Coastguard Worker   // Repopulates derived data off our ground truths.
736*8b6cd535SAndroid Build Coastguard Worker   //
737*8b6cd535SAndroid Build Coastguard Worker   // Returns:
738*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
739*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on any IO errors
740*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::Status RegenerateDerivedFiles(
741*8b6cd535SAndroid Build Coastguard Worker       InitializeStatsProto* initialize_stats = nullptr,
742*8b6cd535SAndroid Build Coastguard Worker       bool log_document_store_stats = false)
743*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
744*8b6cd535SAndroid Build Coastguard Worker 
745*8b6cd535SAndroid Build Coastguard Worker   // Optimizes the DocumentStore by removing any unneeded documents (i.e.
746*8b6cd535SAndroid Build Coastguard Worker   // deleted, expired, etc.) from the filesystem storage.
747*8b6cd535SAndroid Build Coastguard Worker   //
748*8b6cd535SAndroid Build Coastguard Worker   // NOTE: This may leave the DocumentStore in an invalid/uncreated state. Users
749*8b6cd535SAndroid Build Coastguard Worker   // would need call Initialize() to reinitialize everything into a valid state.
750*8b6cd535SAndroid Build Coastguard Worker   //
751*8b6cd535SAndroid Build Coastguard Worker   // Returns:
752*8b6cd535SAndroid Build Coastguard Worker   //   On success, OptimizeResult which contains a vector mapping from old
753*8b6cd535SAndroid Build Coastguard Worker   //   document id to new document id and another vector mapping from old
754*8b6cd535SAndroid Build Coastguard Worker   //   namespace id to new namespace id. A value of kInvalidDocumentId indicates
755*8b6cd535SAndroid Build Coastguard Worker   //   that the old document id has been deleted.
756*8b6cd535SAndroid Build Coastguard Worker   //   ABORTED_ERROR if any error happens before the actual optimization, the
757*8b6cd535SAndroid Build Coastguard Worker   //                 original document store should be still available
758*8b6cd535SAndroid Build Coastguard Worker   //   DATA_LOSS_ERROR on errors that could potentially cause data loss,
759*8b6cd535SAndroid Build Coastguard Worker   //                   document store is still available
760*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on any IO errors or other errors that we can't recover
761*8b6cd535SAndroid Build Coastguard Worker   //                  from
762*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::StatusOr<DocumentStore::OptimizeResult>
763*8b6cd535SAndroid Build Coastguard Worker   OptimizeDocumentStore(std::unordered_set<std::string>&& mature_blob_handles,
764*8b6cd535SAndroid Build Coastguard Worker                         OptimizeStatsProto* optimize_stats)
765*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
766*8b6cd535SAndroid Build Coastguard Worker 
767*8b6cd535SAndroid Build Coastguard Worker   // Helper method to restore missing document data in index_, integer_index_,
768*8b6cd535SAndroid Build Coastguard Worker   // and qualified_id_join_index_. All documents will be reindexed. This does
769*8b6cd535SAndroid Build Coastguard Worker   // not clear the index, so it is recommended to call ClearAllIndices,
770*8b6cd535SAndroid Build Coastguard Worker   // ClearSearchIndices, or ClearJoinIndices first if needed.
771*8b6cd535SAndroid Build Coastguard Worker   //
772*8b6cd535SAndroid Build Coastguard Worker   // Returns:
773*8b6cd535SAndroid Build Coastguard Worker   //   On success, OK and a bool indicating whether or not restoration was
774*8b6cd535SAndroid Build Coastguard Worker   //     needed.
775*8b6cd535SAndroid Build Coastguard Worker   //   DATA_LOSS, if an error during index merging caused us to lose indexed
776*8b6cd535SAndroid Build Coastguard Worker   //     data in the main index. Despite the data loss, this is still considered
777*8b6cd535SAndroid Build Coastguard Worker   //     a successful run and needed_restoration will be set to true.
778*8b6cd535SAndroid Build Coastguard Worker   //   RESOURCE_EXHAUSTED if the index fills up before finishing indexing
779*8b6cd535SAndroid Build Coastguard Worker   //   NOT_FOUND if some Document's schema type is not in the SchemaStore
780*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on any IO errors
781*8b6cd535SAndroid Build Coastguard Worker   struct IndexRestorationResult {
782*8b6cd535SAndroid Build Coastguard Worker     libtextclassifier3::Status status;
783*8b6cd535SAndroid Build Coastguard Worker     bool index_needed_restoration;
784*8b6cd535SAndroid Build Coastguard Worker     bool integer_index_needed_restoration;
785*8b6cd535SAndroid Build Coastguard Worker     bool qualified_id_join_index_needed_restoration;
786*8b6cd535SAndroid Build Coastguard Worker     bool embedding_index_needed_restoration;
787*8b6cd535SAndroid Build Coastguard Worker   };
788*8b6cd535SAndroid Build Coastguard Worker   IndexRestorationResult RestoreIndexIfNeeded()
789*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
790*8b6cd535SAndroid Build Coastguard Worker 
791*8b6cd535SAndroid Build Coastguard Worker   // If we lost the schema during a previous failure, it may "look" the same as
792*8b6cd535SAndroid Build Coastguard Worker   // not having a schema set before: we don't have a schema proto file. So do
793*8b6cd535SAndroid Build Coastguard Worker   // some extra checks to differentiate between having-lost the schema, and
794*8b6cd535SAndroid Build Coastguard Worker   // never having a schema before. This may determine if we need to do extra
795*8b6cd535SAndroid Build Coastguard Worker   // recovery steps.
796*8b6cd535SAndroid Build Coastguard Worker   //
797*8b6cd535SAndroid Build Coastguard Worker   // Returns:
798*8b6cd535SAndroid Build Coastguard Worker   //   bool indicating if we had a schema and unintentionally lost it
799*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on I/O error
800*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::StatusOr<bool> LostPreviousSchema()
801*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
802*8b6cd535SAndroid Build Coastguard Worker 
803*8b6cd535SAndroid Build Coastguard Worker   // Helper method to create all types of data indexing handlers to index term,
804*8b6cd535SAndroid Build Coastguard Worker   // integer, and join qualified ids.
805*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::StatusOr<
806*8b6cd535SAndroid Build Coastguard Worker       std::vector<std::unique_ptr<DataIndexingHandler>>>
807*8b6cd535SAndroid Build Coastguard Worker   CreateDataIndexingHandlers() ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
808*8b6cd535SAndroid Build Coastguard Worker 
809*8b6cd535SAndroid Build Coastguard Worker   // Helper method to discard parts of (term, integer, qualified id join)
810*8b6cd535SAndroid Build Coastguard Worker   // indices if they contain data for document ids greater than
811*8b6cd535SAndroid Build Coastguard Worker   // last_stored_document_id.
812*8b6cd535SAndroid Build Coastguard Worker   //
813*8b6cd535SAndroid Build Coastguard Worker   // REQUIRES: last_stored_document_id is valid (!= kInvalidDocumentId). Note:
814*8b6cd535SAndroid Build Coastguard Worker   //   if we want to truncate everything in the index, then please call
815*8b6cd535SAndroid Build Coastguard Worker   //   ClearSearchIndices/ClearJoinIndices/ClearAllIndices instead.
816*8b6cd535SAndroid Build Coastguard Worker   //
817*8b6cd535SAndroid Build Coastguard Worker   // Returns:
818*8b6cd535SAndroid Build Coastguard Worker   //   On success, a DocumentId indicating the first document to start for
819*8b6cd535SAndroid Build Coastguard Worker   //     reindexing and 2 bool flags indicating whether term or integer index
820*8b6cd535SAndroid Build Coastguard Worker   //     needs restoration.
821*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL on any I/O errors
822*8b6cd535SAndroid Build Coastguard Worker   struct TruncateIndexResult {
823*8b6cd535SAndroid Build Coastguard Worker     DocumentId first_document_to_reindex;
824*8b6cd535SAndroid Build Coastguard Worker     bool index_needed_restoration;
825*8b6cd535SAndroid Build Coastguard Worker     bool integer_index_needed_restoration;
826*8b6cd535SAndroid Build Coastguard Worker     bool qualified_id_join_index_needed_restoration;
827*8b6cd535SAndroid Build Coastguard Worker     bool embedding_index_needed_restoration;
828*8b6cd535SAndroid Build Coastguard Worker 
TruncateIndexResultTruncateIndexResult829*8b6cd535SAndroid Build Coastguard Worker     explicit TruncateIndexResult(
830*8b6cd535SAndroid Build Coastguard Worker         DocumentId first_document_to_reindex_in,
831*8b6cd535SAndroid Build Coastguard Worker         bool index_needed_restoration_in,
832*8b6cd535SAndroid Build Coastguard Worker         bool integer_index_needed_restoration_in,
833*8b6cd535SAndroid Build Coastguard Worker         bool qualified_id_join_index_needed_restoration_in,
834*8b6cd535SAndroid Build Coastguard Worker         bool embedding_index_needed_restoration_in)
835*8b6cd535SAndroid Build Coastguard Worker         : first_document_to_reindex(first_document_to_reindex_in),
836*8b6cd535SAndroid Build Coastguard Worker           index_needed_restoration(index_needed_restoration_in),
837*8b6cd535SAndroid Build Coastguard Worker           integer_index_needed_restoration(integer_index_needed_restoration_in),
838*8b6cd535SAndroid Build Coastguard Worker           qualified_id_join_index_needed_restoration(
839*8b6cd535SAndroid Build Coastguard Worker               qualified_id_join_index_needed_restoration_in),
840*8b6cd535SAndroid Build Coastguard Worker           embedding_index_needed_restoration(
841*8b6cd535SAndroid Build Coastguard Worker               embedding_index_needed_restoration_in) {}
842*8b6cd535SAndroid Build Coastguard Worker   };
843*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::StatusOr<TruncateIndexResult> TruncateIndicesTo(
844*8b6cd535SAndroid Build Coastguard Worker       DocumentId last_stored_document_id)
845*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
846*8b6cd535SAndroid Build Coastguard Worker 
847*8b6cd535SAndroid Build Coastguard Worker   // Helper method to discard search (term, integer) indices.
848*8b6cd535SAndroid Build Coastguard Worker   //
849*8b6cd535SAndroid Build Coastguard Worker   // Returns:
850*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
851*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on any I/O errors
852*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::Status ClearSearchIndices()
853*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
854*8b6cd535SAndroid Build Coastguard Worker 
855*8b6cd535SAndroid Build Coastguard Worker   // Helper method to discard join (qualified id) indices.
856*8b6cd535SAndroid Build Coastguard Worker   //
857*8b6cd535SAndroid Build Coastguard Worker   // Returns:
858*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
859*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on any I/O errors
860*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::Status ClearJoinIndices()
861*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
862*8b6cd535SAndroid Build Coastguard Worker 
863*8b6cd535SAndroid Build Coastguard Worker   // Helper method to discard all search and join indices.
864*8b6cd535SAndroid Build Coastguard Worker   //
865*8b6cd535SAndroid Build Coastguard Worker   // Returns:
866*8b6cd535SAndroid Build Coastguard Worker   //   OK on success
867*8b6cd535SAndroid Build Coastguard Worker   //   INTERNAL_ERROR on any I/O errors
868*8b6cd535SAndroid Build Coastguard Worker   libtextclassifier3::Status ClearAllIndices()
869*8b6cd535SAndroid Build Coastguard Worker       ICING_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
870*8b6cd535SAndroid Build Coastguard Worker };
871*8b6cd535SAndroid Build Coastguard Worker 
872*8b6cd535SAndroid Build Coastguard Worker }  // namespace lib
873*8b6cd535SAndroid Build Coastguard Worker }  // namespace icing
874*8b6cd535SAndroid Build Coastguard Worker 
875*8b6cd535SAndroid Build Coastguard Worker #endif  // ICING_ICING_SEARCH_ENGINE_H_
876