Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[27/testing] improve di performance for cache #39902

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,7 @@
'OC\\Files\\AppData\\AppData' => $baseDir . '/lib/private/Files/AppData/AppData.php',
'OC\\Files\\AppData\\Factory' => $baseDir . '/lib/private/Files/AppData/Factory.php',
'OC\\Files\\Cache\\Cache' => $baseDir . '/lib/private/Files/Cache/Cache.php',
'OC\\Files\\Cache\\CacheDependencies' => $baseDir . '/lib/private/Files/Cache/CacheDependencies.php',
'OC\\Files\\Cache\\CacheEntry' => $baseDir . '/lib/private/Files/Cache/CacheEntry.php',
'OC\\Files\\Cache\\CacheQueryBuilder' => $baseDir . '/lib/private/Files/Cache/CacheQueryBuilder.php',
'OC\\Files\\Cache\\FailedCache' => $baseDir . '/lib/private/Files/Cache/FailedCache.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Files\\AppData\\AppData' => __DIR__ . '/../../..' . '/lib/private/Files/AppData/AppData.php',
'OC\\Files\\AppData\\Factory' => __DIR__ . '/../../..' . '/lib/private/Files/AppData/Factory.php',
'OC\\Files\\Cache\\Cache' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/Cache.php',
'OC\\Files\\Cache\\CacheDependencies' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/CacheDependencies.php',
'OC\\Files\\Cache\\CacheEntry' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/CacheEntry.php',
'OC\\Files\\Cache\\CacheQueryBuilder' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/CacheQueryBuilder.php',
'OC\\Files\\Cache\\FailedCache' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/FailedCache.php',
Expand Down
76 changes: 33 additions & 43 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchQuery;
use OC\Files\Storage\Wrapper\Encryption;
use OC\SystemConfig;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\CacheEntryInsertedEvent;
Expand Down Expand Up @@ -81,61 +82,50 @@ class Cache implements ICache {
/**
* @var array partial data for the cache
*/
protected $partial = [];

/**
* @var string
*/
protected $storageId;

private $storage;

/**
* @var Storage $storageCache
*/
protected $storageCache;

/** @var IMimeTypeLoader */
protected $mimetypeLoader;

/**
* @var IDBConnection
*/
protected $connection;

/**
* @var IEventDispatcher
*/
protected $eventDispatcher;

/** @var QuerySearchHelper */
protected $querySearchHelper;

/**
* @param IStorage $storage
*/
public function __construct(IStorage $storage) {
protected array $partial = [];
protected string $storageId;
protected Storage $storageCache;
protected IMimeTypeLoader$mimetypeLoader;
protected IDBConnection $connection;
protected SystemConfig $systemConfig;
protected LoggerInterface $logger;
protected QuerySearchHelper $querySearchHelper;
protected IEventDispatcher $eventDispatcher;

public function __construct(
private IStorage $storage,
// this constructor is used in to many pleases to easily do proper di
// so instead we group it all together
CacheDependencies $dependencies = null,
) {
$this->storageId = $storage->getId();
$this->storage = $storage;
if (strlen($this->storageId) > 64) {
$this->storageId = md5($this->storageId);
}

$this->storageCache = new Storage($storage);
$this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
$this->connection = \OC::$server->getDatabaseConnection();
$this->eventDispatcher = \OC::$server->get(IEventDispatcher::class);
$this->querySearchHelper = \OC::$server->query(QuerySearchHelper::class);
if (!$dependencies) {
$dependencies = \OC::$server->get(CacheDependencies::class);
}
$this->storageCache = new Storage($this->storage, true, $dependencies->getConnection());
$this->mimetypeLoader = $dependencies->getMimeTypeLoader();
$this->connection = $dependencies->getConnection();
$this->systemConfig = $dependencies->getSystemConfig();
$this->logger = $dependencies->getLogger();
$this->querySearchHelper = $dependencies->getQuerySearchHelper();
$this->eventDispatcher = $dependencies->getEventDispatcher();
}

protected function getQueryBuilder() {
return new CacheQueryBuilder(
$this->connection,
\OC::$server->getSystemConfig(),
\OC::$server->get(LoggerInterface::class)
$this->systemConfig,
$this->logger,
);
}

public function getStorageCache(): Storage {
return $this->storageCache;
}

/**
* Get the numeric storage id for this cache's storage
*
Expand Down
45 changes: 45 additions & 0 deletions lib/private/Files/Cache/CacheDependencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace OC\Files\Cache;

use OC\SystemConfig;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IMimeTypeLoader;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;

class CacheDependencies {
public function __construct(
private IMimeTypeLoader $mimeTypeLoader,
private IDBConnection $connection,
private IEventDispatcher $eventDispatcher,
private QuerySearchHelper $querySearchHelper,
private SystemConfig $systemConfig,
private LoggerInterface $logger,
) {
}

public function getMimeTypeLoader(): IMimeTypeLoader {
return $this->mimeTypeLoader;
}

public function getConnection(): IDBConnection {
return $this->connection;
}

public function getEventDispatcher(): IEventDispatcher {
return $this->eventDispatcher;
}

public function getQuerySearchHelper(): QuerySearchHelper {
return $this->querySearchHelper;
}

public function getSystemConfig(): SystemConfig {
return $this->systemConfig;
}

public function getLogger(): LoggerInterface {
return $this->logger;
}
}
4 changes: 2 additions & 2 deletions lib/private/Files/Cache/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\Storage\IStorage;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -65,7 +66,7 @@ public static function getGlobalCache() {
* @param bool $isAvailable
* @throws \RuntimeException
*/
public function __construct($storage, $isAvailable = true) {
public function __construct($storage, $isAvailable, IDBConnection $connection) {
if ($storage instanceof IStorage) {
$this->storageId = $storage->getId();
} else {
Expand All @@ -76,7 +77,6 @@ public function __construct($storage, $isAvailable = true) {
if ($row = self::getStorageById($this->storageId)) {
$this->numericId = (int)$row['numeric_id'];
} else {
$connection = \OC::$server->getDatabaseConnection();
$available = $isAvailable ? 1 : 0;
if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) {
$this->numericId = $connection->lastInsertId('*PREFIX*storages');
Expand Down
2 changes: 0 additions & 2 deletions lib/private/Files/Cache/Wrapper/CacheJail.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class CacheJail extends CacheWrapper {
public function __construct($cache, $root) {
parent::__construct($cache);
$this->root = $root;
$this->connection = \OC::$server->getDatabaseConnection();
$this->mimetypeLoader = \OC::$server->getMimeTypeLoader();

if ($cache instanceof CacheJail) {
$this->unjailedRoot = $cache->getSourcePath($root);
Expand Down
19 changes: 11 additions & 8 deletions lib/private/Files/Storage/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
namespace OC\Files\Storage;

use OC\Files\Cache\Cache;
use OC\Files\Cache\CacheDependencies;
use OC\Files\Cache\Propagator;
use OC\Files\Cache\Scanner;
use OC\Files\Cache\Updater;
Expand Down Expand Up @@ -338,12 +339,20 @@ public function hasUpdated($path, $time) {
return $this->filemtime($path) > $time;
}

protected function getCacheDependencies(): CacheDependencies {
static $dependencies = null;
if (!$dependencies) {
$dependencies = \OC::$server->get(CacheDependencies::class);
}
return $dependencies;
}

public function getCache($path = '', $storage = null) {
if (!$storage) {
$storage = $this;
}
if (!isset($storage->cache)) {
$storage->cache = new Cache($storage);
$storage->cache = new Cache($storage, $this->getCacheDependencies());
}
return $storage->cache;
}
Expand Down Expand Up @@ -398,13 +407,7 @@ public function getUpdater($storage = null) {
}

public function getStorageCache($storage = null) {
if (!$storage) {
$storage = $this;
}
if (!isset($this->storageCache)) {
$this->storageCache = new \OC\Files\Cache\Storage($storage);
}
return $this->storageCache;
return $this->getCache($storage)->getStorageCache();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Storage/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getCache($path = '', $storage = null) {
$storage = $this;
}
if (!isset($this->cache)) {
$this->cache = new \OC\Files\Cache\HomeCache($storage);
$this->cache = new \OC\Files\Cache\HomeCache($storage, $this->getCacheDependencies());
}
return $this->cache;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/Files/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use OCP\Files\GenericFileException;
use OCP\Files\Mount\IMountManager;
use OCP\Files\Storage\IStorage;
use OCP\IDBConnection;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use OCP\Share\IShare;
Expand Down Expand Up @@ -1590,7 +1591,7 @@ private function createTestMovableMountPoints($mountPoints) {
->getMock();
$storage->method('getId')->willReturn('non-null-id');
$storage->method('getStorageCache')->willReturnCallback(function () use ($storage) {
return new \OC\Files\Cache\Storage($storage);
return new \OC\Files\Cache\Storage($storage, false, \OC::$server->get(IDBConnection::class));
});

$mounts[] = $this->getMockBuilder(TestMoveableMountPoint::class)
Expand Down
Loading