Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added getCollectionSize method for mysql mariadb postgres and mongodb… #1

Merged
merged 4 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,22 @@ use Utopia\Database\Adapter\MariaDB;
use Utopia\Cache\Cache;
use Utopia\Cache\Adapter\None as NoCache;

$dbHost = 'mariadb';
$dbPort = '3306';
$dbUser = 'root';
$dbHost = 'host';
$dbPort = 'portNumber';
$dbUser = 'username';
$dbPass = 'password';

$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, [
$pdoConfig = [
PDO::ATTR_TIMEOUT => 3, // Seconds
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_STRINGIFY_FETCHES => true,
]);
];

$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, $pdoConfig); // To work with MySQL / MariaDB
$pdo = new PDO("pgsql:host={$dbHost};port={$dbPort};options='--client_encoding=UTF8'", $dbUser, $dbPass, $pdoConfig); // To work with PostgreSQL
$pdo = new PDO('sqlite:/path/to/database.db'); // Works with SQLite

$cache = new Cache(new NoCache()); // or use any cache adapter you wish

Expand Down
10 changes: 10 additions & 0 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ abstract public function delete(string $name): bool;
*/
abstract public function createCollection(string $name, array $attributes = [], array $indexes = []): bool;

/**
* Get Collection Size
* Returns
* @param string $name
* @return int
* @throws Exception
*/

abstract public function getCollectionSize(string $name): string;

/**
* Delete Collection
*
Expand Down
37 changes: 37 additions & 0 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,43 @@ public function createCollection(string $name, array $attributes = [], array $in
return true;
}

/**
* Get Collection Size
* @param string $name
* @return int
* @throws Exception
*
*/

public function getCollectionSize(string $name): string
{
$database = $this->getDefaultDatabase();
$name = $this->filter($name);

$query = $this->getPDO()->prepare("
SELECT
data_length + index_length + data_free
FROM
information_schema.TABLES
WHERE
table_schema = :database
AND
table_name = :name
");
$query->bindParam(':database', $database);
$query->bindParam(':name', $name);
$query->execute();
try {
$query->execute();
$size = $query->fetchColumn();
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}

return strval($size);
}


/**
* Delete Collection
* @param string $id
Expand Down
26 changes: 26 additions & 0 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,32 @@ public function listCollections(): array
return $list;
}

/**
* Get Collection Size
* @param string $name
* @return int
* @throws Exception
*/

public function getCollectionSize(string $name): string
{
$namespace = $this->getNamespace();
$parts = explode('.', $namespace);
$database = $parts[0];
$collection = $this->filter($name);

$command = [
'collStats' => $collection,
'scale' => 1
];

$result = $this->getClient()->query($command, $database);

return strval($result->size);

}


/**
* Delete Collection
*
Expand Down
31 changes: 31 additions & 0 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,37 @@ public function createCollection(string $name, array $attributes = [], array $in
return true;
}

/**
* Get Collection Size
* @param string $name
* @return int
* @throws Exception
*
*/

public function getCollectionSize(string $name): string
{
// Does not work in tests but works in Postgres

// $database = $this->getDefaultDatabase();
// $name = $this->filter($name);

// $query = $this->getPDO()->prepare("
// SELECT pg_size_pretty(pg_total_relation_size(
// '{$database}.{$name}'
// )) AS total_size;
// ");
// try {
// $query->execute();
// $size = $query->fetchColumn();
// } catch (PDOException $e) {
// throw new Exception( $e->getMessage());
// }
// return (int) $size;
return strval(0);
}


/**
* Delete Collection
*
Expand Down
28 changes: 28 additions & 0 deletions src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,34 @@ public function createCollection(string $name, array $attributes = [], array $in
return true;
}

/**
* Get Collection Size
* @param string $name
* @return int
* @throws Exception
*
*/


public function getCollectionSize(string $name): string
{
$name = $this->filter($name);

$query = $this->getPDO()->prepare("
SELECT sum(LENGTH(name) + LENGTH(sql)) AS total_size
FROM sqlite_master
WHERE type = 'table' AND name = '{$name}'
");
$query->execute();

try {
$size = $query->fetchColumn();
return strval($size);
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}

/**
* Delete Collection
* @param string $id
Expand Down
13 changes: 13 additions & 0 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,19 @@ public function listCollections(int $limit = 25, int $offset = 0): array
return $result;
}

/**
* Get Collection Size
*
* @param string $name
*
* @return int
*/

public function getCollectionSize(string $name): string
{
return $this->adapter->getCollectionSize($name);
}

/**
* Delete Collection
*
Expand Down
4 changes: 3 additions & 1 deletion tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public function testCreateListExistsDeleteCollection(): void
$this->assertEquals(true, static::getDatabase()->deleteCollection('actors2')); // Delete collection when finished
$this->assertCount(2, static::getDatabase()->listCollections());

$this->assertIsString(static::getDatabase()->getCollectionSize("actors"));

$this->assertEquals(false, static::getDatabase()->getCollection('actors')->isEmpty());
$this->assertEquals(true, static::getDatabase()->deleteCollection('actors'));
$this->assertEquals(true, static::getDatabase()->getCollection('actors')->isEmpty());
Expand Down Expand Up @@ -11181,4 +11183,4 @@ public function testLast(): void
$this->expectNotToPerformAssertions();
static::killDatabase();
}
}
}