aboutsummaryrefslogtreecommitdiff
path: root/icing/schema/joinable-property-manager.h
blob: a175ae413b1034631a0509665eeaeaf8974df217 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (C) 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef ICING_SCHEMA_JOINABLE_PROPERTY_MANAGER_H_
#define ICING_SCHEMA_JOINABLE_PROPERTY_MANAGER_H_

#include <memory>
#include <string>
#include <vector>

#include "icing/text_classifier/lib3/utils/base/status.h"
#include "icing/text_classifier/lib3/utils/base/statusor.h"
#include "icing/proto/document.pb.h"
#include "icing/schema/joinable-property.h"
#include "icing/store/document-filter-data.h"
#include "icing/store/key-mapper.h"

namespace icing {
namespace lib {

// This class provides joinable-property-related operations. It assigns joinable
// properties according to JoinableConfig and extracts joinable property values
// from documents.
class JoinablePropertyManager {
 public:
  // Builder class to create a JoinablePropertyManager which does not take
  // ownership of any input components, and all pointers must refer to valid
  // objects that outlive the created JoinablePropertyManager instance.
  class Builder {
   public:
    explicit Builder(const KeyMapper<SchemaTypeId>& schema_type_mapper)
        : schema_type_mapper_(schema_type_mapper),
          joinable_property_metadata_cache_(schema_type_mapper.num_keys()) {}

    // Checks and appends a new JoinablePropertyMetadata for the schema type id
    // if the given property config is joinable.
    //
    // Returns:
    //   - OK on success
    //   - INVALID_ARGUMENT_ERROR if schema type id is invalid (not in range [0,
    //     schema_type_mapper_.num_keys() - 1])
    //   - OUT_OF_RANGE_ERROR if # of joinable properties in a single Schema
    //     exceeds the threshold (kTotalNumJoinableProperties)
    libtextclassifier3::Status ProcessSchemaTypePropertyConfig(
        SchemaTypeId schema_type_id, const PropertyConfigProto& property_config,
        std::string&& property_path);

    // Builds and returns a JoinablePropertyManager instance.
    std::unique_ptr<JoinablePropertyManager> Build() && {
      return std::unique_ptr<JoinablePropertyManager>(
          new JoinablePropertyManager(
              schema_type_mapper_,
              std::move(joinable_property_metadata_cache_)));
    }

   private:
    const KeyMapper<SchemaTypeId>& schema_type_mapper_;  // Does not own.
    std::vector<std::vector<JoinablePropertyMetadata>>
        joinable_property_metadata_cache_;
  };

  JoinablePropertyManager(const JoinablePropertyManager&) = delete;
  JoinablePropertyManager& operator=(const JoinablePropertyManager&) = delete;

  // Extracts all joinable property contents of different types from the given
  // document and group them by joinable value type.
  // - Joinable properties are sorted by joinable property id in ascending
  //   order.
  // - Joinable property ids start from 0.
  // - Joinable properties with empty content won't be returned.
  //
  // Returns:
  //   - A JoinablePropertyGroup instance on success
  //   - NOT_FOUND_ERROR if the type config name of document is not present in
  //     schema_type_mapper_
  libtextclassifier3::StatusOr<JoinablePropertyGroup> ExtractJoinableProperties(
      const DocumentProto& document) const;

  // Returns the JoinablePropertyMetadata associated with the JoinablePropertyId
  // that's in the SchemaTypeId.
  //
  // Returns:
  //   - Pointer to JoinablePropertyMetadata on success
  //   - INVALID_ARGUMENT_ERROR if schema type id or JoinablePropertyId is
  //     invalid
  libtextclassifier3::StatusOr<const JoinablePropertyMetadata*>
  GetJoinablePropertyMetadata(SchemaTypeId schema_type_id,
                              JoinablePropertyId joinable_property_id) const;

  // Returns:
  //   - On success, the joinable property metadatas for the specified type
  //   - NOT_FOUND_ERROR if the type config name is not present in
  //     schema_type_mapper_
  libtextclassifier3::StatusOr<const std::vector<JoinablePropertyMetadata>*>
  GetMetadataList(const std::string& type_config_name) const;

 private:
  explicit JoinablePropertyManager(
      const KeyMapper<SchemaTypeId>& schema_type_mapper,
      std::vector<std::vector<JoinablePropertyMetadata>>&&
          joinable_property_metadata_cache)
      : schema_type_mapper_(schema_type_mapper),
        joinable_property_metadata_cache_(joinable_property_metadata_cache) {}

  // Maps schema types to a densely-assigned unique id.
  const KeyMapper<SchemaTypeId>& schema_type_mapper_;  // Does not own

  // The index of joinable_property_metadata_cache_ corresponds to a schema
  // type's SchemaTypeId. At that SchemaTypeId index, we store an inner vector.
  // The inner vector's index corresponds to a joinable property's
  // JoinablePropertyId. At the JoinablePropertyId index, we store the
  // JoinablePropertyMetadata of that joinable property.
  //
  // For example, suppose "email" has a SchemaTypeId of 0 and it has a joinable
  // property called "senderQualifiedId" with a JoinablePropertyId of 1. Then
  // the "senderQualifiedId" property's JoinablePropertyMetadata will be at
  // joinable_property_metadata_cache_[0][1].
  const std::vector<std::vector<JoinablePropertyMetadata>>
      joinable_property_metadata_cache_;
};

}  // namespace lib
}  // namespace icing

#endif  // ICING_SCHEMA_JOINABLE_PROPERTY_MANAGER_H_