diff options
Diffstat (limited to 'library/Director/Import/PurgeStrategy')
3 files changed, 132 insertions, 0 deletions
diff --git a/library/Director/Import/PurgeStrategy/ImportRunBasedPurgeStrategy.php b/library/Director/Import/PurgeStrategy/ImportRunBasedPurgeStrategy.php new file mode 100644 index 0000000..9f0e8ab --- /dev/null +++ b/library/Director/Import/PurgeStrategy/ImportRunBasedPurgeStrategy.php @@ -0,0 +1,90 @@ +<?php + +namespace Icinga\Module\Director\Import\PurgeStrategy; + +use Icinga\Module\Director\Import\SyncUtils; +use Icinga\Module\Director\Objects\ImportRun; +use Icinga\Module\Director\Objects\ImportSource; + +class ImportRunBasedPurgeStrategy extends PurgeStrategy +{ + public function listObjectsToPurge() + { + $remove = array(); + + foreach ($this->getSyncRule()->fetchInvolvedImportSources() as $source) { + $remove += $this->checkImportSource($source); + } + + return $remove; + } + + protected function getLastSync() + { + return strtotime($this->getSyncRule()->getLastSyncTimestamp()); + } + + // TODO: NAMING! + protected function checkImportSource(ImportSource $source) + { + if (null === ($lastSync = $this->getLastSync())) { + // No last sync, nothing to purge + return array(); + } + + $runA = $source->fetchLastRunBefore($lastSync); + if ($runA === null) { + // Nothing to purge for this source + return array(); + } + + $runB = $source->fetchLastRun(); + if ($runA->rowset_checksum === $runB->rowset_checksum) { + // Same source data, nothing to purge + return array(); + } + + return $this->listKeysRemovedBetween($runA, $runB); + } + + public function listKeysRemovedBetween(ImportRun $runA, ImportRun $runB) + { + $rule = $this->getSyncRule(); + $db = $rule->getDb(); + + $selectA = $runA->prepareImportedObjectQuery(); + $selectB = $runB->prepareImportedObjectQuery(); + + $query = $db->select()->from( + array('a' => $selectA), + 'a.object_name' + )->where('a.object_name NOT IN (?)', $selectB); + + $result = $db->fetchCol($query); + + if (empty($result)) { + return array(); + } + + if ($rule->hasCombinedKey()) { + $pattern = $rule->getSourceKeyPattern(); + $columns = SyncUtils::getRootVariables( + SyncUtils::extractVariableNames($pattern) + ); + $resultForCombinedKey = array(); + foreach (array_chunk($result, 1000) as $keys) { + $rows = $runA->fetchRows($columns, null, $keys); + foreach ($rows as $row) { + $resultForCombinedKey[] = SyncUtils::fillVariables($pattern, $row); + } + } + $result = $resultForCombinedKey; + } + + if (empty($result)) { + return array(); + } + + return array_combine($result, $result); + } +} diff --git a/library/Director/Import/PurgeStrategy/PurgeNothingPurgeStrategy.php b/library/Director/Import/PurgeStrategy/PurgeNothingPurgeStrategy.php new file mode 100644 index 0000000..3da8d4f --- /dev/null +++ b/library/Director/Import/PurgeStrategy/PurgeNothingPurgeStrategy.php @@ -0,0 +1,11 @@ +<?php + +namespace Icinga\Module\Director\Import\PurgeStrategy; + +class PurgeNothingPurgeStrategy extends PurgeStrategy +{ + public function listObjectsToPurge() + { + return array(); + } +} diff --git a/library/Director/Import/PurgeStrategy/PurgeStrategy.php b/library/Director/Import/PurgeStrategy/PurgeStrategy.php new file mode 100644 index 0000000..ffbe14f --- /dev/null +++ b/library/Director/Import/PurgeStrategy/PurgeStrategy.php @@ -0,0 +1,31 @@ +<?php + +namespace Icinga\Module\Director\Import\PurgeStrategy; + +use Icinga\Module\Director\Objects\SyncRule; + +abstract class PurgeStrategy +{ + private $rule; + + public function __construct(SyncRule $rule) + { + $this->rule = $rule; + } + + protected function getSyncRule() + { + return $this->rule; + } + + abstract public function listObjectsToPurge(); + + /** + * @return PurgeStrategy + */ + public static function load($name, SyncRule $rule) + { + $class = __NAMESPACE__ . '\\' . $name . 'PurgeStrategy'; + return new $class($rule); + } +} |