forked from utopia-php/database
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.php
More file actions
377 lines (322 loc) · 11.9 KB
/
Copy pathIndex.php
File metadata and controls
377 lines (322 loc) · 11.9 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
namespace Utopia\Database\Validator;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Validator;
class Index extends Validator
{
protected string $message = 'Invalid index';
protected int $maxLength;
/**
* @var array<Document> $attributes
*/
protected array $attributes;
/**
* @var array<string> $reservedKeys
*/
protected array $reservedKeys;
protected bool $arrayIndexSupport;
protected bool $spatialIndexSupport;
protected bool $spatialIndexNullSupport;
protected bool $spatialIndexOrderSupport;
/**
* @param array<Document> $attributes
* @param int $maxLength
* @param array<string> $reservedKeys
* @param bool $arrayIndexSupport
* @param bool $spatialIndexSupport
* @param bool $spatialIndexNullSupport
* @param bool $spatialIndexOrderSupport
* @throws DatabaseException
*/
public function __construct(array $attributes, int $maxLength, array $reservedKeys = [], bool $arrayIndexSupport = false, bool $spatialIndexSupport = false, bool $spatialIndexNullSupport = false, bool $spatialIndexOrderSupport = false)
{
$this->maxLength = $maxLength;
$this->reservedKeys = $reservedKeys;
$this->arrayIndexSupport = $arrayIndexSupport;
$this->spatialIndexSupport = $spatialIndexSupport;
$this->spatialIndexNullSupport = $spatialIndexNullSupport;
$this->spatialIndexOrderSupport = $spatialIndexOrderSupport;
foreach ($attributes as $attribute) {
$key = \strtolower($attribute->getAttribute('key', $attribute->getAttribute('$id')));
$this->attributes[$key] = $attribute;
}
foreach (Database::INTERNAL_ATTRIBUTES as $attribute) {
$key = \strtolower($attribute['$id']);
$this->attributes[$key] = new Document($attribute);
}
}
/**
* Returns validator description
* @return string
*/
public function getDescription(): string
{
return $this->message;
}
/**
* @param Document $index
* @return bool
*/
public function checkAttributesNotFound(Document $index): bool
{
foreach ($index->getAttribute('attributes', []) as $attribute) {
if (!isset($this->attributes[\strtolower($attribute)])) {
$this->message = 'Invalid index attribute "' . $attribute . '" not found';
return false;
}
}
return true;
}
/**
* @param Document $index
* @return bool
*/
public function checkEmptyIndexAttributes(Document $index): bool
{
if (empty($index->getAttribute('attributes', []))) {
$this->message = 'No attributes provided for index';
return false;
}
return true;
}
/**
* @param Document $index
* @return bool
*/
public function checkDuplicatedAttributes(Document $index): bool
{
$attributes = $index->getAttribute('attributes', []);
$stack = [];
foreach ($attributes as $attribute) {
$value = \strtolower($attribute);
if (\in_array($value, $stack)) {
$this->message = 'Duplicate attributes provided';
return false;
}
$stack[] = $value;
}
return true;
}
/**
* @param Document $index
* @return bool
*/
public function checkFulltextIndexNonString(Document $index): bool
{
if ($index->getAttribute('type') === Database::INDEX_FULLTEXT) {
foreach ($index->getAttribute('attributes', []) as $attribute) {
$attribute = $this->attributes[\strtolower($attribute)] ?? new Document();
if ($attribute->getAttribute('type', '') !== Database::VAR_STRING) {
$this->message = 'Attribute "' . $attribute->getAttribute('key', $attribute->getAttribute('$id')) . '" cannot be part of a FULLTEXT index, must be of type string';
return false;
}
}
}
return true;
}
/**
* @param Document $index
* @return bool
*/
public function checkArrayIndex(Document $index): bool
{
$attributes = $index->getAttribute('attributes', []);
$orders = $index->getAttribute('orders', []);
$lengths = $index->getAttribute('lengths', []);
$arrayAttributes = [];
foreach ($attributes as $attributePosition => $attributeName) {
$attribute = $this->attributes[\strtolower($attributeName)] ?? new Document();
if ($attribute->getAttribute('array', false)) {
// Database::INDEX_UNIQUE Is not allowed! since mariaDB VS MySQL makes the unique Different on values
if ($index->getAttribute('type') != Database::INDEX_KEY) {
$this->message = '"' . ucfirst($index->getAttribute('type')) . '" index is forbidden on array attributes';
return false;
}
if (empty($lengths[$attributePosition])) {
$this->message = 'Index length for array not specified';
return false;
}
$arrayAttributes[] = $attribute->getAttribute('key', '');
if (count($arrayAttributes) > 1) {
$this->message = 'An index may only contain one array attribute';
return false;
}
$direction = $orders[$attributePosition] ?? '';
if (!empty($direction)) {
$this->message = 'Invalid index order "' . $direction . '" on array attribute "'. $attribute->getAttribute('key', '') .'"';
return false;
}
if ($this->arrayIndexSupport === false) {
$this->message = 'Indexing an array attribute is not supported';
return false;
}
} elseif ($attribute->getAttribute('type') !== Database::VAR_STRING && !empty($lengths[$attributePosition])) {
$this->message = 'Cannot set a length on "'. $attribute->getAttribute('type') . '" attributes';
return false;
}
}
return true;
}
/**
* @param Document $index
* @return bool
*/
public function checkIndexLength(Document $index): bool
{
if ($index->getAttribute('type') === Database::INDEX_FULLTEXT) {
return true;
}
$total = 0;
$lengths = $index->getAttribute('lengths', []);
$attributes = $index->getAttribute('attributes', []);
if (count($lengths) > count($attributes)) {
$this->message = 'Invalid index lengths. Count of lengths must be equal or less than the number of attributes.';
return false;
}
foreach ($attributes as $attributePosition => $attributeName) {
$attribute = $this->attributes[\strtolower($attributeName)];
switch ($attribute->getAttribute('type')) {
case Database::VAR_STRING:
$attributeSize = $attribute->getAttribute('size', 0);
$indexLength = !empty($lengths[$attributePosition]) ? $lengths[$attributePosition] : $attributeSize;
break;
case Database::VAR_FLOAT:
$attributeSize = 2; // 8 bytes / 4 mb4
$indexLength = 2;
break;
default:
$attributeSize = 1; // 4 bytes / 4 mb4
$indexLength = 1;
break;
}
if ($indexLength < 0) {
$this->message = 'Negative index length provided for ' . $attributeName;
return false;
}
if ($attribute->getAttribute('array', false)) {
$attributeSize = Database::ARRAY_INDEX_LENGTH;
$indexLength = Database::ARRAY_INDEX_LENGTH;
}
if ($indexLength > $attributeSize) {
$this->message = 'Index length ' . $indexLength . ' is larger than the size for ' . $attributeName . ': ' . $attributeSize . '"';
return false;
}
$total += $indexLength;
}
if ($total > $this->maxLength && $this->maxLength > 0) {
$this->message = 'Index length is longer than the maximum: ' . $this->maxLength;
return false;
}
return true;
}
/**
* @param Document $index
* @return bool
*/
public function checkReservedNames(Document $index): bool
{
$key = $index->getAttribute('key', $index->getAttribute('$id'));
foreach ($this->reservedKeys as $reserved) {
if (\strtolower($key) === \strtolower($reserved)) {
$this->message = 'Index key name is reserved';
return false;
}
}
return true;
}
/**
* Is valid.
*
* Returns true index if valid.
* @param Document $value
* @return bool
* @throws DatabaseException
*/
public function isValid($value): bool
{
if (!$this->checkAttributesNotFound($value)) {
return false;
}
if (!$this->checkEmptyIndexAttributes($value)) {
return false;
}
if (!$this->checkDuplicatedAttributes($value)) {
return false;
}
if (!$this->checkFulltextIndexNonString($value)) {
return false;
}
if (!$this->checkArrayIndex($value)) {
return false;
}
if (!$this->checkIndexLength($value)) {
return false;
}
if (!$this->checkReservedNames($value)) {
return false;
}
if (!$this->checkSpatialIndex($value)) {
return false;
}
return true;
}
/**
* Is array
*
* Function will return true if object is array.
*
* @return bool
*/
public function isArray(): bool
{
return false;
}
/**
* Get Type
*
* Returns validator type.
*
* @return string
*/
public function getType(): string
{
return self::TYPE_OBJECT;
}
/**
* @param Document $index
* @return bool
*/
public function checkSpatialIndex(Document $index): bool
{
$type = $index->getAttribute('type');
if ($type !== Database::INDEX_SPATIAL) {
return true;
}
if (!$this->spatialIndexSupport) {
$this->message = 'Spatial indexes are not supported';
return false;
}
$attributes = $index->getAttribute('attributes', []);
$orders = $index->getAttribute('orders', []);
foreach ($attributes as $attributeName) {
$attribute = $this->attributes[\strtolower($attributeName)] ?? new Document();
$attributeType = $attribute->getAttribute('type', '');
if (!\in_array($attributeType, Database::SPATIAL_TYPES, true)) {
$this->message = 'Spatial index can only be created on spatial attributes (point, linestring, polygon). Attribute "' . $attributeName . '" is of type "' . $attributeType . '"';
return false;
}
$required = (bool) $attribute->getAttribute('required', false);
if (!$required && !$this->spatialIndexNullSupport) {
$this->message = 'Spatial indexes do not allow null values. Mark the attribute "' . $attributeName . '" as required or create the index on a column with no null values.';
return false;
}
}
if (!empty($orders) && !$this->spatialIndexOrderSupport) {
$this->message = 'Spatial indexes with explicit orders are not supported. Remove the orders to create this index.';
return false;
}
return true;
}
}