From 847f248829fd1e1c3dc956b0019d3cd015e5570b Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 19 Nov 2014 10:22:59 +0100 Subject: [PATCH] Added snapshot compression --- public_html/templates/history.tpl | 24 ++++++----- .../SnapshotEntityConverter.php | 8 ++-- src/Services/HistoryService.php | 7 ++- src/Upgrades/Upgrade34.php | 43 +++++++++++++++++++ src/di.php | 1 + tests/Services/HistoryServiceTest.php | 28 ++++++------ 6 files changed, 80 insertions(+), 31 deletions(-) create mode 100644 src/Upgrades/Upgrade34.php diff --git a/public_html/templates/history.tpl b/public_html/templates/history.tpl index f946657f..2e677d67 100644 --- a/public_html/templates/history.tpl +++ b/public_html/templates/history.tpl @@ -5,6 +5,17 @@ var reprValue = function(value) { } return JSON.stringify(value); }; + +var showDifference = function(className, difference) { + _.each(difference, function(value, key) { + if (!Array.isArray(value)) { + value = [value]; + } + _.each(value, function(v) { + %>
  • <%= key + ':' + reprValue(v) %>
  • <% + }); + }); +}; %> @@ -59,17 +70,8 @@ var reprValue = function(value) { <% if (historyEntry.dataDifference) { %> <% } %> <% } %> diff --git a/src/Dao/EntityConverters/SnapshotEntityConverter.php b/src/Dao/EntityConverters/SnapshotEntityConverter.php index 938f18fe..17c37a81 100644 --- a/src/Dao/EntityConverters/SnapshotEntityConverter.php +++ b/src/Dao/EntityConverters/SnapshotEntityConverter.php @@ -14,8 +14,8 @@ class SnapshotEntityConverter extends AbstractEntityConverter implements IEntity 'primaryKey' => $entity->getPrimaryKey(), 'userId' => $entity->getUserId(), 'operation' => $entity->getOperation(), - 'data' => json_encode($entity->getData()), - 'dataDifference' => json_encode($entity->getDataDifference()), + 'data' => gzdeflate(json_encode($entity->getData())), + 'dataDifference' => gzdeflate(json_encode($entity->getDataDifference())), ]; } @@ -27,8 +27,8 @@ class SnapshotEntityConverter extends AbstractEntityConverter implements IEntity $entity->setPrimaryKey($array['primaryKey']); $entity->setUserId(intval($array['userId'])); $entity->setOperation($array['operation']); - $entity->setData(json_decode($array['data'], true)); - $entity->setDataDifference(json_decode($array['dataDifference'], true)); + $entity->setData(json_decode(gzinflate($array['data']), true)); + $entity->setDataDifference(json_decode(gzinflate($array['dataDifference']), true)); return $entity; } } diff --git a/src/Services/HistoryService.php b/src/Services/HistoryService.php index cbb29d6a..52b92f98 100644 --- a/src/Services/HistoryService.php +++ b/src/Services/HistoryService.php @@ -87,15 +87,18 @@ class HistoryService { if (is_array($base[$key])) { + $result[$key] = []; foreach ($base[$key] as $subValue) { if (!isset($other[$key]) || !in_array($subValue, $other[$key])) - $result[] = [$key, $subValue]; + $result[$key] []= $subValue; } + if (empty($result[$key])) + unset($result[$key]); } elseif (!isset($other[$key]) || $base[$key] !== $other[$key]) { - $result[] = [$key, $value]; + $result[$key] = $value; } } return $result; diff --git a/src/Upgrades/Upgrade34.php b/src/Upgrades/Upgrade34.php new file mode 100644 index 00000000..0328acfa --- /dev/null +++ b/src/Upgrades/Upgrade34.php @@ -0,0 +1,43 @@ +getPDO(); + $pdo->exec('ALTER TABLE snapshots CHANGE operation operation INT(1) NOT NULL'); + + foreach ($pdo->from('snapshots') as $row) + { + $newDifference = ['+' => [], '-' => []]; + $oldDifference = json_decode($row['dataDifference'], true); + foreach (['+', '-'] as $type) + { + foreach ($oldDifference[$type] as $item) + { + $target = &$newDifference[$type][$item[0]]; + if (isset($target)) + { + if (!is_array($target)) + $target = [$target]; + $target []= $item[1]; + } + else + { + $target = $item[1]; + } + } + } + $newDifference = json_encode($newDifference); + + $pdo->update('snapshots') + ->set([ + 'data' => gzdeflate($row['data']), + 'dataDifference' => gzdeflate($newDifference)]) + ->where('id', $row['id']) + ->execute(); + } + } +} diff --git a/src/di.php b/src/di.php index 1a37fdc9..b43799a0 100644 --- a/src/di.php +++ b/src/di.php @@ -50,6 +50,7 @@ return [ $container->get(\Szurubooru\Upgrades\Upgrade31::class), $container->get(\Szurubooru\Upgrades\Upgrade32::class), $container->get(\Szurubooru\Upgrades\Upgrade33::class), + $container->get(\Szurubooru\Upgrades\Upgrade34::class), ]; }), diff --git a/tests/Services/HistoryServiceTest.php b/tests/Services/HistoryServiceTest.php index ef359613..59770654 100644 --- a/tests/Services/HistoryServiceTest.php +++ b/tests/Services/HistoryServiceTest.php @@ -36,7 +36,7 @@ final class HistoryServiceTest extends AbstractTestCase ['key' => 'newValue'], [], [ - '+' => [['key', 'newValue']], + '+' => ['key' => 'newValue'], '-' => [] ] ]; @@ -47,7 +47,7 @@ final class HistoryServiceTest extends AbstractTestCase ['key' => 'deletedValue'], [ '+' => [], - '-' => [['key', 'deletedValue']] + '-' => ['key' => 'deletedValue'] ] ]; @@ -56,8 +56,8 @@ final class HistoryServiceTest extends AbstractTestCase ['key' => 'changedValue'], ['key' => 'oldValue'], [ - '+' => [['key', 'changedValue']], - '-' => [['key', 'oldValue']] + '+' => ['key' => 'changedValue'], + '-' => ['key' => 'oldValue'] ] ]; @@ -76,7 +76,7 @@ final class HistoryServiceTest extends AbstractTestCase ['key' => ['newArrayElement']], ['key' => []], [ - '+' => [['key', 'newArrayElement']], + '+' => ['key' => ['newArrayElement']], '-' => [] ] ]; @@ -87,17 +87,17 @@ final class HistoryServiceTest extends AbstractTestCase ['key' => ['removedArrayElement']], [ '+' => [], - '-' => [['key', 'removedArrayElement']] + '-' => ['key' => ['removedArrayElement']] ] ]; yield [ - ['key' => ['unchangedValue', 'newValue']], - ['key' => ['unchangedValue', 'oldValue']], + ['key' => ['unchangedArrayElement', 'newArrayElement']], + ['key' => ['unchangedArrayElement', 'oldArrayElement']], [ - '+' => [['key', 'newValue']], - '-' => [['key', 'oldValue']] + '+' => ['key' => ['newArrayElement']], + '-' => ['key' => ['oldArrayElement']] ] ]; } @@ -137,7 +137,7 @@ final class HistoryServiceTest extends AbstractTestCase $expectedSnapshot->setTime(date('c', 3)); $expectedSnapshot->setOperation(Snapshot::OPERATION_CREATION); $expectedSnapshot->setData(['new' => '2']); - $expectedSnapshot->setDataDifference(['+' => [['new', '2']], '-' => []]); + $expectedSnapshot->setDataDifference(['+' => ['new' => '2'], '-' => []]); yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, date('c', 3)]; } @@ -155,7 +155,7 @@ final class HistoryServiceTest extends AbstractTestCase $expectedSnapshot->setTime(date('c', 3000)); $expectedSnapshot->setOperation(Snapshot::OPERATION_CHANGE); $expectedSnapshot->setData(['new' => '2']); - $expectedSnapshot->setDataDifference(['+' => [['new', '2']], '-' => [['old', '1']]]); + $expectedSnapshot->setDataDifference(['+' => ['new' => '2'], '-' => ['old' => '1']]); yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, date('c', 3000)]; } @@ -174,7 +174,7 @@ final class HistoryServiceTest extends AbstractTestCase $expectedSnapshot = new Snapshot(2); $expectedSnapshot->setOperation(Snapshot::OPERATION_CHANGE); $expectedSnapshot->setData(['new' => '2']); - $expectedSnapshot->setDataDifference(['+' => [['new', '2']], '-' => [['old', '1']]]); + $expectedSnapshot->setDataDifference(['+' => ['new' => '2'], '-' => ['old' => '1']]); $expectedSnapshot->setUserId(null); yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, null]; @@ -192,7 +192,7 @@ final class HistoryServiceTest extends AbstractTestCase $expectedSnapshot = new Snapshot(2); $expectedSnapshot->setOperation(Snapshot::OPERATION_DELETE); $expectedSnapshot->setData(['new' => '2']); - $expectedSnapshot->setDataDifference(['+' => [['new', '2']], '-' => [['old', '1']]]); + $expectedSnapshot->setDataDifference(['+' => ['new' => '2'], '-' => ['old' => '1']]); yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, null]; }