1
0
Fork 0
mirror of synced 2024-06-03 11:24:48 +12:00

Merge remote-tracking branch 'origin/feat-database-indexing' into feat-response-models-new-attributes

This commit is contained in:
kodumbeats 2021-08-27 16:55:00 -04:00
commit b293c5dd65
5 changed files with 131 additions and 23 deletions

View file

@ -14,6 +14,7 @@ use Utopia\Validator\JSON;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Query;
use Utopia\Database\Adapter\MariaDB;
use Utopia\Database\Validator\Key;
use Utopia\Database\Validator\Permissions;
use Utopia\Database\Validator\QueryValidator;
@ -22,6 +23,7 @@ use Utopia\Database\Validator\Structure;
use Utopia\Database\Validator\UID;
use Utopia\Database\Exception\Authorization as AuthorizationException;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Limit as LimitException;
use Utopia\Database\Exception\Structure as StructureException;
use Appwrite\Database\Validator\CustomId;
use Appwrite\Network\Validator\Email;
@ -962,6 +964,16 @@ App::post('/v1/database/collections/:collectionId/indexes')
throw new Exception('Collection not found', 404);
}
$count = $dbForInternal->count('indexes', [
new Query('collectionId', Query::TYPE_EQUAL, [$collectionId])
], 61);
$limit = 64 - MariaDB::getNumberOfDefaultIndexes();
if ($count >= $limit) {
throw new Exception('Index limit exceeded', 400);
}
// Convert Document[] to array of attribute metadata
$oldAttributes = \array_map(function ($a) {
return $a->getArrayCopy();
@ -986,7 +998,6 @@ App::post('/v1/database/collections/:collectionId/indexes')
$lengths[$key] = ($attributeType === Database::VAR_STRING) ? $attributeSize : null;
}
// TODO@kodumbeats should $lengths be a part of the response model?
try {
$index = $dbForInternal->createDocument('indexes', new Document([
'$id' => $collectionId.'_'.$indexId,
@ -999,7 +1010,7 @@ App::post('/v1/database/collections/:collectionId/indexes')
'orders' => $orders,
]));
} catch (DuplicateException $th) {
throw new Exception('Attribute already exists', 409);
throw new Exception('Index already exists', 409);
}
$dbForInternal->purgeDocument('collections', $collectionId);

View file

@ -423,13 +423,13 @@ $auth = $this->getParam('auth', []);
data-scope="console">
<ul class="tiles cell-3 margin-bottom-small">
<?php foreach ($providers as $provider => $data):
if (isset($data['enabled']) && !$data['enabled']) {continue;}
if (isset($data['mock']) && $data['mock']) {continue;}
$sandbox = $data['sandbox'] ?? false;
$form = $data['form'] ?? false;
$name = $data['name'] ?? 'Unknown';
$beta = $data['beta'] ?? false;
?>
if (isset($data['enabled']) && !$data['enabled']) {continue;}
// if (isset($data['mock']) && $data['mock']) {continue;}
$sandbox = $data['sandbox'] ?? false;
$form = $data['form'] ?? false;
$name = $data['name'] ?? 'Unknown';
$beta = $data['beta'] ?? false;
?>
<li class="<?php echo (isset($data['enabled']) && !$data['enabled']) ? 'dev-feature' : ''; ?>">
<div data-ui-modal class="modal close" data-button-alias="none" data-open-event="provider-update-<?php echo $provider; ?>">
<button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>

View file

@ -63,7 +63,12 @@
"adhocore/jwt": "1.1.2",
"slickdeals/statsd": "3.1.0"
},
"repositories": [],
"repositories": [
{
"type": "git",
"url": "https://github.com/utopia-php/database"
}
],
"require-dev": {
"appwrite/sdk-generator": "0.13.0",
"swoole/ide-helper": "4.6.7",

20
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "95e162b8789c0727b4afac3191962fff",
"content-hash": "31670d6cc60a22007b4ed59a7dc9448f",
"packages": [
{
"name": "adhocore/jwt",
@ -1987,15 +1987,9 @@
"version": "dev-feat-adjust-encodeAttribute",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/database.git",
"url": "https://github.com/utopia-php/database",
"reference": "5ef32ec85143daf78796e7826453244d24f8a86a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/database/zipball/5ef32ec85143daf78796e7826453244d24f8a86a",
"reference": "5ef32ec85143daf78796e7826453244d24f8a86a",
"shasum": ""
},
"require": {
"ext-mongodb": "*",
"ext-pdo": "*",
@ -2017,7 +2011,11 @@
"Utopia\\Database\\": "src/Database"
}
},
"notification-url": "https://packagist.org/downloads/",
"autoload-dev": {
"psr-4": {
"Utopia\\Tests\\": "tests/Database"
}
},
"license": [
"MIT"
],
@ -2039,10 +2037,6 @@
"upf",
"utopia"
],
"support": {
"issues": "https://github.com/utopia-php/database/issues",
"source": "https://github.com/utopia-php/database/tree/feat-adjust-encodeAttribute"
},
"time": "2021-08-27T20:39:51+00:00"
},
{

View file

@ -306,4 +306,102 @@ class DatabaseCustomServerTest extends Scope
$this->assertEquals($response['headers']['status-code'], 404);
}
public function testIndexLimitException()
{
$collection = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'collectionId' => 'testLimitException',
'name' => 'testLimitException',
'read' => ['role:all'],
'write' => ['role:all'],
'permission' => 'document',
]);
$this->assertEquals($collection['headers']['status-code'], 201);
$this->assertEquals($collection['body']['name'], 'testLimitException');
$collectionId = $collection['body']['$id'];
// add unique attributes for indexing
for ($i=0; $i < 64; $i++) {
// $this->assertEquals(true, static::getDatabase()->createAttribute('indexLimit', "test{$i}", Database::VAR_STRING, 16, true));
$attribute = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => "attribute{$i}",
'size' => 64,
'required' => true,
]);
$this->assertEquals($attribute['headers']['status-code'], 201);
}
sleep(5);
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collectionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]));
$this->assertEquals($collection['headers']['status-code'], 200);
$this->assertEquals($collection['body']['name'], 'testLimitException');
$this->assertIsArray($collection['body']['attributes']);
$this->assertIsArray($collection['body']['indexes']);
$this->assertCount(64, $collection['body']['attributes']);
$this->assertCount(0, $collection['body']['indexes']);
// testing for indexLimit = 64
// MariaDB, MySQL, and MongoDB create 3 indexes per new collection
// Add up to the limit, then check if the next index throws IndexLimitException
for ($i=0; $i < 61; $i++) {
// $this->assertEquals(true, static::getDatabase()->createIndex('indexLimit', "index{$i}", Database::INDEX_KEY, ["test{$i}"], [16]));
$index = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/indexes', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'indexId' => "key_attribute{$i}",
'type' => 'key',
'attributes' => ["attribute{$i}"],
]);
$this->assertEquals(201, $index['headers']['status-code']);
$this->assertEquals("key_attribute{$i}", $index['body']['key']);
}
sleep(5);
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collectionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]));
$this->assertEquals($collection['headers']['status-code'], 200);
$this->assertEquals($collection['body']['name'], 'testLimitException');
$this->assertIsArray($collection['body']['attributes']);
$this->assertIsArray($collection['body']['indexes']);
$this->assertCount(64, $collection['body']['attributes']);
$this->assertCount(61, $collection['body']['indexes']);
$tooMany = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/indexes', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'indexId' => 'tooMany',
'type' => 'key',
'attributes' => ['attribute61'],
]);
$this->assertEquals(400, $tooMany['headers']['status-code']);
$this->assertEquals('Index limit exceeded', $tooMany['body']['message']);
}
}