2015-11-10 15:14:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2015-11-10 15:14:08 +00: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,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Files\Cache;
|
|
|
|
|
2016-02-25 12:14:20 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2015-12-02 13:03:52 +00:00
|
|
|
use OCP\Files\Cache\IPropagator;
|
2016-02-25 12:14:20 +00:00
|
|
|
use OCP\IDBConnection;
|
2015-12-02 13:03:52 +00:00
|
|
|
|
2015-11-10 15:14:08 +00:00
|
|
|
/**
|
|
|
|
* Propagate etags and mtimes within the storage
|
|
|
|
*/
|
2015-12-02 13:03:52 +00:00
|
|
|
class Propagator implements IPropagator {
|
2016-05-20 13:10:18 +00:00
|
|
|
private $inBatch = false;
|
|
|
|
|
|
|
|
private $batch = [];
|
|
|
|
|
2015-11-10 15:14:08 +00:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage
|
|
|
|
*/
|
|
|
|
protected $storage;
|
|
|
|
|
2016-02-25 12:14:20 +00:00
|
|
|
/**
|
|
|
|
* @var IDBConnection
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
2015-11-10 15:14:08 +00:00
|
|
|
/**
|
2018-12-05 21:06:19 +00:00
|
|
|
* @var array
|
2015-11-10 15:14:08 +00:00
|
|
|
*/
|
2018-12-05 21:06:19 +00:00
|
|
|
private $ignore = [];
|
|
|
|
|
|
|
|
public function __construct(\OC\Files\Storage\Storage $storage, IDBConnection $connection, array $ignore = []) {
|
2015-11-10 15:14:08 +00:00
|
|
|
$this->storage = $storage;
|
2016-02-25 12:14:20 +00:00
|
|
|
$this->connection = $connection;
|
2018-12-05 21:06:19 +00:00
|
|
|
$this->ignore = $ignore;
|
2015-11-10 15:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $internalPath
|
|
|
|
* @param int $time
|
2016-01-07 17:13:35 +00:00
|
|
|
* @param int $sizeDifference number of bytes the file has grown
|
2017-07-20 20:48:13 +00:00
|
|
|
* @suppress SqlInjectionChecker
|
2015-11-10 15:14:08 +00:00
|
|
|
*/
|
2016-01-07 17:13:35 +00:00
|
|
|
public function propagateChange($internalPath, $time, $sizeDifference = 0) {
|
2018-12-05 21:06:19 +00:00
|
|
|
// Do not propogate changes in ignored paths
|
|
|
|
foreach ($this->ignore as $ignore) {
|
|
|
|
if (strpos($internalPath, $ignore) === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 12:14:20 +00:00
|
|
|
$storageId = (int)$this->storage->getStorageCache()->getNumericId();
|
|
|
|
|
|
|
|
$parents = $this->getParents($internalPath);
|
|
|
|
|
2016-05-20 13:10:18 +00:00
|
|
|
if ($this->inBatch) {
|
|
|
|
foreach ($parents as $parent) {
|
|
|
|
$this->addToBatch($parent, $time, $sizeDifference);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-25 12:14:20 +00: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 15:14:08 +00:00
|
|
|
|
2016-02-25 12:14:20 +00:00
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
$hashParams = array_map(function ($hash) use ($builder) {
|
|
|
|
return $builder->expr()->literal($hash);
|
|
|
|
}, $parentHashes);
|
2015-11-10 15:14:08 +00:00
|
|
|
|
2016-02-25 12:14:20 +00:00
|
|
|
$builder->update('filecache')
|
2018-10-19 14:44:28 +00:00
|
|
|
->set('mtime', $builder->createFunction('GREATEST(' . $builder->getColumnName('mtime') . ', ' . $builder->createNamedParameter((int)$time, IQueryBuilder::PARAM_INT) . ')'))
|
2016-02-25 12:14:20 +00: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 15:14:08 +00:00
|
|
|
|
2016-02-25 12:14:20 +00: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')
|
2018-10-19 14:44:28 +00:00
|
|
|
->set('size', $builder->func()->add('size', $builder->createNamedParameter($sizeDifference)))
|
2016-02-25 12:14:20 +00: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 15:14:08 +00:00
|
|
|
|
2018-12-05 19:32:02 +00:00
|
|
|
$builder->execute();
|
|
|
|
}
|
2016-02-25 12:14:20 +00: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 15:14:08 +00:00
|
|
|
}
|
2016-05-20 13:10:18 +00: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 20:48:13 +00:00
|
|
|
* @suppress SqlInjectionChecker
|
2016-05-20 13:10:18 +00: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 14:44:28 +00:00
|
|
|
->set('mtime', $query->createFunction('GREATEST(' . $query->getColumnName('mtime') . ', ' . $query->createParameter('time') . ')'))
|
2016-05-20 13:10:18 +00: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 14:44:28 +00:00
|
|
|
->set('size', $sizeQuery->func()->add('size', $sizeQuery->createParameter('size')))
|
2016-05-20 13:10:18 +00: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 13:43:43 +00:00
|
|
|
$query->setParameter('time', $item['time'], IQueryBuilder::PARAM_INT);
|
2016-05-20 13:10:18 +00:00
|
|
|
$query->setParameter('hash', $item['hash']);
|
|
|
|
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
if ($item['size']) {
|
2016-06-02 13:43:43 +00:00
|
|
|
$sizeQuery->setParameter('size', $item['size'], IQueryBuilder::PARAM_INT);
|
2016-05-20 13:10:18 +00:00
|
|
|
$sizeQuery->setParameter('hash', $item['hash']);
|
|
|
|
|
|
|
|
$sizeQuery->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->batch = [];
|
|
|
|
|
|
|
|
$this->connection->commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-10 15:14:08 +00:00
|
|
|
}
|