1
0

Mapper.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Files\ObjectStore;
  24. use OCP\IConfig;
  25. use OCP\IUser;
  26. /**
  27. * Class Mapper
  28. *
  29. * @package OC\Files\ObjectStore
  30. *
  31. * Map a user to a bucket.
  32. */
  33. class Mapper {
  34. /** @var IUser */
  35. private $user;
  36. /** @var IConfig */
  37. private $config;
  38. /**
  39. * Mapper constructor.
  40. *
  41. * @param IUser $user
  42. * @param IConfig $config
  43. */
  44. public function __construct(IUser $user, IConfig $config) {
  45. $this->user = $user;
  46. $this->config = $config;
  47. }
  48. /**
  49. * @param int $numBuckets
  50. * @return string
  51. */
  52. public function getBucket($numBuckets = 64) {
  53. // Get the bucket config and shift if provided.
  54. // Allow us to prevent writing in old filled buckets
  55. $config = $this->config->getSystemValue('objectstore_multibucket');
  56. $minBucket = is_array($config) && isset($config['arguments']['min_bucket'])
  57. ? (int)$config['arguments']['min_bucket']
  58. : 0;
  59. $hash = md5($this->user->getUID());
  60. $num = hexdec(substr($hash, 0, 4));
  61. return (string)(($num % ($numBuckets - $minBucket)) + $minBucket);
  62. }
  63. }