HashShardMapper.php 502 B

123456789101112131415161718192021
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Robin Appelman <robin@icewind.nl>
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\DB\QueryBuilder\Sharded;
  8. use OCP\DB\QueryBuilder\Sharded\IShardMapper;
  9. /**
  10. * Map string key to an int-range by hashing the key
  11. */
  12. class HashShardMapper implements IShardMapper {
  13. public function getShardForKey(int $key, int $count): int {
  14. $int = unpack('L', substr(md5((string)$key, true), 0, 4))[1];
  15. return $int % $count;
  16. }
  17. }