• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 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_TESTING_SCHEMA_GENERATOR_H_
16 #define ICING_TESTING_SCHEMA_GENERATOR_H_
17 
18 #include <random>
19 #include <string>
20 
21 #include "icing/proto/schema.pb.h"
22 
23 namespace icing {
24 namespace lib {
25 
26 class ExactStringPropertyGenerator {
27  public:
operator()28   PropertyConfigProto operator()(std::string_view name) const {
29     PropertyConfigProto prop;
30     prop.set_property_name(name.data(), name.length());
31     prop.set_data_type(PropertyConfigProto::DataType::STRING);
32     prop.set_cardinality(PropertyConfigProto::Cardinality::OPTIONAL);
33     StringIndexingConfig* string_indexing_config =
34         prop.mutable_string_indexing_config();
35     string_indexing_config->set_term_match_type(TermMatchType::EXACT_ONLY);
36     string_indexing_config->set_tokenizer_type(
37         StringIndexingConfig::TokenizerType::PLAIN);
38     return prop;
39   }
40 };
41 
42 // Schema generator with random number of properties
43 template <typename Rand, typename PropertyGenerator>
44 class RandomSchemaGenerator {
45  public:
RandomSchemaGenerator(Rand * rand,PropertyGenerator * prop_generator)46   explicit RandomSchemaGenerator(Rand* rand, PropertyGenerator* prop_generator)
47       : rand_(rand), prop_generator_(prop_generator) {}
48 
GenerateSchema(int num_types,int max_num_properties)49   SchemaProto GenerateSchema(int num_types, int max_num_properties) {
50     SchemaProto schema;
51     std::uniform_int_distribution<> dist(1, max_num_properties);
52     while (--num_types >= 0) {
53       int num_properties = dist(*rand_);
54       SetType(schema.add_types(), "Type" + std::to_string(num_types),
55               num_properties);
56     }
57     return schema;
58   }
59 
60  private:
SetType(SchemaTypeConfigProto * type_config,std::string_view name,int num_properties)61   void SetType(SchemaTypeConfigProto* type_config, std::string_view name,
62                int num_properties) const {
63     type_config->set_schema_type(name.data(), name.length());
64     while (--num_properties >= 0) {
65       std::string prop_name = "Prop" + std::to_string(num_properties);
66       (*type_config->add_properties()) = (*prop_generator_)(prop_name);
67     }
68   }
69 
70   Rand* rand_;
71   PropertyGenerator* prop_generator_;
72 };
73 
74 // Schema generator with number of properties specified by the caller
75 template <typename PropertyGenerator>
76 class SchemaGenerator {
77  public:
SchemaGenerator(int num_properties,PropertyGenerator * prop_generator)78   explicit SchemaGenerator(int num_properties,
79                            PropertyGenerator* prop_generator)
80       : num_properties_(num_properties), prop_generator_(prop_generator) {}
81 
GenerateSchema(int num_types)82   SchemaProto GenerateSchema(int num_types) {
83     SchemaProto schema;
84     while (--num_types >= 0) {
85       SetType(schema.add_types(), "Type" + std::to_string(num_types),
86               num_properties_);
87     }
88     return schema;
89   }
90 
91  private:
SetType(SchemaTypeConfigProto * type_config,std::string_view name,int num_properties)92   void SetType(SchemaTypeConfigProto* type_config, std::string_view name,
93                int num_properties) const {
94     type_config->set_schema_type(name.data(), name.length());
95     while (--num_properties >= 0) {
96       std::string prop_name = "Prop" + std::to_string(num_properties);
97       (*type_config->add_properties()) = (*prop_generator_)(prop_name);
98     }
99   }
100 
101   int num_properties_;
102   PropertyGenerator* prop_generator_;
103 };
104 
105 }  // namespace lib
106 }  // namespace icing
107 
108 #endif  // ICING_TESTING_SCHEMA_GENERATOR_H_
109