Merge custom data only when necessary (#3475)

This commit is contained in:
louib 2019-09-16 14:01:13 -04:00 committed by Jonathan White
parent c99b656279
commit c19703c39f
3 changed files with 33 additions and 12 deletions

View File

@ -622,11 +622,11 @@ Merger::ChangeList Merger::mergeMetadata(const MergeContext& context)
} }
// Merge Custom Data if source is newer // Merge Custom Data if source is newer
const auto targetCustomDataModificationTime = sourceMetadata->customData()->getLastModified(); const auto targetCustomDataModificationTime = targetMetadata->customData()->getLastModified();
const auto sourceCustomDataModificationTime = targetMetadata->customData()->getLastModified(); const auto sourceCustomDataModificationTime = sourceMetadata->customData()->getLastModified();
if (!targetMetadata->customData()->contains(CustomData::LastModified) if (!targetMetadata->customData()->contains(CustomData::LastModified)
|| (targetCustomDataModificationTime.isValid() && sourceCustomDataModificationTime.isValid() || (targetCustomDataModificationTime.isValid() && sourceCustomDataModificationTime.isValid()
&& targetCustomDataModificationTime > sourceCustomDataModificationTime)) { && targetCustomDataModificationTime < sourceCustomDataModificationTime)) {
const auto sourceCustomDataKeys = sourceMetadata->customData()->keys(); const auto sourceCustomDataKeys = sourceMetadata->customData()->keys();
const auto targetCustomDataKeys = targetMetadata->customData()->keys(); const auto targetCustomDataKeys = targetMetadata->customData()->keys();
@ -641,9 +641,18 @@ Merger::ChangeList Merger::mergeMetadata(const MergeContext& context)
// Transfer new/existing keys // Transfer new/existing keys
for (const auto& key : sourceCustomDataKeys) { for (const auto& key : sourceCustomDataKeys) {
auto value = sourceMetadata->customData()->value(key); // Don't merge this meta field, it is updated automatically.
targetMetadata->customData()->set(key, value); if (key == CustomData::LastModified) {
changes << tr("Adding custom data %1 [%2]").arg(key, value); continue;
}
auto sourceValue = sourceMetadata->customData()->value(key);
auto targetValue = targetMetadata->customData()->value(key);
// Merge only if the values are not the same.
if (sourceValue != targetValue) {
targetMetadata->customData()->set(key, sourceValue);
changes << tr("Adding custom data %1 [%2]").arg(key, sourceValue);
}
} }
} }

View File

@ -1159,12 +1159,12 @@ void TestMerge::testMetadata()
{ {
QSKIP("Sophisticated merging for Metadata not implemented"); QSKIP("Sophisticated merging for Metadata not implemented");
// TODO HNH: I think a merge of recycle bins would be nice since duplicating them // TODO HNH: I think a merge of recycle bins would be nice since duplicating them
// is not realy a good solution - the one to use as final recycle bin // is not really a good solution - the one to use as final recycle bin
// is determined by the merge method - if only one has a bin, this one // is determined by the merge method - if only one has a bin, this one
// will be used - exception is the target has no recycle bin activated // will be used - exception is the target has no recycle bin activated
} }
void TestMerge::testCustomdata() void TestMerge::testCustomData()
{ {
QScopedPointer<Database> dbDestination(new Database()); QScopedPointer<Database> dbDestination(new Database());
QScopedPointer<Database> dbSource(createTestDatabase()); QScopedPointer<Database> dbSource(createTestDatabase());
@ -1198,10 +1198,9 @@ void TestMerge::testCustomdata()
m_clock->advanceSecond(1); m_clock->advanceSecond(1);
Merger merger(dbSource.data(), dbDestination.data()); Merger merger(dbSource.data(), dbDestination.data());
merger.merge(); QStringList changes = merger.merge();
Merger merger2(dbSource2.data(), dbDestination2.data()); QVERIFY(!changes.isEmpty());
merger2.merge();
// Source is newer, data should be merged // Source is newer, data should be merged
QVERIFY(!dbDestination->metadata()->customData()->isEmpty()); QVERIFY(!dbDestination->metadata()->customData()->isEmpty());
@ -1215,6 +1214,19 @@ void TestMerge::testCustomdata()
QCOMPARE(dbDestination->metadata()->customData()->value("key3"), QCOMPARE(dbDestination->metadata()->customData()->value("key3"),
QString("newValue")); // Old value should be replaced QString("newValue")); // Old value should be replaced
// Merging again should not do anything if the values are the same.
m_clock->advanceSecond(1);
dbSource->metadata()->customData()->set("key3", "oldValue");
dbSource->metadata()->customData()->set("key3", "newValue");
Merger merger2(dbSource.data(), dbDestination.data());
QStringList changes2 = merger2.merge();
QVERIFY(changes2.isEmpty());
Merger merger3(dbSource2.data(), dbDestination2.data());
merger3.merge();
// Target is newer, no data is merged // Target is newer, no data is merged
QVERIFY(!dbDestination2->metadata()->customData()->isEmpty()); QVERIFY(!dbDestination2->metadata()->customData()->isEmpty());
QVERIFY(!dbDestination2->metadata()->customData()->contains("key1")); QVERIFY(!dbDestination2->metadata()->customData()->contains("key1"));

View File

@ -59,7 +59,7 @@ private slots:
void testMergeCustomIcons(); void testMergeCustomIcons();
void testMergeDuplicateCustomIcons(); void testMergeDuplicateCustomIcons();
void testMetadata(); void testMetadata();
void testCustomdata(); void testCustomData();
void testDeletedEntry(); void testDeletedEntry();
void testDeletedGroup(); void testDeletedGroup();
void testDeletedRevertedEntry(); void testDeletedRevertedEntry();