Propagator.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. * @param \OC\Files\Storage\Storage $storage
  43. * @param IDBConnection $connection
  44. */
  45. public function __construct(\OC\Files\Storage\Storage $storage, IDBConnection $connection) {
  46. $this->storage = $storage;
  47. $this->connection = $connection;
  48. }
  49. /**
  50. * @param string $internalPath
  51. * @param int $time
  52. * @param int $sizeDifference number of bytes the file has grown
  53. * @suppress SqlInjectionChecker
  54. */
  55. public function propagateChange($internalPath, $time, $sizeDifference = 0) {
  56. $storageId = (int)$this->storage->getStorageCache()->getNumericId();
  57. $parents = $this->getParents($internalPath);
  58. if ($this->inBatch) {
  59. foreach ($parents as $parent) {
  60. $this->addToBatch($parent, $time, $sizeDifference);
  61. }
  62. return;
  63. }
  64. $parentHashes = array_map('md5', $parents);
  65. $etag = uniqid(); // since we give all folders the same etag we don't ask the storage for the etag
  66. $builder = $this->connection->getQueryBuilder();
  67. $hashParams = array_map(function ($hash) use ($builder) {
  68. return $builder->expr()->literal($hash);
  69. }, $parentHashes);
  70. $builder->update('filecache')
  71. ->set('mtime', $builder->createFunction('GREATEST(`mtime`, ' . $builder->createNamedParameter((int)$time, IQueryBuilder::PARAM_INT) . ')'))
  72. ->set('etag', $builder->createNamedParameter($etag, IQueryBuilder::PARAM_STR))
  73. ->where($builder->expr()->eq('storage', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
  74. ->andWhere($builder->expr()->in('path_hash', $hashParams));
  75. $builder->execute();
  76. if ($sizeDifference !== 0) {
  77. // we need to do size separably so we can ignore entries with uncalculated size
  78. $builder = $this->connection->getQueryBuilder();
  79. $builder->update('filecache')
  80. ->set('size', $builder->createFunction('`size` + ' . $builder->createNamedParameter($sizeDifference)))
  81. ->where($builder->expr()->eq('storage', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
  82. ->andWhere($builder->expr()->in('path_hash', $hashParams))
  83. ->andWhere($builder->expr()->gt('size', $builder->expr()->literal(-1, IQueryBuilder::PARAM_INT)));
  84. }
  85. $builder->execute();
  86. }
  87. protected function getParents($path) {
  88. $parts = explode('/', $path);
  89. $parent = '';
  90. $parents = [];
  91. foreach ($parts as $part) {
  92. $parents[] = $parent;
  93. $parent = trim($parent . '/' . $part, '/');
  94. }
  95. return $parents;
  96. }
  97. /**
  98. * Mark the beginning of a propagation batch
  99. *
  100. * Note that not all cache setups support propagation in which case this will be a noop
  101. *
  102. * Batching for cache setups that do support it has to be explicit since the cache state is not fully consistent
  103. * before the batch is committed.
  104. */
  105. public function beginBatch() {
  106. $this->inBatch = true;
  107. }
  108. private function addToBatch($internalPath, $time, $sizeDifference) {
  109. if (!isset($this->batch[$internalPath])) {
  110. $this->batch[$internalPath] = [
  111. 'hash' => md5($internalPath),
  112. 'time' => $time,
  113. 'size' => $sizeDifference
  114. ];
  115. } else {
  116. $this->batch[$internalPath]['size'] += $sizeDifference;
  117. if ($time > $this->batch[$internalPath]['time']) {
  118. $this->batch[$internalPath]['time'] = $time;
  119. }
  120. }
  121. }
  122. /**
  123. * Commit the active propagation batch
  124. * @suppress SqlInjectionChecker
  125. */
  126. public function commitBatch() {
  127. if (!$this->inBatch) {
  128. throw new \BadMethodCallException('Not in batch');
  129. }
  130. $this->inBatch = false;
  131. $this->connection->beginTransaction();
  132. $query = $this->connection->getQueryBuilder();
  133. $storageId = (int)$this->storage->getStorageCache()->getNumericId();
  134. $query->update('filecache')
  135. ->set('mtime', $query->createFunction('GREATEST(`mtime`, ' . $query->createParameter('time') . ')'))
  136. ->set('etag', $query->expr()->literal(uniqid()))
  137. ->where($query->expr()->eq('storage', $query->expr()->literal($storageId, IQueryBuilder::PARAM_INT)))
  138. ->andWhere($query->expr()->eq('path_hash', $query->createParameter('hash')));
  139. $sizeQuery = $this->connection->getQueryBuilder();
  140. $sizeQuery->update('filecache')
  141. ->set('size', $sizeQuery->createFunction('`size` + ' . $sizeQuery->createParameter('size')))
  142. ->where($query->expr()->eq('storage', $query->expr()->literal($storageId, IQueryBuilder::PARAM_INT)))
  143. ->andWhere($query->expr()->eq('path_hash', $query->createParameter('hash')))
  144. ->andWhere($sizeQuery->expr()->gt('size', $sizeQuery->expr()->literal(-1, IQueryBuilder::PARAM_INT)));
  145. foreach ($this->batch as $item) {
  146. $query->setParameter('time', $item['time'], IQueryBuilder::PARAM_INT);
  147. $query->setParameter('hash', $item['hash']);
  148. $query->execute();
  149. if ($item['size']) {
  150. $sizeQuery->setParameter('size', $item['size'], IQueryBuilder::PARAM_INT);
  151. $sizeQuery->setParameter('hash', $item['hash']);
  152. $sizeQuery->execute();
  153. }
  154. }
  155. $this->batch = [];
  156. $this->connection->commit();
  157. }
  158. }