Propagator.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Files\Cache;
  24. use OCP\DB\QueryBuilder\IQueryBuilder;
  25. use OCP\Files\Cache\IPropagator;
  26. use OCP\IDBConnection;
  27. /**
  28. * Propagate etags and mtimes within the storage
  29. */
  30. class Propagator implements IPropagator {
  31. private $inBatch = false;
  32. private $batch = [];
  33. /**
  34. * @var \OC\Files\Storage\Storage
  35. */
  36. protected $storage;
  37. /**
  38. * @var IDBConnection
  39. */
  40. private $connection;
  41. /**
  42. * @var array
  43. */
  44. private $ignore = [];
  45. public function __construct(\OC\Files\Storage\Storage $storage, IDBConnection $connection, array $ignore = []) {
  46. $this->storage = $storage;
  47. $this->connection = $connection;
  48. $this->ignore = $ignore;
  49. }
  50. /**
  51. * @param string $internalPath
  52. * @param int $time
  53. * @param int $sizeDifference number of bytes the file has grown
  54. * @suppress SqlInjectionChecker
  55. */
  56. public function propagateChange($internalPath, $time, $sizeDifference = 0) {
  57. // Do not propogate changes in ignored paths
  58. foreach ($this->ignore as $ignore) {
  59. if (strpos($internalPath, $ignore) === 0) {
  60. return;
  61. }
  62. }
  63. $storageId = (int)$this->storage->getStorageCache()->getNumericId();
  64. $parents = $this->getParents($internalPath);
  65. if ($this->inBatch) {
  66. foreach ($parents as $parent) {
  67. $this->addToBatch($parent, $time, $sizeDifference);
  68. }
  69. return;
  70. }
  71. $parentHashes = array_map('md5', $parents);
  72. $etag = uniqid(); // since we give all folders the same etag we don't ask the storage for the etag
  73. $builder = $this->connection->getQueryBuilder();
  74. $hashParams = array_map(function ($hash) use ($builder) {
  75. return $builder->expr()->literal($hash);
  76. }, $parentHashes);
  77. $builder->update('filecache')
  78. ->set('mtime', $builder->createFunction('GREATEST(' . $builder->getColumnName('mtime') . ', ' . $builder->createNamedParameter((int)$time, IQueryBuilder::PARAM_INT) . ')'))
  79. ->set('etag', $builder->createNamedParameter($etag, IQueryBuilder::PARAM_STR))
  80. ->where($builder->expr()->eq('storage', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
  81. ->andWhere($builder->expr()->in('path_hash', $hashParams));
  82. $builder->execute();
  83. if ($sizeDifference !== 0) {
  84. // we need to do size separably so we can ignore entries with uncalculated size
  85. $builder = $this->connection->getQueryBuilder();
  86. $builder->update('filecache')
  87. ->set('size', $builder->func()->add('size', $builder->createNamedParameter($sizeDifference)))
  88. ->where($builder->expr()->eq('storage', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
  89. ->andWhere($builder->expr()->in('path_hash', $hashParams))
  90. ->andWhere($builder->expr()->gt('size', $builder->expr()->literal(-1, IQueryBuilder::PARAM_INT)));
  91. $builder->execute();
  92. }
  93. }
  94. protected function getParents($path) {
  95. $parts = explode('/', $path);
  96. $parent = '';
  97. $parents = [];
  98. foreach ($parts as $part) {
  99. $parents[] = $parent;
  100. $parent = trim($parent . '/' . $part, '/');
  101. }
  102. return $parents;
  103. }
  104. /**
  105. * Mark the beginning of a propagation batch
  106. *
  107. * Note that not all cache setups support propagation in which case this will be a noop
  108. *
  109. * Batching for cache setups that do support it has to be explicit since the cache state is not fully consistent
  110. * before the batch is committed.
  111. */
  112. public function beginBatch() {
  113. $this->inBatch = true;
  114. }
  115. private function addToBatch($internalPath, $time, $sizeDifference) {
  116. if (!isset($this->batch[$internalPath])) {
  117. $this->batch[$internalPath] = [
  118. 'hash' => md5($internalPath),
  119. 'time' => $time,
  120. 'size' => $sizeDifference
  121. ];
  122. } else {
  123. $this->batch[$internalPath]['size'] += $sizeDifference;
  124. if ($time > $this->batch[$internalPath]['time']) {
  125. $this->batch[$internalPath]['time'] = $time;
  126. }
  127. }
  128. }
  129. /**
  130. * Commit the active propagation batch
  131. * @suppress SqlInjectionChecker
  132. */
  133. public function commitBatch() {
  134. if (!$this->inBatch) {
  135. throw new \BadMethodCallException('Not in batch');
  136. }
  137. $this->inBatch = false;
  138. $this->connection->beginTransaction();
  139. $query = $this->connection->getQueryBuilder();
  140. $storageId = (int)$this->storage->getStorageCache()->getNumericId();
  141. $query->update('filecache')
  142. ->set('mtime', $query->createFunction('GREATEST(' . $query->getColumnName('mtime') . ', ' . $query->createParameter('time') . ')'))
  143. ->set('etag', $query->expr()->literal(uniqid()))
  144. ->where($query->expr()->eq('storage', $query->expr()->literal($storageId, IQueryBuilder::PARAM_INT)))
  145. ->andWhere($query->expr()->eq('path_hash', $query->createParameter('hash')));
  146. $sizeQuery = $this->connection->getQueryBuilder();
  147. $sizeQuery->update('filecache')
  148. ->set('size', $sizeQuery->func()->add('size', $sizeQuery->createParameter('size')))
  149. ->where($query->expr()->eq('storage', $query->expr()->literal($storageId, IQueryBuilder::PARAM_INT)))
  150. ->andWhere($query->expr()->eq('path_hash', $query->createParameter('hash')))
  151. ->andWhere($sizeQuery->expr()->gt('size', $sizeQuery->expr()->literal(-1, IQueryBuilder::PARAM_INT)));
  152. foreach ($this->batch as $item) {
  153. $query->setParameter('time', $item['time'], IQueryBuilder::PARAM_INT);
  154. $query->setParameter('hash', $item['hash']);
  155. $query->execute();
  156. if ($item['size']) {
  157. $sizeQuery->setParameter('size', $item['size'], IQueryBuilder::PARAM_INT);
  158. $sizeQuery->setParameter('hash', $item['hash']);
  159. $sizeQuery->execute();
  160. }
  161. }
  162. $this->batch = [];
  163. $this->connection->commit();
  164. }
  165. }