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

Move all child entries in the cache in a single query #13956

Closed
wants to merge 3 commits 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/private/db/adapteroci8.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function fixupStatement($statement) {
$statement = str_replace('`', '"', $statement);
$statement = str_ireplace('NOW()', 'CURRENT_TIMESTAMP', $statement);
$statement = str_ireplace('UNIX_TIMESTAMP()', self::UNIX_TIMESTAMP_REPLACEMENT, $statement);
$statement = preg_replace('/MD5\(([^)]+)\)/i', 'LOWER(DBMS_OBFUSCATION_TOOLKIT.md5 (input => UTL_RAW.cast_to_raw($1)))', $statement);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets all take a moment to appreciate oracle's sql dialect

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol

return $statement;
}
}
1 change: 1 addition & 0 deletions lib/private/db/adaptersqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function fixupStatement($statement) {
$statement = str_replace( '`', '"', $statement );
$statement = str_ireplace( 'NOW()', 'datetime(\'now\')', $statement );
$statement = str_ireplace( 'UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $statement );
$statement = preg_replace('/CONCAT\(([^)]+?), ?([^)]+?)\)/i', '$1 || $2', $statement);
return $statement;
}

Expand Down
3 changes: 3 additions & 0 deletions lib/private/db/sqlitesessioninit.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function postConnect(ConnectionEventArgs $args) {
$sensitive = ($this->caseSensitiveLike) ? 'true' : 'false';
$args->getConnection()->executeUpdate('PRAGMA case_sensitive_like = ' . $sensitive);
$args->getConnection()->executeUpdate('PRAGMA journal_mode = ' . $this->journalMode);
/** @var \PDO $pdo */
$pdo = $args->getConnection()->getWrappedConnection();
$pdo->sqliteCreateFunction('md5', 'md5', 1);
}

public function getSubscribedEvents() {
Expand Down
32 changes: 13 additions & 19 deletions lib/private/files/cache/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function get($file) {
$where = 'WHERE `fileid` = ?';
$params = array($file);
}
$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
$sql = 'SELECT `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not used?

`storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum`
FROM `*PREFIX*filecache` ' . $where;
$result = $this->connection->executeQuery($sql, $params);
Expand Down Expand Up @@ -505,27 +505,21 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath);
list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath);

// sql for final update
$moveSql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` =? WHERE `fileid` = ?';

$this->connection->beginTransaction();
if ($sourceData['mimetype'] === 'httpd/unix-directory') {
//find all child entries
$sql = 'SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path` LIKE ?';
$result = $this->connection->executeQuery($sql, [$sourceStorageId, $this->connection->escapeLikeParameter($sourcePath) . '/%']);
$childEntries = $result->fetchAll();
//update all child entries
$sourceLength = strlen($sourcePath);
$this->connection->beginTransaction();
$query = $this->connection->prepare('UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ? WHERE `fileid` = ?');

foreach ($childEntries as $child) {
$newTargetPath = $targetPath . substr($child['path'], $sourceLength);
$query->execute([$targetStorageId, $newTargetPath, md5($newTargetPath), $child['fileid']]);
}
$this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
$this->connection->commit();
} else {
$this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
$query = $this->connection->prepare('UPDATE `*PREFIX*filecache` SET
`storage` = ?,
`path_hash` = MD5(CONCAT(?, SUBSTR(`path`, ?))),
`path` = CONCAT(?, SUBSTR(`path`, ?))
WHERE `storage` = ? AND `path` LIKE ?');
$query->execute([$targetStorageId, $targetPath, $sourceLength + 1,$targetPath, $sourceLength + 1, $sourceStorageId, $this->connection->escapeLikeParameter($sourcePath) . '/%']);
}

$sql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` = ? WHERE `fileid` = ?';
$this->connection->executeQuery($sql, array($targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId));
$this->connection->commit();
} else {
$this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,15 @@ public function testILIKEWildcard() {
$result = $query->execute(array('%ba%'));
$this->assertCount(1, $result->fetchAll());
}

public function testMD5() {
$table = "*PREFIX*{$this->table2}";

$query = OC_DB::prepare("INSERT INTO `$table` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)");
$query->execute(array('foobar', 'foo', 'bar'));

$query = OC_DB::prepare("SELECT MD5(`fullname`) FROM `$table` WHERE `fullname` LIKE ?");
$result = $query->execute(array('foobar'));
$this->assertEquals(md5('foobar'), $result->fetchOne());
}
}
32 changes: 23 additions & 9 deletions tests/lib/files/cache/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public function testPartial() {
* @dataProvider folderDataProvider
*/
public function testFolder($folder) {
$file2 = $folder.'/bar';
$file3 = $folder.'/foo';
$file2 = $folder . '/bar';
$file3 = $folder . '/foo';
$data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
$fileData = array();
$fileData['bar'] = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file');
Expand All @@ -129,7 +129,7 @@ public function testFolder($folder) {
}
}

$file4 = $folder.'/unkownSize';
$file4 = $folder . '/unkownSize';
$fileData['unkownSize'] = array('size' => -1, 'mtime' => 25, 'mimetype' => 'foo/file');
$this->cache->put($file4, $fileData['unkownSize']);

Expand All @@ -146,8 +146,8 @@ public function testFolder($folder) {
$this->assertEquals(0, $this->cache->calculateFolderSize($folder));

$this->cache->remove($folder);
$this->assertFalse($this->cache->inCache($folder.'/foo'));
$this->assertFalse($this->cache->inCache($folder.'/bar'));
$this->assertFalse($this->cache->inCache($folder . '/foo'));
$this->assertFalse($this->cache->inCache($folder . '/bar'));
}

public function testRemoveRecursive() {
Expand All @@ -156,7 +156,7 @@ public function testRemoveRecursive() {
$folders = ['folder', 'folder/subfolder', 'folder/sub2', 'folder/sub2/sub3'];
$files = ['folder/foo.txt', 'folder/bar.txt', 'folder/subfolder/asd.txt', 'folder/sub2/qwerty.txt', 'folder/sub2/sub3/foo.txt'];

foreach($folders as $folder){
foreach ($folders as $folder) {
$this->cache->put($folder, $folderData);
}
foreach ($files as $file) {
Expand Down Expand Up @@ -351,19 +351,25 @@ function testSearchByTag() {

$this->assertEquals(2, count($results));

usort($results, function($value1, $value2) { return $value1['name'] >= $value2['name']; });
usort($results, function ($value1, $value2) {
return $value1['name'] >= $value2['name'];
});

$this->assertEquals('folder', $results[0]['name']);
$this->assertEquals('foo', $results[1]['name']);

// use tag id
$tags = $tagManager->getTagsForUser($userId);
$this->assertNotEmpty($tags);
$tags = array_filter($tags, function($tag) { return $tag->getName() === 'tag2'; });
$tags = array_filter($tags, function ($tag) {
return $tag->getName() === 'tag2';
});
$results = $this->cache->searchByTag(current($tags)->getId(), $userId);
$this->assertEquals(3, count($results));

usort($results, function($value1, $value2) { return $value1['name'] >= $value2['name']; });
usort($results, function ($value1, $value2) {
return $value1['name'] >= $value2['name'];
});

$this->assertEquals('folder', $results[0]['name']);
$this->assertEquals('foo2', $results[1]['name']);
Expand Down Expand Up @@ -399,8 +405,16 @@ function testMove() {
$this->cache2->put($file4, $data);
$this->cache2->put($file5, $data);

$id1 = (int)$this->cache->getId('folder/foo/1');
$id2 = (int)$this->cache->getId('folder/foo/2');

$this->cache->move('folder/foo', 'folder/foobar');

$this->assertEquals('folder/foobar/1', $this->cache->get($id1)['path']);
$this->assertEquals('folder/foobar/2', $this->cache->get($id2)['path']);
$this->assertEquals(md5('folder/foobar/1'), $this->cache->get($id1)['path_hash']);
$this->assertEquals(md5('folder/foobar/2'), $this->cache->get($id2)['path_hash']);

$this->assertFalse($this->cache->inCache('folder/foo'));
$this->assertFalse($this->cache->inCache('folder/foo/1'));
$this->assertFalse($this->cache->inCache('folder/foo/2'));
Expand Down