diff --git a/composer.lock b/composer.lock index c1ce86a9b..6c5cb2f70 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "43c3ff88660f90baf3ddf7ec8bc5136d", + "content-hash": "0ad14b2121f07c2fe936eaaa82186cbc", "packages": [ { "name": "brick/math", diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 4ad975706..a21ce60be 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -700,7 +700,7 @@ abstract public function sum(string $collection, string $attribute, array $queri abstract public function count(string $collection, array $queries = [], ?int $max = null): int; /** - * Get Collection Size + * Get Collection Size of the raw data * * @param string $collection * @return int @@ -972,7 +972,7 @@ protected function getAttributeSelections(array $queries): array */ public function filter(string $value): string { - $value = \preg_replace("/[^A-Za-z0-9\_\-]/", '', $value); + $value = \preg_replace("/[^A-Za-z0-9_\-]/", '', $value); if (\is_null($value)) { throw new DatabaseException('Failed to filter key'); diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 449ac4708..45d12e6fd 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -2481,4 +2481,5 @@ public function getSupportForSchemaAttributes(): bool { return true; } + } diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 7d6f7ddcb..5df46e28e 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -283,7 +283,18 @@ public function listCollections(): array } /** - * Get Collection Size + * Get Collection Size on disk + * @param string $collection + * @return int + * @throws DatabaseException + */ + public function getSizeOfCollectionOnDisk(string $collection): int + { + return $this->getSizeOfCollection($collection); + } + + /** + * Get Collection Size of raw data * @param string $collection * @return int * @throws DatabaseException @@ -311,17 +322,6 @@ public function getSizeOfCollection(string $collection): int } } - /** - * Get Collection Size on disk - * @param string $collection - * @return int - * @throws DatabaseException - */ - public function getSizeOfCollectionOnDisk(string $collection): int - { - return $this->getSizeOfCollection($collection); - } - /** * Delete Collection * @@ -1962,4 +1962,5 @@ public function getSchemaAttributes(string $collection): array { return []; } + } diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 50862c620..b55365961 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -298,13 +298,12 @@ public function createCollection(string $name, array $attributes = [], array $in } /** - * Get Collection Size + * Get Collection Size on disk * @param string $collection * @return int * @throws DatabaseException - * */ - public function getSizeOfCollection(string $collection): int + public function getSizeOfCollectionOnDisk(string $collection): int { $collection = $this->filter($collection); $name = $this->getSQLTable($collection); @@ -333,14 +332,38 @@ public function getSizeOfCollection(string $collection): int } /** - * Get Collection Size on disk + * Get Collection Size of raw data * @param string $collection * @return int * @throws DatabaseException + * */ - public function getSizeOfCollectionOnDisk(string $collection): int + public function getSizeOfCollection(string $collection): int { - return $this->getSizeOfCollection($collection); + $collection = $this->filter($collection); + $name = $this->getSQLTable($collection); + $permissions = $this->getSQLTable($collection . '_perms'); + + $collectionSize = $this->getPDO()->prepare(" + SELECT pg_relation_size(:name); + "); + + $permissionsSize = $this->getPDO()->prepare(" + SELECT pg_relation_size(:permissions); + "); + + $collectionSize->bindParam(':name', $name); + $permissionsSize->bindParam(':permissions', $permissions); + + try { + $collectionSize->execute(); + $permissionsSize->execute(); + $size = $collectionSize->fetchColumn() + $permissionsSize->fetchColumn(); + } catch (PDOException $e) { + throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); + } + + return $size; } /** diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index 31bc38f38..07cad6999 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -233,8 +233,9 @@ public function createCollection(string $name, array $attributes = [], array $in return true; } + /** - * Get Collection Size + * Get Collection Size of raw data * @param string $collection * @return int * @throws DatabaseException diff --git a/src/Database/Database.php b/src/Database/Database.php index 6aba14d0b..b8a66c16d 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1391,7 +1391,7 @@ public function deleteCollection(string $id): bool $relationships = \array_filter( $collection->getAttribute('attributes'), fn ($attribute) => - $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP + $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP ); foreach ($relationships as $relationship) { @@ -3002,7 +3002,7 @@ public function getDocument(string $collection, string $id, array $queries = [], $relationships = \array_filter( $collection->getAttribute('attributes', []), fn ($attribute) => - $attribute['type'] === Database::VAR_RELATIONSHIP + $attribute['type'] === Database::VAR_RELATIONSHIP ); $hasTwoWayRelationship = false; @@ -3118,8 +3118,8 @@ private function populateDocumentRelationships(Document $collection, Document $d // collection as an existing relationship, but a different two-way key (the third condition), // or the same two-way key as an existing relationship, but a different key (the fourth condition). $transitive = (($existingKey === $twoWayKey - && $existingCollection === $relatedCollection->getId() - && $existingSide !== $side) + && $existingCollection === $relatedCollection->getId() + && $existingSide !== $side) || ($existingTwoWayKey === $key && $existingRelatedCollection === $collection->getId() && $existingSide !== $side) @@ -3456,7 +3456,7 @@ private function createDocumentRelationships(Document $collection, Document $doc $relationships = \array_filter( $attributes, fn ($attribute) => - $attribute['type'] === Database::VAR_RELATIONSHIP + $attribute['type'] === Database::VAR_RELATIONSHIP ); $stackCount = count($this->relationshipWriteStack); @@ -4838,8 +4838,8 @@ private function deleteDocumentRelationships(Document $collection, Document $doc // collection as an existing relationship, but a different two-way key (the third condition), // or the same two-way key as an existing relationship, but a different key (the fourth condition). $transitive = (($existingKey === $twoWayKey - && $existingCollection === $relatedCollection->getId() - && $existingSide !== $side) + && $existingCollection === $relatedCollection->getId() + && $existingSide !== $side) || ($existingTwoWayKey === $key && $existingRelatedCollection === $collection->getId() && $existingSide !== $side) @@ -5694,13 +5694,13 @@ public function decode(Document $collection, Document $document, array $selectio $attributes = \array_filter( $collection->getAttribute('attributes', []), fn ($attribute) => - $attribute['type'] !== self::VAR_RELATIONSHIP + $attribute['type'] !== self::VAR_RELATIONSHIP ); $relationships = \array_filter( $collection->getAttribute('attributes', []), fn ($attribute) => - $attribute['type'] === self::VAR_RELATIONSHIP + $attribute['type'] === self::VAR_RELATIONSHIP ); foreach ($relationships as $relationship) { @@ -6021,7 +6021,7 @@ private function purgeRelatedDocuments(Document $collection, string $id): void $relationships = \array_filter( $collection->getAttribute('attributes', []), fn ($attribute) => - $attribute['type'] === Database::VAR_RELATIONSHIP + $attribute['type'] === Database::VAR_RELATIONSHIP ); if (empty($relationships)) { diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 90298d472..3a80da72c 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -6994,8 +6994,8 @@ public function testOneToOneOneWayRelationship(): void $person1->setAttribute( 'library', $person1 - ->getAttribute('library') - ->setAttribute('name', 'Library 1 Updated') + ->getAttribute('library') + ->setAttribute('name', 'Library 1 Updated') ) ); @@ -7466,8 +7466,8 @@ public function testOneToOneTwoWayRelationship(): void $country1->setAttribute( 'city', $country1 - ->getAttribute('city') - ->setAttribute('name', 'City 1 Updated') + ->getAttribute('city') + ->setAttribute('name', 'City 1 Updated') ) ); @@ -7482,8 +7482,8 @@ public function testOneToOneTwoWayRelationship(): void $city2->setAttribute( 'country', $city2 - ->getAttribute('country') - ->setAttribute('name', 'Country 2 Updated') + ->getAttribute('country') + ->setAttribute('name', 'Country 2 Updated') ) ); @@ -8485,8 +8485,8 @@ public function testOneToManyTwoWayRelationship(): void $account2->setAttribute( 'customer', $account2 - ->getAttribute('customer') - ->setAttribute('name', 'Customer 2 Updated') + ->getAttribute('customer') + ->setAttribute('name', 'Customer 2 Updated') ) ); @@ -12398,19 +12398,19 @@ public function testExceedMaxDepthOneToMany(): void $level1Collection, 'level1', $level1 - ->setAttribute($level2Collection, [new Document([ - '$id' => 'level2new', - $level3Collection => [ - [ - '$id' => 'level3new', - $level4Collection => [ - [ - '$id' => 'level4new', + ->setAttribute($level2Collection, [new Document([ + '$id' => 'level2new', + $level3Collection => [ + [ + '$id' => 'level3new', + $level4Collection => [ + [ + '$id' => 'level4new', + ], ], ], ], - ], - ])]) + ])]) ); $this->assertEquals(1, count($level1[$level2Collection])); $this->assertEquals('level2new', $level1[$level2Collection][0]->getId());