Added snapshot compression

This commit is contained in:
Marcin Kurczewski 2014-11-19 10:22:59 +01:00
parent a0133ea632
commit 847f248829
6 changed files with 80 additions and 31 deletions

View file

@ -5,6 +5,17 @@ var reprValue = function(value) {
} }
return JSON.stringify(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) {
%><li class="<%= className %> difference-<%= key %>"><%= key + ':' + reprValue(v) %></li><%
});
});
};
%> %>
<table class="history"> <table class="history">
@ -59,17 +70,8 @@ var reprValue = function(value) {
<% if (historyEntry.dataDifference) { %> <% if (historyEntry.dataDifference) { %>
<ul><!-- <ul><!--
--><% _.each(historyEntry.dataDifference['+'], function (difference) { %><!-- --><% showDifference('addition', historyEntry.dataDifference['+']) %><!--
--><li class="addition difference-<%= difference[0] %>"><!-- --><% showDifference('removal', historyEntry.dataDifference['-']) %><!--
--><%= difference[0] + ':' + reprValue(difference[1]) %><!--
--></li><!--
--><% }) %><!--
--><% _.each(historyEntry.dataDifference['-'], function (difference) { %><!--
--><li class="removal difference-<%= difference[0] %>"><!--
--><%= difference[0] + ':' + reprValue(difference[1]) %><!--
--></li><!--
--><% }) %><!--
--></ul> --></ul>
<% } %> <% } %>
<% } %> <% } %>

View file

@ -14,8 +14,8 @@ class SnapshotEntityConverter extends AbstractEntityConverter implements IEntity
'primaryKey' => $entity->getPrimaryKey(), 'primaryKey' => $entity->getPrimaryKey(),
'userId' => $entity->getUserId(), 'userId' => $entity->getUserId(),
'operation' => $entity->getOperation(), 'operation' => $entity->getOperation(),
'data' => json_encode($entity->getData()), 'data' => gzdeflate(json_encode($entity->getData())),
'dataDifference' => json_encode($entity->getDataDifference()), 'dataDifference' => gzdeflate(json_encode($entity->getDataDifference())),
]; ];
} }
@ -27,8 +27,8 @@ class SnapshotEntityConverter extends AbstractEntityConverter implements IEntity
$entity->setPrimaryKey($array['primaryKey']); $entity->setPrimaryKey($array['primaryKey']);
$entity->setUserId(intval($array['userId'])); $entity->setUserId(intval($array['userId']));
$entity->setOperation($array['operation']); $entity->setOperation($array['operation']);
$entity->setData(json_decode($array['data'], true)); $entity->setData(json_decode(gzinflate($array['data']), true));
$entity->setDataDifference(json_decode($array['dataDifference'], true)); $entity->setDataDifference(json_decode(gzinflate($array['dataDifference']), true));
return $entity; return $entity;
} }
} }

View file

@ -87,15 +87,18 @@ class HistoryService
{ {
if (is_array($base[$key])) if (is_array($base[$key]))
{ {
$result[$key] = [];
foreach ($base[$key] as $subValue) foreach ($base[$key] as $subValue)
{ {
if (!isset($other[$key]) || !in_array($subValue, $other[$key])) 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]) elseif (!isset($other[$key]) || $base[$key] !== $other[$key])
{ {
$result[] = [$key, $value]; $result[$key] = $value;
} }
} }
return $result; return $result;

View file

@ -0,0 +1,43 @@
<?php
namespace Szurubooru\Upgrades;
use Szurubooru\DatabaseConnection;
class Upgrade34 implements IUpgrade
{
public function run(DatabaseConnection $databaseConnection)
{
$pdo = $databaseConnection->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();
}
}
}

View file

@ -50,6 +50,7 @@ return [
$container->get(\Szurubooru\Upgrades\Upgrade31::class), $container->get(\Szurubooru\Upgrades\Upgrade31::class),
$container->get(\Szurubooru\Upgrades\Upgrade32::class), $container->get(\Szurubooru\Upgrades\Upgrade32::class),
$container->get(\Szurubooru\Upgrades\Upgrade33::class), $container->get(\Szurubooru\Upgrades\Upgrade33::class),
$container->get(\Szurubooru\Upgrades\Upgrade34::class),
]; ];
}), }),

View file

@ -36,7 +36,7 @@ final class HistoryServiceTest extends AbstractTestCase
['key' => 'newValue'], ['key' => 'newValue'],
[], [],
[ [
'+' => [['key', 'newValue']], '+' => ['key' => 'newValue'],
'-' => [] '-' => []
] ]
]; ];
@ -47,7 +47,7 @@ final class HistoryServiceTest extends AbstractTestCase
['key' => 'deletedValue'], ['key' => 'deletedValue'],
[ [
'+' => [], '+' => [],
'-' => [['key', 'deletedValue']] '-' => ['key' => 'deletedValue']
] ]
]; ];
@ -56,8 +56,8 @@ final class HistoryServiceTest extends AbstractTestCase
['key' => 'changedValue'], ['key' => 'changedValue'],
['key' => 'oldValue'], ['key' => 'oldValue'],
[ [
'+' => [['key', 'changedValue']], '+' => ['key' => 'changedValue'],
'-' => [['key', 'oldValue']] '-' => ['key' => 'oldValue']
] ]
]; ];
@ -76,7 +76,7 @@ final class HistoryServiceTest extends AbstractTestCase
['key' => ['newArrayElement']], ['key' => ['newArrayElement']],
['key' => []], ['key' => []],
[ [
'+' => [['key', 'newArrayElement']], '+' => ['key' => ['newArrayElement']],
'-' => [] '-' => []
] ]
]; ];
@ -87,17 +87,17 @@ final class HistoryServiceTest extends AbstractTestCase
['key' => ['removedArrayElement']], ['key' => ['removedArrayElement']],
[ [
'+' => [], '+' => [],
'-' => [['key', 'removedArrayElement']] '-' => ['key' => ['removedArrayElement']]
] ]
]; ];
yield yield
[ [
['key' => ['unchangedValue', 'newValue']], ['key' => ['unchangedArrayElement', 'newArrayElement']],
['key' => ['unchangedValue', 'oldValue']], ['key' => ['unchangedArrayElement', 'oldArrayElement']],
[ [
'+' => [['key', 'newValue']], '+' => ['key' => ['newArrayElement']],
'-' => [['key', 'oldValue']] '-' => ['key' => ['oldArrayElement']]
] ]
]; ];
} }
@ -137,7 +137,7 @@ final class HistoryServiceTest extends AbstractTestCase
$expectedSnapshot->setTime(date('c', 3)); $expectedSnapshot->setTime(date('c', 3));
$expectedSnapshot->setOperation(Snapshot::OPERATION_CREATION); $expectedSnapshot->setOperation(Snapshot::OPERATION_CREATION);
$expectedSnapshot->setData(['new' => '2']); $expectedSnapshot->setData(['new' => '2']);
$expectedSnapshot->setDataDifference(['+' => [['new', '2']], '-' => []]); $expectedSnapshot->setDataDifference(['+' => ['new' => '2'], '-' => []]);
yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, date('c', 3)]; yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, date('c', 3)];
} }
@ -155,7 +155,7 @@ final class HistoryServiceTest extends AbstractTestCase
$expectedSnapshot->setTime(date('c', 3000)); $expectedSnapshot->setTime(date('c', 3000));
$expectedSnapshot->setOperation(Snapshot::OPERATION_CHANGE); $expectedSnapshot->setOperation(Snapshot::OPERATION_CHANGE);
$expectedSnapshot->setData(['new' => '2']); $expectedSnapshot->setData(['new' => '2']);
$expectedSnapshot->setDataDifference(['+' => [['new', '2']], '-' => [['old', '1']]]); $expectedSnapshot->setDataDifference(['+' => ['new' => '2'], '-' => ['old' => '1']]);
yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, date('c', 3000)]; yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, date('c', 3000)];
} }
@ -174,7 +174,7 @@ final class HistoryServiceTest extends AbstractTestCase
$expectedSnapshot = new Snapshot(2); $expectedSnapshot = new Snapshot(2);
$expectedSnapshot->setOperation(Snapshot::OPERATION_CHANGE); $expectedSnapshot->setOperation(Snapshot::OPERATION_CHANGE);
$expectedSnapshot->setData(['new' => '2']); $expectedSnapshot->setData(['new' => '2']);
$expectedSnapshot->setDataDifference(['+' => [['new', '2']], '-' => [['old', '1']]]); $expectedSnapshot->setDataDifference(['+' => ['new' => '2'], '-' => ['old' => '1']]);
$expectedSnapshot->setUserId(null); $expectedSnapshot->setUserId(null);
yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, null]; yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, null];
@ -192,7 +192,7 @@ final class HistoryServiceTest extends AbstractTestCase
$expectedSnapshot = new Snapshot(2); $expectedSnapshot = new Snapshot(2);
$expectedSnapshot->setOperation(Snapshot::OPERATION_DELETE); $expectedSnapshot->setOperation(Snapshot::OPERATION_DELETE);
$expectedSnapshot->setData(['new' => '2']); $expectedSnapshot->setData(['new' => '2']);
$expectedSnapshot->setDataDifference(['+' => [['new', '2']], '-' => [['old', '1']]]); $expectedSnapshot->setDataDifference(['+' => ['new' => '2'], '-' => ['old' => '1']]);
yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, null]; yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, null];
} }