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

View file

@ -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;
}
}

View file

@ -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;

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\Upgrade32::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'],
'-' => []
]
];
@ -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];
}