Skip to content

Commit 99beaf1

Browse files
authored
Merge pull request #674 from ArnabChatterjee20k/spatial-attribute-support
added index validtor for spatial index
2 parents 653e19d + 0f72b9b commit 99beaf1

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

src/Database/Database.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,10 @@ public function createCollection(string $id, array $attributes = [], array $inde
13061306
$attributes,
13071307
$this->adapter->getMaxIndexLength(),
13081308
$this->adapter->getInternalIndexesKeys(),
1309-
$this->adapter->getSupportForIndexArray()
1309+
$this->adapter->getSupportForIndexArray(),
1310+
$this->adapter->getSupportForSpatialAttributes(),
1311+
$this->adapter->getSupportForSpatialIndexNull(),
1312+
$this->adapter->getSupportForSpatialIndexOrder(),
13101313
);
13111314
foreach ($indexes as $index) {
13121315
if (!$validator->isValid($index)) {
@@ -2251,7 +2254,10 @@ public function updateAttribute(string $collection, string $id, ?string $type =
22512254
$attributes,
22522255
$this->adapter->getMaxIndexLength(),
22532256
$this->adapter->getInternalIndexesKeys(),
2254-
$this->adapter->getSupportForIndexArray()
2257+
$this->adapter->getSupportForIndexArray(),
2258+
$this->adapter->getSupportForSpatialAttributes(),
2259+
$this->adapter->getSupportForSpatialIndexNull(),
2260+
$this->adapter->getSupportForSpatialIndexOrder(),
22552261
);
22562262

22572263
foreach ($indexes as $index) {
@@ -3191,7 +3197,10 @@ public function createIndex(string $collection, string $id, string $type, array
31913197
$collection->getAttribute('attributes', []),
31923198
$this->adapter->getMaxIndexLength(),
31933199
$this->adapter->getInternalIndexesKeys(),
3194-
$this->adapter->getSupportForIndexArray()
3200+
$this->adapter->getSupportForIndexArray(),
3201+
$this->adapter->getSupportForSpatialAttributes(),
3202+
$this->adapter->getSupportForSpatialIndexNull(),
3203+
$this->adapter->getSupportForSpatialIndexOrder(),
31953204
);
31963205
if (!$validator->isValid($index)) {
31973206
throw new IndexException($validator->getDescription());

src/Database/Validator/Index.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,30 @@ class Index extends Validator
2424

2525
protected bool $arrayIndexSupport;
2626

27+
protected bool $spatialIndexSupport;
28+
29+
protected bool $spatialIndexNullSupport;
30+
31+
protected bool $spatialIndexOrderSupport;
32+
2733
/**
2834
* @param array<Document> $attributes
2935
* @param int $maxLength
3036
* @param array<string> $reservedKeys
3137
* @param bool $arrayIndexSupport
38+
* @param bool $spatialIndexSupport
39+
* @param bool $spatialIndexNullSupport
40+
* @param bool $spatialIndexOrderSupport
3241
* @throws DatabaseException
3342
*/
34-
public function __construct(array $attributes, int $maxLength, array $reservedKeys = [], bool $arrayIndexSupport = false)
43+
public function __construct(array $attributes, int $maxLength, array $reservedKeys = [], bool $arrayIndexSupport = false, bool $spatialIndexSupport = false, bool $spatialIndexNullSupport = false, bool $spatialIndexOrderSupport = false)
3544
{
3645
$this->maxLength = $maxLength;
3746
$this->reservedKeys = $reservedKeys;
3847
$this->arrayIndexSupport = $arrayIndexSupport;
48+
$this->spatialIndexSupport = $spatialIndexSupport;
49+
$this->spatialIndexNullSupport = $spatialIndexNullSupport;
50+
$this->spatialIndexOrderSupport = $spatialIndexOrderSupport;
3951

4052
foreach ($attributes as $attribute) {
4153
$key = \strtolower($attribute->getAttribute('key', $attribute->getAttribute('$id')));
@@ -289,6 +301,10 @@ public function isValid($value): bool
289301
return false;
290302
}
291303

304+
if (!$this->checkSpatialIndex($value)) {
305+
return false;
306+
}
307+
292308
return true;
293309
}
294310

@@ -315,4 +331,47 @@ public function getType(): string
315331
{
316332
return self::TYPE_OBJECT;
317333
}
334+
335+
/**
336+
* @param Document $index
337+
* @return bool
338+
*/
339+
public function checkSpatialIndex(Document $index): bool
340+
{
341+
$type = $index->getAttribute('type');
342+
if ($type !== Database::INDEX_SPATIAL) {
343+
return true;
344+
}
345+
346+
if (!$this->spatialIndexSupport) {
347+
$this->message = 'Spatial indexes are not supported';
348+
return false;
349+
}
350+
351+
$attributes = $index->getAttribute('attributes', []);
352+
$orders = $index->getAttribute('orders', []);
353+
354+
foreach ($attributes as $attributeName) {
355+
$attribute = $this->attributes[\strtolower($attributeName)] ?? new Document();
356+
$attributeType = $attribute->getAttribute('type', '');
357+
358+
if (!\in_array($attributeType, Database::SPATIAL_TYPES, true)) {
359+
$this->message = 'Spatial index can only be created on spatial attributes (point, linestring, polygon). Attribute "' . $attributeName . '" is of type "' . $attributeType . '"';
360+
return false;
361+
}
362+
363+
$required = (bool) $attribute->getAttribute('required', false);
364+
if (!$required && !$this->spatialIndexNullSupport) {
365+
$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.';
366+
return false;
367+
}
368+
}
369+
370+
if (!empty($orders) && !$this->spatialIndexOrderSupport) {
371+
$this->message = 'Spatial indexes with explicit orders are not supported. Remove the orders to create this index.';
372+
return false;
373+
}
374+
375+
return true;
376+
}
318377
}

0 commit comments

Comments
 (0)