aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/DB/QueryBuilder/Sharded/HashShardMapper.php
blob: af778489a2dfd7de6fe8ad0ea535c5048bbf176a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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;
	}
}