summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2016-02-03 14:32:28 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2016-02-03 14:32:28 +0100
commitab1c79827e627a4d72194842f98b5bae1185a4b2 (patch)
treef9f1f2d0037123487d525dae8ceb9003c9bbe5d8 /lib
parent74abbbc0d62152a4482fde90ecc09a0a38e7b961 (diff)
parentf2bba59b79ab2595b78f7a28b0d5a95b80d6f40e (diff)
downloadnextcloud-server-ab1c79827e627a4d72194842f98b5bae1185a4b2.tar.gz
nextcloud-server-ab1c79827e627a4d72194842f98b5bae1185a4b2.zip
Merge pull request #22071 from owncloud/cache-split-insert
split cache->insert from cache->put
Diffstat (limited to 'lib')
-rw-r--r--lib/private/files/cache/cache.php91
-rw-r--r--lib/private/files/cache/wrapper/cachejail.php7
-rw-r--r--lib/private/files/cache/wrapper/cachewrapper.php23
-rw-r--r--lib/public/files/cache/icache.php13
4 files changed, 90 insertions, 44 deletions
diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php
index cbe48f21bd8..22b9f49e528 100644
--- a/lib/private/files/cache/cache.php
+++ b/lib/private/files/cache/cache.php
@@ -201,7 +201,7 @@ class Cache implements ICache {
}
/**
- * store meta data for a file or folder
+ * insert or update meta data for a file or folder
*
* @param string $file
* @param array $data
@@ -214,49 +214,62 @@ class Cache implements ICache {
$this->update($id, $data);
return $id;
} else {
- // normalize file
- $file = $this->normalize($file);
+ return $this->insert($file, $data);
+ }
+ }
- if (isset($this->partial[$file])) { //add any saved partial data
- $data = array_merge($this->partial[$file], $data);
- unset($this->partial[$file]);
- }
+ /**
+ * insert meta data for a new file or folder
+ *
+ * @param string $file
+ * @param array $data
+ *
+ * @return int file id
+ * @throws \RuntimeException
+ */
+ public function insert($file, array $data) {
+ // normalize file
+ $file = $this->normalize($file);
- $requiredFields = array('size', 'mtime', 'mimetype');
- foreach ($requiredFields as $field) {
- if (!isset($data[$field])) { //data not complete save as partial and return
- $this->partial[$file] = $data;
- return -1;
- }
- }
+ if (isset($this->partial[$file])) { //add any saved partial data
+ $data = array_merge($this->partial[$file], $data);
+ unset($this->partial[$file]);
+ }
- $data['path'] = $file;
- $data['parent'] = $this->getParentId($file);
- $data['name'] = \OC_Util::basename($file);
-
- list($queryParts, $params) = $this->buildParts($data);
- $queryParts[] = '`storage`';
- $params[] = $this->getNumericStorageId();
-
- $queryParts = array_map(function ($item) {
- return trim($item, "`");
- }, $queryParts);
- $values = array_combine($queryParts, $params);
- if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [
- 'storage',
- 'path_hash',
- ])
- ) {
- return (int)$this->connection->lastInsertId('*PREFIX*filecache');
+ $requiredFields = array('size', 'mtime', 'mimetype');
+ foreach ($requiredFields as $field) {
+ if (!isset($data[$field])) { //data not complete save as partial and return
+ $this->partial[$file] = $data;
+ return -1;
}
+ }
- // The file was created in the mean time
- if (($id = $this->getId($file)) > -1) {
- $this->update($id, $data);
- return $id;
- } else {
- throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.');
- }
+ $data['path'] = $file;
+ $data['parent'] = $this->getParentId($file);
+ $data['name'] = \OC_Util::basename($file);
+
+ list($queryParts, $params) = $this->buildParts($data);
+ $queryParts[] = '`storage`';
+ $params[] = $this->getNumericStorageId();
+
+ $queryParts = array_map(function ($item) {
+ return trim($item, "`");
+ }, $queryParts);
+ $values = array_combine($queryParts, $params);
+ if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [
+ 'storage',
+ 'path_hash',
+ ])
+ ) {
+ return (int)$this->connection->lastInsertId('*PREFIX*filecache');
+ }
+
+ // The file was created in the mean time
+ if (($id = $this->getId($file)) > -1) {
+ $this->update($id, $data);
+ return $id;
+ } else {
+ throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.');
}
}
diff --git a/lib/private/files/cache/wrapper/cachejail.php b/lib/private/files/cache/wrapper/cachejail.php
index 32bd3626fcb..868e63cdf81 100644
--- a/lib/private/files/cache/wrapper/cachejail.php
+++ b/lib/private/files/cache/wrapper/cachejail.php
@@ -95,15 +95,16 @@ class CacheJail extends CacheWrapper {
}
/**
- * store meta data for a file or folder
+ * insert meta data for a new file or folder
*
* @param string $file
* @param array $data
*
* @return int file id
+ * @throws \RuntimeException
*/
- public function put($file, array $data) {
- return $this->cache->put($this->getSourcePath($file), $data);
+ public function insert($file, array $data) {
+ return $this->cache->insert($this->getSourcePath($file), $data);
}
/**
diff --git a/lib/private/files/cache/wrapper/cachewrapper.php b/lib/private/files/cache/wrapper/cachewrapper.php
index 1ce4f028c75..4080883419e 100644
--- a/lib/private/files/cache/wrapper/cachewrapper.php
+++ b/lib/private/files/cache/wrapper/cachewrapper.php
@@ -90,15 +90,34 @@ class CacheWrapper extends Cache {
}
/**
- * store meta data for a file or folder
+ * insert or update meta data for a file or folder
*
* @param string $file
* @param array $data
*
* @return int file id
+ * @throws \RuntimeException
*/
public function put($file, array $data) {
- return $this->cache->put($file, $data);
+ if (($id = $this->getId($file)) > -1) {
+ $this->update($id, $data);
+ return $id;
+ } else {
+ return $this->insert($file, $data);
+ }
+ }
+
+ /**
+ * insert meta data for a new file or folder
+ *
+ * @param string $file
+ * @param array $data
+ *
+ * @return int file id
+ * @throws \RuntimeException
+ */
+ public function insert($file, array $data) {
+ return $this->cache->insert($file, $data);
}
/**
diff --git a/lib/public/files/cache/icache.php b/lib/public/files/cache/icache.php
index e80c6fa2cb0..4ffb298a9e2 100644
--- a/lib/public/files/cache/icache.php
+++ b/lib/public/files/cache/icache.php
@@ -76,6 +76,7 @@ interface ICache {
/**
* store meta data for a file or folder
+ * This will automatically call either insert or update depending on if the file exists
*
* @param string $file
* @param array $data
@@ -87,6 +88,18 @@ interface ICache {
public function put($file, array $data);
/**
+ * insert meta data for a new file or folder
+ *
+ * @param string $file
+ * @param array $data
+ *
+ * @return int file id
+ * @throws \RuntimeException
+ * @since 9.0.0
+ */
+ public function insert($file, array $data);
+
+ /**
* update the metadata of an existing file or folder in the cache
*
* @param int $id the fileid of the existing file or folder