No longer support removal in config transactions

This commit is contained in:
Ben Olden-Cooligan 2022-12-03 22:10:47 -08:00
parent e4b8820217
commit 51d2763a5c
3 changed files with 4 additions and 119 deletions

View File

@ -13,7 +13,6 @@ public class TransactionConfigScopeTests
{
_baseScope = ConfigScope.Memory<CommonConfig>();
_baseScope.Set(c => c.Culture, "fr");
_baseScope.Set(c => c.PdfSettings.DefaultFileName, "test");
_transact = _baseScope.BeginTransaction();
}
@ -76,60 +75,4 @@ public class TransactionConfigScopeTests
mockHandler.Verify(x => x(_transact, EventArgs.Empty));
mockHandler.VerifyNoOtherCalls();
}
[Fact]
public void SetThenRemove()
{
_transact.Set(c => c.Culture, "de");
Assert.True(_transact.TryGet(c => c.Culture, out _));
_transact.Remove(c => c.Culture);
Assert.False(_transact.TryGet(c => c.Culture, out _));
Assert.True(_transact.HasChanges);
_transact.Commit();
Assert.False(_transact.HasChanges);
Assert.False(_baseScope.TryGet(c => c.Culture, out _));
}
[Fact]
public void RemoveThenSet()
{
_transact.Remove(c => c.Culture);
Assert.False(_transact.TryGet(c => c.Culture, out _));
_transact.Set(c => c.Culture, "de");
Assert.True(_transact.TryGet(c => c.Culture, out _));
Assert.True(_transact.HasChanges);
_transact.Commit();
Assert.False(_transact.HasChanges);
Assert.Equal("de", _baseScope.GetOrDefault(c => c.Culture));
}
[Fact]
public void RemoveParentThenSet()
{
_transact.Remove(c => c.PdfSettings);
Assert.False(_transact.TryGet(c => c.PdfSettings.DefaultFileName, out _));
_transact.Set(c => c.PdfSettings.DefaultFileName, "test");
Assert.True(_transact.TryGet(c => c.PdfSettings.DefaultFileName, out _));
Assert.True(_transact.HasChanges);
_transact.Commit();
Assert.False(_transact.HasChanges);
Assert.Equal("test", _baseScope.GetOrDefault(c => c.PdfSettings.DefaultFileName));
}
[Fact]
public void SetThenRemoveParent()
{
_transact.Set(c => c.PdfSettings.DefaultFileName, "test");
Assert.True(_transact.TryGet(c => c.PdfSettings.DefaultFileName, out _));
_transact.Remove(c => c.PdfSettings);
Assert.False(_transact.TryGet(c => c.PdfSettings.DefaultFileName, out _));
Assert.True(_transact.HasChanges);
_transact.Commit();
Assert.False(_transact.HasChanges);
Assert.False(_transact.TryGet(c => c.PdfSettings.DefaultFileName, out _));
}
}

View File

@ -1,44 +0,0 @@
namespace NAPS2.Config.Model;
internal class NodeTree
{
private readonly Node _root = new();
public void Add(ConfigLookup lookup)
{
var node = _root;
for (var lookupNode = lookup.Head; lookupNode != null; lookupNode = lookupNode.Next)
{
if (!node.Children.ContainsKey(lookupNode.Key))
{
node.Children.Add(lookupNode.Key, new());
}
node = node.Children[lookupNode.Key];
}
node.IsPresent = true;
}
public bool ContainsPrefix(ConfigLookup lookup)
{
var node = _root;
for (var lookupNode = lookup.Head; lookupNode != null; lookupNode = lookupNode.Next)
{
if (node.IsPresent)
{
return true;
}
if (!node.Children.ContainsKey(lookupNode.Key))
{
return false;
}
node = node.Children[lookupNode.Key];
}
return node.IsPresent;
}
private class Node
{
public bool IsPresent { get; set; }
public Dictionary<string, Node> Children { get; } = new();
}
}

View File

@ -15,8 +15,6 @@ namespace NAPS2.Config.Model;
/// </summary>
public class TransactionConfigScope<TConfig> : ConfigScope<TConfig>
{
private List<Action> _removeActions = new();
private NodeTree _removedNodes = new();
private ConfigStorage<TConfig> _changes = new();
public TransactionConfigScope(ConfigScope<TConfig> originalScope) : base(ConfigScopeMode.ReadWrite)
@ -53,14 +51,8 @@ public class TransactionConfigScope<TConfig> : ConfigScope<TConfig>
{
lock (OriginalScope)
{
foreach (var action in _removeActions)
{
action();
}
OriginalScope.CopyFrom(_changes);
_changes = new();
_removeActions = new();
_removedNodes = new();
}
ChangesFlushed();
}
@ -74,8 +66,6 @@ public class TransactionConfigScope<TConfig> : ConfigScope<TConfig>
lock (this)
{
_changes = new();
_removeActions = new();
_removedNodes = new();
ChangesFlushed();
}
}
@ -86,10 +76,6 @@ public class TransactionConfigScope<TConfig> : ConfigScope<TConfig>
{
return true;
}
if (_removedNodes.ContainsPrefix(lookup))
{
return false;
}
return OriginalScope.TryGet(lookup, out value);
}
@ -101,10 +87,10 @@ public class TransactionConfigScope<TConfig> : ConfigScope<TConfig>
protected override void RemoveInternal<T>(Expression<Func<TConfig, T>> accessor)
{
_changes.Remove(accessor);
_removeActions.Add(() => OriginalScope.Remove(accessor));
_removedNodes.Add(ConfigLookup.ExpandExpression(accessor));
ChangesMade();
// TODO: Supporting arbitrary removal is very complicated - probably the best is a full transaction log and
// then, to commit, apply each operation to the underlying scope in order. But that's probably not worth
// supporting.
throw new NotSupportedException("Can't remove config properties in a transaction");
}
protected override void CopyFromInternal(ConfigStorage<TConfig> source)