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/HashShardMapper.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

21 lines
502 B
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 OCP\DB\QueryBuilder\Sharded\IShardMapper;
/**
* Map string key to an int-range by hashing the key
*/
class HashShardMapper implements IShardMapper {
public function getShardForKey(int $key, int $count): int {
$int = unpack('L', substr(md5((string)$key, true), 0, 4))[1];
return $int % $count;
}
}