From 54ef7ba55d40b258516f47ee93059b585db5a626 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Mon, 7 Oct 2019 13:00:26 +0200 Subject: [PATCH] ODB Diff WIP --- odb/ng/diff/Diff.cs | 8 +++++ odb/ng/diff/DocumentDiff.cs | 17 ++++++++++ odb/ng/diff/ListDiff.cs | 62 +++++++++++++++++++++++++++++++++++-- 3 files changed, 84 insertions(+), 3 deletions(-) diff --git a/odb/ng/diff/Diff.cs b/odb/ng/diff/Diff.cs index b784f69..ef0b5ad 100644 --- a/odb/ng/diff/Diff.cs +++ b/odb/ng/diff/Diff.cs @@ -24,6 +24,9 @@ namespace ln.types.odb.ng.diff return new SimpleDiff(dst); } + public string TreeString => ToTreeString(0); + public abstract string ToTreeString(int indent); + class SimpleDiff : Diff { @@ -43,6 +46,11 @@ namespace ln.types.odb.ng.diff { return String.Format("[SimpleDiff DestinationValue={0}]",DestinationValue); } + + public override string ToTreeString(int indent) + { + return String.Format("= {0}", DestinationValue.ToTreeString(indent+2)); + } } } diff --git a/odb/ng/diff/DocumentDiff.cs b/odb/ng/diff/DocumentDiff.cs index 4bf9f1f..ca00fb0 100644 --- a/odb/ng/diff/DocumentDiff.cs +++ b/odb/ng/diff/DocumentDiff.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using ln.types.odb.values; using System.Linq; +using System.Text; namespace ln.types.odb.ng.diff { public class DocumentDiff : Diff @@ -38,5 +39,21 @@ namespace ln.types.odb.ng.diff return String.Format("[DocumentDiff ChangedProperties=({0})]",string.Join(",",propertyDiffs.Keys)); } + public override string ToTreeString(int indent) + { + indent += 2; + StringBuilder stringBuilder = new StringBuilder(); + + stringBuilder.AppendFormat("DocumentDiff ChangedProperties=({0})",string.Join(",",propertyDiffs.Keys)); + foreach (ODBEntity key in propertyDiffs.Keys) + { + stringBuilder.AppendLine(); + stringBuilder.AppendFormat("{0}{1,-32} {2}", new string(' ', indent), key, propertyDiffs[key].ToTreeString(indent)); + } + + return stringBuilder.ToString(); + + } + } } diff --git a/odb/ng/diff/ListDiff.cs b/odb/ng/diff/ListDiff.cs index cf0d796..1b652d1 100644 --- a/odb/ng/diff/ListDiff.cs +++ b/odb/ng/diff/ListDiff.cs @@ -2,6 +2,7 @@ using ln.types.odb.values; using System.Collections.Generic; using System.Linq; +using System.Text; namespace ln.types.odb.ng.diff { @@ -27,20 +28,75 @@ namespace ln.types.odb.ng.diff remove.AddRange(srcItems.Select((i) => i.Identity)); add.AddRange(dstItems); + Dictionary srcLookup = new Dictionary(); foreach (ODBEntity entity in common) - { - } + srcLookup.Add(entity.Identity, entity); + foreach (ODBEntity entity in commonDst) + { + ODBValue identity = entity.Identity; + ODBEntity srcEntity = srcLookup[identity]; + + if (entity.CompareTo(srcEntity) != 0) + update.Add(identity, Diff.Construct(srcEntity, entity)); + } } public override ODBEntity Apply(ODBEntity src) { - throw new NotImplementedException(); + ODBList list = src as ODBList; + + foreach (ODBEntity entity in list.ToArray()) + { + ODBValue identity = entity.Identity; + + if (remove.Contains(identity)) + { + list.Remove(entity); + } else if (update.ContainsKey(identity)) + { + update[identity].Apply(entity); + } + } + + foreach (ODBEntity entity in add) + { + list.Add(entity.Clone()); + } + + return src; } public override string ToString() { return base.ToString(); } + + public override string ToTreeString(int indent) + { + indent += 2; + StringBuilder stringBuilder = new StringBuilder(); + + stringBuilder.AppendFormat("ListDiff Remove={0} Change={1} Add={2}",remove.Count,update.Count,add.Count); + foreach (ODBEntity key in remove) + { + stringBuilder.AppendLine(); + stringBuilder.AppendFormat("{0}-{1}", new string(' ', indent), key); + } + foreach (ODBValue key in update.Keys) + { + stringBuilder.AppendLine(); + stringBuilder.AppendFormat("{0} {1} {2}", new string(' ', indent), key, update[key].ToTreeString(indent)); + } + foreach (ODBEntity key in add) + { + stringBuilder.AppendLine(); + stringBuilder.AppendFormat("{0}+{1}", new string(' ', indent), key.ToTreeString(indent)); + } + + return stringBuilder.ToString(); + + } + } }