server/lib/private/Files/Cache/Propagator.php

201 lines
6.1 KiB
PHP
Raw Normal View History

<?php
/**
2016-07-21 15:07:57 +00:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Lukas Reschke <lukas@statuscode.ch>
2016-07-21 16:13:36 +00:00
* @author Robin Appelman <robin@icewind.nl>
*
* @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
/**
* 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 = [];
/**
* @var \OC\Files\Storage\Storage
*/
protected $storage;
2016-02-25 12:14:20 +00:00
/**
* @var IDBConnection
*/
private $connection;
/**
* @var array
*/
private $ignore = [];
public function __construct(\OC\Files\Storage\Storage $storage, IDBConnection $connection, array $ignore = []) {
$this->storage = $storage;
2016-02-25 12:14:20 +00:00
$this->connection = $connection;
$this->ignore = $ignore;
}
/**
* @param string $internalPath
* @param int $time
* @param int $sizeDifference number of bytes the file has grown
* @suppress SqlInjectionChecker
*/
public function propagateChange($internalPath, $time, $sizeDifference = 0) {
// 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
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);
2016-02-25 12:14:20 +00:00
$builder->update('filecache')
->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));
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')
->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)));
$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;
}
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
* @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')
->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')
->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();
}
}