From 2711213e23038cbd1d6e0b38cf31cf8b1191cd07 Mon Sep 17 00:00:00 2001 From: faisalill Date: Fri, 26 May 2023 17:28:56 +0530 Subject: [PATCH 01/32] fixed stuff --- src/Database/Adapter.php | 16 ++--- src/Database/Adapter/MariaDB.php | 43 ++++++-------- src/Database/Adapter/Mongo.php | 35 ++++++----- src/Database/Adapter/Postgres.php | 33 +++++------ src/Database/Adapter/SQLite.php | 38 ++++++------ src/Database/Database.php | 18 +++--- tests/Database/Base.php | 99 ++++++++++++------------------- 7 files changed, 127 insertions(+), 155 deletions(-) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 025354489..5b057aa1e 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -398,14 +398,14 @@ 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 - * Returns - * @param string $collection - * @return int - * @throws DatabaseException - */ - abstract public function getSizeOfCollection(string $collection): int; + /** + * Get Collection Size + * Returns + * @param string $collection + * @return int + * @throws DatabaseException + */ + abstract public function getSizeOfCollection(string $collection): int; /** * Get max STRING limit diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 29dff187c..9322d2e1a 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -147,42 +147,37 @@ public function createCollection(string $name, array $attributes = [], array $in * @param string $collection * @return int * @throws DatabaseException - * */ - public function getSizeOfCollection(string $collection): int - { + public function getSizeOfCollection(string $collection): int + { $name = $this->filter($collection); $tableName = $this->getSQLTable($name); $database = str_replace('`', '', explode('.', $tableName)[0]); $collectionName = str_replace('`', '', explode('.', $tableName)[1]); $query = $this->getPDO()->prepare(" - SELECT - data_length + index_length - FROM - information_schema.TABLES - WHERE - table_schema = :database - AND - table_name = :name + SELECT data_length + index_length + FROM information_schema.TABLES + WHERE table_schema = :database + AND table_name = :name "); $query->bindParam(':database', $database); $query->bindParam(':name', $collectionName); try { - $query->execute(); - $size = $query->fetchColumn(); - } catch (PDOException $e) { - throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); - } - return $size; - } + $query->execute(); + $size = $query->fetchColumn(); + } catch (PDOException $e) { + throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); + } + return $size; + } /** - * Delete Collection - * @param string $id - * @return bool - * @throws Exception - * @throws PDOException - */ + * Delete Collection + * @param string $id + * @return bool + * @throws Exception + * @throws PDOException + */ public function deleteCollection(string $id): bool { $id = $this->filter($id); diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 0af0a7748..bbf4c4c41 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -260,28 +260,27 @@ public function listCollections(): array * @throws DatabaseException */ public function getSizeOfCollection(string $collection): int - { + { $namespace = $this->getNamespace(); $command = [ 'collStats' => $namespace . '_' . $this->filter($collection), - 'scale' => 1 + 'scale' => 1 ]; - try{ - $result = $this->getClient()->query($command); - } - catch(Exception $e){ - throw new DatabaseException("Failed to get collection size" . $e->getMessage()); - } - return $result->size; - } - - /** - * Delete Collection - * - * @param string $id - * @return bool - * @throws Exception - */ + try { + $result = $this->getClient()->query($command); + } catch(Exception $e) { + throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); + } + return $result->size; + } + + /** + * Delete Collection + * + * @param string $id + * @return bool + * @throws Exception + */ public function deleteCollection(string $id): bool { $id = $this->getNamespace() . '_' . $this->filter($id); diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 1c5139ab1..88c1d17bb 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -153,31 +153,30 @@ public function createCollection(string $name, array $attributes = [], array $in * @param string $collection * @return int * @throws DatabaseException - * + * */ public function getSizeOfCollection(string $collection): int - { - // Does not work in tests. + { $name = $this->filter($collection); $query = $this->getPDO()->prepare(" SELECT pg_total_relation_size('{$this->getSQLTable($name)}'); "); - + try { - $query->execute(); - $size = $query->fetchColumn(); - } catch (PDOException $e) { - throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); - } - return $size; - } - + $query->execute(); + $size = $query->fetchColumn(); + } catch (PDOException $e) { + throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); + } + return $size; + } + /** - * Delete Collection - * - * @param string $id - * @return bool - */ + * Delete Collection + * + * @param string $id + * @return bool + */ public function deleteCollection(string $id): bool { $id = $this->filter($id); diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index 4d2c14262..9cdf91215 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -163,32 +163,32 @@ public function createCollection(string $name, array $attributes = [], array $in * @param string $collection * @return int * @throws DatabaseException - * + * */ public function getSizeOfCollection(string $collection): int - { - $name = $this->filter($collection); - $namespace = $this->getNamespace(); - $query = $this->getPDO()->prepare(" + { + $name = $this->filter($collection); + $namespace = $this->getNamespace(); + $query = $this->getPDO()->prepare(" SELECT SUM(\"pgsize\") FROM \"dbstat\" WHERE name='{$namespace}_{$name}'; "); - try { - $query->execute(); - $size = $query->fetchColumn(); - } catch (PDOException $e) { - throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); - } - return $size; - } + try { + $query->execute(); + $size = $query->fetchColumn(); + } catch (PDOException $e) { + throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); + } + return $size; + } /** - * Delete Collection - * @param string $id - * @return bool - * @throws Exception - * @throws PDOException - */ + * Delete Collection + * @param string $id + * @return bool + * @throws Exception + * @throws PDOException + */ public function deleteCollection(string $id): bool { $id = $this->filter($id); diff --git a/src/Database/Database.php b/src/Database/Database.php index 2e3f3e64f..b92badbac 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -762,18 +762,18 @@ public function listCollections(int $limit = 25, int $offset = 0): array * * @return int */ - public function getSizeOfCollection(string $collection): int - { + public function getSizeOfCollection(string $collection): int + { return $this->adapter->getSizeOfCollection($collection); - } + } /** - * Delete Collection - * - * @param string $id - * - * @return bool - */ + * Delete Collection + * + * @param string $id + * + * @return bool + */ public function deleteCollection(string $id): bool { $collection = $this->silent(fn () => $this->getDocument(self::METADATA, $id)); diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 7fdcfb4e2..122af5e75 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -118,69 +118,48 @@ public function testCreateListExistsDeleteCollection(): void $this->assertEquals(true, static::getDatabase()->getCollection('actors')->isEmpty()); $this->assertEquals(false, static::getDatabase()->exists($this->testDatabase, 'actors')); } - + public function testSizeCollection(): void { + static::getDatabase()->createCollection('size_testing'); - static::getDatabase()->createCollection('size_testing'); - - $valueBeforeAddingData = static::getDatabase()->getSizeOfCollection('size_testing'); - - static::getDatabase()->createAttribute('size_testing', 'string1', Database::VAR_STRING, 128, true); - static::getDatabase()->createAttribute('size_testing', 'string2', Database::VAR_STRING, 2147483646 + 1, true); - static::getDatabase()->createAttribute('size_testing', 'string3', Database::VAR_STRING, 2147483646 + 1, true); - static::getDatabase()->createAttribute('size_testing', 'string4', Database::VAR_STRING, 2147483646 + 1, true); - static::getDatabase()->createAttribute('size_testing', 'integer', Database::VAR_INTEGER, 0, true); - static::getDatabase()->createAttribute('size_testing', 'bigint', Database::VAR_INTEGER, 8, true); - static::getDatabase()->createAttribute('size_testing', 'float', Database::VAR_FLOAT, 0, true); - static::getDatabase()->createAttribute('size_testing', 'boolean', Database::VAR_BOOLEAN, 0, true); - static::getDatabase()->createAttribute('size_testing', 'string1new', Database::VAR_STRING, 128, false); - static::getDatabase()->createAttribute('size_testing', 'string2new', Database::VAR_STRING, 2147483646 + 1, false); - static::getDatabase()->createAttribute('size_testing', 'string3new', Database::VAR_STRING, 2147483646 + 1, false); - static::getDatabase()->createAttribute('size_testing', 'string4new', Database::VAR_STRING, 2147483646 + 1, false); - static::getDatabase()->createAttribute('size_testing', 'integernew', Database::VAR_INTEGER, 0, false); - static::getDatabase()->createAttribute('size_testing', 'bigintnew', Database::VAR_INTEGER, 8, false); - static::getDatabase()->createAttribute('size_testing', 'floatnew', Database::VAR_FLOAT, 0, false); - static::getDatabase()->createAttribute('size_testing', 'booleannew', Database::VAR_BOOLEAN, 0, false); - static::getDatabase()->createIndex('size_testing', 'string1_index', Database::INDEX_KEY, ['string1']); - static::getDatabase()->createIndex('size_testing', 'string2_index', Database::INDEX_KEY, ['string2'], [255]); - static::getDatabase()->createIndex('size_testing', 'multi_index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); - - for ($i = 0; $i < 100; $i++) { - static::getDatabase()->createDocument('size_testing', new Document([ - '$permissions' => [ - Permission::read(Role::any()), - Permission::read(Role::user(ID::custom('1'))), - Permission::read(Role::user(ID::custom('2'))), - Permission::create(Role::any()), - Permission::create(Role::user(ID::custom('1x'))), - Permission::create(Role::user(ID::custom('2x'))), - Permission::update(Role::any()), - Permission::update(Role::user(ID::custom('1x'))), - Permission::update(Role::user(ID::custom('2x'))), - Permission::delete(Role::any()), - Permission::delete(Role::user(ID::custom('1x'))), - Permission::delete(Role::user(ID::custom('2x'))), - ], - 'string1' => 'string1', - 'string2' => 'string2', - 'string3' => 'string3', // 2^33 - 'string4' => "string4", - 'integer' => 35635353, - 'bigint' => 3535353, - 'float' => 353.3535, - 'boolean' => true, - ])); - } + $valueBeforeAddingData = static::getDatabase()->getSizeOfCollection('size_testing'); - $valueAfterAddingData = static::getDatabase()->getSizeOfCollection('size_testing'); - - if(static::getAdapterName() === 'mysql'){ - $this->assertGreaterThan(65535,$valueAfterAddingData); - } - else{ - $this->assertGreaterThan($valueBeforeAddingData,$valueAfterAddingData); - } + static::getDatabase()->createAttribute('size_testing', 'string1', Database::VAR_STRING, 128, true); + static::getDatabase()->createAttribute('size_testing', 'string2', Database::VAR_STRING, 16383 + 1, true); + static::getDatabase()->createAttribute('size_testing', 'string3', Database::VAR_STRING, 65535 + 1, true); + static::getDatabase()->createAttribute('size_testing', 'string4', Database::VAR_STRING, 65535 + 1, true); + static::getDatabase()->createAttribute('size_testing', 'integer', Database::VAR_INTEGER, 10, true); + static::getDatabase()->createAttribute('size_testing', 'bigint', Database::VAR_INTEGER, 8, true); + static::getDatabase()->createAttribute('size_testing', 'float', Database::VAR_FLOAT, 0, true); + static::getDatabase()->createIndex('size_testing', 'multi_index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); + + + for ($i = 0; $i < 20; $i++) { + static::getDatabase()->createDocument('size_testing', new Document([ + '$permissions' => [ + Permission::read(Role::any()), + Permission::create(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + 'string1' => 'string1' . $i, + 'string2' => 'string2' . $i, + 'string3' => 'string3' . $i, + 'string4' => "string4" . $i, + 'integer' => 35635353 + $i, + 'bigint' => 3535353 + $i, + 'float' => 353.3535 + $i, + ])); + } + + $valueAfterAddingData = static::getDatabase()->getSizeOfCollection('size_testing'); + + if (static::getAdapterName() === 'mysql') { + $this->assertGreaterThan(65535, $valueAfterAddingData); + } else { + $this->assertGreaterThan($valueBeforeAddingData, $valueAfterAddingData); + } } public function testCreateDeleteAttribute(): void @@ -11245,4 +11224,4 @@ public function testLast(): void $this->expectNotToPerformAssertions(); static::killDatabase(); } -} \ No newline at end of file +} From e6458170a97c0049315cba12dfcf4c08e534c6aa Mon Sep 17 00:00:00 2001 From: faisalill Date: Mon, 29 May 2023 20:26:41 +0530 Subject: [PATCH 02/32] fixed indentation --- src/Database/Adapter.php | 12 ++++++------ src/Database/Adapter/MariaDB.php | 26 +++++++++++++++----------- src/Database/Adapter/Mongo.php | 13 ++++++++----- src/Database/Adapter/Postgres.php | 17 ++++++++++------- src/Database/Adapter/SQLite.php | 17 ++++++++++------- src/Database/Database.php | 8 ++++---- tests/Database/Base.php | 7 +++---- 7 files changed, 56 insertions(+), 44 deletions(-) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 5b057aa1e..c0d5f5d54 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -399,12 +399,12 @@ 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 - * Returns - * @param string $collection - * @return int - * @throws DatabaseException - */ + * Get Collection Size + * Returns + * @param string $collection + * @return int + * @throws DatabaseException + */ abstract public function getSizeOfCollection(string $collection): int; /** diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 9322d2e1a..0c01feb43 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -148,28 +148,32 @@ public function createCollection(string $name, array $attributes = [], array $in * @return int * @throws DatabaseException */ - public function getSizeOfCollection(string $collection): int - { + public function getSizeOfCollection(string $collection): int + { $name = $this->filter($collection); - $tableName = $this->getSQLTable($name); - $database = str_replace('`', '', explode('.', $tableName)[0]); - $collectionName = str_replace('`', '', explode('.', $tableName)[1]); + $collectionName = "{$this->getNamespace()}_{$name}"; + $database = $this->getDefaultDatabase(); + $query = $this->getPDO()->prepare(" - SELECT data_length + index_length - FROM information_schema.TABLES - WHERE table_schema = :database - AND table_name = :name + SELECT SUM(data_length + index_length) AS total_size + FROM information_schema.TABLES + WHERE table_schema = :database + AND table_name = :name "); + $query->bindParam(':database', $database); $query->bindParam(':name', $collectionName); + try { $query->execute(); $size = $query->fetchColumn(); - } catch (PDOException $e) { + } + catch (PDOException $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } + return $size; - } + } /** * Delete Collection diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index bbf4c4c41..80277bffc 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -253,26 +253,29 @@ public function listCollections(): array return $list; } - /** + /** * Get Collection Size * @param string $collection * @return int * @throws DatabaseException */ - public function getSizeOfCollection(string $collection): int - { + public function getSizeOfCollection(string $collection): int + { $namespace = $this->getNamespace(); $command = [ 'collStats' => $namespace . '_' . $this->filter($collection), 'scale' => 1 ]; + try { $result = $this->getClient()->query($command); - } catch(Exception $e) { + } + catch(Exception $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } + return $result->size; - } + } /** * Delete Collection diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 88c1d17bb..b97de24a4 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -148,28 +148,31 @@ public function createCollection(string $name, array $attributes = [], array $in return true; } - /** + /** * Get Collection Size * @param string $collection * @return int * @throws DatabaseException * */ - public function getSizeOfCollection(string $collection): int - { + public function getSizeOfCollection(string $collection): int + { $name = $this->filter($collection); + $query = $this->getPDO()->prepare(" - SELECT pg_total_relation_size('{$this->getSQLTable($name)}'); - "); + SELECT pg_total_relation_size('{$this->getSQLTable($name)}'); + "); try { $query->execute(); $size = $query->fetchColumn(); - } catch (PDOException $e) { + } + catch (PDOException $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } + return $size; - } + } /** * Delete Collection diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index 9cdf91215..204e2ab36 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -158,29 +158,32 @@ public function createCollection(string $name, array $attributes = [], array $in return true; } - /** + /** * Get Collection Size * @param string $collection * @return int * @throws DatabaseException * */ - public function getSizeOfCollection(string $collection): int - { + public function getSizeOfCollection(string $collection): int + { $name = $this->filter($collection); $namespace = $this->getNamespace(); + $query = $this->getPDO()->prepare(" - SELECT SUM(\"pgsize\") FROM \"dbstat\" WHERE name='{$namespace}_{$name}'; - "); + SELECT SUM(\"pgsize\") FROM \"dbstat\" WHERE name='{$namespace}_{$name}'; + "); try { $query->execute(); $size = $query->fetchColumn(); - } catch (PDOException $e) { + } + catch (PDOException $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } + return $size; - } + } /** * Delete Collection diff --git a/src/Database/Database.php b/src/Database/Database.php index b92badbac..189e979a6 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -762,10 +762,10 @@ public function listCollections(int $limit = 25, int $offset = 0): array * * @return int */ - public function getSizeOfCollection(string $collection): int - { - return $this->adapter->getSizeOfCollection($collection); - } + public function getSizeOfCollection(string $collection): int + { + return $this->adapter->getSizeOfCollection($collection); + } /** * Delete Collection diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 122af5e75..efd316fc3 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -126,15 +126,14 @@ public function testSizeCollection(): void $valueBeforeAddingData = static::getDatabase()->getSizeOfCollection('size_testing'); static::getDatabase()->createAttribute('size_testing', 'string1', Database::VAR_STRING, 128, true); - static::getDatabase()->createAttribute('size_testing', 'string2', Database::VAR_STRING, 16383 + 1, true); - static::getDatabase()->createAttribute('size_testing', 'string3', Database::VAR_STRING, 65535 + 1, true); - static::getDatabase()->createAttribute('size_testing', 'string4', Database::VAR_STRING, 65535 + 1, true); + static::getDatabase()->createAttribute('size_testing', 'string2', Database::VAR_STRING, 254 + 1, true); + static::getDatabase()->createAttribute('size_testing', 'string3', Database::VAR_STRING, 254 + 1, true); + static::getDatabase()->createAttribute('size_testing', 'string4', Database::VAR_STRING, 254 + 1, true); static::getDatabase()->createAttribute('size_testing', 'integer', Database::VAR_INTEGER, 10, true); static::getDatabase()->createAttribute('size_testing', 'bigint', Database::VAR_INTEGER, 8, true); static::getDatabase()->createAttribute('size_testing', 'float', Database::VAR_FLOAT, 0, true); static::getDatabase()->createIndex('size_testing', 'multi_index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); - for ($i = 0; $i < 20; $i++) { static::getDatabase()->createDocument('size_testing', new Document([ '$permissions' => [ From 32166d1485c089adeb8a3d70ee9e62a17187bd21 Mon Sep 17 00:00:00 2001 From: faisalill Date: Mon, 29 May 2023 20:32:51 +0530 Subject: [PATCH 03/32] indentation cleanup --- src/Database/Adapter/MariaDB.php | 12 ++++++------ src/Database/Adapter/Mongo.php | 12 ++++++------ src/Database/Adapter/Postgres.php | 10 +++++----- src/Database/Adapter/SQLite.php | 12 ++++++------ src/Database/Database.php | 12 ++++++------ 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 0c01feb43..760e840f1 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -176,12 +176,12 @@ public function getSizeOfCollection(string $collection): int } /** - * Delete Collection - * @param string $id - * @return bool - * @throws Exception - * @throws PDOException - */ + * Delete Collection + * @param string $id + * @return bool + * @throws Exception + * @throws PDOException + */ public function deleteCollection(string $id): bool { $id = $this->filter($id); diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 80277bffc..170130386 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -278,12 +278,12 @@ public function getSizeOfCollection(string $collection): int } /** - * Delete Collection - * - * @param string $id - * @return bool - * @throws Exception - */ + * Delete Collection + * + * @param string $id + * @return bool + * @throws Exception + */ public function deleteCollection(string $id): bool { $id = $this->getNamespace() . '_' . $this->filter($id); diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index b97de24a4..8da4f85a7 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -175,11 +175,11 @@ public function getSizeOfCollection(string $collection): int } /** - * Delete Collection - * - * @param string $id - * @return bool - */ + * Delete Collection + * + * @param string $id + * @return bool + */ public function deleteCollection(string $id): bool { $id = $this->filter($id); diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index 204e2ab36..a8c76eae3 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -186,12 +186,12 @@ public function getSizeOfCollection(string $collection): int } /** - * Delete Collection - * @param string $id - * @return bool - * @throws Exception - * @throws PDOException - */ + * Delete Collection + * @param string $id + * @return bool + * @throws Exception + * @throws PDOException + */ public function deleteCollection(string $id): bool { $id = $this->filter($id); diff --git a/src/Database/Database.php b/src/Database/Database.php index 189e979a6..4433badf1 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -768,12 +768,12 @@ public function getSizeOfCollection(string $collection): int } /** - * Delete Collection - * - * @param string $id - * - * @return bool - */ + * Delete Collection + * + * @param string $id + * + * @return bool + */ public function deleteCollection(string $id): bool { $collection = $this->silent(fn () => $this->getDocument(self::METADATA, $id)); From de08dca4c0af58538c03d9b88bdd23c8880755c0 Mon Sep 17 00:00:00 2001 From: faisalill Date: Tue, 30 May 2023 22:34:46 +0530 Subject: [PATCH 04/32] fixed indentation --- src/Database/Adapter/MariaDB.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 760e840f1..6a3dd2103 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -150,29 +150,29 @@ public function createCollection(string $name, array $attributes = [], array $in */ public function getSizeOfCollection(string $collection): int { - $name = $this->filter($collection); - $collectionName = "{$this->getNamespace()}_{$name}"; - $database = $this->getDefaultDatabase(); + $name = $this->filter($collection); + $collectionName = "{$this->getNamespace()}_{$name}"; + $database = $this->getDefaultDatabase(); - $query = $this->getPDO()->prepare(" + $query = $this->getPDO()->prepare(" SELECT SUM(data_length + index_length) AS total_size FROM information_schema.TABLES WHERE table_schema = :database AND table_name = :name "); - $query->bindParam(':database', $database); - $query->bindParam(':name', $collectionName); + $query->bindParam(':database', $database); + $query->bindParam(':name', $collectionName); - try { + try { $query->execute(); $size = $query->fetchColumn(); - } - catch (PDOException $e) { + } + catch (PDOException $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); - } + } - return $size; + return $size; } /** From aec995d9fd263b3719949acc9d9b31d2e8d78f8c Mon Sep 17 00:00:00 2001 From: faisalill Date: Tue, 30 May 2023 22:38:14 +0530 Subject: [PATCH 05/32] fixed indentation for all adapters --- src/Database/Adapter/Mongo.php | 16 ++++++++-------- src/Database/Adapter/Postgres.php | 16 ++++++++-------- src/Database/Adapter/SQLite.php | 18 +++++++++--------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 170130386..f6dd234ef 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -261,20 +261,20 @@ public function listCollections(): array */ public function getSizeOfCollection(string $collection): int { - $namespace = $this->getNamespace(); - $command = [ + $namespace = $this->getNamespace(); + $command = [ 'collStats' => $namespace . '_' . $this->filter($collection), 'scale' => 1 - ]; + ]; - try { + try { $result = $this->getClient()->query($command); - } - catch(Exception $e) { + } + catch(Exception $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); - } + } - return $result->size; + return $result->size; } /** diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 8da4f85a7..bb200acad 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -157,21 +157,21 @@ public function createCollection(string $name, array $attributes = [], array $in */ public function getSizeOfCollection(string $collection): int { - $name = $this->filter($collection); + $name = $this->filter($collection); - $query = $this->getPDO()->prepare(" + $query = $this->getPDO()->prepare(" SELECT pg_total_relation_size('{$this->getSQLTable($name)}'); - "); + "); - try { + try { $query->execute(); $size = $query->fetchColumn(); - } - catch (PDOException $e) { + } + catch (PDOException $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); - } + } - return $size; + return $size; } /** diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index a8c76eae3..1738b9f2d 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -167,22 +167,22 @@ public function createCollection(string $name, array $attributes = [], array $in */ public function getSizeOfCollection(string $collection): int { - $name = $this->filter($collection); - $namespace = $this->getNamespace(); + $name = $this->filter($collection); + $namespace = $this->getNamespace(); - $query = $this->getPDO()->prepare(" + $query = $this->getPDO()->prepare(" SELECT SUM(\"pgsize\") FROM \"dbstat\" WHERE name='{$namespace}_{$name}'; - "); + "); - try { + try { $query->execute(); $size = $query->fetchColumn(); - } - catch (PDOException $e) { + } + catch (PDOException $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); - } + } - return $size; + return $size; } /** From d66d2f8aea916e2a1f1f26d3a195879063c13191 Mon Sep 17 00:00:00 2001 From: faisalill Date: Sun, 4 Jun 2023 02:05:17 +0530 Subject: [PATCH 06/32] fixed mysql test issue --- tests/Database/Base.php | 47 +++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index f380550e0..6ff548223 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -122,21 +122,29 @@ public function testCreateListExistsDeleteCollection(): void public function testSizeCollection(): void { - static::getDatabase()->createCollection('size_testing'); - - $valueBeforeAddingData = static::getDatabase()->getSizeOfCollection('size_testing'); - - static::getDatabase()->createAttribute('size_testing', 'string1', Database::VAR_STRING, 128, true); - static::getDatabase()->createAttribute('size_testing', 'string2', Database::VAR_STRING, 254 + 1, true); - static::getDatabase()->createAttribute('size_testing', 'string3', Database::VAR_STRING, 254 + 1, true); - static::getDatabase()->createAttribute('size_testing', 'string4', Database::VAR_STRING, 254 + 1, true); - static::getDatabase()->createAttribute('size_testing', 'integer', Database::VAR_INTEGER, 10, true); - static::getDatabase()->createAttribute('size_testing', 'bigint', Database::VAR_INTEGER, 8, true); - static::getDatabase()->createAttribute('size_testing', 'float', Database::VAR_FLOAT, 0, true); - static::getDatabase()->createIndex('size_testing', 'multi_index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); - - for ($i = 0; $i < 20; $i++) { - static::getDatabase()->createDocument('size_testing', new Document([ + if(static::getAdapterName() === 'mysql'){ + $loopCount = 3000; + } + else{ + $loopCount = 20; + } + + static::getDatabase()->createCollection('size_test1'); + static::getDatabase()->createCollection('size_test2'); + + $size_test_1 = static::getDatabase()->getSizeOfCollection('size_test1'); + + static::getDatabase()->createAttribute('size_test2', 'string1', Database::VAR_STRING, 128, true); + static::getDatabase()->createAttribute('size_test2', 'string2', Database::VAR_STRING, 254 + 1, true); + static::getDatabase()->createAttribute('size_test2', 'string3', Database::VAR_STRING, 254 + 1, true); + static::getDatabase()->createAttribute('size_test2', 'string4', Database::VAR_STRING, 254 + 1, true); + static::getDatabase()->createAttribute('size_test2', 'integer', Database::VAR_INTEGER, 10, true); + static::getDatabase()->createAttribute('size_test2', 'bigint', Database::VAR_INTEGER, 8, true); + static::getDatabase()->createAttribute('size_test2', 'float', Database::VAR_FLOAT, 0, true); + static::getDatabase()->createIndex('size_test2', 'multi_index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); + + for ($i = 0; $i < $loopCount; $i++) { + static::getDatabase()->createDocument('size_test2', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::create(Role::any()), @@ -153,13 +161,10 @@ public function testSizeCollection(): void ])); } - $valueAfterAddingData = static::getDatabase()->getSizeOfCollection('size_testing'); + $size_test_2 = static::getDatabase()->getSizeOfCollection('size_test2'); + + $this->assertGreaterThan($size_test_1, $size_test_2); - if (static::getAdapterName() === 'mysql') { - $this->assertGreaterThan(65535, $valueAfterAddingData); - } else { - $this->assertGreaterThan($valueBeforeAddingData, $valueAfterAddingData); - } } public function testCreateDeleteAttribute(): void From 6afaf6fbce89943b40e159c81a535b5837c54169 Mon Sep 17 00:00:00 2001 From: faisalill Date: Fri, 9 Jun 2023 23:54:20 +0530 Subject: [PATCH 07/32] getSizeOfCollection analyzes the table before getting the size --- src/Database/Adapter/MariaDB.php | 3 +++ tests/Database/Base.php | 17 ++--------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 1e5519f25..8228b33b6 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -154,6 +154,9 @@ public function getSizeOfCollection(string $collection): int $collectionName = "{$this->getNamespace()}_{$name}"; $database = $this->getDefaultDatabase(); + $analyzeTable = $this->getPDO()->prepare("ANALYZE TABLE {$database}.{$collectionName}"); + $analyzeTable->execute(); + $query = $this->getPDO()->prepare(" SELECT SUM(data_length + index_length) AS total_size FROM information_schema.TABLES diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 6ff548223..9a23eb5ea 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -122,13 +122,6 @@ public function testCreateListExistsDeleteCollection(): void public function testSizeCollection(): void { - if(static::getAdapterName() === 'mysql'){ - $loopCount = 3000; - } - else{ - $loopCount = 20; - } - static::getDatabase()->createCollection('size_test1'); static::getDatabase()->createCollection('size_test2'); @@ -137,12 +130,10 @@ public function testSizeCollection(): void static::getDatabase()->createAttribute('size_test2', 'string1', Database::VAR_STRING, 128, true); static::getDatabase()->createAttribute('size_test2', 'string2', Database::VAR_STRING, 254 + 1, true); static::getDatabase()->createAttribute('size_test2', 'string3', Database::VAR_STRING, 254 + 1, true); - static::getDatabase()->createAttribute('size_test2', 'string4', Database::VAR_STRING, 254 + 1, true); - static::getDatabase()->createAttribute('size_test2', 'integer', Database::VAR_INTEGER, 10, true); - static::getDatabase()->createAttribute('size_test2', 'bigint', Database::VAR_INTEGER, 8, true); - static::getDatabase()->createAttribute('size_test2', 'float', Database::VAR_FLOAT, 0, true); static::getDatabase()->createIndex('size_test2', 'multi_index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); + $loopCount = 25; + for ($i = 0; $i < $loopCount; $i++) { static::getDatabase()->createDocument('size_test2', new Document([ '$permissions' => [ @@ -154,10 +145,6 @@ public function testSizeCollection(): void 'string1' => 'string1' . $i, 'string2' => 'string2' . $i, 'string3' => 'string3' . $i, - 'string4' => "string4" . $i, - 'integer' => 35635353 + $i, - 'bigint' => 3535353 + $i, - 'float' => 353.3535 + $i, ])); } From 540fe3c0e8402a37c38fd5af439d04ec1d34f7a9 Mon Sep 17 00:00:00 2001 From: faisalill Date: Tue, 13 Jun 2023 00:30:23 +0530 Subject: [PATCH 08/32] added analyzeCollection function --- composer.json | 4 ++++ src/Database/Adapter.php | 9 +++++++++ src/Database/Adapter/MariaDB.php | 25 ++++++++++++++++++++++--- src/Database/Adapter/Mongo.php | 10 ++++++++++ src/Database/Adapter/Postgres.php | 25 +++++++++++++++++++++++++ src/Database/Adapter/SQLite.php | 26 ++++++++++++++++++++++++++ src/Database/Database.php | 12 ++++++++++++ tests/Database/Base.php | 3 ++- 8 files changed, 110 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index c28e846a0..eb73a8ef2 100755 --- a/composer.json +++ b/composer.json @@ -24,6 +24,10 @@ "Composer\\Config::disableProcessTimeout", "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], + "mytest": [ + "Composer\\Config::disableProcessTimeout", + "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter/ --filter testSizeCollection" + ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", "check": "./vendor/bin/phpstan analyse --level 7 src tests --memory-limit 512M", diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index c0d5f5d54..72030c481 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -407,6 +407,15 @@ abstract public function count(string $collection, array $queries = [], ?int $ma */ abstract public function getSizeOfCollection(string $collection): int; + /** + * Analyze Collection + * Returns + * @param string $collection + * @return bool + * @throws DatabaseException + */ + abstract public function analyzeCollection(string $collection): bool; + /** * Get max STRING limit * diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 8228b33b6..4d479f337 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -154,9 +154,6 @@ public function getSizeOfCollection(string $collection): int $collectionName = "{$this->getNamespace()}_{$name}"; $database = $this->getDefaultDatabase(); - $analyzeTable = $this->getPDO()->prepare("ANALYZE TABLE {$database}.{$collectionName}"); - $analyzeTable->execute(); - $query = $this->getPDO()->prepare(" SELECT SUM(data_length + index_length) AS total_size FROM information_schema.TABLES @@ -178,6 +175,28 @@ public function getSizeOfCollection(string $collection): int return $size; } + /** + * Analyze Collection + * @param string $collection + * @return bool + * @throws DatabaseException + */ + public function analyzeCollection(string $collection): bool + { + $name = $this->filter($collection); + + $query = $this->getPDO()->prepare("ANALYZE TABLE {$this->getSQLTable($name)}"); + + try { + $query->execute(); + return true; + } + catch (PDOException $e) { + throw new DatabaseException('Failed to analyze collection: ' . $e->getMessage()); + return false; + } + } + /** * Delete Collection * @param string $id diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index f6dd234ef..e257f81fc 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -277,6 +277,16 @@ public function getSizeOfCollection(string $collection): int return $result->size; } + /** + * Analyze Collection + * @param string $collection + * @return bool + */ + public function analyzeCollection(string $collection): bool + { + return true; + } + /** * Delete Collection * diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 61868948a..7076c73c3 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -174,6 +174,31 @@ public function getSizeOfCollection(string $collection): int return $size; } + /** + * Analyze Collection + * @param string $collection + * @return bool + * @throws DatabaseException + * + */ + public function analyzeCollection(string $collection): bool + { + $name = $this->filter($collection); + + $query = $this->getPDO()->prepare(" + ANALYZE {$this->getSQLTable($name)}; + "); + + try { + $query->execute(); + return true; + } + catch (PDOException $e) { + throw new DatabaseException('Failed to analyze collection: ' . $e->getMessage()); + return false; + } + } + /** * Delete Collection * diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index 1738b9f2d..0f991bea2 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -185,6 +185,32 @@ public function getSizeOfCollection(string $collection): int return $size; } + /** + * Analyze Collection + * @param string $collection + * @return bool + * @throws DatabaseException + * + */ + public function analyzeCollection(string $collection): bool + { + $name = $this->filter($collection); + $namespace = $this->getNamespace(); + + $query = $this->getPDO()->prepare(" + ANALYZE {$namespace}_{$name}; + "); + + try { + $query->execute(); + return true; + } + catch (PDOException $e) { + throw new DatabaseException('Failed to analyze collection: ' . $e->getMessage()); + return false; + } + } + /** * Delete Collection * @param string $id diff --git a/src/Database/Database.php b/src/Database/Database.php index 031a71a62..f83051ae0 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -768,6 +768,18 @@ public function getSizeOfCollection(string $collection): int return $this->adapter->getSizeOfCollection($collection); } + /** + * Analyze Collection + * + * @param string $collection + * + * @return bool + */ + public function analyzeCollection(string $collection): bool + { + return $this->adapter->analyzeCollection($collection); + } + /** * Delete Collection * diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 9a23eb5ea..222b41187 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -148,10 +148,11 @@ public function testSizeCollection(): void ])); } + static::getDatabase()->analyzeCollection('size_test2'); + $size_test_2 = static::getDatabase()->getSizeOfCollection('size_test2'); $this->assertGreaterThan($size_test_1, $size_test_2); - } public function testCreateDeleteAttribute(): void From a664c6b8fbb36bc3759ae213b847864eea800315 Mon Sep 17 00:00:00 2001 From: faisalill Date: Tue, 13 Jun 2023 01:02:04 +0530 Subject: [PATCH 09/32] called analyzeCollection from getSizeOfCollection --- composer.json | 4 ---- src/Database/Adapter/MariaDB.php | 1 + src/Database/Adapter/Postgres.php | 1 + src/Database/Adapter/SQLite.php | 1 + tests/Database/Base.php | 2 -- 5 files changed, 3 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index eb73a8ef2..c28e846a0 100755 --- a/composer.json +++ b/composer.json @@ -24,10 +24,6 @@ "Composer\\Config::disableProcessTimeout", "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], - "mytest": [ - "Composer\\Config::disableProcessTimeout", - "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter/ --filter testSizeCollection" - ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", "check": "./vendor/bin/phpstan analyse --level 7 src tests --memory-limit 512M", diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 14c66207f..80de41d9d 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -165,6 +165,7 @@ public function getSizeOfCollection(string $collection): int $query->bindParam(':name', $collectionName); try { + $this->analyzeCollection($name); $query->execute(); $size = $query->fetchColumn(); } diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index c5fc461be..2a78aff42 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -167,6 +167,7 @@ public function getSizeOfCollection(string $collection): int "); try { + $this->analyzeCollection($name); $query->execute(); $size = $query->fetchColumn(); } diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index 0f991bea2..eff9ac151 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -175,6 +175,7 @@ public function getSizeOfCollection(string $collection): int "); try { + $this->analyzeCollection($name); $query->execute(); $size = $query->fetchColumn(); } diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 3c578ee4b..7118e36d6 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -276,8 +276,6 @@ public function testSizeCollection(): void ])); } - static::getDatabase()->analyzeCollection('size_test2'); - $size_test_2 = static::getDatabase()->getSizeOfCollection('size_test2'); $this->assertGreaterThan($size_test_1, $size_test_2); From 7b0a74a8c2c99f59d0f0bf31af7856c58fa83e80 Mon Sep 17 00:00:00 2001 From: faisalill Date: Tue, 13 Jun 2023 01:08:21 +0530 Subject: [PATCH 10/32] removed Return from comments --- src/Database/Adapter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 2fb28de3f..24f529b17 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -400,7 +400,7 @@ abstract public function count(string $collection, array $queries = [], ?int $ma /** * Get Collection Size - * Returns + * * @param string $collection * @return int * @throws DatabaseException @@ -409,7 +409,7 @@ abstract public function getSizeOfCollection(string $collection): int; /** * Analyze Collection - * Returns + * * @param string $collection * @return bool * @throws DatabaseException From 2e60d71d03591cd565425ace1211a2877d60de67 Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 21 Jun 2023 22:03:54 +0530 Subject: [PATCH 11/32] remove analyzeCollection and changed query for mariadb and mysql --- composer.json | 4 + src/Database/Adapter.php | 9 -- src/Database/Adapter/MariaDB.php | 30 +--- src/Database/Adapter/Mongo.php | 10 -- src/Database/Adapter/MySQL.php | 32 ++++ src/Database/Adapter/Postgres.php | 26 ---- src/Database/Adapter/SQLite.php | 34 +---- src/Database/Database.php | 12 -- tests/Database/Base.php | 240 +++--------------------------- 9 files changed, 62 insertions(+), 335 deletions(-) diff --git a/composer.json b/composer.json index c28e846a0..9d9b2b58f 100755 --- a/composer.json +++ b/composer.json @@ -24,6 +24,10 @@ "Composer\\Config::disableProcessTimeout", "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], + "mytest": [ + "Composer\\Config::disableProcessTimeout", + "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter --filter testSizeCollection" + ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", "check": "./vendor/bin/phpstan analyse --level 7 src tests --memory-limit 512M", diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 24f529b17..5a4b7664c 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -407,15 +407,6 @@ abstract public function count(string $collection, array $queries = [], ?int $ma */ abstract public function getSizeOfCollection(string $collection): int; - /** - * Analyze Collection - * - * @param string $collection - * @return bool - * @throws DatabaseException - */ - abstract public function analyzeCollection(string $collection): bool; - /** * Get max STRING limit * diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index bae03ff62..15061f717 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -155,17 +155,15 @@ public function getSizeOfCollection(string $collection): int $database = $this->getDefaultDatabase(); $query = $this->getPDO()->prepare(" - SELECT SUM(data_length + index_length) AS total_size - FROM information_schema.TABLES - WHERE table_schema = :database - AND table_name = :name + SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) + FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES + WHERE NAME = CONCAT(:database, '/', :name) "); $query->bindParam(':database', $database); $query->bindParam(':name', $collectionName); try { - $this->analyzeCollection($name); $query->execute(); $size = $query->fetchColumn(); } @@ -176,28 +174,6 @@ public function getSizeOfCollection(string $collection): int return $size; } - /** - * Analyze Collection - * @param string $collection - * @return bool - * @throws DatabaseException - */ - public function analyzeCollection(string $collection): bool - { - $name = $this->filter($collection); - - $query = $this->getPDO()->prepare("ANALYZE TABLE {$this->getSQLTable($name)}"); - - try { - $query->execute(); - return true; - } - catch (PDOException $e) { - throw new DatabaseException('Failed to analyze collection: ' . $e->getMessage()); - return false; - } - } - /** * Delete Collection * @param string $id diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 06fc15e3c..f6113697d 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -277,16 +277,6 @@ public function getSizeOfCollection(string $collection): int return $result->size; } - /** - * Analyze Collection - * @param string $collection - * @return bool - */ - public function analyzeCollection(string $collection): bool - { - return true; - } - /** * Delete Collection * diff --git a/src/Database/Adapter/MySQL.php b/src/Database/Adapter/MySQL.php index 6bc4e9b0f..3f6fcfc90 100644 --- a/src/Database/Adapter/MySQL.php +++ b/src/Database/Adapter/MySQL.php @@ -78,4 +78,36 @@ protected function processException(PDOException $e): void throw $e; } + + /** + * Get Collection Size + * @param string $collection + * @return int + * @throws DatabaseException + */ + public function getSizeOfCollection(string $collection): int + { + $name = $this->filter($collection); + $collectionName = "{$this->getNamespace()}_{$name}"; + $database = $this->getDefaultDatabase(); + + $query = $this->getPDO()->prepare(" + SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) + FROM INFORMATION_SCHEMA.INNODB_TABLESPACES + WHERE NAME = CONCAT(:database, '/', :name) + "); + + $query->bindParam(':database', $database); + $query->bindParam(':name', $collectionName); + + try { + $query->execute(); + $size = $query->fetchColumn(); + } + catch (PDOException $e) { + throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); + } + + return $size; + } } diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 1d2a569db..0d0a6d0eb 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -167,7 +167,6 @@ public function getSizeOfCollection(string $collection): int "); try { - $this->analyzeCollection($name); $query->execute(); $size = $query->fetchColumn(); } @@ -178,31 +177,6 @@ public function getSizeOfCollection(string $collection): int return $size; } - /** - * Analyze Collection - * @param string $collection - * @return bool - * @throws DatabaseException - * - */ - public function analyzeCollection(string $collection): bool - { - $name = $this->filter($collection); - - $query = $this->getPDO()->prepare(" - ANALYZE {$this->getSQLTable($name)}; - "); - - try { - $query->execute(); - return true; - } - catch (PDOException $e) { - throw new DatabaseException('Failed to analyze collection: ' . $e->getMessage()); - return false; - } - } - /** * Delete Collection * diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index eff9ac151..3e9894064 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -169,13 +169,17 @@ public function getSizeOfCollection(string $collection): int { $name = $this->filter($collection); $namespace = $this->getNamespace(); - + + $analyze = $this->getPDO()->prepare(" + ANALYZE {$namespace}_{$name}; + "); + $query = $this->getPDO()->prepare(" SELECT SUM(\"pgsize\") FROM \"dbstat\" WHERE name='{$namespace}_{$name}'; "); try { - $this->analyzeCollection($name); + $analyze->execute(); $query->execute(); $size = $query->fetchColumn(); } @@ -186,32 +190,6 @@ public function getSizeOfCollection(string $collection): int return $size; } - /** - * Analyze Collection - * @param string $collection - * @return bool - * @throws DatabaseException - * - */ - public function analyzeCollection(string $collection): bool - { - $name = $this->filter($collection); - $namespace = $this->getNamespace(); - - $query = $this->getPDO()->prepare(" - ANALYZE {$namespace}_{$name}; - "); - - try { - $query->execute(); - return true; - } - catch (PDOException $e) { - throw new DatabaseException('Failed to analyze collection: ' . $e->getMessage()); - return false; - } - } - /** * Delete Collection * @param string $id diff --git a/src/Database/Database.php b/src/Database/Database.php index 415d99c0e..c9b46fc00 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -778,18 +778,6 @@ public function getSizeOfCollection(string $collection): int return $this->adapter->getSizeOfCollection($collection); } - /** - * Analyze Collection - * - * @param string $collection - * - * @return bool - */ - public function analyzeCollection(string $collection): bool - { - return $this->adapter->analyzeCollection($collection); - } - /** * Delete Collection * diff --git a/tests/Database/Base.php b/tests/Database/Base.php index d84eb09c8..f0ef28698 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -5,7 +5,6 @@ use Exception; use InvalidArgumentException; use PHPUnit\Framework\TestCase; -use Throwable; use Utopia\Database\Adapter\SQL; use Utopia\Database\Database; use Utopia\Database\DateTime; @@ -22,7 +21,6 @@ use Utopia\Database\Helpers\Role; use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Datetime as DatetimeValidator; -use Utopia\Database\Validator\Index; use Utopia\Database\Validator\Query\Filter; use Utopia\Database\Validator\Structure; use Utopia\Validator\Range; @@ -78,132 +76,6 @@ public function testCreateExistsDelete(): void $this->assertEquals(true, static::getDatabase()->create()); } - /** - * @throws Exception|Throwable - */ - public function testIndexValidation(): void - { - $attributes = [ - new Document([ - '$id' => ID::custom('title1'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 700, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ]), - new Document([ - '$id' => ID::custom('title2'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 500, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ]), - ]; - - $indexes = [ - new Document([ - '$id' => ID::custom('index1'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['title1', 'title2'], - 'lengths' => [701,50], - 'orders' => [], - ]), - ]; - - $collection = new Document([ - '$id' => ID::custom('index_length'), - 'name' => 'test', - 'attributes' => $attributes, - 'indexes' => $indexes - ]); - - $validator = new Index(static::getDatabase()->getAdapter()->getMaxIndexLength()); - - $errorMessage = 'Index length 701 is larger than the size for title1: 700"'; - $this->assertFalse($validator->isValid($collection)); - $this->assertEquals($errorMessage, $validator->getDescription()); - - try { - static::getDatabase()->createCollection($collection->getId(), $attributes, $indexes); - $this->fail('Failed to throw exception'); - } catch (Exception $e) { - $this->assertEquals($errorMessage, $e->getMessage()); - } - - $indexes = [ - new Document([ - '$id' => ID::custom('index1'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['title1', 'title2'], - 'lengths' => [700], // 700, 500 (length(title2)) - 'orders' => [], - ]), - ]; - - $collection->setAttribute('indexes', $indexes); - - if (static::getDatabase()->getAdapter()->getMaxIndexLength() > 0) { - $errorMessage = 'Index length is longer than the maximum: ' . static::getDatabase()->getAdapter()->getMaxIndexLength(); - $this->assertFalse($validator->isValid($collection)); - $this->assertEquals($errorMessage, $validator->getDescription()); - - try { - static::getDatabase()->createCollection($collection->getId(), $attributes, $indexes); - $this->fail('Failed to throw exception'); - } catch (Exception $e) { - $this->assertEquals($errorMessage, $e->getMessage()); - } - } - - $attributes[] = new Document([ - '$id' => ID::custom('integer'), - 'type' => Database::VAR_INTEGER, - 'format' => '', - 'size' => 10000, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ]); - - $indexes = [ - new Document([ - '$id' => ID::custom('index1'), - 'type' => Database::INDEX_FULLTEXT, - 'attributes' => ['title1', 'integer'], - 'lengths' => [], - 'orders' => [], - ]), - ]; - - $collection = new Document([ - '$id' => ID::custom('index_length'), - 'name' => 'test', - 'attributes' => $attributes, - 'indexes' => $indexes - ]); - - $errorMessage = 'Attribute "integer" cannot be part of a FULLTEXT index, must be of type string'; - $this->assertFalse($validator->isValid($collection)); - $this->assertEquals($errorMessage, $validator->getDescription()); - - try { - static::getDatabase()->createCollection($collection->getId(), $attributes, $indexes); - $this->fail('Failed to throw exception'); - } catch (Exception $e) { - $this->assertEquals($errorMessage, $e->getMessage()); - } - } - public function testCreatedAtUpdatedAt(): void { $this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createCollection('created_at')); @@ -250,23 +122,25 @@ public function testCreateListExistsDeleteCollection(): void public function testSizeCollection(): void { - static::getDatabase()->createCollection('size_test1'); - static::getDatabase()->createCollection('size_test2'); + static::getDatabase()->createCollection('sizeTest1'); + static::getDatabase()->createCollection('sizeTest2'); - $size_test_1 = static::getDatabase()->getSizeOfCollection('size_test1'); + $size1 = static::getDatabase()->getSizeOfCollection('sizeTest1'); + $size2 = static::getDatabase()->getSizeOfCollection('sizeTest2'); + + $this->assertEquals($size1,$size2); - static::getDatabase()->createAttribute('size_test2', 'string1', Database::VAR_STRING, 128, true); - static::getDatabase()->createAttribute('size_test2', 'string2', Database::VAR_STRING, 254 + 1, true); - static::getDatabase()->createAttribute('size_test2', 'string3', Database::VAR_STRING, 254 + 1, true); - static::getDatabase()->createIndex('size_test2', 'multi_index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); + static::getDatabase()->createAttribute('sizeTest2', 'string1', Database::VAR_STRING, 128, true); + static::getDatabase()->createAttribute('sizeTest2', 'string2', Database::VAR_STRING, 254 + 1, true); + static::getDatabase()->createAttribute('sizeTest2', 'string3', Database::VAR_STRING, 254 + 1, true); + static::getDatabase()->createIndex('sizeTest2', 'multi_index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); $loopCount = 25; for ($i = 0; $i < $loopCount; $i++) { - static::getDatabase()->createDocument('size_test2', new Document([ + static::getDatabase()->createDocument('sizeTest2', new Document([ '$permissions' => [ Permission::read(Role::any()), - Permission::create(Role::any()), Permission::update(Role::any()), Permission::delete(Role::any()), ], @@ -276,9 +150,9 @@ public function testSizeCollection(): void ])); } - $size_test_2 = static::getDatabase()->getSizeOfCollection('size_test2'); + $size2 = static::getDatabase()->getSizeOfCollection('sizeTest2'); - $this->assertGreaterThan($size_test_1, $size_test_2); + $this->assertGreaterThan($size1, $size2); } public function testCreateDeleteAttribute(): void @@ -286,7 +160,7 @@ public function testCreateDeleteAttribute(): void static::getDatabase()->createCollection('attributes'); $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string1', Database::VAR_STRING, 128, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string2', Database::VAR_STRING, 16382 + 1, true)); + $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string2', Database::VAR_STRING, 16383 + 1, true)); $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string3', Database::VAR_STRING, 65535 + 1, true)); $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string4', Database::VAR_STRING, 16777215 + 1, true)); $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'integer', Database::VAR_INTEGER, 0, true)); @@ -432,16 +306,6 @@ public function testAttributeKeyWithSymbols(): void $this->assertEquals('value', $document->getAttribute('key_with.sym$bols')); } - public function testCollectionNotFound(): void - { - try { - static::getDatabase()->find('not_exist', []); - $this->fail('Failed to throw Exception'); - } catch (Exception $e) { - $this->assertEquals('Collection "not_exist" not found', $e->getMessage()); - } - } - public function testAttributeNamesWithDots(): void { static::getDatabase()->createCollection('dots.parent'); @@ -949,7 +813,7 @@ public function testCreateDocumentDefaults(): void } /** - * @throws AuthorizationException|LimitException|DuplicateException|StructureException|Exception|Throwable + * @throws AuthorizationException|LimitException|DuplicateException|StructureException|Exception|\Throwable */ public function testIncreaseDecrease(): Document { @@ -1072,13 +936,7 @@ public function testGetDocumentSelect(Document $document): Document public function testFulltextIndexWithInteger(): void { $this->expectException(Exception::class); - - if (!$this->getDatabase()->getAdapter()->getSupportForFulltextIndex()) { - $this->expectExceptionMessage('Fulltext index is not supported'); - } else { - $this->expectExceptionMessage('Attribute "integer" cannot be part of a FULLTEXT index, must be of type string'); - } - + $this->expectExceptionMessage('Attribute "integer" cannot be part of a FULLTEXT index'); static::getDatabase()->createIndex('documents', 'fulltext_integer', Database::INDEX_FULLTEXT, ['string','integer']); } @@ -1650,70 +1508,6 @@ public function testFindFulltext(): void $this->assertEquals(true, true); // Test must do an assertion } - public function testFindFulltextSpecialChars(): void - { - if (!static::getDatabase()->getAdapter()->getSupportForFulltextIndex()) { - $this->expectNotToPerformAssertions(); - return; - } - - $collection = 'full_text'; - static::getDatabase()->createCollection($collection, permissions: [ - Permission::create(Role::any()), - Permission::update(Role::users()) - ]); - - $this->assertTrue(static::getDatabase()->createAttribute($collection, 'ft', Database::VAR_STRING, 128, true)); - $this->assertTrue(static::getDatabase()->createIndex($collection, 'ft-index', Database::INDEX_FULLTEXT, ['ft'])); - - static::getDatabase()->createDocument($collection, new Document([ - '$permissions' => [Permission::read(Role::any())], - 'ft' => 'Alf: chapter_4@nasa.com' - ])); - - $documents = static::getDatabase()->find($collection, [ - Query::search('ft', 'chapter_4'), - ]); - $this->assertEquals(1, count($documents)); - - static::getDatabase()->createDocument($collection, new Document([ - '$permissions' => [Permission::read(Role::any())], - 'ft' => 'al@ba.io +-*)(<>~' - ])); - - $documents = static::getDatabase()->find($collection, [ - Query::search('ft', 'al@ba.io'), // === al ba io* - ]); - - if (static::getDatabase()->getAdapter()->getSupportForFulltextWildcardIndex()) { - $this->assertEquals(0, count($documents)); - } else { - $this->assertEquals(1, count($documents)); - } - - static::getDatabase()->createDocument($collection, new Document([ - '$permissions' => [Permission::read(Role::any())], - 'ft' => 'donald duck' - ])); - - static::getDatabase()->createDocument($collection, new Document([ - '$permissions' => [Permission::read(Role::any())], - 'ft' => 'donald trump' - ])); - - $documents = static::getDatabase()->find($collection, [ - Query::search('ft', 'donald trump'), - Query::orderAsc('ft'), - ]); - $this->assertEquals(2, count($documents)); - - $documents = static::getDatabase()->find($collection, [ - Query::search('ft', '"donald trump"'), // Exact match - ]); - - $this->assertEquals(1, count($documents)); - } - public function testFindMultipleConditions(): void { /** @@ -4439,7 +4233,7 @@ public function testOneToOneOneWayRelationship(): void * @throws LimitException * @throws DuplicateException * @throws StructureException - * @throws Throwable + * @throws \Throwable */ public function testOneToOneTwoWayRelationship(): void { From 416fa5f309a3975eac667d51c394e32bcf57a29d Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 21 Jun 2023 22:09:13 +0530 Subject: [PATCH 12/32] cleaner code --- composer.json | 4 - tests/Database/Base.php | 226 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 217 insertions(+), 13 deletions(-) diff --git a/composer.json b/composer.json index 9d9b2b58f..c28e846a0 100755 --- a/composer.json +++ b/composer.json @@ -24,10 +24,6 @@ "Composer\\Config::disableProcessTimeout", "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], - "mytest": [ - "Composer\\Config::disableProcessTimeout", - "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter --filter testSizeCollection" - ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", "check": "./vendor/bin/phpstan analyse --level 7 src tests --memory-limit 512M", diff --git a/tests/Database/Base.php b/tests/Database/Base.php index f0ef28698..071300211 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -5,6 +5,7 @@ use Exception; use InvalidArgumentException; use PHPUnit\Framework\TestCase; +use Throwable; use Utopia\Database\Adapter\SQL; use Utopia\Database\Database; use Utopia\Database\DateTime; @@ -21,6 +22,7 @@ use Utopia\Database\Helpers\Role; use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Datetime as DatetimeValidator; +use Utopia\Database\Validator\Index; use Utopia\Database\Validator\Query\Filter; use Utopia\Database\Validator\Structure; use Utopia\Validator\Range; @@ -76,6 +78,132 @@ public function testCreateExistsDelete(): void $this->assertEquals(true, static::getDatabase()->create()); } + /** + * @throws Exception|Throwable + */ + public function testIndexValidation(): void + { + $attributes = [ + new Document([ + '$id' => ID::custom('title1'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 700, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ]), + new Document([ + '$id' => ID::custom('title2'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 500, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ]), + ]; + + $indexes = [ + new Document([ + '$id' => ID::custom('index1'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['title1', 'title2'], + 'lengths' => [701,50], + 'orders' => [], + ]), + ]; + + $collection = new Document([ + '$id' => ID::custom('index_length'), + 'name' => 'test', + 'attributes' => $attributes, + 'indexes' => $indexes + ]); + + $validator = new Index(static::getDatabase()->getAdapter()->getMaxIndexLength()); + + $errorMessage = 'Index length 701 is larger than the size for title1: 700"'; + $this->assertFalse($validator->isValid($collection)); + $this->assertEquals($errorMessage, $validator->getDescription()); + + try { + static::getDatabase()->createCollection($collection->getId(), $attributes, $indexes); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertEquals($errorMessage, $e->getMessage()); + } + + $indexes = [ + new Document([ + '$id' => ID::custom('index1'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['title1', 'title2'], + 'lengths' => [700], // 700, 500 (length(title2)) + 'orders' => [], + ]), + ]; + + $collection->setAttribute('indexes', $indexes); + + if (static::getDatabase()->getAdapter()->getMaxIndexLength() > 0) { + $errorMessage = 'Index length is longer than the maximum: ' . static::getDatabase()->getAdapter()->getMaxIndexLength(); + $this->assertFalse($validator->isValid($collection)); + $this->assertEquals($errorMessage, $validator->getDescription()); + + try { + static::getDatabase()->createCollection($collection->getId(), $attributes, $indexes); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertEquals($errorMessage, $e->getMessage()); + } + } + + $attributes[] = new Document([ + '$id' => ID::custom('integer'), + 'type' => Database::VAR_INTEGER, + 'format' => '', + 'size' => 10000, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ]); + + $indexes = [ + new Document([ + '$id' => ID::custom('index1'), + 'type' => Database::INDEX_FULLTEXT, + 'attributes' => ['title1', 'integer'], + 'lengths' => [], + 'orders' => [], + ]), + ]; + + $collection = new Document([ + '$id' => ID::custom('index_length'), + 'name' => 'test', + 'attributes' => $attributes, + 'indexes' => $indexes + ]); + + $errorMessage = 'Attribute "integer" cannot be part of a FULLTEXT index, must be of type string'; + $this->assertFalse($validator->isValid($collection)); + $this->assertEquals($errorMessage, $validator->getDescription()); + + try { + static::getDatabase()->createCollection($collection->getId(), $attributes, $indexes); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertEquals($errorMessage, $e->getMessage()); + } + } + public function testCreatedAtUpdatedAt(): void { $this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createCollection('created_at')); @@ -124,18 +252,18 @@ public function testSizeCollection(): void { static::getDatabase()->createCollection('sizeTest1'); static::getDatabase()->createCollection('sizeTest2'); - + $size1 = static::getDatabase()->getSizeOfCollection('sizeTest1'); $size2 = static::getDatabase()->getSizeOfCollection('sizeTest2'); $this->assertEquals($size1,$size2); - + static::getDatabase()->createAttribute('sizeTest2', 'string1', Database::VAR_STRING, 128, true); static::getDatabase()->createAttribute('sizeTest2', 'string2', Database::VAR_STRING, 254 + 1, true); static::getDatabase()->createAttribute('sizeTest2', 'string3', Database::VAR_STRING, 254 + 1, true); static::getDatabase()->createIndex('sizeTest2', 'multi_index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); - $loopCount = 25; + $loopCount = 30; for ($i = 0; $i < $loopCount; $i++) { static::getDatabase()->createDocument('sizeTest2', new Document([ @@ -154,13 +282,13 @@ public function testSizeCollection(): void $this->assertGreaterThan($size1, $size2); } - + public function testCreateDeleteAttribute(): void { static::getDatabase()->createCollection('attributes'); $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string1', Database::VAR_STRING, 128, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string2', Database::VAR_STRING, 16383 + 1, true)); + $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string2', Database::VAR_STRING, 16382 + 1, true)); $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string3', Database::VAR_STRING, 65535 + 1, true)); $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string4', Database::VAR_STRING, 16777215 + 1, true)); $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'integer', Database::VAR_INTEGER, 0, true)); @@ -306,6 +434,16 @@ public function testAttributeKeyWithSymbols(): void $this->assertEquals('value', $document->getAttribute('key_with.sym$bols')); } + public function testCollectionNotFound(): void + { + try { + static::getDatabase()->find('not_exist', []); + $this->fail('Failed to throw Exception'); + } catch (Exception $e) { + $this->assertEquals('Collection "not_exist" not found', $e->getMessage()); + } + } + public function testAttributeNamesWithDots(): void { static::getDatabase()->createCollection('dots.parent'); @@ -813,7 +951,7 @@ public function testCreateDocumentDefaults(): void } /** - * @throws AuthorizationException|LimitException|DuplicateException|StructureException|Exception|\Throwable + * @throws AuthorizationException|LimitException|DuplicateException|StructureException|Exception|Throwable */ public function testIncreaseDecrease(): Document { @@ -936,7 +1074,13 @@ public function testGetDocumentSelect(Document $document): Document public function testFulltextIndexWithInteger(): void { $this->expectException(Exception::class); - $this->expectExceptionMessage('Attribute "integer" cannot be part of a FULLTEXT index'); + + if (!$this->getDatabase()->getAdapter()->getSupportForFulltextIndex()) { + $this->expectExceptionMessage('Fulltext index is not supported'); + } else { + $this->expectExceptionMessage('Attribute "integer" cannot be part of a FULLTEXT index, must be of type string'); + } + static::getDatabase()->createIndex('documents', 'fulltext_integer', Database::INDEX_FULLTEXT, ['string','integer']); } @@ -1508,6 +1652,70 @@ public function testFindFulltext(): void $this->assertEquals(true, true); // Test must do an assertion } + public function testFindFulltextSpecialChars(): void + { + if (!static::getDatabase()->getAdapter()->getSupportForFulltextIndex()) { + $this->expectNotToPerformAssertions(); + return; + } + + $collection = 'full_text'; + static::getDatabase()->createCollection($collection, permissions: [ + Permission::create(Role::any()), + Permission::update(Role::users()) + ]); + + $this->assertTrue(static::getDatabase()->createAttribute($collection, 'ft', Database::VAR_STRING, 128, true)); + $this->assertTrue(static::getDatabase()->createIndex($collection, 'ft-index', Database::INDEX_FULLTEXT, ['ft'])); + + static::getDatabase()->createDocument($collection, new Document([ + '$permissions' => [Permission::read(Role::any())], + 'ft' => 'Alf: chapter_4@nasa.com' + ])); + + $documents = static::getDatabase()->find($collection, [ + Query::search('ft', 'chapter_4'), + ]); + $this->assertEquals(1, count($documents)); + + static::getDatabase()->createDocument($collection, new Document([ + '$permissions' => [Permission::read(Role::any())], + 'ft' => 'al@ba.io +-*)(<>~' + ])); + + $documents = static::getDatabase()->find($collection, [ + Query::search('ft', 'al@ba.io'), // === al ba io* + ]); + + if (static::getDatabase()->getAdapter()->getSupportForFulltextWildcardIndex()) { + $this->assertEquals(0, count($documents)); + } else { + $this->assertEquals(1, count($documents)); + } + + static::getDatabase()->createDocument($collection, new Document([ + '$permissions' => [Permission::read(Role::any())], + 'ft' => 'donald duck' + ])); + + static::getDatabase()->createDocument($collection, new Document([ + '$permissions' => [Permission::read(Role::any())], + 'ft' => 'donald trump' + ])); + + $documents = static::getDatabase()->find($collection, [ + Query::search('ft', 'donald trump'), + Query::orderAsc('ft'), + ]); + $this->assertEquals(2, count($documents)); + + $documents = static::getDatabase()->find($collection, [ + Query::search('ft', '"donald trump"'), // Exact match + ]); + + $this->assertEquals(1, count($documents)); + } + public function testFindMultipleConditions(): void { /** @@ -4233,7 +4441,7 @@ public function testOneToOneOneWayRelationship(): void * @throws LimitException * @throws DuplicateException * @throws StructureException - * @throws \Throwable + * @throws Throwable */ public function testOneToOneTwoWayRelationship(): void { @@ -11193,4 +11401,4 @@ public function testLast(): void $this->expectNotToPerformAssertions(); static::killDatabase(); } -} +} \ No newline at end of file From b6c4a03a8ddb8cef156a51db39f5633deaf7d64b Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 21 Jun 2023 22:11:12 +0530 Subject: [PATCH 13/32] added camel case for index --- tests/Database/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 071300211..ba8f9c9bb 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -261,7 +261,7 @@ public function testSizeCollection(): void static::getDatabase()->createAttribute('sizeTest2', 'string1', Database::VAR_STRING, 128, true); static::getDatabase()->createAttribute('sizeTest2', 'string2', Database::VAR_STRING, 254 + 1, true); static::getDatabase()->createAttribute('sizeTest2', 'string3', Database::VAR_STRING, 254 + 1, true); - static::getDatabase()->createIndex('sizeTest2', 'multi_index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); + static::getDatabase()->createIndex('sizeTest2', 'multiIndex', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); $loopCount = 30; From 682ca9fb0bd9a782775aee58a01e1a4c68dbebdf Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 21 Jun 2023 22:16:15 +0530 Subject: [PATCH 14/32] removed analyze for sqlite --- composer.json | 4 ++++ src/Database/Adapter/SQLite.php | 5 ----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index c28e846a0..9d9b2b58f 100755 --- a/composer.json +++ b/composer.json @@ -24,6 +24,10 @@ "Composer\\Config::disableProcessTimeout", "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], + "mytest": [ + "Composer\\Config::disableProcessTimeout", + "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter --filter testSizeCollection" + ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", "check": "./vendor/bin/phpstan analyse --level 7 src tests --memory-limit 512M", diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index 3e9894064..dd1d5ef91 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -170,16 +170,11 @@ public function getSizeOfCollection(string $collection): int $name = $this->filter($collection); $namespace = $this->getNamespace(); - $analyze = $this->getPDO()->prepare(" - ANALYZE {$namespace}_{$name}; - "); - $query = $this->getPDO()->prepare(" SELECT SUM(\"pgsize\") FROM \"dbstat\" WHERE name='{$namespace}_{$name}'; "); try { - $analyze->execute(); $query->execute(); $size = $query->fetchColumn(); } From 3dc00b32502730085f84d043759d999e5ed35c7c Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 21 Jun 2023 22:16:37 +0530 Subject: [PATCH 15/32] cleaner code --- composer.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/composer.json b/composer.json index 9d9b2b58f..c28e846a0 100755 --- a/composer.json +++ b/composer.json @@ -24,10 +24,6 @@ "Composer\\Config::disableProcessTimeout", "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], - "mytest": [ - "Composer\\Config::disableProcessTimeout", - "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter --filter testSizeCollection" - ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", "check": "./vendor/bin/phpstan analyse --level 7 src tests --memory-limit 512M", From ecda582921f3278754445c066f38eab041e85176 Mon Sep 17 00:00:00 2001 From: faisalill Date: Thu, 22 Jun 2023 20:48:36 +0530 Subject: [PATCH 16/32] changed mariadb query --- src/Database/Adapter/MariaDB.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 15061f717..2f0405384 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -150,18 +150,18 @@ public function createCollection(string $name, array $attributes = [], array $in */ public function getSizeOfCollection(string $collection): int { - $name = $this->filter($collection); - $collectionName = "{$this->getNamespace()}_{$name}"; + $filteredName = $this->filter($collection); + $collectionName = "{$this->getNamespace()}_{$filteredName}"; $database = $this->getDefaultDatabase(); + $name = $database . '/' . $collectionName; $query = $this->getPDO()->prepare(" SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES - WHERE NAME = CONCAT(:database, '/', :name) + WHERE NAME = CONCAT(:name) "); - $query->bindParam(':database', $database); - $query->bindParam(':name', $collectionName); + $query->bindParam(':name', $name); try { $query->execute(); From 4a6ce3f20ac8ed136c2cdb634425952d7ccf21a6 Mon Sep 17 00:00:00 2001 From: faisalill Date: Thu, 29 Jun 2023 18:11:23 +0530 Subject: [PATCH 17/32] changed query of getSizeOfCollection --- src/Database/Adapter/MariaDB.php | 11 +++++------ src/Database/Adapter/Mongo.php | 8 +++++--- src/Database/Adapter/MySQL.php | 13 ++++++------- src/Database/Adapter/Postgres.php | 10 ++++++---- src/Database/Adapter/SQLite.php | 10 ++++++---- tests/Database/Base.php | 9 ++------- 6 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 2f0405384..4effad04e 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -150,15 +150,15 @@ public function createCollection(string $name, array $attributes = [], array $in */ public function getSizeOfCollection(string $collection): int { - $filteredName = $this->filter($collection); - $collectionName = "{$this->getNamespace()}_{$filteredName}"; + $collection = $this->filter($collection); + $collection = $this->getNamespace() . '_' . $collection; $database = $this->getDefaultDatabase(); - $name = $database . '/' . $collectionName; + $name = $database . '/' . $collection; $query = $this->getPDO()->prepare(" SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES - WHERE NAME = CONCAT(:name) + WHERE NAME = :name "); $query->bindParam(':name', $name); @@ -166,8 +166,7 @@ public function getSizeOfCollection(string $collection): int try { $query->execute(); $size = $query->fetchColumn(); - } - catch (PDOException $e) { + } catch (PDOException $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index f6113697d..76c6eb6a7 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -262,15 +262,17 @@ public function listCollections(): array public function getSizeOfCollection(string $collection): int { $namespace = $this->getNamespace(); + $collection = $this->filter($collection); + $collection = $namespace. '_' . $collection; + $command = [ - 'collStats' => $namespace . '_' . $this->filter($collection), + 'collStats' => $collection, 'scale' => 1 ]; try { $result = $this->getClient()->query($command); - } - catch(Exception $e) { + } catch(Exception $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } diff --git a/src/Database/Adapter/MySQL.php b/src/Database/Adapter/MySQL.php index 3f6fcfc90..792de191a 100644 --- a/src/Database/Adapter/MySQL.php +++ b/src/Database/Adapter/MySQL.php @@ -87,24 +87,23 @@ protected function processException(PDOException $e): void */ public function getSizeOfCollection(string $collection): int { - $name = $this->filter($collection); - $collectionName = "{$this->getNamespace()}_{$name}"; + $collection = $this->filter($collection); + $collection = $this->getNamespace() . '_' . $collection; $database = $this->getDefaultDatabase(); + $name = $database . '/' . $collection; $query = $this->getPDO()->prepare(" SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) FROM INFORMATION_SCHEMA.INNODB_TABLESPACES - WHERE NAME = CONCAT(:database, '/', :name) + WHERE NAME = :name "); - $query->bindParam(':database', $database); - $query->bindParam(':name', $collectionName); + $query->bindParam(':name', $name); try { $query->execute(); $size = $query->fetchColumn(); - } - catch (PDOException $e) { + } catch (PDOException $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 0d0a6d0eb..e5ea70f5c 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -160,17 +160,19 @@ public function createCollection(string $name, array $attributes = [], array $in */ public function getSizeOfCollection(string $collection): int { - $name = $this->filter($collection); + $collection = $this->filter($collection); + $name = $this->getSQLTable($collection); $query = $this->getPDO()->prepare(" - SELECT pg_total_relation_size('{$this->getSQLTable($name)}'); + SELECT pg_total_relation_size(:name); "); + $query->bindParam(':name', $name); + try { $query->execute(); $size = $query->fetchColumn(); - } - catch (PDOException $e) { + } catch (PDOException $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index dd1d5ef91..9abd946a8 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -167,18 +167,20 @@ public function createCollection(string $name, array $attributes = [], array $in */ public function getSizeOfCollection(string $collection): int { - $name = $this->filter($collection); + $collection = $this->filter($collection); $namespace = $this->getNamespace(); + $name = $namespace . '_' . $collection; $query = $this->getPDO()->prepare(" - SELECT SUM(\"pgsize\") FROM \"dbstat\" WHERE name='{$namespace}_{$name}'; + SELECT SUM(\"pgsize\") FROM \"dbstat\" WHERE name=:name; "); + $query->bindParam(':name', $name); + try { $query->execute(); $size = $query->fetchColumn(); - } - catch (PDOException $e) { + } catch (PDOException $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } diff --git a/tests/Database/Base.php b/tests/Database/Base.php index ba8f9c9bb..41d75ac55 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -261,17 +261,12 @@ public function testSizeCollection(): void static::getDatabase()->createAttribute('sizeTest2', 'string1', Database::VAR_STRING, 128, true); static::getDatabase()->createAttribute('sizeTest2', 'string2', Database::VAR_STRING, 254 + 1, true); static::getDatabase()->createAttribute('sizeTest2', 'string3', Database::VAR_STRING, 254 + 1, true); - static::getDatabase()->createIndex('sizeTest2', 'multiIndex', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); + static::getDatabase()->createIndex('sizeTest2', 'index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); - $loopCount = 30; + $loopCount = 40; for ($i = 0; $i < $loopCount; $i++) { static::getDatabase()->createDocument('sizeTest2', new Document([ - '$permissions' => [ - Permission::read(Role::any()), - Permission::update(Role::any()), - Permission::delete(Role::any()), - ], 'string1' => 'string1' . $i, 'string2' => 'string2' . $i, 'string3' => 'string3' . $i, From faab82718ecd012066b39dd6ba10e1fc0cb22fbf Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 12 Jul 2023 23:14:38 +0530 Subject: [PATCH 18/32] fixed lint issues --- src/Database/Adapter.php | 2 +- src/Database/Adapter/MariaDB.php | 6 +++--- src/Database/Adapter/Mongo.php | 6 +++--- src/Database/Adapter/MySQL.php | 6 +++--- src/Database/Adapter/Postgres.php | 6 +++--- src/Database/Adapter/SQLite.php | 8 ++++---- tests/Database/Base.php | 6 +++--- 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 5a4b7664c..e2b62e35d 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -400,7 +400,7 @@ abstract public function count(string $collection, array $queries = [], ?int $ma /** * Get Collection Size - * + * * @param string $collection * @return int * @throws DatabaseException diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 4effad04e..41adb8435 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -164,10 +164,10 @@ public function getSizeOfCollection(string $collection): int $query->bindParam(':name', $name); try { - $query->execute(); - $size = $query->fetchColumn(); + $query->execute(); + $size = $query->fetchColumn(); } catch (PDOException $e) { - throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); + throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } return $size; diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 76c6eb6a7..3f863d8ef 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -264,16 +264,16 @@ public function getSizeOfCollection(string $collection): int $namespace = $this->getNamespace(); $collection = $this->filter($collection); $collection = $namespace. '_' . $collection; - + $command = [ 'collStats' => $collection, 'scale' => 1 ]; try { - $result = $this->getClient()->query($command); + $result = $this->getClient()->query($command); } catch(Exception $e) { - throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); + throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } return $result->size; diff --git a/src/Database/Adapter/MySQL.php b/src/Database/Adapter/MySQL.php index 792de191a..15194c26c 100644 --- a/src/Database/Adapter/MySQL.php +++ b/src/Database/Adapter/MySQL.php @@ -101,10 +101,10 @@ public function getSizeOfCollection(string $collection): int $query->bindParam(':name', $name); try { - $query->execute(); - $size = $query->fetchColumn(); + $query->execute(); + $size = $query->fetchColumn(); } catch (PDOException $e) { - throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); + throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } return $size; diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index e5ea70f5c..c2975ec86 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -170,10 +170,10 @@ public function getSizeOfCollection(string $collection): int $query->bindParam(':name', $name); try { - $query->execute(); - $size = $query->fetchColumn(); + $query->execute(); + $size = $query->fetchColumn(); } catch (PDOException $e) { - throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); + 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 9abd946a8..02d494c8d 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -170,7 +170,7 @@ public function getSizeOfCollection(string $collection): int $collection = $this->filter($collection); $namespace = $this->getNamespace(); $name = $namespace . '_' . $collection; - + $query = $this->getPDO()->prepare(" SELECT SUM(\"pgsize\") FROM \"dbstat\" WHERE name=:name; "); @@ -178,10 +178,10 @@ public function getSizeOfCollection(string $collection): int $query->bindParam(':name', $name); try { - $query->execute(); - $size = $query->fetchColumn(); + $query->execute(); + $size = $query->fetchColumn(); } catch (PDOException $e) { - throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); + throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } return $size; diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 41d75ac55..232620b03 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -256,7 +256,7 @@ public function testSizeCollection(): void $size1 = static::getDatabase()->getSizeOfCollection('sizeTest1'); $size2 = static::getDatabase()->getSizeOfCollection('sizeTest2'); - $this->assertEquals($size1,$size2); + $this->assertEquals($size1, $size2); static::getDatabase()->createAttribute('sizeTest2', 'string1', Database::VAR_STRING, 128, true); static::getDatabase()->createAttribute('sizeTest2', 'string2', Database::VAR_STRING, 254 + 1, true); @@ -277,7 +277,7 @@ public function testSizeCollection(): void $this->assertGreaterThan($size1, $size2); } - + public function testCreateDeleteAttribute(): void { static::getDatabase()->createCollection('attributes'); @@ -11396,4 +11396,4 @@ public function testLast(): void $this->expectNotToPerformAssertions(); static::killDatabase(); } -} \ No newline at end of file +} From 51ffb2cc044f9d67d79c37f669b7ff4a7f587b09 Mon Sep 17 00:00:00 2001 From: faisalill Date: Thu, 13 Jul 2023 20:53:52 +0530 Subject: [PATCH 19/32] fixed phpstan issue --- composer.json | 4 ++++ src/Database/Adapter/Mongo.php | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index c28e846a0..088a9a253 100755 --- a/composer.json +++ b/composer.json @@ -24,6 +24,10 @@ "Composer\\Config::disableProcessTimeout", "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], + "mytest": [ + "Composer\\Config::disableProcessTimeout", + "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter/MongoDBTest.php" + ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", "check": "./vendor/bin/phpstan analyse --level 7 src tests --memory-limit 512M", diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 3f863d8ef..6c5f62404 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -272,11 +272,14 @@ public function getSizeOfCollection(string $collection): int try { $result = $this->getClient()->query($command); + if (is_object($result) && property_exists($result, 'size')) { + return $result->size; + } else { + throw new DatabaseException('Failed to get collection size'); + } } catch(Exception $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } - - return $result->size; } /** From 2761977a982805ac2477960bfaf870c1a709bcfe Mon Sep 17 00:00:00 2001 From: faisalill Date: Thu, 13 Jul 2023 20:56:08 +0530 Subject: [PATCH 20/32] removed mytest from composer.json --- composer.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/composer.json b/composer.json index 088a9a253..c28e846a0 100755 --- a/composer.json +++ b/composer.json @@ -24,10 +24,6 @@ "Composer\\Config::disableProcessTimeout", "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], - "mytest": [ - "Composer\\Config::disableProcessTimeout", - "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter/MongoDBTest.php" - ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", "check": "./vendor/bin/phpstan analyse --level 7 src tests --memory-limit 512M", From af69df4840052c9080cabf22053c3a3862abb518 Mon Sep 17 00:00:00 2001 From: faisalill Date: Tue, 18 Jul 2023 21:20:22 +0530 Subject: [PATCH 21/32] removed mytest from composer.json --- src/Database/Adapter/MariaDB.php | 17 +++++++++++++---- src/Database/Adapter/MySQL.php | 18 ++++++++++++++---- src/Database/Adapter/Postgres.php | 15 +++++++++++---- src/Database/Adapter/SQLite.php | 15 +++++++++++---- tests/Database/Base.php | 4 ++-- 5 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 41adb8435..c65184144 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -154,18 +154,27 @@ public function getSizeOfCollection(string $collection): int $collection = $this->getNamespace() . '_' . $collection; $database = $this->getDefaultDatabase(); $name = $database . '/' . $collection; + $permissions = $database . '/' . $collection . '_perms'; - $query = $this->getPDO()->prepare(" + $collectionSize = $this->getPDO()->prepare(" SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES WHERE NAME = :name "); - $query->bindParam(':name', $name); + $permissionsSize = $this->getPDO()->prepare(" + SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) + FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES + WHERE NAME = :permissions + "); + + $collectionSize->bindParam(':name', $name); + $permissionsSize->bindParam(':permissions', $permissions); try { - $query->execute(); - $size = $query->fetchColumn(); + $collectionSize->execute(); + $permissionsSize->execute(); + $size = $collectionSize->fetchColumn() + $permissionsSize->fetchColumn(); } catch (PDOException $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } diff --git a/src/Database/Adapter/MySQL.php b/src/Database/Adapter/MySQL.php index 15194c26c..179532abd 100644 --- a/src/Database/Adapter/MySQL.php +++ b/src/Database/Adapter/MySQL.php @@ -91,18 +91,28 @@ public function getSizeOfCollection(string $collection): int $collection = $this->getNamespace() . '_' . $collection; $database = $this->getDefaultDatabase(); $name = $database . '/' . $collection; + $permissions = $database . '/' . $collection . '_perms'; - $query = $this->getPDO()->prepare(" + $collectionSize = $this->getPDO()->prepare(" SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) FROM INFORMATION_SCHEMA.INNODB_TABLESPACES WHERE NAME = :name "); - $query->bindParam(':name', $name); + $permissionsSize = $this->getPDO()->prepare(" + SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) + FROM INFORMATION_SCHEMA.INNODB_TABLESPACES + WHERE NAME = :permissions + "); + + + $collectionSize->bindParam(':name', $name); + $permissionsSize->bindParam(':permissions', $permissions); try { - $query->execute(); - $size = $query->fetchColumn(); + $collectionSize->execute(); + $permissionsSize->execute(); + $size = $collectionSize->fetchColumn() + $permissionsSize->fetchColumn(); } catch (PDOException $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index c2975ec86..c8535c4b6 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -162,16 +162,23 @@ public function getSizeOfCollection(string $collection): int { $collection = $this->filter($collection); $name = $this->getSQLTable($collection); + $permissions = $this->getSQLTable($collection . '_perms'); - $query = $this->getPDO()->prepare(" + $collectionSize = $this->getPDO()->prepare(" SELECT pg_total_relation_size(:name); "); - $query->bindParam(':name', $name); + $permissionsSize = $this->getPDO()->prepare(" + SELECT pg_total_relation_size(:permissions); + "); + + $collectionSize->bindParam(':name', $name); + $permissionsSize->bindParam(':permissions', $permissions); try { - $query->execute(); - $size = $query->fetchColumn(); + $collectionSize->execute(); + $permissionsSize->execute(); + $size = $collectionSize->fetchColumn() + $permissionsSize->fetchColumn(); } catch (PDOException $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index 02d494c8d..6af7c138a 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -170,16 +170,23 @@ public function getSizeOfCollection(string $collection): int $collection = $this->filter($collection); $namespace = $this->getNamespace(); $name = $namespace . '_' . $collection; + $permissions = $namespace . '_' . $collection . '_perms'; - $query = $this->getPDO()->prepare(" + $collectionSize = $this->getPDO()->prepare(" SELECT SUM(\"pgsize\") FROM \"dbstat\" WHERE name=:name; "); - $query->bindParam(':name', $name); + $permissionsSize = $this->getPDO()->prepare(" + SELECT SUM(\"pgsize\") FROM \"dbstat\" WHERE name=:name; + "); + + $collectionSize->bindParam(':name', $name); + $permissionsSize->bindParam(':name', $permissions); try { - $query->execute(); - $size = $query->fetchColumn(); + $collectionSize->execute(); + $permissionsSize->execute(); + $size = $collectionSize->fetchColumn() + $permissionsSize->fetchColumn(); } catch (PDOException $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); } diff --git a/tests/Database/Base.php b/tests/Database/Base.php index c4579a004..7424e0225 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -255,8 +255,8 @@ public function testSizeCollection(): void $size1 = static::getDatabase()->getSizeOfCollection('sizeTest1'); $size2 = static::getDatabase()->getSizeOfCollection('sizeTest2'); - - $this->assertEquals($size1, $size2); + $sizeDifference = abs($size1 - $size2); + $this->assertLessThan(4100, $sizeDifference); static::getDatabase()->createAttribute('sizeTest2', 'string1', Database::VAR_STRING, 128, true); static::getDatabase()->createAttribute('sizeTest2', 'string2', Database::VAR_STRING, 254 + 1, true); From ac215746c0de3e4d83954f830a8fc41719419089 Mon Sep 17 00:00:00 2001 From: faisalill Date: Tue, 18 Jul 2023 21:49:47 +0530 Subject: [PATCH 22/32] Add explanation for asserting a constant with size difference --- tests/Database/Base.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 7424e0225..ac4b403b8 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -256,7 +256,10 @@ public function testSizeCollection(): void $size1 = static::getDatabase()->getSizeOfCollection('sizeTest1'); $size2 = static::getDatabase()->getSizeOfCollection('sizeTest2'); $sizeDifference = abs($size1 - $size2); - $this->assertLessThan(4100, $sizeDifference); + // Size of an empty collection returns either 172032 or 167936 bytes randomly + // Therefore asserting with a tolerance of 5000 bytes + $byteDifference = 5000; + $this->assertLessThan($byteDifference, $sizeDifference); static::getDatabase()->createAttribute('sizeTest2', 'string1', Database::VAR_STRING, 128, true); static::getDatabase()->createAttribute('sizeTest2', 'string2', Database::VAR_STRING, 254 + 1, true); From 7abfde57c8ac6e7dbf385c429c9febacdca6e82f Mon Sep 17 00:00:00 2001 From: faisalill Date: Tue, 18 Jul 2023 22:06:37 +0530 Subject: [PATCH 23/32] Fix lint issue --- tests/Database/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index ac4b403b8..38b3501bf 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -257,7 +257,7 @@ public function testSizeCollection(): void $size2 = static::getDatabase()->getSizeOfCollection('sizeTest2'); $sizeDifference = abs($size1 - $size2); // Size of an empty collection returns either 172032 or 167936 bytes randomly - // Therefore asserting with a tolerance of 5000 bytes + // Therefore asserting with a tolerance of 5000 bytes $byteDifference = 5000; $this->assertLessThan($byteDifference, $sizeDifference); From d12f27bedcca48f8a09c47b5439332db22e0c741 Mon Sep 17 00:00:00 2001 From: faisalill Date: Wed, 26 Jul 2023 07:47:50 +0530 Subject: [PATCH 24/32] Made changes as per review --- src/Database/Adapter/MariaDB.php | 6 +++--- src/Database/Adapter/Mongo.php | 2 +- src/Database/Adapter/MySQL.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index c65184144..94eed13ac 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -163,9 +163,9 @@ public function getSizeOfCollection(string $collection): int "); $permissionsSize = $this->getPDO()->prepare(" - SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) - FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES - WHERE NAME = :permissions + SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) + FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES + WHERE NAME = :permissions "); $collectionSize->bindParam(':name', $name); diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 6c5f62404..e57988f92 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -275,7 +275,7 @@ public function getSizeOfCollection(string $collection): int if (is_object($result) && property_exists($result, 'size')) { return $result->size; } else { - throw new DatabaseException('Failed to get collection size'); + throw new DatabaseException(); } } catch(Exception $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); diff --git a/src/Database/Adapter/MySQL.php b/src/Database/Adapter/MySQL.php index 179532abd..cad8f83c6 100644 --- a/src/Database/Adapter/MySQL.php +++ b/src/Database/Adapter/MySQL.php @@ -97,13 +97,13 @@ public function getSizeOfCollection(string $collection): int SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) FROM INFORMATION_SCHEMA.INNODB_TABLESPACES WHERE NAME = :name - "); + "); $permissionsSize = $this->getPDO()->prepare(" SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) FROM INFORMATION_SCHEMA.INNODB_TABLESPACES WHERE NAME = :permissions - "); + "); $collectionSize->bindParam(':name', $name); From a0ec4165391824c6083445763b936d6c7eee1992 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 26 Jul 2023 18:01:30 -0400 Subject: [PATCH 25/32] Apply suggestions from code review --- src/Database/Adapter/MariaDB.php | 2 +- src/Database/Adapter/MySQL.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 94eed13ac..4b40fefb2 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -166,7 +166,7 @@ public function getSizeOfCollection(string $collection): int SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES WHERE NAME = :permissions - "); + "); $collectionSize->bindParam(':name', $name); $permissionsSize->bindParam(':permissions', $permissions); diff --git a/src/Database/Adapter/MySQL.php b/src/Database/Adapter/MySQL.php index cad8f83c6..a87fe7fc4 100644 --- a/src/Database/Adapter/MySQL.php +++ b/src/Database/Adapter/MySQL.php @@ -105,7 +105,6 @@ public function getSizeOfCollection(string $collection): int WHERE NAME = :permissions "); - $collectionSize->bindParam(':name', $name); $permissionsSize->bindParam(':permissions', $permissions); From ff872db8006d72982c27634983e8db37c37cc620 Mon Sep 17 00:00:00 2001 From: faisalill Date: Thu, 27 Jul 2023 06:48:22 +0530 Subject: [PATCH 26/32] Make changes as per review --- src/Database/Adapter/MariaDB.php | 12 ++++++------ src/Database/Adapter/Mongo.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 4b40fefb2..2fe0ca707 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -157,15 +157,15 @@ public function getSizeOfCollection(string $collection): int $permissions = $database . '/' . $collection . '_perms'; $collectionSize = $this->getPDO()->prepare(" - SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) - FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES - WHERE NAME = :name + SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) + FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES + WHERE NAME = :name "); $permissionsSize = $this->getPDO()->prepare(" - SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) - FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES - WHERE NAME = :permissions + SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) + FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES + WHERE NAME = :permissions "); $collectionSize->bindParam(':name', $name); diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index e57988f92..f16bdaf6f 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -275,7 +275,7 @@ public function getSizeOfCollection(string $collection): int if (is_object($result) && property_exists($result, 'size')) { return $result->size; } else { - throw new DatabaseException(); + throw new DatabaseException("No size found"); } } catch(Exception $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); From 06fe605baf1024508c38023854be97297c340aa0 Mon Sep 17 00:00:00 2001 From: faisalill Date: Tue, 8 Aug 2023 21:05:10 +0530 Subject: [PATCH 27/32] Add full text test --- composer.json | 6 +++++- tests/Database/Base.php | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c28e846a0..38ba9337e 100755 --- a/composer.json +++ b/composer.json @@ -22,7 +22,11 @@ ], "test": [ "Composer\\Config::disableProcessTimeout", - "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" + "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml --filter testSizeFullText" + ], + "mytest": [ + "Composer\\Config::disableProcessTimeout", + "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter/MongoDBTest.php --stop-on-failure" ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 3c6625f57..a69398656 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -281,6 +281,42 @@ public function testSizeCollection(): void $this->assertGreaterThan($size1, $size2); } + public function testSizeFullText(): void + { + if (static::getDatabase()->getAdapter()->getSupportForFulltextIndex()) { + + static::getDatabase()->createCollection('fullTextTest'); + + $size1 = static::getDatabase()->getSizeOfCollection('fullTextTest'); + + static::getDatabase()->createAttribute('fullTextTest', 'string1', Database::VAR_STRING, 128, true); + static::getDatabase()->createAttribute('fullTextTest', 'string2', Database::VAR_STRING, 254, true); + static::getDatabase()->createAttribute('fullTextTest', 'string3', Database::VAR_STRING, 254, true); + + $loopCount = 1000; + + for ($i = 0; $i < $loopCount; $i++) { + static::getDatabase()->createDocument('fullTextTest', new Document([ + 'string1' => 'string1' . $i, + 'string2' => 'string2' . $i, + 'string3' => 'string3' . $i, + ])); + } + + $size2 = static::getDatabase()->getSizeOfCollection('fullTextTest'); + + $this->assertGreaterThan($size1, $size2); + + static::getDatabase()->createIndex('fullTextTest', 'index_fulltext1', Database::INDEX_FULLTEXT, ['string1']); + + $size3 = static::getDatabase()->getSizeOfCollection('fullTextTest'); + + $this->assertGreaterThan($size2, $size3); + } else { + $this->expectNotToPerformAssertions(); + } + } + public function testCreateDeleteAttribute(): void { static::getDatabase()->createCollection('attributes'); From 1763e864997906b43781ea1310c4673dc4b5aa51 Mon Sep 17 00:00:00 2001 From: faisalill Date: Thu, 10 Aug 2023 23:30:12 +0530 Subject: [PATCH 28/32] Change MongoDB return value --- composer.json | 4 ++-- src/Database/Adapter/Mongo.php | 8 +++---- tests/Database/Base.php | 39 +++++++++++++++++----------------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/composer.json b/composer.json index 38ba9337e..22cef0066 100755 --- a/composer.json +++ b/composer.json @@ -22,11 +22,11 @@ ], "test": [ "Composer\\Config::disableProcessTimeout", - "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml --filter testSizeFullText" + "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml --stop-on-failure" ], "mytest": [ "Composer\\Config::disableProcessTimeout", - "docker compose exec tests vendor/bin/phpunit ./tests/Database/Adapter/MongoDBTest.php --stop-on-failure" + "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml ./tests/Database/Adapter/MongoDBTest.php --filter testSizeFullText --stop-on-failure" ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index f16bdaf6f..22a42a527 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -266,14 +266,14 @@ public function getSizeOfCollection(string $collection): int $collection = $namespace. '_' . $collection; $command = [ - 'collStats' => $collection, - 'scale' => 1 + 'collStats' => $collection, + 'scale' => 1 ]; try { $result = $this->getClient()->query($command); - if (is_object($result) && property_exists($result, 'size')) { - return $result->size; + if (is_object($result)) { + return $result->totalSize; } else { throw new DatabaseException("No size found"); } diff --git a/tests/Database/Base.php b/tests/Database/Base.php index a69398656..ee9e959c7 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -283,35 +283,36 @@ public function testSizeCollection(): void public function testSizeFullText(): void { + // SQLite does not support fulltext indexes if (static::getDatabase()->getAdapter()->getSupportForFulltextIndex()) { + static::getDatabase()->createCollection('fullTextTest'); - static::getDatabase()->createCollection('fullTextTest'); + $size1 = static::getDatabase()->getSizeOfCollection('fullTextTest'); - $size1 = static::getDatabase()->getSizeOfCollection('fullTextTest'); + static::getDatabase()->createAttribute('fullTextTest', 'string1', Database::VAR_STRING, 128, true); + static::getDatabase()->createAttribute('fullTextTest', 'string2', Database::VAR_STRING, 254, true); + static::getDatabase()->createAttribute('fullTextTest', 'string3', Database::VAR_STRING, 254, true); + static::getDatabase()->createIndex('fullTextTest', 'index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); - static::getDatabase()->createAttribute('fullTextTest', 'string1', Database::VAR_STRING, 128, true); - static::getDatabase()->createAttribute('fullTextTest', 'string2', Database::VAR_STRING, 254, true); - static::getDatabase()->createAttribute('fullTextTest', 'string3', Database::VAR_STRING, 254, true); + $loopCount = 10; - $loopCount = 1000; - - for ($i = 0; $i < $loopCount; $i++) { - static::getDatabase()->createDocument('fullTextTest', new Document([ - 'string1' => 'string1' . $i, - 'string2' => 'string2' . $i, - 'string3' => 'string3' . $i, - ])); - } + for ($i = 0; $i < $loopCount; $i++) { + static::getDatabase()->createDocument('fullTextTest', new Document([ + 'string1' => 'string1' . $i, + 'string2' => 'string2' . $i, + 'string3' => 'string3' . $i, + ])); + } - $size2 = static::getDatabase()->getSizeOfCollection('fullTextTest'); + $size2 = static::getDatabase()->getSizeOfCollection('fullTextTest'); - $this->assertGreaterThan($size1, $size2); + $this->assertGreaterThan($size1, $size2); - static::getDatabase()->createIndex('fullTextTest', 'index_fulltext1', Database::INDEX_FULLTEXT, ['string1']); + static::getDatabase()->createIndex('fullTextTest', 'index_fulltext1', Database::INDEX_FULLTEXT, ['string1']); - $size3 = static::getDatabase()->getSizeOfCollection('fullTextTest'); + $size3 = static::getDatabase()->getSizeOfCollection('fullTextTest'); - $this->assertGreaterThan($size2, $size3); + $this->assertGreaterThan($size2, $size3); } else { $this->expectNotToPerformAssertions(); } From 1ebd9f289bea269ac0265de503ccd2768b35da41 Mon Sep 17 00:00:00 2001 From: faisalill Date: Thu, 10 Aug 2023 23:32:20 +0530 Subject: [PATCH 29/32] Remove mytest script from composer.json --- composer.json | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 22cef0066..c28e846a0 100755 --- a/composer.json +++ b/composer.json @@ -22,11 +22,7 @@ ], "test": [ "Composer\\Config::disableProcessTimeout", - "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml --stop-on-failure" - ], - "mytest": [ - "Composer\\Config::disableProcessTimeout", - "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml ./tests/Database/Adapter/MongoDBTest.php --filter testSizeFullText --stop-on-failure" + "docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml" ], "lint": "./vendor/bin/pint --test", "format": "./vendor/bin/pint", From 8484dc8a7ea1538e404df979a57462d94ca94d4f Mon Sep 17 00:00:00 2001 From: faisalill Date: Thu, 10 Aug 2023 23:38:10 +0530 Subject: [PATCH 30/32] Use better collection name --- tests/Database/Base.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index ee9e959c7..690b96a01 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -285,32 +285,32 @@ public function testSizeFullText(): void { // SQLite does not support fulltext indexes if (static::getDatabase()->getAdapter()->getSupportForFulltextIndex()) { - static::getDatabase()->createCollection('fullTextTest'); + static::getDatabase()->createCollection('fullTextSizeTest'); - $size1 = static::getDatabase()->getSizeOfCollection('fullTextTest'); + $size1 = static::getDatabase()->getSizeOfCollection('fullTextSizeTest'); - static::getDatabase()->createAttribute('fullTextTest', 'string1', Database::VAR_STRING, 128, true); - static::getDatabase()->createAttribute('fullTextTest', 'string2', Database::VAR_STRING, 254, true); - static::getDatabase()->createAttribute('fullTextTest', 'string3', Database::VAR_STRING, 254, true); - static::getDatabase()->createIndex('fullTextTest', 'index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); + static::getDatabase()->createAttribute('fullTextSizeTest', 'string1', Database::VAR_STRING, 128, true); + static::getDatabase()->createAttribute('fullTextSizeTest', 'string2', Database::VAR_STRING, 254, true); + static::getDatabase()->createAttribute('fullTextSizeTest', 'string3', Database::VAR_STRING, 254, true); + static::getDatabase()->createIndex('fullTextSizeTest', 'index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); $loopCount = 10; for ($i = 0; $i < $loopCount; $i++) { - static::getDatabase()->createDocument('fullTextTest', new Document([ + static::getDatabase()->createDocument('fullTextSizeTest', new Document([ 'string1' => 'string1' . $i, 'string2' => 'string2' . $i, 'string3' => 'string3' . $i, ])); } - $size2 = static::getDatabase()->getSizeOfCollection('fullTextTest'); + $size2 = static::getDatabase()->getSizeOfCollection('fullTextSizeTest'); $this->assertGreaterThan($size1, $size2); - static::getDatabase()->createIndex('fullTextTest', 'index_fulltext1', Database::INDEX_FULLTEXT, ['string1']); + static::getDatabase()->createIndex('fullTextSizeTest', 'fulltext_index', Database::INDEX_FULLTEXT, ['string1']); - $size3 = static::getDatabase()->getSizeOfCollection('fullTextTest'); + $size3 = static::getDatabase()->getSizeOfCollection('fullTextSizeTest'); $this->assertGreaterThan($size2, $size3); } else { From 5048976c26561ebf54da612c4afbf6200534225a Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 10 Aug 2023 18:31:25 -0400 Subject: [PATCH 31/32] Apply suggestions from code review --- src/Database/Adapter/MariaDB.php | 12 ++++---- src/Database/Adapter/Mongo.php | 2 +- tests/Database/Base.php | 47 ++++++++++++++++---------------- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 2fe0ca707..e850a63ff 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -157,15 +157,15 @@ public function getSizeOfCollection(string $collection): int $permissions = $database . '/' . $collection . '_perms'; $collectionSize = $this->getPDO()->prepare(" - SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) - FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES - WHERE NAME = :name + SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) + FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES + WHERE NAME = :name "); $permissionsSize = $this->getPDO()->prepare(" - SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) - FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES - WHERE NAME = :permissions + SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE) + FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES + WHERE NAME = :permissions "); $collectionSize->bindParam(':name', $name); diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 22a42a527..4cfe6edd7 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -275,7 +275,7 @@ public function getSizeOfCollection(string $collection): int if (is_object($result)) { return $result->totalSize; } else { - throw new DatabaseException("No size found"); + throw new DatabaseException('No size found'); } } catch(Exception $e) { throw new DatabaseException('Failed to get collection size: ' . $e->getMessage()); diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 690b96a01..c42da1760 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -284,38 +284,39 @@ public function testSizeCollection(): void public function testSizeFullText(): void { // SQLite does not support fulltext indexes - if (static::getDatabase()->getAdapter()->getSupportForFulltextIndex()) { - static::getDatabase()->createCollection('fullTextSizeTest'); + if (!static::getDatabase()->getAdapter()->getSupportForFulltextIndex()) { + $this->expectNotToPerformAssertions(); + return; + } + + static::getDatabase()->createCollection('fullTextSizeTest'); - $size1 = static::getDatabase()->getSizeOfCollection('fullTextSizeTest'); + $size1 = static::getDatabase()->getSizeOfCollection('fullTextSizeTest'); - static::getDatabase()->createAttribute('fullTextSizeTest', 'string1', Database::VAR_STRING, 128, true); - static::getDatabase()->createAttribute('fullTextSizeTest', 'string2', Database::VAR_STRING, 254, true); - static::getDatabase()->createAttribute('fullTextSizeTest', 'string3', Database::VAR_STRING, 254, true); - static::getDatabase()->createIndex('fullTextSizeTest', 'index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); + static::getDatabase()->createAttribute('fullTextSizeTest', 'string1', Database::VAR_STRING, 128, true); + static::getDatabase()->createAttribute('fullTextSizeTest', 'string2', Database::VAR_STRING, 254, true); + static::getDatabase()->createAttribute('fullTextSizeTest', 'string3', Database::VAR_STRING, 254, true); + static::getDatabase()->createIndex('fullTextSizeTest', 'index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); - $loopCount = 10; + $loopCount = 10; - for ($i = 0; $i < $loopCount; $i++) { - static::getDatabase()->createDocument('fullTextSizeTest', new Document([ - 'string1' => 'string1' . $i, - 'string2' => 'string2' . $i, - 'string3' => 'string3' . $i, - ])); - } + for ($i = 0; $i < $loopCount; $i++) { + static::getDatabase()->createDocument('fullTextSizeTest', new Document([ + 'string1' => 'string1' . $i, + 'string2' => 'string2' . $i, + 'string3' => 'string3' . $i, + ])); + } - $size2 = static::getDatabase()->getSizeOfCollection('fullTextSizeTest'); + $size2 = static::getDatabase()->getSizeOfCollection('fullTextSizeTest'); - $this->assertGreaterThan($size1, $size2); + $this->assertGreaterThan($size1, $size2); - static::getDatabase()->createIndex('fullTextSizeTest', 'fulltext_index', Database::INDEX_FULLTEXT, ['string1']); + static::getDatabase()->createIndex('fullTextSizeTest', 'fulltext_index', Database::INDEX_FULLTEXT, ['string1']); - $size3 = static::getDatabase()->getSizeOfCollection('fullTextSizeTest'); + $size3 = static::getDatabase()->getSizeOfCollection('fullTextSizeTest'); - $this->assertGreaterThan($size2, $size3); - } else { - $this->expectNotToPerformAssertions(); - } + $this->assertGreaterThan($size2, $size3); } public function testCreateDeleteAttribute(): void From 3b6541633b7aa186580529f700a17a419bbded6d Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 10 Aug 2023 18:39:31 -0400 Subject: [PATCH 32/32] Fix lint --- tests/Database/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index ade5651f9..18c39e63b 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -320,7 +320,7 @@ public function testSizeFullText(): void $this->expectNotToPerformAssertions(); return; } - + static::getDatabase()->createCollection('fullTextSizeTest'); $size1 = static::getDatabase()->getSizeOfCollection('fullTextSizeTest');