Skip to content

Commit 2cb3d98

Browse files
renamed gin to object index to have a general term
1 parent 739a4c3 commit 2cb3d98

5 files changed

Lines changed: 55 additions & 51 deletions

File tree

src/Database/Adapter/Postgres.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,8 @@ public function createIndex(string $collection, string $id, string $type, array
886886
Database::INDEX_HNSW_COSINE,
887887
Database::INDEX_HNSW_DOT => 'INDEX',
888888
Database::INDEX_UNIQUE => 'UNIQUE INDEX',
889-
Database::INDEX_GIN => 'INDEX',
890-
default => throw new DatabaseException('Unknown index type: ' . $type . '. Must be one of ' . Database::INDEX_KEY . ', ' . Database::INDEX_UNIQUE . ', ' . Database::INDEX_FULLTEXT . ', ' . Database::INDEX_SPATIAL . ', ' . Database::INDEX_GIN . ', ' . Database::INDEX_HNSW_EUCLIDEAN . ', ' . Database::INDEX_HNSW_COSINE . ', ' . Database::INDEX_HNSW_DOT),
889+
Database::Index_Object => 'INDEX',
890+
default => throw new DatabaseException('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),
891891
};
892892

893893
$key = "\"{$this->getNamespace()}_{$this->tenant}_{$collection}_{$id}\"";
@@ -906,7 +906,7 @@ public function createIndex(string $collection, string $id, string $type, array
906906
Database::INDEX_HNSW_EUCLIDEAN => " USING HNSW ({$attributes} vector_l2_ops)",
907907
Database::INDEX_HNSW_COSINE => " USING HNSW ({$attributes} vector_cosine_ops)",
908908
Database::INDEX_HNSW_DOT => " USING HNSW ({$attributes} vector_ip_ops)",
909-
Database::INDEX_GIN => " USING GIN ({$attributes})",
909+
Database::Index_Object => " USING GIN ({$attributes})",
910910
default => " ({$attributes})",
911911
};
912912

src/Database/Database.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class Database
7373
public const INDEX_FULLTEXT = 'fulltext';
7474
public const INDEX_UNIQUE = 'unique';
7575
public const INDEX_SPATIAL = 'spatial';
76-
public const INDEX_GIN = 'gin';
76+
// keeping
77+
public const Index_Object = 'object';
7778
public const INDEX_HNSW_EUCLIDEAN = 'hnsw_euclidean';
7879
public const INDEX_HNSW_COSINE = 'hnsw_cosine';
7980
public const INDEX_HNSW_DOT = 'hnsw_dot';
@@ -3489,14 +3490,14 @@ public function createIndex(string $collection, string $id, string $type, array
34893490
}
34903491
break;
34913492

3492-
case self::INDEX_GIN:
3493+
case self::Index_Object:
34933494
if (!$this->adapter->getSupportForObject()) {
3494-
throw new DatabaseException('GIN indexes are not supported');
3495+
throw new DatabaseException('Object indexes are not supported');
34953496
}
34963497
break;
34973498

34983499
default:
3499-
throw new DatabaseException('Unknown index type: ' . $type . '. Must be one of ' . Database::INDEX_KEY . ', ' . Database::INDEX_UNIQUE . ', ' . Database::INDEX_FULLTEXT . ', ' . Database::INDEX_SPATIAL . ', ' . Database::INDEX_GIN . ', ' . Database::INDEX_HNSW_EUCLIDEAN . ', ' . Database::INDEX_HNSW_COSINE . ', ' . Database::INDEX_HNSW_DOT);
3500+
throw new DatabaseException('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);
35003501
}
35013502

35023503
/** @var array<Document> $collectionAttributes */
@@ -3528,9 +3529,9 @@ public function createIndex(string $collection, string $id, string $type, array
35283529
}
35293530
}
35303531

3531-
if ($type === self::INDEX_GIN) {
3532+
if ($type === self::Index_Object) {
35323533
if (count($attributes) !== 1) {
3533-
throw new IndexException('GIN index can be created on a single object attribute');
3534+
throw new IndexException('Object index can be created on a single object attribute');
35343535
}
35353536

35363537
foreach ($attributes as $attr) {
@@ -3540,12 +3541,12 @@ public function createIndex(string $collection, string $id, string $type, array
35403541

35413542
$attributeType = $indexAttributesWithTypes[$attr];
35423543
if ($attributeType !== self::TYPE_OBJECT) {
3543-
throw new IndexException('GIN index can only be created on object attributes. Attribute "' . $attr . '" is of type "' . $attributeType . '"');
3544+
throw new IndexException('Object index can only be created on object attributes. Attribute "' . $attr . '" is of type "' . $attributeType . '"');
35443545
}
35453546
}
35463547

35473548
if (!empty($orders)) {
3548-
throw new IndexException('GIN indexes do not support explicit orders. Remove the orders to create this index.');
3549+
throw new IndexException('Object indexes do not support explicit orders. Remove the orders to create this index.');
35493550
}
35503551
}
35513552

src/Database/Validator/Index.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Index extends Validator
2828
* @param bool $supportForAttributes
2929
* @param bool $supportForMultipleFulltextIndexes
3030
* @param bool $supportForIdenticalIndexes
31-
* @param bool $objectIndexSupport
31+
* @param bool $supportForObjectIndexes
3232
* @throws DatabaseException
3333
*/
3434
public function __construct(
@@ -43,7 +43,7 @@ public function __construct(
4343
protected bool $supportForAttributes = true,
4444
protected bool $supportForMultipleFulltextIndexes = true,
4545
protected bool $supportForIdenticalIndexes = true,
46-
protected bool $objectIndexSupport = false
46+
protected bool $supportForObjectIndexes = false
4747
) {
4848
foreach ($attributes as $attribute) {
4949
$key = \strtolower($attribute->getAttribute('key', $attribute->getAttribute('$id')));
@@ -134,6 +134,9 @@ public function isValid($value): bool
134134
if (!$this->checkIdenticalIndexes($value)) {
135135
return false;
136136
}
137+
if (!$this->checkObjectIndexes($value)) {
138+
return false;
139+
}
137140
return true;
138141
}
139142

@@ -536,29 +539,29 @@ public function checkIdenticalIndexes(Document $index): bool
536539
* @param Document $index
537540
* @return bool
538541
*/
539-
public function checkGinIndex(Document $index): bool
542+
public function checkObjectIndexes(Document $index): bool
540543
{
541544
$type = $index->getAttribute('type');
542545

543546
$attributes = $index->getAttribute('attributes', []);
544547
$orders = $index->getAttribute('orders', []);
545548

546-
if ($type !== Database::INDEX_GIN) {
549+
if ($type !== Database::Index_Object) {
547550
return true;
548551
}
549552

550-
if (!$this->objectIndexSupport) {
551-
$this->message = 'GIN indexes are not supported';
553+
if (!$this->supportForObjectIndexes) {
554+
$this->message = 'Object indexes are not supported';
552555
return false;
553556
}
554557

555558
if (count($attributes) !== 1) {
556-
$this->message = 'GIN index can be created on a single object attribute';
559+
$this->message = 'Object index can be created on a single object attribute';
557560
return false;
558561
}
559562

560563
if (!empty($orders)) {
561-
$this->message = 'GIN indexes do not support explicit orders. Remove the orders to create this index.';
564+
$this->message = 'Object index do not support explicit orders. Remove the orders to create this index.';
562565
return false;
563566
}
564567

@@ -567,7 +570,7 @@ public function checkGinIndex(Document $index): bool
567570
$attributeType = $attribute->getAttribute('type', '');
568571

569572
if ($attributeType !== Database::TYPE_OBJECT) {
570-
$this->message = 'GIN index can only be created on object attributes. Attribute "' . $attributeName . '" is of type "' . $attributeType . '"';
573+
$this->message = 'Object index can only be created on object attributes. Attribute "' . $attributeName . '" is of type "' . $attributeType . '"';
571574
return false;
572575
}
573576

tests/e2e/Adapter/Scopes/ObjectAttributeTests.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,8 @@ public function testObjectAttributeGinIndex(): void
555555
// Create object attribute
556556
$this->assertEquals(true, $database->createAttribute($collectionId, 'data', Database::TYPE_OBJECT, 0, false));
557557

558-
// Test 1: Create GIN index on object attribute
559-
$ginIndex = $database->createIndex($collectionId, 'idx_data_gin', Database::INDEX_GIN, ['data']);
558+
// Test 1: Create Object index on object attribute
559+
$ginIndex = $database->createIndex($collectionId, 'idx_data_gin', Database::Index_Object, ['data']);
560560
$this->assertTrue($ginIndex);
561561

562562
// Test 2: Create documents with JSONB data
@@ -590,7 +590,7 @@ public function testObjectAttributeGinIndex(): void
590590
$results = $database->find($collectionId, [
591591
Query::equal('data', [['env' => 'production']])
592592
]);
593-
$this->assertCount(0, $results); // Note: GIN index doesn't make equal queries work differently
593+
$this->assertCount(0, $results); // Note: Object index doesn't make equal queries work differently
594594

595595
// Test 4: Query with contains on indexed JSONB column
596596
$results = $database->find($collectionId, [
@@ -599,49 +599,49 @@ public function testObjectAttributeGinIndex(): void
599599
$this->assertCount(1, $results);
600600
$this->assertEquals('gin1', $results[0]->getId());
601601

602-
// Test 5: Verify GIN index improves performance for containment queries
602+
// Test 5: Verify Object index improves performance for containment queries
603603
$results = $database->find($collectionId, [
604604
Query::contains('data', [['tags' => 'kotlin']])
605605
]);
606606
$this->assertCount(1, $results);
607607
$this->assertEquals('gin2', $results[0]->getId());
608608

609-
// Test 6: Try to create GIN index on non-object attribute (should fail)
609+
// Test 6: Try to create Object index on non-object attribute (should fail)
610610
$database->createAttribute($collectionId, 'name', Database::VAR_STRING, 255, false);
611611

612612
$exceptionThrown = false;
613613
try {
614-
$database->createIndex($collectionId, 'idx_name_gin', Database::INDEX_GIN, ['name']);
614+
$database->createIndex($collectionId, 'idx_name_gin', Database::Index_Object, ['name']);
615615
} catch (\Exception $e) {
616616
$exceptionThrown = true;
617617
$this->assertInstanceOf(IndexException::class, $e);
618-
$this->assertStringContainsString('GIN index can only be created on object attributes', $e->getMessage());
618+
$this->assertStringContainsString('Object index can only be created on object attributes', $e->getMessage());
619619
}
620-
$this->assertTrue($exceptionThrown, 'Expected Index exception for GIN index on non-object attribute');
620+
$this->assertTrue($exceptionThrown, 'Expected Index exception for Object index on non-object attribute');
621621

622-
// Test 7: Try to create GIN index on multiple attributes (should fail)
622+
// Test 7: Try to create Object index on multiple attributes (should fail)
623623
$database->createAttribute($collectionId, 'metadata', Database::TYPE_OBJECT, 0, false);
624624

625625
$exceptionThrown = false;
626626
try {
627-
$database->createIndex($collectionId, 'idx_multi_gin', Database::INDEX_GIN, ['data', 'metadata']);
627+
$database->createIndex($collectionId, 'idx_multi_gin', Database::Index_Object, ['data', 'metadata']);
628628
} catch (\Exception $e) {
629629
$exceptionThrown = true;
630630
$this->assertInstanceOf(IndexException::class, $e);
631-
$this->assertStringContainsString('GIN index can be created on a single object attribute', $e->getMessage());
631+
$this->assertStringContainsString('Object index can be created on a single object attribute', $e->getMessage());
632632
}
633-
$this->assertTrue($exceptionThrown, 'Expected Index exception for GIN index on multiple attributes');
633+
$this->assertTrue($exceptionThrown, 'Expected Index exception for Object index on multiple attributes');
634634

635-
// Test 8: Try to create GIN index with orders (should fail)
635+
// Test 8: Try to create Object index with orders (should fail)
636636
$exceptionThrown = false;
637637
try {
638-
$database->createIndex($collectionId, 'idx_ordered_gin', Database::INDEX_GIN, ['metadata'], [], [Database::ORDER_ASC]);
638+
$database->createIndex($collectionId, 'idx_ordered_gin', Database::Index_Object, ['metadata'], [], [Database::ORDER_ASC]);
639639
} catch (\Exception $e) {
640640
$exceptionThrown = true;
641641
$this->assertInstanceOf(IndexException::class, $e);
642-
$this->assertStringContainsString('GIN indexes do not support explicit orders', $e->getMessage());
642+
$this->assertStringContainsString('Object indexes do not support explicit orders', $e->getMessage());
643643
}
644-
$this->assertTrue($exceptionThrown, 'Expected Index exception for GIN index with orders');
644+
$this->assertTrue($exceptionThrown, 'Expected Index exception for Object index with orders');
645645

646646
// Clean up
647647
$database->deleteCollection($collectionId);

tests/unit/Validator/IndexTest.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -273,56 +273,56 @@ public function testGinIndexValidation(): void
273273
'indexes' => []
274274
]);
275275

276-
// Validator with objectIndexSupport enabled
277-
$validator = new Index($collection->getAttribute('attributes'), $collection->getAttribute('indexes', []), 768, [], false, false, false, false, true);
276+
// Validator with supportForObjectIndexes enabled
277+
$validator = new Index($collection->getAttribute('attributes'), $collection->getAttribute('indexes', []), 768, [], false, false, false, false, supportForObjectIndexes:true);
278278

279-
// Valid: GIN index on single TYPE_OBJECT attribute
279+
// Valid: Object index on single TYPE_OBJECT attribute
280280
$validIndex = new Document([
281281
'$id' => ID::custom('idx_gin_valid'),
282-
'type' => Database::INDEX_GIN,
282+
'type' => Database::Index_Object,
283283
'attributes' => ['data'],
284284
'lengths' => [],
285285
'orders' => [],
286286
]);
287287
$this->assertTrue($validator->isValid($validIndex));
288288

289-
// Invalid: GIN index on non-object attribute
289+
// Invalid: Object index on non-object attribute
290290
$invalidIndexType = new Document([
291291
'$id' => ID::custom('idx_gin_invalid_type'),
292-
'type' => Database::INDEX_GIN,
292+
'type' => Database::Index_Object,
293293
'attributes' => ['name'],
294294
'lengths' => [],
295295
'orders' => [],
296296
]);
297297
$this->assertFalse($validator->isValid($invalidIndexType));
298-
$this->assertStringContainsString('GIN index can only be created on object attributes', $validator->getDescription());
298+
$this->assertStringContainsString('Object index can only be created on object attributes', $validator->getDescription());
299299

300-
// Invalid: GIN index on multiple attributes
300+
// Invalid: Object index on multiple attributes
301301
$invalidIndexMulti = new Document([
302302
'$id' => ID::custom('idx_gin_multi'),
303-
'type' => Database::INDEX_GIN,
303+
'type' => Database::Index_Object,
304304
'attributes' => ['data', 'name'],
305305
'lengths' => [],
306306
'orders' => [],
307307
]);
308308
$this->assertFalse($validator->isValid($invalidIndexMulti));
309-
$this->assertStringContainsString('GIN index can be created on a single object attribute', $validator->getDescription());
309+
$this->assertStringContainsString('Object index can be created on a single object attribute', $validator->getDescription());
310310

311-
// Invalid: GIN index with orders
311+
// Invalid: Object index with orders
312312
$invalidIndexOrder = new Document([
313313
'$id' => ID::custom('idx_gin_order'),
314-
'type' => Database::INDEX_GIN,
314+
'type' => Database::Index_Object,
315315
'attributes' => ['data'],
316316
'lengths' => [],
317317
'orders' => ['asc'],
318318
]);
319319
$this->assertFalse($validator->isValid($invalidIndexOrder));
320-
$this->assertStringContainsString('GIN indexes do not support explicit orders', $validator->getDescription());
320+
$this->assertStringContainsString('Object index do not support explicit orders', $validator->getDescription());
321321

322-
// Validator with objectIndexSupport disabled should reject GIN
322+
// Validator with supportForObjectIndexes disabled should reject GIN
323323
$validatorNoSupport = new Index($collection->getAttribute('attributes'), $collection->getAttribute('indexes', []), 768, [], false, false, false, false, false);
324324
$this->assertFalse($validatorNoSupport->isValid($validIndex));
325-
$this->assertEquals('GIN indexes are not supported', $validatorNoSupport->getDescription());
325+
$this->assertEquals('Object indexes are not supported', $validatorNoSupport->getDescription());
326326
}
327327

328328
/**

0 commit comments

Comments
 (0)