1
0
Fork 0
mirror of synced 2024-06-26 18:20:43 +12:00

feat: use internal ids for database indexes and attributes

This commit is contained in:
Torsten Dittmann 2022-06-20 13:21:36 +02:00
parent fbfb871edf
commit 50e50fa94b
5 changed files with 38 additions and 20 deletions

View file

@ -85,7 +85,7 @@ function createAttribute(string $collectionId, Document $attribute, Response $re
try {
$attribute = new Document([
'$id' => $collectionId . '_' . $key,
'$id' => $collection->getInternalId() . '_' . $key,
'key' => $key,
'collectionInternalId' => $collection->getInternalId(),
'collectionId' => $collectionId,
@ -1122,7 +1122,7 @@ App::get('/v1/database/collections/:collectionId/attributes/:key')
throw new Exception('Collection not found', 404, Exception::COLLECTION_NOT_FOUND);
}
$attribute = $dbForProject->getDocument('attributes', $collectionId . '_' . $key);
$attribute = $dbForProject->getDocument('attributes', $collection->getInternalId() . '_' . $key);
if ($attribute->isEmpty()) {
throw new Exception('Attribute not found', 404, Exception::ATTRIBUTE_NOT_FOUND);
@ -1178,7 +1178,7 @@ App::delete('/v1/database/collections/:collectionId/attributes/:key')
throw new Exception('Collection not found', 404, Exception::COLLECTION_NOT_FOUND);
}
$attribute = $dbForProject->getDocument('attributes', $collectionId . '_' . $key);
$attribute = $dbForProject->getDocument('attributes', $collection->getInternalId() . '_' . $key);
if ($attribute->isEmpty()) {
throw new Exception('Attribute not found', 404, Exception::ATTRIBUTE_NOT_FOUND);
@ -1303,7 +1303,7 @@ App::post('/v1/database/collections/:collectionId/indexes')
try {
$index = $dbForProject->createDocument('indexes', new Document([
'$id' => $collectionId . '_' . $key,
'$id' => $collection->getInternalId() . '_' . $key,
'key' => $key,
'status' => 'processing', // processing, available, failed, deleting, stuck
'collectionInternalId' => $collection->getInternalId(),
@ -1444,7 +1444,7 @@ App::delete('/v1/database/collections/:collectionId/indexes/:key')
throw new Exception('Collection not found', 404, Exception::COLLECTION_NOT_FOUND);
}
$index = $dbForProject->getDocument('indexes', $collectionId . '_' . $key);
$index = $dbForProject->getDocument('indexes', $collection->getInternalId() . '_' . $key);
if (empty($index->getId())) {
throw new Exception('Index not found', 404, Exception::INDEX_NOT_FOUND);

View file

@ -15,6 +15,9 @@ use Utopia\Database\Validator\Authorization;
use Utopia\Registry\Registry;
use Utopia\Logger\Log;
Authorization::disable();
Authorization::setDefaultStatus(false);
function getDatabase(Registry &$register, string $namespace): Database
{
$attempts = 0;
@ -120,11 +123,8 @@ $cli
$influxDB = getInfluxDB($register);
$usage = new Usage($database, $influxDB, $logError);
$usageDB = new UsageDB($database, $logError);
Authorization::disable();
$iterations = 0;
Console::loop(function () use ($interval, $usage, $usageDB, &$iterations) {
$now = date('d-m-Y H:i:s', time());

View file

@ -5,7 +5,6 @@ use Appwrite\Messaging\Adapter\Realtime;
use Appwrite\Resque\Worker;
use Utopia\CLI\Console;
use Utopia\Database\Document;
use Utopia\Database\Validator\Authorization;
require_once __DIR__ . '/../init.php';

View file

@ -180,7 +180,10 @@ class Usage
private function createOrUpdateMetric(string $projectId, int $time, string $period, string $metric, int $value, int $type): void
{
$id = \md5("{$time}_{$period}_{$metric}");
$this->database->setNamespace('_' . $projectId);
$this->database->setNamespace('_console');
$project = $this->database->getDocument('projects', $projectId);
$this->database->setNamespace('_' . $project->getInternalId());
try {
$document = $this->database->getDocument('stats', $id);
if ($document->isEmpty()) {

View file

@ -28,7 +28,10 @@ class UsageDB extends Usage
$period = $options['key'];
$time = (int) (floor(time() / $options['multiplier']) * $options['multiplier']);
$id = \md5("{$time}_{$period}_{$metric}");
$this->database->setNamespace('_' . $projectId);
$this->database->setNamespace('_console');
$project = $this->database->getDocument('projects', $projectId);
$this->database->setNamespace('_' . $project->getInternalId());
try {
$document = $this->database->getDocument('stats', $id);
if ($document->isEmpty()) {
@ -70,15 +73,21 @@ class UsageDB extends Usage
*/
private function foreachDocument(string $projectId, string $collection, array $queries, callable $callback): void
{
if ($projectId === 'console') {
return;
}
$limit = 50;
$results = [];
$sum = $limit;
$latestDocument = null;
$this->database->setNamespace('_' . $projectId);
$this->database->setNamespace('_console');
$project = $this->database->getDocument('projects', $projectId);
$this->database->setNamespace('_' . $project->getInternalId());
while ($sum === $limit) {
try {
$results = $this->database->find($collection, $queries, $limit, cursor:$latestDocument);
$results = $this->database->find($collection, $queries, $limit, cursor: $latestDocument);
} catch (\Exception $e) {
if (is_callable($this->errorHandler)) {
call_user_func($this->errorHandler, $e, "fetch_documents_project_{$projectId}_collection_{$collection}");
@ -115,7 +124,10 @@ class UsageDB extends Usage
*/
private function sum(string $projectId, string $collection, string $attribute, string $metric): int
{
$this->database->setNamespace('_' . $projectId);
$this->database->setNamespace('_console');
$project = $this->database->getDocument('projects', $projectId);
$this->database->setNamespace('_' . $project->getInternalId());
try {
$sum = (int) $this->database->sum($collection, $attribute);
$this->createOrUpdateMetric($projectId, $metric, $sum);
@ -141,7 +153,10 @@ class UsageDB extends Usage
*/
private function count(string $projectId, string $collection, string $metric): int
{
$this->database->setNamespace("_{$projectId}");
$this->database->setNamespace('_console');
$project = $this->database->getDocument('projects', $projectId);
$this->database->setNamespace('_' . $project->getInternalId());
try {
$count = $this->database->count($collection);
$this->createOrUpdateMetric($projectId, $metric, $count);
@ -252,11 +267,12 @@ class UsageDB extends Usage
*/
public function collect(): void
{
$this->foreachDocument('console', 'projects', [], function ($project) {
$projectId = $project->getId();
$this->usersStats($projectId);
$this->databaseStats($projectId);
$this->storageStats($projectId);
$this->foreachDocument('console', 'projects', [], function (Document $project) {
$projectId = $project->getId();
$this->usersStats($projectId);
$this->databaseStats($projectId);
$this->storageStats($projectId);
});
}
}