-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Store storage availability in database #13641
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,27 +43,25 @@ class Storage { | |
|
||
/** | ||
* @param \OC\Files\Storage\Storage|string $storage | ||
* @param bool $isAvailable | ||
* @throws \RuntimeException | ||
*/ | ||
public function __construct($storage) { | ||
public function __construct($storage, $isAvailable = true) { | ||
if ($storage instanceof \OC\Files\Storage\Storage) { | ||
$this->storageId = $storage->getId(); | ||
} else { | ||
$this->storageId = $storage; | ||
} | ||
$this->storageId = self::adjustStorageId($this->storageId); | ||
|
||
$sql = 'SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?'; | ||
$result = \OC_DB::executeAudited($sql, array($this->storageId)); | ||
if ($row = $result->fetchRow()) { | ||
if ($row = self::getStorageById($this->storageId)) { | ||
$this->numericId = $row['numeric_id']; | ||
} else { | ||
$connection = \OC_DB::getConnection(); | ||
if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId])) { | ||
if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $isAvailable])) { | ||
$this->numericId = \OC_DB::insertid('*PREFIX*storages'); | ||
} else { | ||
$result = \OC_DB::executeAudited($sql, array($this->storageId)); | ||
if ($row = $result->fetchRow()) { | ||
if ($row = self::getStorageById($this->storageId)) { | ||
$this->numericId = $row['numeric_id']; | ||
} else { | ||
throw new \RuntimeException('Storage could neither be inserted nor be selected from the database'); | ||
|
@@ -72,6 +70,16 @@ public function __construct($storage) { | |
} | ||
} | ||
|
||
/** | ||
* @param string $storageId | ||
* @return array|null | ||
*/ | ||
public static function getStorageById($storageId) { | ||
$sql = 'SELECT * FROM `*PREFIX*storages` WHERE `id` = ?'; | ||
$result = \OC_DB::executeAudited($sql, array($storageId)); | ||
return $result->fetchRow(); | ||
} | ||
|
||
/** | ||
* Adjusts the storage id to use md5 if too long | ||
* @param string $storageId storage id | ||
|
@@ -120,15 +128,35 @@ public static function getStorageId($numericId) { | |
public static function getNumericStorageId($storageId) { | ||
$storageId = self::adjustStorageId($storageId); | ||
|
||
$sql = 'SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?'; | ||
$result = \OC_DB::executeAudited($sql, array($storageId)); | ||
if ($row = $result->fetchRow()) { | ||
if ($row = self::getStorageById($storageId)) { | ||
return $row['numeric_id']; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* @return array|null [ available, last_checked ] | ||
*/ | ||
public function getAvailability() { | ||
if ($row = self::getStorageById($this->storageId)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it's not possible to retrieve + cache this together when reading the full storage entry ? (to avoid extra SQL calls) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The caching would be done in |
||
return [ | ||
'available' => $row['available'], | ||
'last_checked' => $row['last_checked'] | ||
]; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* @param bool $isAvailable | ||
*/ | ||
public function setAvailability($isAvailable) { | ||
$sql = 'UPDATE `*PREFIX*storages` SET `available` = ?, `last_checked` = ? WHERE `id` = ?'; | ||
\OC_DB::executeAudited($sql, array($isAvailable, time(), $this->storageId)); | ||
} | ||
|
||
/** | ||
* Check if a string storage id is known | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
use \OC\Files\Filesystem; | ||
use OC\Files\Storage\StorageFactory; | ||
use OC\Files\Storage\Storage; | ||
use OC\Files\Storage\Wrapper\Wrapper; | ||
use OCP\Files\Mount\IMountPoint; | ||
|
||
class MountPoint implements IMountPoint { | ||
|
@@ -92,7 +93,11 @@ public function __construct($storage, $mountpoint, $arguments = null, $loader = | |
$this->mountPoint = $mountpoint; | ||
if ($storage instanceof Storage) { | ||
$this->class = get_class($storage); | ||
$this->storage = $this->loader->wrap($this, $storage); | ||
$this->storage = $storage; | ||
// only wrap if not already wrapped | ||
if (!($this->storage instanceof Wrapper)) { | ||
$this->storage = $this->loader->wrap($this, $this->storage); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this required by this PR or is an additional fix ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without this fix, unit tests will fail, since our unit tests attempt to re-wrap storages sometimes causing function nesting limit failures. In the actual codebase it makes no difference, since we never pass a wrapped storage to be re-wrapped. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, this is a poor fix. There may be usecases where instead of a Storage being passed in, a Wrapper is (aka pre-wrapped storage) but still needs to be wrapped with the other storage wrappers too. |
||
} | ||
} else { | ||
// Update old classes to new namespace | ||
if (strpos($storage, 'OC_Filestorage_') !== false) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd merge this into one function where all information is returned. With the current design two queries will be fired to get availability and last checked