Skip to content

Commit 8bd5e0e

Browse files
Refactor index validation and enhance support checks in Database and Validator classes
1 parent ccb69f0 commit 8bd5e0e

4 files changed

Lines changed: 76 additions & 27 deletions

File tree

src/Database/Database.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,6 @@ class Database
8787
public const INDEX_HNSW_DOT = 'hnsw_dot';
8888
public const INDEX_TRIGRAM = 'trigram';
8989

90-
public const VALID_INDEX_TYPES = [
91-
self::INDEX_KEY,
92-
self::INDEX_UNIQUE,
93-
self::INDEX_FULLTEXT,
94-
self::INDEX_SPATIAL,
95-
self::INDEX_OBJECT,
96-
self::INDEX_HNSW_EUCLIDEAN,
97-
self::INDEX_HNSW_COSINE,
98-
self::INDEX_HNSW_DOT,
99-
self::INDEX_TRIGRAM,
100-
];
101-
10290
// Max limits
10391
public const MAX_INT = 2147483647;
10492
public const MAX_BIG_INT = PHP_INT_MAX;
@@ -3646,13 +3634,6 @@ public function createIndex(string $collection, string $id, string $type, array
36463634
throw new LimitException('Index limit reached. Cannot create new index.');
36473635
}
36483636

3649-
if (!\in_array($type, self::VALID_INDEX_TYPES, true)) {
3650-
throw new DatabaseException(
3651-
'Unknown index type: ' . $type . '. Must be one of ' .
3652-
\implode(', ', \array_map(fn ($t) => $t, self::VALID_INDEX_TYPES))
3653-
);
3654-
}
3655-
36563637
/** @var array<Document> $collectionAttributes */
36573638
$collectionAttributes = $collection->getAttribute('attributes', []);
36583639
$indexAttributesWithTypes = [];

src/Database/Validator/Index.php

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ public function isArray(): bool
108108
*/
109109
public function isValid($value): bool
110110
{
111+
if (!$this->checkValidIndex($value)) {
112+
return false;
113+
}
111114
if (!$this->checkValidAttributes($value)) {
112115
return false;
113116
}
@@ -156,6 +159,76 @@ public function isValid($value): bool
156159
return true;
157160
}
158161

162+
/**
163+
* @param Document $index
164+
* @return bool
165+
*/
166+
public function checkValidIndex(Document $index): bool
167+
{
168+
$type = $index->getAttribute('type');
169+
switch ($type) {
170+
case Database::INDEX_KEY:
171+
if (!$this->supportForKeyIndexes) {
172+
$this->message = 'Key index is not supported';
173+
return false;
174+
}
175+
break;
176+
177+
case Database::INDEX_UNIQUE:
178+
if (!$this->supportForUniqueIndexes) {
179+
$this->message = 'Unique index is not supported';
180+
return false;
181+
}
182+
break;
183+
184+
case Database::INDEX_FULLTEXT:
185+
if (!$this->supportForFulltextIndexes) {
186+
$this->message = 'Fulltext index is not supported';
187+
return false;
188+
}
189+
break;
190+
191+
case Database::INDEX_SPATIAL:
192+
if (!$this->supportForSpatialIndexes) {
193+
$this->message = 'Spatial indexes are not supported';
194+
return false;
195+
}
196+
if (!empty($index->getAttribute('orders')) && !$this->supportForSpatialIndexOrder) {
197+
$this->message = 'Spatial indexes with explicit orders are not supported. Remove the orders to create this index.';
198+
return false;
199+
}
200+
break;
201+
202+
case Database::INDEX_HNSW_EUCLIDEAN:
203+
case Database::INDEX_HNSW_COSINE:
204+
case Database::INDEX_HNSW_DOT:
205+
if (!$this->supportForVectorIndexes) {
206+
$this->message = 'Vector indexes are not supported';
207+
return false;
208+
}
209+
break;
210+
211+
case Database::INDEX_OBJECT:
212+
if (!$this->supportForObjectIndexes) {
213+
$this->message = 'Object indexes are not supported';
214+
return false;
215+
}
216+
break;
217+
218+
case Database::INDEX_TRIGRAM:
219+
if (!$this->supportForTrigramIndexes) {
220+
$this->message = 'Trigram indexes are not supported';
221+
return false;
222+
}
223+
break;
224+
225+
default:
226+
$this->message = 'Unknown index type: ' . $type . '. Must be one of ' . Database::INDEX_KEY . ', ' . Database::INDEX_UNIQUE . ', ' . Database::INDEX_FULLTEXT . ', ' . Database::INDEX_SPATIAL . ', ' . Database::INDEX_OBJECT . ', ' . Database::INDEX_HNSW_EUCLIDEAN . ', ' . Database::INDEX_HNSW_COSINE . ', ' . Database::INDEX_HNSW_DOT . ', '.Database::INDEX_TRIGRAM;
227+
return false;
228+
}
229+
return true;
230+
}
231+
159232
/**
160233
* @param Document $index
161234
* @return bool
@@ -539,11 +612,6 @@ public function checkKeyUniqueFulltextSupport(Document $index): bool
539612
return false;
540613
}
541614

542-
if ($type === Database::INDEX_FULLTEXT && $this->supportForFulltextIndexes === false) {
543-
$this->message = 'Fulltext index is not supported';
544-
return false;
545-
}
546-
547615
return true;
548616
}
549617

tests/e2e/Adapter/Scopes/AttributeTests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ public function testArrayAttribute(): void
15311531
if ($database->getAdapter()->getSupportForIndexArray()) {
15321532
if ($database->getAdapter()->getSupportForAttributes() && $database->getAdapter()->getMaxIndexLength() > 0) {
15331533
// If getMaxIndexLength() > 0 We clear length for array attributes
1534-
$database->createIndex($collection, 'indx1', Database::INDEX_KEY, ['long_size'], [], []);
1534+
$database->createIndex($collection, 'indx1', Database::INDEX_KEY, ['long_size'], [100], []);
15351535
$database->deleteIndex($collection, 'indx1');
15361536
$database->createIndex($collection, 'indx2', Database::INDEX_KEY, ['long_size'], [1000], []);
15371537

tests/e2e/Adapter/Scopes/IndexTests.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public function testIndexValidation(): void
267267
$database->getAdapter()->getSupportForAttributes(),
268268
$database->getAdapter()->getSupportForMultipleFulltextIndexes(),
269269
$database->getAdapter()->getSupportForIdenticalIndexes(),
270-
false,
270+
$database->getAdapter()->getSupportForObject(),
271271
$database->getAdapter()->getSupportForTrigramIndex()
272272
);
273273

@@ -285,7 +285,7 @@ public function testIndexValidation(): void
285285
$this->fail('Failed to throw exception');
286286
}
287287
} catch (Exception $e) {
288-
$this->assertEquals('Attribute "integer" cannot be part of a fulltext index, must be of type string', $e->getMessage());
288+
$this->assertEquals('Fulltext index is not supported', $e->getMessage());
289289
}
290290

291291

0 commit comments

Comments
 (0)