private InstanceKeyView(InstanceKeyView source) { this.InstanceKey = source.InstanceKey; this.InstanceKeyState = source.InstanceKeyState; this.InstanceKeyMetadata = source.InstanceKeyMetadata; this.InstanceKeyMetadataConsistency = source.InstanceKeyMetadataConsistency; }
InstanceKeyView(InstanceKeyView source) { InstanceKey = source.InstanceKey; InstanceKeyState = source.InstanceKeyState; InstanceKeyMetadata = source.InstanceKeyMetadata; InstanceKeyMetadataConsistency = source.InstanceKeyMetadataConsistency; }
public void AssociatedInstanceKey(Guid key) { if (key == Guid.Empty) { throw Fx.Exception.Argument("key", SRCore.InvalidKeyArgument); } this.ThrowIfNotLocked(); this.ThrowIfCompleted(); this.ThrowIfNotTransactional("AssociatedInstanceKey"); Dictionary<Guid, InstanceKeyView> dictionary = new Dictionary<Guid, InstanceKeyView>(this.InstanceView.InstanceKeys); if (((this.InstanceView.InstanceKeysConsistency & InstanceValueConsistency.InDoubt) == InstanceValueConsistency.None) && dictionary.ContainsKey(key)) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.KeyAlreadyAssociated)); } InstanceKeyView view = new InstanceKeyView(key) { InstanceKeyState = InstanceKeyState.Associated, InstanceKeyMetadataConsistency = InstanceValueConsistency.None }; dictionary[view.InstanceKey] = view; this.InstanceView.InstanceKeys = new ReadOnlyDictionary<Guid, InstanceKeyView>(dictionary, false); }
public void WroteInstanceKeyMetadataValue(Guid key, XName name, InstanceValue value) { if (key == Guid.Empty) { throw Fx.Exception.Argument("key", SRCore.InvalidKeyArgument); } if (name == null) { throw Fx.Exception.ArgumentNull("name"); } if (value == null) { throw Fx.Exception.ArgumentNull("value"); } ThrowIfNotLocked(); ThrowIfCompleted(); ThrowIfNotTransactional("WroteInstanceKeyMetadataValue"); InstanceKeyView keyView; if (!InstanceView.InstanceKeys.TryGetValue(key, out keyView)) { if (InstanceView.InstanceKeysConsistency == InstanceValueConsistency.None) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.KeyNotAssociated)); } if (!value.IsWriteOnly() && !value.IsDeletedValue) { Dictionary<Guid, InstanceKeyView> copy = new Dictionary<Guid, InstanceKeyView>(InstanceView.InstanceKeys); keyView = new InstanceKeyView(key); keyView.AccumulatedMetadataWrites.Add(name, value); keyView.InstanceKeyMetadataConsistency = InstanceValueConsistency.Partial; copy[keyView.InstanceKey] = keyView; InstanceView.InstanceKeys = new ReadOnlyDictionaryInternal<Guid, InstanceKeyView>(copy); InstanceView.InstanceKeysConsistency |= InstanceValueConsistency.Partial; } } else { keyView.AccumulatedMetadataWrites.Add(name, value); } }
public void ReadInstanceKeyMetadata(Guid key, IDictionary<XName, InstanceValue> metadata, bool complete) { if (key == Guid.Empty) { throw Fx.Exception.Argument("key", SRCore.InvalidKeyArgument); } ThrowIfNoInstance(); ThrowIfNotActive("ReadInstanceKeyMetadata"); InstanceKeyView keyView; if (!InstanceView.InstanceKeys.TryGetValue(key, out keyView)) { if (InstanceView.InstanceKeysConsistency == InstanceValueConsistency.None) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.KeyNotAssociated)); } Dictionary<Guid, InstanceKeyView> copy = new Dictionary<Guid, InstanceKeyView>(InstanceView.InstanceKeys); keyView = new InstanceKeyView(key); if (complete) { keyView.InstanceKeyMetadata = metadata.ReadOnlyCopy(false); keyView.InstanceKeyMetadataConsistency = InstanceValueConsistency.None; } else { keyView.InstanceKeyMetadata = metadata.ReadOnlyMergeInto(null, false); keyView.InstanceKeyMetadataConsistency = InstanceValueConsistency.Partial; } if (!InstanceView.IsBoundToLock && InstanceView.InstanceState != InstanceState.Completed) { keyView.InstanceKeyMetadataConsistency |= InstanceValueConsistency.InDoubt; } copy[keyView.InstanceKey] = keyView; InstanceView.InstanceKeys = new ReadOnlyDictionaryInternal<Guid, InstanceKeyView>(copy); } else { if (keyView.InstanceKeyMetadataConsistency == InstanceValueConsistency.None) { return; } if (complete) { keyView.InstanceKeyMetadata = metadata.ReadOnlyCopy(false); keyView.InstanceKeyMetadataConsistency = InstanceView.IsBoundToLock || InstanceView.InstanceState == InstanceState.Completed ? InstanceValueConsistency.None : InstanceValueConsistency.InDoubt; } else { if ((InstanceView.IsBoundToLock || InstanceView.InstanceState == InstanceState.Completed) && (keyView.InstanceKeyMetadataConsistency & InstanceValueConsistency.InDoubt) != 0) { // In this case, prefer throwing out old data and keeping only authoritative data. keyView.InstanceKeyMetadata = metadata.ReadOnlyMergeInto(null, false); keyView.InstanceKeyMetadataConsistency = InstanceValueConsistency.Partial; } else { keyView.InstanceKeyMetadata = metadata.ReadOnlyMergeInto(keyView.InstanceKeyMetadata, false); keyView.InstanceKeyMetadataConsistency |= InstanceValueConsistency.Partial; } } } }
public void CompletedInstanceKey(Guid key) { if (key == Guid.Empty) { throw Fx.Exception.Argument("key", SRCore.InvalidKeyArgument); } ThrowIfNotLocked(); ThrowIfCompleted(); ThrowIfNotTransactional("CompletedInstanceKey"); InstanceKeyView existingKeyView; InstanceView.InstanceKeys.TryGetValue(key, out existingKeyView); if ((InstanceView.InstanceKeysConsistency & InstanceValueConsistency.InDoubt) == 0) { if (existingKeyView != null) { if (existingKeyView.InstanceKeyState == InstanceKeyState.Completed) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.KeyAlreadyCompleted)); } } else if ((InstanceView.InstanceKeysConsistency & InstanceValueConsistency.Partial) == 0) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.KeyNotAssociated)); } } if (existingKeyView != null) { existingKeyView.InstanceKeyState = InstanceKeyState.Completed; } else { Dictionary<Guid, InstanceKeyView> copy = new Dictionary<Guid, InstanceKeyView>(InstanceView.InstanceKeys); InstanceKeyView keyView = new InstanceKeyView(key); keyView.InstanceKeyState = InstanceKeyState.Completed; keyView.InstanceKeyMetadataConsistency = InstanceValueConsistency.Partial; copy[keyView.InstanceKey] = keyView; InstanceView.InstanceKeys = new ReadOnlyDictionaryInternal<Guid, InstanceKeyView>(copy); } }
public void AssociatedInstanceKey(Guid key) { if (key == Guid.Empty) { throw Fx.Exception.Argument("key", SRCore.InvalidKeyArgument); } ThrowIfNotLocked(); ThrowIfCompleted(); ThrowIfNotTransactional("AssociatedInstanceKey"); Dictionary<Guid, InstanceKeyView> copy = new Dictionary<Guid, InstanceKeyView>(InstanceView.InstanceKeys); if ((InstanceView.InstanceKeysConsistency & InstanceValueConsistency.InDoubt) == 0 && copy.ContainsKey(key)) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.KeyAlreadyAssociated)); } InstanceKeyView keyView = new InstanceKeyView(key); keyView.InstanceKeyState = InstanceKeyState.Associated; keyView.InstanceKeyMetadataConsistency = InstanceValueConsistency.None; copy[keyView.InstanceKey] = keyView; InstanceView.InstanceKeys = new ReadOnlyDictionaryInternal<Guid, InstanceKeyView>(copy); }
public void LoadedInstance(InstanceState state, IDictionary<XName, InstanceValue> instanceData, IDictionary<XName, InstanceValue> instanceMetadata, IDictionary<Guid, IDictionary<XName, InstanceValue>> associatedInstanceKeyMetadata, IDictionary<Guid, IDictionary<XName, InstanceValue>> completedInstanceKeyMetadata) { if (state == InstanceState.Uninitialized) { if (instanceData != null && instanceData.Count > 0) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.UninitializedCannotHaveData)); } } else if (state == InstanceState.Completed) { if (associatedInstanceKeyMetadata != null && associatedInstanceKeyMetadata.Count > 0) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.CompletedMustNotHaveAssociatedKeys)); } } else if (state != InstanceState.Initialized) { throw Fx.Exception.Argument("state", SRCore.InvalidInstanceState); } ThrowIfNoInstance(); ThrowIfNotActive("PersistedInstance"); InstanceValueConsistency consistency = InstanceView.IsBoundToLock || state == InstanceState.Completed ? InstanceValueConsistency.None : InstanceValueConsistency.InDoubt; ReadOnlyDictionaryInternal<XName, InstanceValue> instanceDataCopy = instanceData.ReadOnlyCopy(false); ReadOnlyDictionaryInternal<XName, InstanceValue> instanceMetadataCopy = instanceMetadata.ReadOnlyCopy(false); Dictionary<Guid, InstanceKeyView> keysCopy = null; int totalKeys = (associatedInstanceKeyMetadata != null ? associatedInstanceKeyMetadata.Count : 0) + (completedInstanceKeyMetadata != null ? completedInstanceKeyMetadata.Count : 0); if (totalKeys > 0) { keysCopy = new Dictionary<Guid, InstanceKeyView>(totalKeys); } if (associatedInstanceKeyMetadata != null && associatedInstanceKeyMetadata.Count > 0) { foreach (KeyValuePair<Guid, IDictionary<XName, InstanceValue>> keyMetadata in associatedInstanceKeyMetadata) { InstanceKeyView view = new InstanceKeyView(keyMetadata.Key); view.InstanceKeyState = InstanceKeyState.Associated; view.InstanceKeyMetadata = keyMetadata.Value.ReadOnlyCopy(false); view.InstanceKeyMetadataConsistency = InstanceView.IsBoundToLock ? InstanceValueConsistency.None : InstanceValueConsistency.InDoubt; keysCopy.Add(view.InstanceKey, view); } } if (completedInstanceKeyMetadata != null && completedInstanceKeyMetadata.Count > 0) { foreach (KeyValuePair<Guid, IDictionary<XName, InstanceValue>> keyMetadata in completedInstanceKeyMetadata) { InstanceKeyView view = new InstanceKeyView(keyMetadata.Key); view.InstanceKeyState = InstanceKeyState.Completed; view.InstanceKeyMetadata = keyMetadata.Value.ReadOnlyCopy(false); view.InstanceKeyMetadataConsistency = consistency; keysCopy.Add(view.InstanceKey, view); } } InstanceView.InstanceState = state; InstanceView.InstanceData = instanceDataCopy; InstanceView.InstanceDataConsistency = consistency; InstanceView.InstanceMetadata = instanceMetadataCopy; InstanceView.InstanceMetadataConsistency = consistency; InstanceView.InstanceKeys = keysCopy == null ? null : new ReadOnlyDictionaryInternal<Guid, InstanceKeyView>(keysCopy); InstanceView.InstanceKeysConsistency = consistency; }
public void WroteInstanceKeyMetadataValue(Guid key, XName name, InstanceValue value) { InstanceKeyView view; if (key == Guid.Empty) { throw Fx.Exception.Argument("key", SRCore.InvalidKeyArgument); } if (name == null) { throw Fx.Exception.ArgumentNull("name"); } if (value == null) { throw Fx.Exception.ArgumentNull("value"); } this.ThrowIfNotLocked(); this.ThrowIfCompleted(); this.ThrowIfNotTransactional("WroteInstanceKeyMetadataValue"); if (!this.InstanceView.InstanceKeys.TryGetValue(key, out view)) { if (this.InstanceView.InstanceKeysConsistency == InstanceValueConsistency.None) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.KeyNotAssociated)); } if (!value.IsWriteOnly() && !value.IsDeletedValue) { Dictionary<Guid, InstanceKeyView> dictionary = new Dictionary<Guid, InstanceKeyView>(this.InstanceView.InstanceKeys); view = new InstanceKeyView(key); view.AccumulatedMetadataWrites.Add(name, value); view.InstanceKeyMetadataConsistency = InstanceValueConsistency.Partial; dictionary[view.InstanceKey] = view; this.InstanceView.InstanceKeys = new ReadOnlyDictionary<Guid, InstanceKeyView>(dictionary, false); System.Runtime.DurableInstancing.InstanceView instanceView = this.InstanceView; instanceView.InstanceKeysConsistency |= InstanceValueConsistency.Partial; } } else { view.AccumulatedMetadataWrites.Add(name, value); } }
public void ReadInstanceKeyMetadata(Guid key, IDictionary<XName, InstanceValue> metadata, bool complete) { InstanceKeyView view; if (key == Guid.Empty) { throw Fx.Exception.Argument("key", SRCore.InvalidKeyArgument); } this.ThrowIfNoInstance(); this.ThrowIfNotActive("ReadInstanceKeyMetadata"); if (!this.InstanceView.InstanceKeys.TryGetValue(key, out view)) { if (this.InstanceView.InstanceKeysConsistency == InstanceValueConsistency.None) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.KeyNotAssociated)); } Dictionary<Guid, InstanceKeyView> dictionary = new Dictionary<Guid, InstanceKeyView>(this.InstanceView.InstanceKeys); view = new InstanceKeyView(key); if (complete) { view.InstanceKeyMetadata = metadata.ReadOnlyCopy(false); view.InstanceKeyMetadataConsistency = InstanceValueConsistency.None; } else { view.InstanceKeyMetadata = metadata.ReadOnlyMergeInto(null, false); view.InstanceKeyMetadataConsistency = InstanceValueConsistency.Partial; } if (!this.InstanceView.IsBoundToLock && (this.InstanceView.InstanceState != InstanceState.Completed)) { view.InstanceKeyMetadataConsistency |= InstanceValueConsistency.InDoubt; } dictionary[view.InstanceKey] = view; this.InstanceView.InstanceKeys = new ReadOnlyDictionary<Guid, InstanceKeyView>(dictionary, false); } else if (view.InstanceKeyMetadataConsistency != InstanceValueConsistency.None) { if (complete) { view.InstanceKeyMetadata = metadata.ReadOnlyCopy(false); view.InstanceKeyMetadataConsistency = (this.InstanceView.IsBoundToLock || (this.InstanceView.InstanceState == InstanceState.Completed)) ? InstanceValueConsistency.None : InstanceValueConsistency.InDoubt; } else if ((this.InstanceView.IsBoundToLock || (this.InstanceView.InstanceState == InstanceState.Completed)) && ((view.InstanceKeyMetadataConsistency & InstanceValueConsistency.InDoubt) != InstanceValueConsistency.None)) { view.InstanceKeyMetadata = metadata.ReadOnlyMergeInto(null, false); view.InstanceKeyMetadataConsistency = InstanceValueConsistency.Partial; } else { view.InstanceKeyMetadata = metadata.ReadOnlyMergeInto(view.InstanceKeyMetadata, false); view.InstanceKeyMetadataConsistency |= InstanceValueConsistency.Partial; } } }
public void LoadedInstance(InstanceState state, IDictionary<XName, InstanceValue> instanceData, IDictionary<XName, InstanceValue> instanceMetadata, IDictionary<Guid, IDictionary<XName, InstanceValue>> associatedInstanceKeyMetadata, IDictionary<Guid, IDictionary<XName, InstanceValue>> completedInstanceKeyMetadata) { if (state == InstanceState.Uninitialized) { if ((instanceData != null) && (instanceData.Count > 0)) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.UninitializedCannotHaveData)); } } else if (state == InstanceState.Completed) { if ((associatedInstanceKeyMetadata != null) && (associatedInstanceKeyMetadata.Count > 0)) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.CompletedMustNotHaveAssociatedKeys)); } } else if (state != InstanceState.Initialized) { throw Fx.Exception.Argument("state", SRCore.InvalidInstanceState); } this.ThrowIfNoInstance(); this.ThrowIfNotActive("PersistedInstance"); InstanceValueConsistency consistency = (this.InstanceView.IsBoundToLock || (state == InstanceState.Completed)) ? InstanceValueConsistency.None : InstanceValueConsistency.InDoubt; ReadOnlyDictionary<XName, InstanceValue> dictionary = instanceData.ReadOnlyCopy(false); ReadOnlyDictionary<XName, InstanceValue> dictionary2 = instanceMetadata.ReadOnlyCopy(false); Dictionary<Guid, InstanceKeyView> dictionary3 = null; int capacity = ((associatedInstanceKeyMetadata != null) ? associatedInstanceKeyMetadata.Count : 0) + ((completedInstanceKeyMetadata != null) ? completedInstanceKeyMetadata.Count : 0); if (capacity > 0) { dictionary3 = new Dictionary<Guid, InstanceKeyView>(capacity); } if ((associatedInstanceKeyMetadata != null) && (associatedInstanceKeyMetadata.Count > 0)) { foreach (KeyValuePair<Guid, IDictionary<XName, InstanceValue>> pair in associatedInstanceKeyMetadata) { InstanceKeyView view = new InstanceKeyView(pair.Key) { InstanceKeyState = InstanceKeyState.Associated, InstanceKeyMetadata = pair.Value.ReadOnlyCopy(false), InstanceKeyMetadataConsistency = this.InstanceView.IsBoundToLock ? InstanceValueConsistency.None : InstanceValueConsistency.InDoubt }; dictionary3.Add(view.InstanceKey, view); } } if ((completedInstanceKeyMetadata != null) && (completedInstanceKeyMetadata.Count > 0)) { foreach (KeyValuePair<Guid, IDictionary<XName, InstanceValue>> pair2 in completedInstanceKeyMetadata) { InstanceKeyView view2 = new InstanceKeyView(pair2.Key) { InstanceKeyState = InstanceKeyState.Completed, InstanceKeyMetadata = pair2.Value.ReadOnlyCopy(false), InstanceKeyMetadataConsistency = consistency }; dictionary3.Add(view2.InstanceKey, view2); } } this.InstanceView.InstanceState = state; this.InstanceView.InstanceData = dictionary; this.InstanceView.InstanceDataConsistency = consistency; this.InstanceView.InstanceMetadata = dictionary2; this.InstanceView.InstanceMetadataConsistency = consistency; this.InstanceView.InstanceKeys = (dictionary3 == null) ? null : new ReadOnlyDictionary<Guid, InstanceKeyView>(dictionary3, false); this.InstanceView.InstanceKeysConsistency = consistency; }
public void CompletedInstanceKey(Guid key) { InstanceKeyView view; if (key == Guid.Empty) { throw Fx.Exception.Argument("key", SRCore.InvalidKeyArgument); } this.ThrowIfNotLocked(); this.ThrowIfCompleted(); this.ThrowIfNotTransactional("CompletedInstanceKey"); this.InstanceView.InstanceKeys.TryGetValue(key, out view); if ((this.InstanceView.InstanceKeysConsistency & InstanceValueConsistency.InDoubt) == InstanceValueConsistency.None) { if (view != null) { if (view.InstanceKeyState == InstanceKeyState.Completed) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.KeyAlreadyCompleted)); } } else if ((this.InstanceView.InstanceKeysConsistency & InstanceValueConsistency.Partial) == InstanceValueConsistency.None) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.KeyNotAssociated)); } } if (view != null) { view.InstanceKeyState = InstanceKeyState.Completed; } else { Dictionary<Guid, InstanceKeyView> dictionary = new Dictionary<Guid, InstanceKeyView>(this.InstanceView.InstanceKeys); InstanceKeyView view2 = new InstanceKeyView(key) { InstanceKeyState = InstanceKeyState.Completed, InstanceKeyMetadataConsistency = InstanceValueConsistency.Partial }; dictionary[view2.InstanceKey] = view2; this.InstanceView.InstanceKeys = new ReadOnlyDictionary<Guid, InstanceKeyView>(dictionary, false); } }