calculate versions size per user

This commit is contained in:
Björn Schießle 2013-02-21 12:20:29 +01:00
parent c8c0e72e73
commit c24ec867f9
3 changed files with 80 additions and 10 deletions

View file

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<database>
<name>*dbname*</name>
<create>true</create>
<overwrite>false</overwrite>
<charset>utf8</charset>
<table>
<name>*dbprefix*files_versions</name>
<declaration>
<field>
<name>user</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>50</length>
</field>
<field>
<name>size</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>50</length>
</field>
</declaration>
</table>
</database>

View file

@ -1 +1 @@
1.0.2
1.0.3

View file

@ -45,6 +45,37 @@ class Storage {
return array($uid, $filename);
}
/**
* get current size of all versions from a given user
*
* @param $user user who owns the versions
* @return mixed versions size or false if no versions size is stored
*/
private static function getVersionsSize($user) {
$query = \OC_DB::prepare('SELECT size FROM *PREFIX*files_versions WHERE user=?');
$result = $query->execute(array($user))->fetchAll();
if ($result) {
return $result[0]['size'];
}
return false;
}
/**
* write to the database how much space is in use for versions
*
* @param $user owner of the versions
* @param $size size of the versions
*/
private static function setVersionsSize($user, $size) {
if ( self::getVersionsSize($user) === false) {
$query = \OC_DB::prepare('INSERT INTO *PREFIX*files_versions (size, user) VALUES (?, ?)');
}else {
$query = \OC_DB::prepare('UPDATE *PREFIX*files_versions SET size=? WHERE user=?');
}
$query->execute(array($size, $user));
}
/**
* store a new version of a file.
*/
@ -73,17 +104,19 @@ class Storage {
}
// store a new version of a file
$result = $users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename));
if ( ($versionsSize = \OCP\Config::getAppValue('files_versions', 'size')) === null ) {
$users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename));
$versionsSize = self::getVersionsSize($uid);
if ( $versionsSize === false || $versionSize < 0 ) {
$versionsSize = self::calculateSize($uid);
}
$versionsSize += $users_view->filesize('files'.$filename);
// expire old revisions if necessary
$newSize = self::expire($filename, $versionsSize);
if ( $newSize != $versionsSize ) {
\OCP\Config::setAppValue('files_versions', 'size', $versionsSize);
self::setVersionsSize($uid, $newSize);
}
}
}
@ -98,14 +131,15 @@ class Storage {
$abs_path = \OCP\Config::getSystemValue('datadirectory').$versions_fileview->getAbsolutePath('').$filename.'.v';
if( ($versions = self::getVersions($uid, $filename)) ) {
if ( ($versionsSize = \OCP\Config::getAppValue('files_versions', 'size')) === null ) {
$versionsSize = self::getVersionsSize($uid);
if ( $versionsSize === false || $versionsSize < 0 ) {
$versionsSize = self::calculateSize($uid);
}
foreach ($versions as $v) {
unlink($abs_path . $v['version']);
$versionsSize -= $v['size'];
}
\OCP\Config::setAppValue('files_versions', 'size', $versionsSize);
self::setVersionsSize($uid, $versionsSize);
}
}
@ -314,12 +348,13 @@ class Storage {
$quota = \OCP\Util::computerFileSize(\OC_Appconfig::getValue('files', 'default_quota'));
}
if ( $quota == null ) {
$quota = \OC\Files\Filesystem::free_space('/');
$quota = \OC\Files\Filesystem::free_space('/') / count(\OCP\User::getUsers());
}
// make sure that we have the current size of the version history
if ( $versionsSize === null ) {
if ( ($versionsSize = \OCP\Config::getAppValue('files_versions', 'size')) === null ) {
$versionsSize = self::getVersionsSize($uid);
if ( $versionsSize === false || $versionsSize < 0 ) {
$versionsSize = self::calculateSize($uid);
}
}