-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathindex.php
More file actions
113 lines (99 loc) · 4.4 KB
/
index.php
File metadata and controls
113 lines (99 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* @var CLI $cli
*/
global $cli;
use Utopia\Cache\Adapter\None as NoCache;
use Utopia\Cache\Cache;
use Utopia\CLI\CLI;
use Utopia\Console;
use Utopia\Database\Adapter\MariaDB;
use Utopia\Database\Adapter\MySQL;
use Utopia\Database\Adapter\Postgres;
use Utopia\Database\Database;
use Utopia\Database\PDO;
use Utopia\Validator\Boolean;
use Utopia\Validator\Text;
/**
* @Example
* docker compose exec tests bin/index --adapter=mysql --name=testing
*/
$cli
->task('index')
->desc('Index mock data for testing queries')
->param('adapter', '', new Text(0), 'Database adapter')
->param('name', '', new Text(0), 'Name of created database.')
->param('sharedTables', false, new Boolean(true), 'Whether to use shared tables', true)
->action(function (string $adapter, string $name, bool $sharedTables) {
$namespace = '_ns';
$cache = new Cache(new NoCache());
$dbAdapters = [
'mariadb' => [
'host' => 'mariadb',
'port' => 3306,
'user' => 'root',
'pass' => 'password',
'dsn' => static fn (string $host, int $port) => "mysql:host={$host};port={$port};charset=utf8mb4",
'adapter' => MariaDB::class,
'pdoAttr' => MariaDB::getPDOAttributes(),
],
'mysql' => [
'host' => 'mysql',
'port' => 3307,
'user' => 'root',
'pass' => 'password',
'dsn' => static fn (string $host, int $port) => "mysql:host={$host};port={$port};charset=utf8mb4",
'adapter' => MySQL::class,
'pdoAttr' => MySQL::getPDOAttributes(),
],
'postgres' => [
'host' => 'postgres',
'port' => 5432,
'user' => 'postgres',
'pass' => 'password',
'dsn' => static fn (string $host, int $port) => "pgsql:host={$host};port={$port}",
'adapter' => Postgres::class,
'pdoAttr' => Postgres::getPDOAttributes(),
],
];
if (! isset($dbAdapters[$adapter])) {
Console::error("Adapter '{$adapter}' not supported");
return;
}
$cfg = $dbAdapters[$adapter];
$pdo = new PDO(
($cfg['dsn'])($cfg['host'], $cfg['port']),
$cfg['user'],
$cfg['pass'],
$cfg['pdoAttr']
);
$database = (new Database(new ($cfg['adapter'])($pdo), $cache))
->setDatabase($name)
->setNamespace($namespace)
->setSharedTables($sharedTables);
Console::info("Creating key index 'createdGenre' on 'articles' for created > '2010-01-01 05:00:00' and genre = 'travel'");
$start = microtime(true);
$database->createIndex('articles', 'createdGenre', Database::INDEX_KEY, ['created', 'genre'], [], [Database::ORDER_DESC, Database::ORDER_DESC]);
$time = microtime(true) - $start;
Console::success("Index 'createdGenre' created in {$time} seconds");
Console::info("Creating key index 'genre' on 'articles' for genres: fashion, finance, sports");
$start = microtime(true);
$database->createIndex('articles', 'genre', Database::INDEX_KEY, ['genre'], [], [Database::ORDER_ASC]);
$time = microtime(true) - $start;
Console::success("Index 'genre' created in {$time} seconds");
Console::info("Creating key index 'views' on 'articles' for views > 100000");
$start = microtime(true);
$database->createIndex('articles', 'views', Database::INDEX_KEY, ['views'], [], [Database::ORDER_DESC]);
$time = microtime(true) - $start;
Console::success("Index 'views' created in {$time} seconds");
Console::info("Creating fulltext index 'fulltextsearch' on 'articles' for search term 'Alice'");
$start = microtime(true);
$database->createIndex('articles', 'fulltextsearch', Database::INDEX_FULLTEXT, ['text']);
$time = microtime(true) - $start;
Console::success("Index 'fulltextsearch' created in {$time} seconds");
Console::info("Creating key index 'tags' on 'articles' for tags containing 'tag1'");
$start = microtime(true);
$database->createIndex('articles', 'tags', Database::INDEX_KEY, ['tags']);
$time = microtime(true) - $start;
Console::success("Index 'tags' created in {$time} seconds");
});