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