2015-11-10 18:14:08 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-11-10 18:14:08 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-11-10 18:14:08 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Files\Cache;
|
|
|
|
|
2016-02-25 15:14:20 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2015-12-02 16:03:52 +03:00
|
|
|
use OCP\Files\Cache\IPropagator;
|
2016-02-25 15:14:20 +03:00
|
|
|
use OCP\IDBConnection;
|
2015-12-02 16:03:52 +03:00
|
|
|
|
2015-11-10 18:14:08 +03:00
|
|
|
/**
|
|
|
|
* Propagate etags and mtimes within the storage
|
|
|
|
*/
|
2015-12-02 16:03:52 +03:00
|
|
|
class Propagator implements IPropagator {
|
2016-05-20 16:10:18 +03:00
|
|
|
private $inBatch = false;
|
|
|
|
|
|
|
|
private $batch = [];
|
|
|
|
|
2015-11-10 18:14:08 +03:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage
|
|
|
|
*/
|
|
|
|
protected $storage;
|
|
|
|
|
2016-02-25 15:14:20 +03:00
|
|
|
/**
|
|
|
|
* @var IDBConnection
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
2015-11-10 18:14:08 +03:00
|
|
|
/**
|
2018-12-06 00:06:19 +03:00
|
|
|
* @var array
|
2015-11-10 18:14:08 +03:00
|
|
|
*/
|
2018-12-06 00:06:19 +03:00
|
|
|
private $ignore = [];
|
|
|
|
|
|
|
|
public function __construct(\OC\Files\Storage\Storage $storage, IDBConnection $connection, array $ignore = []) {
|
2015-11-10 18:14:08 +03:00
|
|
|
$this->storage = $storage;
|
2016-02-25 15:14:20 +03:00
|
|
|
$this->connection = $connection;
|
2018-12-06 00:06:19 +03:00
|
|
|
$this->ignore = $ignore;
|
2015-11-10 18:14:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $internalPath
|
|
|
|
* @param int $time
|
2016-01-07 20:13:35 +03:00
|
|
|
* @param int $sizeDifference number of bytes the file has grown
|
2017-07-20 23:48:13 +03:00
|
|
|
* @suppress SqlInjectionChecker
|
2015-11-10 18:14:08 +03:00
|
|
|
*/
|
2016-01-07 20:13:35 +03:00
|
|
|
public function propagateChange($internalPath, $time, $sizeDifference = 0) {
|
2018-12-06 00:06:19 +03:00
|
|
|
// Do not propogate changes in ignored paths
|
|
|
|
foreach ($this->ignore as $ignore) {
|
|
|
|
if (strpos($internalPath, $ignore) === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 15:14:20 +03:00
|
|
|
$storageId = (int)$this->storage->getStorageCache()->getNumericId();
|
|
|
|
|
|
|
|
$parents = $this->getParents($internalPath);
|
|
|
|
|
2016-05-20 16:10:18 +03:00
|
|
|
if ($this->inBatch) {
|
|
|
|
foreach ($parents as $parent) {
|
|
|
|
$this->addToBatch($parent, $time, $sizeDifference);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-25 15:14:20 +03:00
|
|
|
$parentHashes = array_map('md5', $parents);
|
|
|
|
$etag = uniqid(); // since we give all folders the same etag we don't ask the storage for the etag
|
2015-11-10 18:14:08 +03:00
|
|
|
|
2016-02-25 15:14:20 +03:00
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
$hashParams = array_map(function ($hash) use ($builder) {
|
|
|
|
return $builder->expr()->literal($hash);
|
|
|
|
}, $parentHashes);
|
2015-11-10 18:14:08 +03:00
|
|
|
|
2016-02-25 15:14:20 +03:00
|
|
|
$builder->update('filecache')
|
2019-11-06 14:05:46 +03:00
|
|
|
->set('mtime', $builder->func()->greatest('mtime', $builder->createNamedParameter((int)$time, IQueryBuilder::PARAM_INT)))
|
2016-02-25 15:14:20 +03:00
|
|
|
->set('etag', $builder->createNamedParameter($etag, IQueryBuilder::PARAM_STR))
|
|
|
|
->where($builder->expr()->eq('storage', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($builder->expr()->in('path_hash', $hashParams));
|
2015-11-10 18:14:08 +03:00
|
|
|
|
2016-02-25 15:14:20 +03:00
|
|
|
$builder->execute();
|
|
|
|
|
|
|
|
if ($sizeDifference !== 0) {
|
|
|
|
// we need to do size separably so we can ignore entries with uncalculated size
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
$builder->update('filecache')
|
2019-11-06 14:05:46 +03:00
|
|
|
->set('size', $builder->func()->greatest(
|
|
|
|
$builder->createNamedParameter(-1, IQueryBuilder::PARAM_INT),
|
|
|
|
$builder->func()->add('size', $builder->createNamedParameter($sizeDifference)))
|
|
|
|
)
|
2016-02-25 15:14:20 +03:00
|
|
|
->where($builder->expr()->eq('storage', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($builder->expr()->in('path_hash', $hashParams))
|
|
|
|
->andWhere($builder->expr()->gt('size', $builder->expr()->literal(-1, IQueryBuilder::PARAM_INT)));
|
2015-11-10 18:14:08 +03:00
|
|
|
|
2018-12-05 22:32:02 +03:00
|
|
|
$builder->execute();
|
|
|
|
}
|
2016-02-25 15:14:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getParents($path) {
|
|
|
|
$parts = explode('/', $path);
|
|
|
|
$parent = '';
|
|
|
|
$parents = [];
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
$parents[] = $parent;
|
|
|
|
$parent = trim($parent . '/' . $part, '/');
|
|
|
|
}
|
|
|
|
return $parents;
|
2015-11-10 18:14:08 +03:00
|
|
|
}
|
2016-05-20 16:10:18 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark the beginning of a propagation batch
|
|
|
|
*
|
|
|
|
* Note that not all cache setups support propagation in which case this will be a noop
|
|
|
|
*
|
|
|
|
* Batching for cache setups that do support it has to be explicit since the cache state is not fully consistent
|
|
|
|
* before the batch is committed.
|
|
|
|
*/
|
|
|
|
public function beginBatch() {
|
|
|
|
$this->inBatch = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function addToBatch($internalPath, $time, $sizeDifference) {
|
|
|
|
if (!isset($this->batch[$internalPath])) {
|
|
|
|
$this->batch[$internalPath] = [
|
|
|
|
'hash' => md5($internalPath),
|
|
|
|
'time' => $time,
|
|
|
|
'size' => $sizeDifference
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$this->batch[$internalPath]['size'] += $sizeDifference;
|
|
|
|
if ($time > $this->batch[$internalPath]['time']) {
|
|
|
|
$this->batch[$internalPath]['time'] = $time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commit the active propagation batch
|
2017-07-20 23:48:13 +03:00
|
|
|
* @suppress SqlInjectionChecker
|
2016-05-20 16:10:18 +03:00
|
|
|
*/
|
|
|
|
public function commitBatch() {
|
|
|
|
if (!$this->inBatch) {
|
|
|
|
throw new \BadMethodCallException('Not in batch');
|
|
|
|
}
|
|
|
|
$this->inBatch = false;
|
|
|
|
|
|
|
|
$this->connection->beginTransaction();
|
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$storageId = (int)$this->storage->getStorageCache()->getNumericId();
|
|
|
|
|
|
|
|
$query->update('filecache')
|
2018-10-19 17:44:28 +03:00
|
|
|
->set('mtime', $query->createFunction('GREATEST(' . $query->getColumnName('mtime') . ', ' . $query->createParameter('time') . ')'))
|
2016-05-20 16:10:18 +03:00
|
|
|
->set('etag', $query->expr()->literal(uniqid()))
|
|
|
|
->where($query->expr()->eq('storage', $query->expr()->literal($storageId, IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($query->expr()->eq('path_hash', $query->createParameter('hash')));
|
|
|
|
|
|
|
|
$sizeQuery = $this->connection->getQueryBuilder();
|
|
|
|
$sizeQuery->update('filecache')
|
2018-10-19 17:44:28 +03:00
|
|
|
->set('size', $sizeQuery->func()->add('size', $sizeQuery->createParameter('size')))
|
2016-05-20 16:10:18 +03:00
|
|
|
->where($query->expr()->eq('storage', $query->expr()->literal($storageId, IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($query->expr()->eq('path_hash', $query->createParameter('hash')))
|
|
|
|
->andWhere($sizeQuery->expr()->gt('size', $sizeQuery->expr()->literal(-1, IQueryBuilder::PARAM_INT)));
|
|
|
|
|
|
|
|
foreach ($this->batch as $item) {
|
2016-06-02 16:43:43 +03:00
|
|
|
$query->setParameter('time', $item['time'], IQueryBuilder::PARAM_INT);
|
2016-05-20 16:10:18 +03:00
|
|
|
$query->setParameter('hash', $item['hash']);
|
|
|
|
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
if ($item['size']) {
|
2016-06-02 16:43:43 +03:00
|
|
|
$sizeQuery->setParameter('size', $item['size'], IQueryBuilder::PARAM_INT);
|
2016-05-20 16:10:18 +03:00
|
|
|
$sizeQuery->setParameter('hash', $item['hash']);
|
|
|
|
|
|
|
|
$sizeQuery->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->batch = [];
|
|
|
|
|
|
|
|
$this->connection->commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-10 18:14:08 +03:00
|
|
|
}
|