-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathTypeInformation.cs
More file actions
202 lines (173 loc) · 6.22 KB
/
TypeInformation.cs
File metadata and controls
202 lines (173 loc) · 6.22 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Waher.Persistence.FullTextSearch.Tokenizers;
using Waher.Persistence.Serialization;
using Waher.Runtime.Collections;
namespace Waher.Persistence.FullTextSearch
{
/// <summary>
/// Contains information about a type, in relation to full-text-search.
/// </summary>
internal class TypeInformation
{
private readonly IEnumerable<FullTextSearchAttribute> searchAttributes;
private readonly FullTextSearchAttribute firstSearchAttribute;
private readonly ITokenizer customTokenizer;
private readonly PropertyDefinition[] properties;
private readonly bool hasCustomTokenizer;
private readonly bool hasPropertyDefinitions;
private readonly bool dynamicIndexCollection;
/// <summary>
/// Contains information about a type, in relation to full-text-search.
/// </summary>
/// <param name="Type">Type</param>
/// <param name="TypeInfo">Type information.</param>
/// <param name="CollectionName">Collection name for objects of the corresponding type.</param>
/// <param name="CollectionInformation">Indexing information for a collection.</param>
/// <param name="CustomTokenizer">Optional custom tokenizer creating tokens for objects
/// of this type.</param>
/// <param name="SearchAttributes">Full-text-search attributes.</param>
public TypeInformation(Type Type, TypeInfo TypeInfo, string CollectionName,
CollectionInformation CollectionInformation, ITokenizer CustomTokenizer,
IEnumerable<FullTextSearchAttribute> SearchAttributes)
{
this.Type = Type;
this.TypeInfo = TypeInfo;
this.CollectionName = CollectionName;
this.HasCollection = !string.IsNullOrEmpty(CollectionName);
this.CollectionInformation = CollectionInformation;
this.customTokenizer = CustomTokenizer;
this.hasCustomTokenizer = !(CustomTokenizer is null);
this.searchAttributes = SearchAttributes;
this.firstSearchAttribute = null;
this.dynamicIndexCollection = false;
this.hasPropertyDefinitions = false;
if (!(this.searchAttributes is null))
{
Dictionary<string, bool> HasPropertyDefinition = null;
ChunkedList<PropertyDefinition> PropertyDefinitions = null;
foreach (FullTextSearchAttribute Attribute in this.searchAttributes)
{
if (this.firstSearchAttribute is null)
this.firstSearchAttribute = Attribute;
if (Attribute.DynamicIndexCollection)
this.dynamicIndexCollection = true;
if (Attribute.HasPropertyDefinitions)
{
this.hasPropertyDefinitions = true;
if (PropertyDefinitions is null)
{
HasPropertyDefinition = new Dictionary<string, bool>();
PropertyDefinitions = new ChunkedList<PropertyDefinition>();
}
foreach (PropertyDefinition PDef in Attribute.Properties)
{
if (!HasPropertyDefinition.ContainsKey(PDef.Definition))
{
HasPropertyDefinition[PDef.Definition] = true;
PropertyDefinitions.Add(PDef);
}
}
}
}
this.properties = PropertyDefinitions?.ToArray();
}
}
/// <summary>
/// Type
/// </summary>
public Type Type { get; }
/// <summary>
/// Type information.
/// </summary>
public TypeInfo TypeInfo { get; }
/// <summary>
/// Collection name for objects of the corresponding type.
/// </summary>
public string CollectionName { get; }
/// <summary>
/// If a collection-name is defined.
/// </summary>
public bool HasCollection { get; }
/// <summary>
/// Indexing information for a collection.
/// </summary>
public CollectionInformation CollectionInformation { get; }
/// <summary>
/// Optional custom tokenizer;
/// </summary>
public ITokenizer CustomTokenizer => this.customTokenizer;
/// <summary>
/// If the type has a custom tokenizer.
/// </summary>
public bool HasCustomTokenizer => this.hasCustomTokenizer;
/// <summary>
/// If the index collection is dynamic (i.e. depends on object instance).
/// </summary>
public bool DynamicIndexCollection => this.dynamicIndexCollection;
/// <summary>
/// If the type has property definitions.
/// </summary>
public bool HasPropertyDefinitions => this.hasPropertyDefinitions;
/// <summary>
/// Property definitions.
/// </summary>
public PropertyDefinition[] Properties => this.properties;
/// <summary>
/// Name of full-text-search index collection.
/// </summary>
/// <param name="Reference">Object reference.</param>
public string GetIndexCollection(object Reference)
{
return this.firstSearchAttribute?.GetIndexCollection(Reference)
?? this.CollectionInformation.IndexCollectionName;
}
/// <summary>
/// Name of full-text-search index collection.
/// </summary>
/// <param name="Reference">Object reference.</param>
public string GetIndexCollection(GenericObject Reference)
{
return this.firstSearchAttribute?.GetIndexCollection(Reference)
?? this.CollectionInformation.IndexCollectionName;
}
/// <summary>
/// Tokenizes properties in an object, given a set of property names.
/// </summary>
/// <param name="Obj">Object instance to tokenize.</param>
/// <param name="Properties">Indexable properties.</param>
/// <returns>Tokens found.</returns>
public async Task<TokenCount[]> Tokenize(object Obj, params PropertyDefinition[] Properties)
{
TokenizationProcess Process = new TokenizationProcess();
await this.Tokenize(Obj, Process, Properties);
return Process.ToArray();
}
/// <summary>
/// Tokenizes properties in an object, given a set of property names.
/// </summary>
/// <param name="Obj">Object instance to tokenize.</param>
/// <param name="Process">Tokenization process.</param>
/// <param name="Properties">Indexable properties.</param>
public async Task Tokenize(object Obj, TokenizationProcess Process, params PropertyDefinition[] Properties)
{
if (this.hasCustomTokenizer)
await this.customTokenizer.Tokenize(Obj, Process);
else
{
ChunkedList<object> Values = new ChunkedList<object>();
object Value;
foreach (PropertyDefinition Property in Properties)
{
Value = await Property.GetValue(Obj);
if (!(Value is null))
Values.Add(Value);
}
if (Values.HasFirstItem)
await FullTextSearchModule.Tokenize(Values, Process);
}
}
}
}