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

Insert filecache data and conflict fallback read in the same transaction #36313

Closed
wants to merge 1 commit into from
Closed
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
76 changes: 41 additions & 35 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@
namespace OC\Files\Cache;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OC\DB\Exceptions\DbalException;
use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchQuery;
use OCP\AppFramework\Db\TTransactional;
use OCP\DB\Exception as DBException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\CacheEntryInsertedEvent;
Expand Down Expand Up @@ -72,6 +75,8 @@
* - ChangePropagator: updates the mtime and etags of parent folders whenever a change to the cache is made to the cache by the updater
*/
class Cache implements ICache {
use TTransactional;

use MoveFromCacheTrait {
MoveFromCacheTrait::moveFromCache as moveFromCacheFallback;
}
Expand Down Expand Up @@ -271,7 +276,8 @@ public function put($file, array $data) {
* @param array $data
*
* @return int file id
* @throws \RuntimeException
* @throws Exception

Check failure

Code scanning / Psalm

UndefinedDocblockClass

Docblock-defined class, interface or enum named OC\Files\Cache\Exception does not exist
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @throws Exception
* @throws \Exception

* @throws \Throwable
*/
public function insert($file, array $data) {
// normalize file
Expand Down Expand Up @@ -300,48 +306,48 @@ public function insert($file, array $data) {
$storageId = $this->getNumericStorageId();
$values['storage'] = $storageId;

try {
$builder = $this->connection->getQueryBuilder();
$builder->insert('filecache');
return $this->atomic(function () use ($values, $extensionValues, $file, $storageId, $data) {
try {
$builder = $this->connection->getQueryBuilder();
$builder->insert('filecache');

foreach ($values as $column => $value) {
$builder->setValue($column, $builder->createNamedParameter($value));
}
foreach ($values as $column => $value) {
$builder->setValue($column, $builder->createNamedParameter($value));
}

if ($builder->execute()) {
$fileId = $builder->getLastInsertId();
if ($builder->executeStatement()) {
$fileId = $builder->getLastInsertId();

if (count($extensionValues)) {
$query = $this->getQueryBuilder();
$query->insert('filecache_extended');
if (count($extensionValues)) {
$query = $this->getQueryBuilder();
$query->insert('filecache_extended');

$query->setValue('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT));
foreach ($extensionValues as $column => $value) {
$query->setValue($column, $query->createNamedParameter($value));
$query->setValue('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT));
foreach ($extensionValues as $column => $value) {
$query->setValue($column, $query->createNamedParameter($value));
}
$query->execute();
}
$query->execute();

$event = new CacheEntryInsertedEvent($this->storage, $file, $fileId, $storageId);
$this->eventDispatcher->dispatch(CacheInsertEvent::class, $event);
$this->eventDispatcher->dispatchTyped($event);
return $fileId;
}
} catch (DbalException $e) {
if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
throw $e;
}

$event = new CacheEntryInsertedEvent($this->storage, $file, $fileId, $storageId);
$this->eventDispatcher->dispatch(CacheInsertEvent::class, $event);
$this->eventDispatcher->dispatchTyped($event);
return $fileId;
}
} catch (UniqueConstraintViolationException $e) {
// entry exists already
if ($this->connection->inTransaction()) {
$this->connection->commit();
$this->connection->beginTransaction();
// The file was created in the mean time
if (($id = $this->getId($file)) > -1) {
$this->update($id, $data);
return $id;
} else {
throw new \RuntimeException('File entry could not be inserted but could also not be selected with getId() in order to perform an update. Please try again.');
}
}
}

// The file was created in the mean time
if (($id = $this->getId($file)) > -1) {
$this->update($id, $data);
return $id;
} else {
throw new \RuntimeException('File entry could not be inserted but could also not be selected with getId() in order to perform an update. Please try again.');
}
}, $this->connection);
}

/**
Expand Down