Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put Mimeloader insertion and read in the same transaction #35744

Merged
merged 1 commit into from
Apr 17, 2023
Merged
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
59 changes: 41 additions & 18 deletions lib/private/Files/Type/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*/
namespace OC\Files\Type;

use OC\DB\Exceptions\DbalException;
use OCP\AppFramework\Db\TTransactional;
use OCP\DB\Exception as DBException;
use OCP\Files\IMimeTypeLoader;
use OCP\IDBConnection;

Expand All @@ -33,6 +36,8 @@
* @package OC\Files\Type
*/
class Loader implements IMimeTypeLoader {
use TTransactional;

/** @var IDBConnection */
private $dbConnection;

Expand Down Expand Up @@ -108,31 +113,49 @@ public function reset() {
* Store a mimetype in the DB
*
* @param string $mimetype
* @param int inserted ID
* @return int inserted ID
Comment on lines -111 to +116
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch :D

*/
protected function store($mimetype) {
$this->dbConnection->insertIfNotExist('*PREFIX*mimetypes', [
'mimetype' => $mimetype
]);

$fetch = $this->dbConnection->getQueryBuilder();
$fetch->select('id')
->from('mimetypes')
->where(
$fetch->expr()->eq('mimetype', $fetch->createNamedParameter($mimetype)
));

$result = $fetch->execute();
$row = $result->fetch();
$result->closeCursor();
$row = $this->atomic(function () use ($mimetype) {
try {
$insert = $this->dbConnection->getQueryBuilder();
$insert->insert('mimetypes')
->values([
'mimetype' => $insert->createNamedParameter($mimetype)
])
->executeStatement();
return [
'mimetype' => $mimetype,
'id' => $insert->getLastInsertId(),
];
} catch (DbalException $e) {
if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
throw $e;
}
$qb = $this->dbConnection->getQueryBuilder();
$row = $qb->select('id')
->from('mimetypes')
->where($qb->expr()->eq('mimetype', $qb->createNamedParameter($mimetype)))
->executeQuery()
->fetchOne();
if ($row) {
return [
'mimetype' => $mimetype,
'id' => $row['id'],
];
}
throw new \Exception("Database threw an unique constraint on inserting a new mimetype, but couldn't return the ID for this very mimetype");
}
}, $this->dbConnection);

if (!$row) {
throw new \Exception("Failed to get mimetype id for $mimetype after trying to store it");
}
$mimetypeId = (int) $row['id'];

$this->mimetypes[$row['id']] = $mimetype;
$this->mimetypeIds[$mimetype] = $row['id'];
return $row['id'];
$this->mimetypes[$mimetypeId] = $mimetype;
$this->mimetypeIds[$mimetype] = $mimetypeId;
return $mimetypeId;
}

/**
Expand Down