Propagator.php 5.8 KB

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