Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions lib/private/Files/ObjectStore/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
namespace OC\Files\ObjectStore;

use OCP\IConfig;
use OCP\IUser;

/**
Expand All @@ -18,33 +17,17 @@
* Map a user to a bucket.
*/
class Mapper {
/** @var IUser */
private $user;

/** @var IConfig */
private $config;

/**
* Mapper constructor.
*
* @param IUser $user
* @param IConfig $config
*/
public function __construct(IUser $user, IConfig $config) {
$this->user = $user;
$this->config = $config;
public function __construct(
private readonly IUser $user,
private readonly array $config,
) {
}

/**
* @param int $numBuckets
* @return string
*/
public function getBucket($numBuckets = 64) {
public function getBucket(int $numBuckets = 64): string {
// Get the bucket config and shift if provided.
// Allow us to prevent writing in old filled buckets
$config = $this->config->getSystemValue('objectstore_multibucket');
$minBucket = is_array($config) && isset($config['arguments']['min_bucket'])
? (int)$config['arguments']['min_bucket']
$minBucket = isset($this->config['arguments']['min_bucket'])
? (int)$this->config['arguments']['min_bucket']
: 0;

$hash = md5($this->user->getUID());
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/ObjectStore/PrimaryObjectStoreConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function getBucketForUser(IUser $user, array $config): string {
if (!isset($config['arguments']['bucket'])) {
$config['arguments']['bucket'] = '';
}
$mapper = new Mapper($user, $this->config);
$mapper = new Mapper($user, $config);
$numBuckets = $config['arguments']['num_buckets'] ?? 64;
$bucket = $config['arguments']['bucket'] . $mapper->getBucket($numBuckets);

Expand Down
17 changes: 2 additions & 15 deletions tests/lib/Files/ObjectStore/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,16 @@
namespace Test\Files\ObjectStore;

use OC\Files\ObjectStore\Mapper;
use OCP\IConfig;
use OCP\IUser;

class MapperTest extends \Test\TestCase {
/** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
private $user;

/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
private $config;

/** @var Mapper */
private $mapper;

protected function setUp(): void {
parent::setUp();

$this->user = $this->createMock(IUser::class);
$this->config = $this->createMock(IConfig::class);
$this->mapper = new Mapper($this->user, $this->config);
}

public static function dataGetBucket(): array {
Expand All @@ -49,16 +40,12 @@ public static function dataGetBucket(): array {
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetBucket')]
public function testGetBucket($username, $numBuckets, $bucketShift, $expectedBucket): void {
$mapper = new Mapper($this->user, ['arguments' => ['min_bucket' => $bucketShift]]);
$this->user->expects($this->once())
->method('getUID')
->willReturn($username);

$this->config->expects($this->once())
->method('getSystemValue')
->with('objectstore_multibucket')
->willReturn(['arguments' => ['min_bucket' => $bucketShift]]);

$result = $this->mapper->getBucket($numBuckets);
$result = $mapper->getBucket($numBuckets);
$this->assertEquals($expectedBucket, $result);
}
}