Skip to content

Commit 532ace6

Browse files
committed
Put Mimeloader insertion and read in the same transaction
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
1 parent 32ef1f5 commit 532ace6

File tree

4 files changed

+47
-55
lines changed

4 files changed

+47
-55
lines changed

lib/private/Files/Cache/SearchBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,18 @@ private function getOperatorFieldAndValue(ISearchComparison $operator) {
145145
if ($field === 'mimetype') {
146146
$value = (string)$value;
147147
if ($operator->getType() === ISearchComparison::COMPARE_EQUAL) {
148-
$value = (int)$this->mimetypeLoader->getId($value);
148+
$value = $this->mimetypeLoader->getId($value);
149149
} elseif ($operator->getType() === ISearchComparison::COMPARE_LIKE) {
150150
// transform "mimetype='foo/%'" to "mimepart='foo'"
151151
if (preg_match('|(.+)/%|', $value, $matches)) {
152152
$field = 'mimepart';
153-
$value = (int)$this->mimetypeLoader->getId($matches[1]);
153+
$value = $this->mimetypeLoader->getId($matches[1]);
154154
$type = ISearchComparison::COMPARE_EQUAL;
155155
} elseif (strpos($value, '%') !== false) {
156156
throw new \InvalidArgumentException('Unsupported query value for mimetype: ' . $value . ', only values in the format "mime/type" or "mime/%" are supported');
157157
} else {
158158
$field = 'mimetype';
159-
$value = (int)$this->mimetypeLoader->getId($value);
159+
$value = $this->mimetypeLoader->getId($value);
160160
$type = ISearchComparison::COMPARE_EQUAL;
161161
}
162162
}

lib/private/Files/Type/Loader.php

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
namespace OC\Files\Type;
2626

27+
use OCP\AppFramework\Db\TTransactional;
2728
use OCP\Files\IMimeTypeLoader;
2829
use OCP\IDBConnection;
2930

@@ -33,15 +34,15 @@
3334
* @package OC\Files\Type
3435
*/
3536
class Loader implements IMimeTypeLoader {
37+
use TTransactional;
3638

37-
/** @var IDBConnection */
38-
private $dbConnection;
39+
private IDBConnection $dbConnection;
3940

40-
/** @var array [id => mimetype] */
41-
protected $mimetypes;
41+
/** @psalm-var array<int, string> */
42+
protected array $mimetypes;
4243

43-
/** @var array [mimetype => id] */
44-
protected $mimetypeIds;
44+
/** @psalm-var array<string, int> */
45+
protected array $mimetypeIds;
4546

4647
/**
4748
* @param IDBConnection $dbConnection
@@ -54,11 +55,8 @@ public function __construct(IDBConnection $dbConnection) {
5455

5556
/**
5657
* Get a mimetype from its ID
57-
*
58-
* @param int $id
59-
* @return string|null
6058
*/
61-
public function getMimetypeById($id) {
59+
public function getMimetypeById(int $id): ?string {
6260
if (!$this->mimetypes) {
6361
$this->loadMimetypes();
6462
}
@@ -70,11 +68,8 @@ public function getMimetypeById($id) {
7068

7169
/**
7270
* Get a mimetype ID, adding the mimetype to the DB if it does not exist
73-
*
74-
* @param string $mimetype
75-
* @return int
7671
*/
77-
public function getId($mimetype) {
72+
public function getId(string $mimetype): int {
7873
if (!$this->mimetypeIds) {
7974
$this->loadMimetypes();
8075
}
@@ -86,11 +81,8 @@ public function getId($mimetype) {
8681

8782
/**
8883
* Test if a mimetype exists in the database
89-
*
90-
* @param string $mimetype
91-
* @return bool
9284
*/
93-
public function exists($mimetype) {
85+
public function exists(string $mimetype): bool {
9486
if (!$this->mimetypeIds) {
9587
$this->loadMimetypes();
9688
}
@@ -100,46 +92,49 @@ public function exists($mimetype) {
10092
/**
10193
* Clear all loaded mimetypes, allow for re-loading
10294
*/
103-
public function reset() {
95+
public function reset(): void {
10496
$this->mimetypes = [];
10597
$this->mimetypeIds = [];
10698
}
10799

108100
/**
109101
* Store a mimetype in the DB
110-
*
111-
* @param string $mimetype
112-
* @param int inserted ID
113102
*/
114-
protected function store($mimetype) {
115-
$this->dbConnection->insertIfNotExist('*PREFIX*mimetypes', [
116-
'mimetype' => $mimetype
117-
]);
118-
119-
$fetch = $this->dbConnection->getQueryBuilder();
120-
$fetch->select('id')
121-
->from('mimetypes')
122-
->where(
123-
$fetch->expr()->eq('mimetype', $fetch->createNamedParameter($mimetype)
124-
));
125-
126-
$result = $fetch->execute();
127-
$row = $result->fetch();
128-
$result->closeCursor();
103+
protected function store(string $mimetype): int {
104+
$row = $this->atomic(function () use ($mimetype) {
105+
$insert = $this->dbConnection->getQueryBuilder();
106+
$insert->insert('mimetypes')
107+
->values([
108+
'mimetype' => $insert->createNamedParameter($mimetype)
109+
])
110+
->executeStatement();
111+
112+
$fetch = $this->dbConnection->getQueryBuilder();
113+
$fetch->select('id')
114+
->from('mimetypes')
115+
->where(
116+
$fetch->expr()->eq('mimetype', $fetch->createNamedParameter($mimetype)
117+
));
118+
119+
$result = $fetch->execute();
120+
$row = $result->fetch();
121+
$result->closeCursor();
122+
return $row;
123+
}, $this->dbConnection);
129124

130125
if (!$row) {
131126
throw new \Exception("Failed to get mimetype id for $mimetype after trying to store it");
132127
}
133128

134129
$this->mimetypes[$row['id']] = $mimetype;
135130
$this->mimetypeIds[$mimetype] = $row['id'];
136-
return $row['id'];
131+
return (int) $row['id'];
137132
}
138133

139134
/**
140135
* Load all mimetypes from DB
141136
*/
142-
private function loadMimetypes() {
137+
private function loadMimetypes(): void {
143138
$qb = $this->dbConnection->getQueryBuilder();
144139
$qb->select('id', 'mimetype')
145140
->from('mimetypes');
@@ -157,11 +152,9 @@ private function loadMimetypes() {
157152
/**
158153
* Update filecache mimetype based on file extension
159154
*
160-
* @param string $ext file extension
161-
* @param int $mimeTypeId
162155
* @return int number of changed rows
163156
*/
164-
public function updateFilecache($ext, $mimeTypeId) {
157+
public function updateFilecache(string $ext, int $mimeTypeId): int {
165158
$folderMimeTypeId = $this->getId('httpd/unix-directory');
166159
$update = $this->dbConnection->getQueryBuilder();
167160
$update->update('filecache')

lib/public/Files/IMimeTypeLoader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ interface IMimeTypeLoader {
3636
* @return string|null
3737
* @since 8.2.0
3838
*/
39-
public function getMimetypeById($id);
39+
public function getMimetypeById(int $id): ?string;
4040

4141
/**
4242
* Get a mimetype ID, adding the mimetype to the DB if it does not exist
@@ -45,7 +45,7 @@ public function getMimetypeById($id);
4545
* @return int
4646
* @since 8.2.0
4747
*/
48-
public function getId($mimetype);
48+
public function getId(string $mimetype): int;
4949

5050
/**
5151
* Test if a mimetype exists in the database
@@ -54,12 +54,12 @@ public function getId($mimetype);
5454
* @return bool
5555
* @since 8.2.0
5656
*/
57-
public function exists($mimetype);
57+
public function exists(string $mimetype): bool;
5858

5959
/**
6060
* Clear all loaded mimetypes, allow for re-loading
6161
*
6262
* @since 8.2.0
6363
*/
64-
public function reset();
64+
public function reset(): void;
6565
}

tests/lib/Files/Type/LoaderTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@
2323

2424
use OC\Files\Type\Loader;
2525
use OCP\IDBConnection;
26+
use Test\TestCase;
2627

27-
class LoaderTest extends \Test\TestCase {
28-
/** @var IDBConnection */
29-
protected $db;
30-
/** @var Loader */
31-
protected $loader;
28+
class LoaderTest extends TestCase {
29+
protected IDBConnection $db;
30+
protected Loader $loader;
3231

3332
protected function setUp(): void {
34-
$this->db = \OC::$server->getDatabaseConnection();
33+
$this->db = \OC::$server->get(IDBConnection::class);
3534
$this->loader = new Loader($this->db);
3635
}
3736

0 commit comments

Comments
 (0)