Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
added validity check of original backup files before merge
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonyCorbett committed Nov 13, 2019
1 parent 263c21c commit 7049b06
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 6 deletions.
8 changes: 4 additions & 4 deletions JWLMerge.BackupFileServices/Helpers/Merger.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Linq;
using System.Security.Cryptography.X509Certificates;

namespace JWLMerge.BackupFileServices.Helpers
namespace JWLMerge.BackupFileServices.Helpers
{
using System;
using System.Linq;
using System.Collections.Generic;
using Events;
using Models.Database;
Expand Down Expand Up @@ -65,6 +63,8 @@ private void ClearMaxIds()
private void Merge(Database source, Database destination)
{
ClearTranslators();

source.FixupAnomalies();

MergeUserMarks(source, destination);
MergeNotes(source, destination);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>..\JWLMerge\JWLMerge.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
132 changes: 130 additions & 2 deletions JWLMerge.BackupFileServices/Models/Database/Database.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace JWLMerge.BackupFileServices.Models.Database
using Serilog;

namespace JWLMerge.BackupFileServices.Models.Database
{
using System;
using System.Collections.Generic;
using System.Linq;
using Exceptions;
using JWLMerge.BackupFileServices.Exceptions;

public class Database
{
Expand Down Expand Up @@ -52,6 +54,22 @@ public void CheckValidity()
CheckUserMarkValidity();
}

public void FixupAnomalies()
{
var count = 0;

count += FixupBlockRangeValidity();
count += FixupBookmarkValidity();
count += FixupNoteValidity();
count += FixupTagMapValidity();
count += FixupUserMarkValidity();

if (count > 0)
{
ReinitializeIndexes();
}
}

public LastModified LastModified { get; set; }

public List<Location> Locations { get; set; }
Expand Down Expand Up @@ -542,5 +560,115 @@ private void ReinitializeIndexes()
_blockRangesUserMarkIdIndex = new Lazy<Dictionary<int, List<BlockRange>>>(BlockRangeIndexValueFactory);
_bookmarksIndex = new Lazy<Dictionary<string, Bookmark>>(BookmarkIndexValueFactory);
}

private int FixupUserMarkValidity()
{
var fixupCount = 0;

for (var n = UserMarks.Count - 1; n >= 0; --n)
{
var userMark = UserMarks[n];

if (FindLocation(userMark.LocationId) == null)
{
++fixupCount;
UserMarks.RemoveAt(n);

Log.Logger.Error($"Removed invalid user mark {userMark.UserMarkId}");
}
}

return fixupCount;
}

private int FixupTagMapValidity()
{
var fixupCount = 0;

for (var n = TagMaps.Count - 1; n >= 0; --n)
{
var tagMap = TagMaps[n];

if (tagMap.Type == 1 &&
(FindTag(tagMap.TagId) == null || FindNote(tagMap.TypeId) == null))
{
++fixupCount;
TagMaps.RemoveAt(n);

Log.Logger.Error($"Removed invalid tag map {tagMap.TagMapId}");
}
}

return fixupCount;
}

private int FixupNoteValidity()
{
var fixupCount = 0;

for (var n = Notes.Count - 1; n >= 0; --n)
{
var note = Notes[n];

if (note.UserMarkId != null && FindUserMark(note.UserMarkId.Value) == null)
{
++fixupCount;
note.UserMarkId = null;

Log.Logger.Error($"Cleared invalid user mark ID for note {note.NoteId}");
}

if (note.LocationId != null && FindLocation(note.LocationId.Value) == null)
{
++fixupCount;
note.LocationId = null;

Log.Logger.Error($"Cleared invalid location ID for note {note.NoteId}");
}
}

return fixupCount;
}

private int FixupBookmarkValidity()
{
var fixupCount = 0;

for (var n = Bookmarks.Count - 1; n >= 0; --n)
{
var bookmark = Bookmarks[n];

if (FindLocation(bookmark.LocationId) == null ||
FindLocation(bookmark.PublicationLocationId) == null)
{
++fixupCount;
Bookmarks.RemoveAt(n);

Log.Logger.Error($"Removed invalid bookmark {bookmark.BookmarkId}");
}
}

return fixupCount;
}

private int FixupBlockRangeValidity()
{
var fixupCount = 0;

for (var n = BlockRanges.Count - 1; n >= 0; --n)
{
var range = BlockRanges[n];

if (FindUserMark(range.UserMarkId) == null)
{
++fixupCount;
BlockRanges.RemoveAt(n);

Log.Logger.Error($"Removed invalid block range {range.BlockRangeId}");
}
}

return fixupCount;
}
}
}
1 change: 1 addition & 0 deletions JWLMerge.Tests/JWLMerge.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>..\JWLMerge\JWLMerge.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
1 change: 1 addition & 0 deletions JWLMerge.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=colour/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=favourites/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Fixup/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=initialised/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Ipsum/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=jwlibrary/@EntryIndexedValue">True</s:Boolean>
Expand Down
1 change: 1 addition & 0 deletions JWLMergeCLI/JWLMergeCLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>..\JWLMerge\JWLMerge.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down

0 comments on commit 7049b06

Please sign in to comment.