public void AddUnrepresentedKeyWithRecreateIfMissing(TWeakOwningKey weakOwningKey, Func <TSharedContained> toCreateIfMissing) { var newKeyHolderID = Guid.NewGuid(); var newKeyHolder = NewWeakOwningKeyLifeWatcher(mLock, IDToWeakOfOwningKey, newKeyHolderID); lock (mLock) { if (PossibleRecovery.TryGetTarget(out var currentContained)) { WeakOwningKeyToContained.Add(weakOwningKey, Tuple.Create(currentContained, newKeyHolder)); // can fail/grenade IDToWeakOfOwningKey.Add(newKeyHolderID, new WeakReference <TWeakOwningKey>(weakOwningKey)); } else { currentContained = toCreateIfMissing(); PossibleRecovery.SetTarget(currentContained); WeakOwningKeyToContained.Add(weakOwningKey, Tuple.Create(currentContained, newKeyHolder)); // can fail/grenade IDToWeakOfOwningKey.Add(newKeyHolderID, new WeakReference <TWeakOwningKey>(weakOwningKey)); } } }
public AddKeyResult AddKeyIfNotAlreadyPresentWithRecreateIfMissing(TWeakOwningKey weakOwningKey, Func <TSharedContained> toCreateIfMissing) { //var newKeyHolderID = Guid.NewGuid(); //var newKeyHolder = NewWeakOwningKeyLifeWatcher(mLock, IDToWeakOfOwningKey, newKeyHolderID); lock (mLock) { if (PossibleRecovery.TryGetTarget(out var currentContained)) { if (WeakOwningKeyToContained.TryGetValue(weakOwningKey, out var existing)) { // already exists - do nothing return(new AddKeyResult(addedWeakOwningKeyAsNew: false, createdIfMissing: false)); // one way or another, it was already there } else { var newKeyHolderID = Guid.NewGuid(); var newKeyHolder = NewWeakOwningKeyLifeWatcher(mLock, IDToWeakOfOwningKey, newKeyHolderID); WeakOwningKeyToContained.Add(weakOwningKey, Tuple.Create(currentContained, newKeyHolder)); // should never fail - no existing IDToWeakOfOwningKey.Add(newKeyHolderID, new WeakReference <TWeakOwningKey>(weakOwningKey)); return(new AddKeyResult(addedWeakOwningKeyAsNew: true, createdIfMissing: false)); } } else { if (WeakOwningKeyToContained.TryGetValue(weakOwningKey, out var existing)) { // already exists - do nothing throw new Exception("Contradiction - Recovery value doesn't exist but value slot does!"); } else { currentContained = toCreateIfMissing(); PossibleRecovery.SetTarget(currentContained); var newKeyHolderID = Guid.NewGuid(); var newKeyHolder = NewWeakOwningKeyLifeWatcher(mLock, IDToWeakOfOwningKey, newKeyHolderID); WeakOwningKeyToContained.Add(weakOwningKey, Tuple.Create(currentContained, newKeyHolder)); // can fail/grenade IDToWeakOfOwningKey.Add(newKeyHolderID, new WeakReference <TWeakOwningKey>(weakOwningKey)); return(new AddKeyResult(addedWeakOwningKeyAsNew: true, createdIfMissing: true)); } } /* * if (PossibleRecovery.TryGetTarget(out var currentContained)) * { * IDToWeakOfOwningKey.Add(newKeyHolderID, new WeakReference<TWeakOwningKey>(weakOwningKey)); * * * } * else * { * IDToWeakOfOwningKey.Add(newKeyHolderID, new WeakReference<TWeakOwningKey>(weakOwningKey)); * * * currentContained = toCreateIfMissing(); * PossibleRecovery.SetTarget(currentContained); * } * * if (WeakOwningKeyToContained.TryGetValue(weakOwningKey, out var existing)) * { // already exists * WeakOwningKeyToContained.Remove() * } * else * { * WeakOwningKeyToContained.Add(weakOwningKey, Tuple.Create(currentContained, newKeyHolder)); * } */ } }