Skip to content

Commit fe5d705

Browse files
committed
Implement multibucket shift for ObjectStore
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
1 parent 70c10f0 commit fe5d705

File tree

4 files changed

+57
-20
lines changed

4 files changed

+57
-20
lines changed

lib/private/Files/Mount/ObjectHomeMountProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private function getMultiBucketObjectStoreConfig(IUser $user) {
121121
if (!isset($config['arguments']['bucket'])) {
122122
$config['arguments']['bucket'] = '';
123123
}
124-
$mapper = new \OC\Files\ObjectStore\Mapper($user);
124+
$mapper = new \OC\Files\ObjectStore\Mapper($user, $this->config);
125125
$numBuckets = isset($config['arguments']['num_buckets']) ? $config['arguments']['num_buckets'] : 64;
126126
$config['arguments']['bucket'] .= $mapper->getBucket($numBuckets);
127127

lib/private/Files/ObjectStore/Mapper.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
namespace OC\Files\ObjectStore;
2424

25+
use OCP\IConfig;
2526
use OCP\IUser;
2627

2728
/**
@@ -35,22 +36,34 @@ class Mapper {
3536
/** @var IUser */
3637
private $user;
3738

39+
/** @var IConfig */
40+
private $config;
41+
3842
/**
3943
* Mapper constructor.
4044
*
4145
* @param IUser $user
46+
* @param IConfig $config
4247
*/
43-
public function __construct(IUser $user) {
48+
public function __construct(IUser $user, IConfig $config) {
4449
$this->user = $user;
50+
$this->config = $config;
4551
}
4652

4753
/**
4854
* @param int $numBuckets
4955
* @return string
5056
*/
5157
public function getBucket($numBuckets = 64) {
58+
// Get the bucket config and shift if provided.
59+
// Allow us to prevent writing in old filled buckets
60+
$config = $this->config->getSystemValue('objectstore_multibucket');
61+
$bucketShift = is_array($config) && isset($config['arguments']['bucket_shift'])
62+
? (int)$config['arguments']['bucket_shift']
63+
: 0;
64+
5265
$hash = md5($this->user->getUID());
5366
$num = hexdec(substr($hash, 0, 4));
54-
return (string)($num % $numBuckets);
67+
return (string)(($num % $numBuckets) + $bucketShift);
5568
}
5669
}

tests/lib/Files/Mount/ObjectHomeMountProviderTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testSingleBucket() {
5454
}
5555

5656
public function testMultiBucket() {
57-
$this->config->expects($this->once())
57+
$this->config->expects($this->exactly(2))
5858
->method('getSystemValue')
5959
->with($this->equalTo('objectstore_multibucket'), '')
6060
->willReturn([
@@ -98,9 +98,9 @@ public function testMultiBucket() {
9898
}
9999

100100
public function testMultiBucketWithPrefix() {
101-
$this->config->expects($this->once())
101+
$this->config->expects($this->exactly(2))
102102
->method('getSystemValue')
103-
->with($this->equalTo('objectstore_multibucket'), '')
103+
->with('objectstore_multibucket')
104104
->willReturn([
105105
'class' => 'Test\Files\Mount\FakeObjectStore',
106106
'arguments' => [
@@ -147,7 +147,7 @@ public function testMultiBucketWithPrefix() {
147147
public function testMultiBucketBucketAlreadySet() {
148148
$this->config->expects($this->once())
149149
->method('getSystemValue')
150-
->with($this->equalTo('objectstore_multibucket'), '')
150+
->with('objectstore_multibucket')
151151
->willReturn([
152152
'class' => 'Test\Files\Mount\FakeObjectStore',
153153
'arguments' => [
@@ -185,9 +185,9 @@ public function testMultiBucketBucketAlreadySet() {
185185
}
186186

187187
public function testMultiBucketConfigFirst() {
188-
$this->config->expects($this->once())
188+
$this->config->expects($this->exactly(2))
189189
->method('getSystemValue')
190-
->with($this->equalTo('objectstore_multibucket'))
190+
->with('objectstore_multibucket')
191191
->willReturn([
192192
'class' => 'Test\Files\Mount\FakeObjectStore',
193193
]);

tests/lib/Files/ObjectStore/MapperTest.php

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,37 @@
2222
namespace Test\Files\ObjectStore;
2323

2424
use OC\Files\ObjectStore\Mapper;
25+
use OCP\IConfig;
2526
use OCP\IUser;
2627

2728
class MapperTest extends \Test\TestCase {
29+
30+
/** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
31+
private $user;
32+
33+
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
34+
private $config;
35+
36+
/** @var Mapper */
37+
private $mapper;
38+
39+
protected function setUp(): void {
40+
parent::setUp();
41+
42+
$this->user = $this->createMock(IUser::class);
43+
$this->config = $this->createMock(IConfig::class);
44+
$this->mapper = new Mapper($this->user, $this->config);
45+
}
46+
2847
public function dataGetBucket() {
2948
return [
30-
['user', 64, '17'],
31-
['USER', 64, '0'],
32-
['bc0e8b52-a66c-1035-90c6-d9663bda9e3f', 64, '56'],
33-
['user', 8, '1'],
34-
['user', 2, '1'],
35-
['USER', 2, '0'],
49+
['user', 64, 0, '17'],
50+
['USER', 64, 0, '0'],
51+
['bc0e8b52-a66c-1035-90c6-d9663bda9e3f', 64, 0, '56'],
52+
['user', 8, 0, '1'],
53+
['user', 2, 0, '1'],
54+
['USER', 2, 0, '0'],
55+
['user', 64, 64, '81'],
3656
];
3757
}
3858

@@ -42,13 +62,17 @@ public function dataGetBucket() {
4262
* @param int $numBuckets
4363
* @param string $expectedBucket
4464
*/
45-
public function testGetBucket($username, $numBuckets, $expectedBucket) {
46-
$user = $this->createMock(IUser::class);
47-
$user->method('getUID')
65+
public function testGetBucket($username, $numBuckets, $bucketShift, $expectedBucket) {
66+
$this->user->expects($this->once())
67+
->method('getUID')
4868
->willReturn($username);
4969

50-
$mapper = new Mapper($user);
70+
$this->config->expects($this->once())
71+
->method('getSystemValue')
72+
->with('objectstore_multibucket')
73+
->willReturn(['arguments' => ['bucket_shift' => $bucketShift]]);
5174

52-
$this->assertSame($expectedBucket, $mapper->getBucket($numBuckets));
75+
$result = $this->mapper->getBucket($numBuckets);
76+
$this->assertEquals($expectedBucket, $result);
5377
}
5478
}

0 commit comments

Comments
 (0)