xref: /aosp_15_r20/external/icing/icing/schema/backup-schema-producer.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ICING_SCHEMA_BACKUP_SCHEMA_PRODUCER_H_
16 #define ICING_SCHEMA_BACKUP_SCHEMA_PRODUCER_H_
17 
18 #include "icing/text_classifier/lib3/utils/base/statusor.h"
19 #include "icing/proto/schema.pb.h"
20 #include "icing/schema/section-manager.h"
21 #include "icing/schema/section.h"
22 
23 namespace icing {
24 namespace lib {
25 
26 class BackupSchemaProducer {
27  public:
28   // Creates a BackupSchemaProducer based off of schema.
29   // If schema doesn't require a backup schema (because it is fully
30   // rollback-proof) then no copies will be made and `is_backup_necessary` will
31   // return false.
32   // If schema *does* require a backup schema, then `is_backup_necessary` will
33   // return true and the backup schema can be retrieved by calling `Produce`.
34   // Returns:
35   //   - On success, a BackupSchemaProducer
36   //   - INTERNAL_ERROR if the schema is inconsistent with the type_manager.
37   static libtextclassifier3::StatusOr<BackupSchemaProducer> Create(
38       const SchemaProto& schema, const SectionManager& type_manager);
39 
Produce()40   SchemaProto Produce() && { return std::move(cached_schema_); }
41 
is_backup_necessary()42   bool is_backup_necessary() const { return !cached_schema_.types().empty(); }
43 
44  private:
45   BackupSchemaProducer() = default;
BackupSchemaProducer(SchemaProto && schema)46   explicit BackupSchemaProducer(SchemaProto&& schema)
47       : cached_schema_(std::move(schema)) {}
48 
49   SchemaProto cached_schema_;
50 };
51 
52 }  // namespace lib
53 }  // namespace icing
54 
55 #endif  // ICING_SCHEMA_BACKUP_SCHEMA_PRODUCER_H_
56