diff --git a/classes/sss_file_pusher.php b/classes/sss_file_pusher.php index 1d202eab..0b4f3492 100644 --- a/classes/sss_file_pusher.php +++ b/classes/sss_file_pusher.php @@ -43,49 +43,45 @@ public function __construct($client, $filesystem, $config) { $this->maxruntime = 60; // Seconds. } + private function get_push_candidate_content_hashes() { + global $DB; + $sql = 'SELECT F.contenthash, MAX(F.filesize), MIN(F.timecreated) + FROM mdl_files F + LEFT JOIN mdl_tool_sssfs_filestate SF on F.contenthash = SF.contenthash + GROUP BY F.contenthash, F.filesize, SF.state + HAVING MIN(F.timecreated) < ? AND MAX(F.filesize) > ? + AND (SF.state IS NULL OR SF.state = ?)'; + + $maxcreatedtimestamp = time() - $this->minimumage; + + $params = array($maxcreatedtimestamp, $this->sizethreshold, SSS_FILE_STATE_LOCAL); + + $contenthashes = $DB->get_records_sql($sql, $params); + + return $contenthashes; + } + public function push() { global $DB; $finishtime = time() + $this->maxruntime; - // This Should filter down the file list the most so we do this first. - // TODO: refactor into get_push_candidate_content_hashes. - $contenthashestopush = $this->get_content_hashes_over_threshold($this->sizethreshold); - - $ssscontenthashes = $this->get_content_hashes_in_sss(); + $contenthashestopush = $this->get_push_candidate_content_hashes(); foreach ($contenthashestopush as $contenthash) { if (time() > $finishtime) { break; } - if (in_array($contenthash, $ssscontenthashes)) { - continue; - } - - $filecontent = $this->filesystem->get_content_from_hash($contenthash); + $filecontent = $this->filesystem->get_content_from_hash($contenthash->contenthash); if ($filecontent) { // TODO: deal with response. - $response = $this->client->push_file($contenthash, $filecontent); - log_file_state($contenthash, SSS_FILE_STATE_DUPLICATED); + $response = $this->client->push_file($contenthash->contenthash, $filecontent); + log_file_state($contenthash->contenthash, SSS_FILE_STATE_DUPLICATED); } } } - - private function get_content_hashes_over_threshold($threshold) { - global $DB; - $sql = "SELECT DISTINCT contenthash FROM {files} WHERE filesize > ?"; - $contenthashes = $DB->get_fieldset_sql($sql, array($threshold)); - return $contenthashes; - } - - private function get_content_hashes_in_sss() { - global $DB; - $sql = 'SELECT contenthash FROM {tool_sssfs_filestate} WHERE STATE in (?, ?)'; - $ssscontenthashes = $DB->get_fieldset_sql($sql, array(SSS_FILE_STATE_DUPLICATED, SSS_FILE_STATE_EXTERNAL)); - return $ssscontenthashes; - } } diff --git a/lib.php b/lib.php index 93cc2a86..41c8d0b2 100644 --- a/lib.php +++ b/lib.php @@ -55,7 +55,7 @@ function save_config_data($data) { $config->secret = $data->secret; $config->bucket = $data->bucket; $config->region = $data->region; - $config->sizethreshold = $data->sizethreshold; + $config->sizethreshold = $data->sizethreshold * 1024; // Convert from kb. $config->minimumage = $data->minimumage; $config->consistancydelay = $data->consistancydelay; $config->logginglocation = $data->logginglocation;