xref: /aosp_15_r20/external/icing/icing/util/scorable_property_set.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2024 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_UTIL_SCORABLE_PROPERTY_SET_H_
16 #define ICING_UTIL_SCORABLE_PROPERTY_SET_H_
17 
18 #include <memory>
19 #include <string>
20 
21 #include "icing/text_classifier/lib3/utils/base/statusor.h"
22 #include "icing/proto/internal/scorable_property_set.pb.h"
23 #include "icing/proto/schema.pb.h"
24 #include "icing/schema/schema-store.h"
25 #include "icing/store/document-filter-data.h"
26 
27 namespace icing {
28 namespace lib {
29 
30 // A class that interprets and represents the data from the proto of
31 // ScorablePropertySetProto.
32 //
33 // This class serves as an utility to access the scorable property data.
34 class ScorablePropertySet {
35  public:
36   // Creates a class that represents the data from the proto of
37   // ScorablePropertySetProto.
38   //
39   // This function is light weight, and it doesn't perform any proto copy.
40   //
41   // Returns:
42   //   - INVALID_ARGUMENT if |schema_type_id| is invalid, or the proto data is
43   //     inconsistent with the schema config.
44   //   - FAILED_PRECONDITION if the schema hasn't been set yet
45   static libtextclassifier3::StatusOr<std::unique_ptr<ScorablePropertySet>>
46   Create(ScorablePropertySetProto&& scorable_property_set_proto,
47          SchemaTypeId schema_type_id, const SchemaStore* schema_store);
48 
49   // Creates a class that represents the data from the proto of
50   // ScorablePropertySetProto.
51   //
52   // This function converts the input |document| to the ScorablePropertySetProto
53   // under the hood.
54   //
55   // Returns:
56   //   - INVALID_ARGUMENT if |schema_type_id| is invalid, or that no scorable
57   //     property is found under the schema config of |schema_type_id|.
58   //   - FAILED_PRECONDITION if the schema hasn't been set yet
59   static libtextclassifier3::StatusOr<std::unique_ptr<ScorablePropertySet>>
60   Create(const DocumentProto& document, SchemaTypeId schema_type_id,
61          const SchemaStore* schema_store);
62 
63   // Delete copy constructor and assignment operator.
64   ScorablePropertySet(const ScorablePropertySet&) = delete;
65   ScorablePropertySet& operator=(const ScorablePropertySet&) = delete;
66 
67   // Returns the scorable property proto for the given property path.
68   //
69   // Return:
70   //   - ScorablePropertyProto on success
71   //   - nullptr if the property path is not found in the schema config
72   //     as a scorable property.
73   const ScorablePropertyProto* GetScorablePropertyProto(
74       const std::string& property_path) const;
75 
76   // Returns the reference to the underlying ScorablePropertySetProto.
GetScorablePropertySetProto()77   const ScorablePropertySetProto& GetScorablePropertySetProto() const {
78     return scorable_property_set_proto_;
79   }
80 
81  private:
82   explicit ScorablePropertySet(
83       ScorablePropertySetProto&& scorable_property_set_proto,
84       SchemaTypeId schema_type_id, const SchemaStore* schema_store);
85 
86   const ScorablePropertySetProto scorable_property_set_proto_;
87   const SchemaTypeId schema_type_id_;
88   const SchemaStore* schema_store_;
89 };
90 
91 }  // namespace lib
92 }  // namespace icing
93 
94 #endif  // ICING_UTIL_SCORABLE_PROPERTY_SET_H_
95