1
0

Mapper.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\ObjectStore;
  8. use OCP\IConfig;
  9. use OCP\IUser;
  10. /**
  11. * Class Mapper
  12. *
  13. * @package OC\Files\ObjectStore
  14. *
  15. * Map a user to a bucket.
  16. */
  17. class Mapper {
  18. /** @var IUser */
  19. private $user;
  20. /** @var IConfig */
  21. private $config;
  22. /**
  23. * Mapper constructor.
  24. *
  25. * @param IUser $user
  26. * @param IConfig $config
  27. */
  28. public function __construct(IUser $user, IConfig $config) {
  29. $this->user = $user;
  30. $this->config = $config;
  31. }
  32. /**
  33. * @param int $numBuckets
  34. * @return string
  35. */
  36. public function getBucket($numBuckets = 64) {
  37. // Get the bucket config and shift if provided.
  38. // Allow us to prevent writing in old filled buckets
  39. $config = $this->config->getSystemValue('objectstore_multibucket');
  40. $minBucket = is_array($config) && isset($config['arguments']['min_bucket'])
  41. ? (int)$config['arguments']['min_bucket']
  42. : 0;
  43. $hash = md5($this->user->getUID());
  44. $num = hexdec(substr($hash, 0, 4));
  45. return (string)(($num % ($numBuckets - $minBucket)) + $minBucket);
  46. }
  47. }