0
0
Fork 0
mirror of https://github.com/nextcloud/server.git synced 2025-01-16 08:09:00 +00:00
nextcloud_server/lib/private/DB/QueryBuilder/Sharded/ShardConnectionManager.php
Robin Appelman 62f8b6517f
feat: implement distributing partitioned queries over multiple shards
Signed-off-by: Robin Appelman <robin@icewind.nl>
2024-08-28 10:21:19 +02:00

43 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Robin Appelman <robin@icewind.nl>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\DB\QueryBuilder\Sharded;
use OC\DB\ConnectionAdapter;
use OC\DB\ConnectionFactory;
use OC\SystemConfig;
use OCP\IDBConnection;
/**
* Keeps track of the db connections to the various shards
*/
class ShardConnectionManager {
/** @var array<string, IDBConnection> */
private array $connections = [];
public function __construct(
private SystemConfig $config,
private ConnectionFactory $factory,
) {
}
public function getConnection(ShardDefinition $shardDefinition, int $shard): IDBConnection {
$connectionKey = $shardDefinition->table . '_' . $shard;
if (!isset($this->connections[$connectionKey])) {
$this->connections[$connectionKey] = $this->createConnection($shardDefinition->shards[$shard]);
}
return $this->connections[$connectionKey];
}
private function createConnection(array $shardConfig): IDBConnection {
$shardConfig['sharding'] = [];
$type = $this->config->getValue('dbtype', 'sqlite');
return new ConnectionAdapter($this->factory->getConnection($type, $shardConfig));
}
}