Example #1
0
        public MockReliableDictionary(Uri uri)
            : base(uri)
        {
            // Set the OnDictionaryChanged callback to fire the DictionaryChanged event.
            InternalDictionaryChanged +=
                (sender, c) =>
            {
                if (DictionaryChanged != null)
                {
                    NotifyDictionaryChangedEventArgs <TKey, TValue> e = null;
                    switch (c.ChangeType)
                    {
                    case ChangeType.Added:
                        e = new NotifyDictionaryItemAddedEventArgs <TKey, TValue>(c.Transaction, c.Key, c.Added);
                        break;

                    case ChangeType.Removed:
                        e = new NotifyDictionaryItemRemovedEventArgs <TKey, TValue>(c.Transaction, c.Key);
                        break;

                    case ChangeType.Updated:
                        e = new NotifyDictionaryItemUpdatedEventArgs <TKey, TValue>(c.Transaction, c.Key, c.Added);
                        break;
                    }

                    DictionaryChanged.Invoke(this, e);
                }

                MockDictionaryChanged?.Invoke(this, c);
            };
        }
Example #2
0
        /// <summary>
        ///     Handles the DictionaryChanged event of the Dictionary control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="string" /> instance containing the event data.</param>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        private void Dictionary_DictionaryChanged(object sender, NotifyDictionaryChangedEventArgs <string, UserState> e)
        {
            if (this.Partition.WriteStatus != PartitionAccessStatus.Granted)
            {
                return;
            }
            IReliableDictionary <string, UserState> dictionary = sender as IReliableDictionary <string, UserState>;

            switch (e.Action)
            {
            case NotifyDictionaryChangedAction.Add:
                NotifyDictionaryItemAddedEventArgs <string, UserState> add = e as NotifyDictionaryItemAddedEventArgs <string, UserState>;
                this.sendService.SendMessageAsync(new FabricNotifyModel
                {
                    Action        = NotifyAction.Add,
                    Value         = add?.Value.ToJson(),
                    ServiceName   = this.Context.ServiceName.AbsoluteUri,
                    DictionaryKey = dictionary?.Name.AbsolutePath,
                    Key           = null
                }).GetAwaiter().GetResult();
                break;

            case NotifyDictionaryChangedAction.Update:
                NotifyDictionaryItemUpdatedEventArgs <string, UserState> update = e as NotifyDictionaryItemUpdatedEventArgs <string, UserState>;
                this.sendService.SendMessageAsync(new FabricNotifyModel
                {
                    Action        = NotifyAction.Update,
                    Value         = update?.Value.ToJson(),
                    ServiceName   = this.Context.ServiceTypeName,
                    DictionaryKey = dictionary?.Name.AbsolutePath,
                    Key           = null
                }).GetAwaiter().GetResult();
                break;

            case NotifyDictionaryChangedAction.Remove:
                NotifyDictionaryItemRemovedEventArgs <string, UserState> remove = e as NotifyDictionaryItemRemovedEventArgs <string, UserState>;
                this.sendService.SendMessageAsync(new FabricNotifyModel
                {
                    Action        = NotifyAction.Delete,
                    Key           = remove?.Key,
                    ServiceName   = this.Context.ServiceTypeName,
                    DictionaryKey = dictionary?.Name.AbsolutePath,
                    Value         = null
                }).GetAwaiter().GetResult();
                break;

            case NotifyDictionaryChangedAction.Clear:
                break;

            case NotifyDictionaryChangedAction.Rebuild:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Example #3
0
 /// <summary>
 /// Called when a dictionary item add notification has been
 /// triggered. Add the new key to our secondary index.
 /// </summary>
 /// <param name="e"></param>
 private void ProcessDictionaryAddNotification(NotifyDictionaryItemAddedEventArgs <string, Order> e)
 {
     if (e?.Value != null)
     {
         lock (this.lockObject)
         {
             this.SecondaryIndex = this.SecondaryIndex.Add(e.Value);
         }
     }
 }
        private void Dictionary_DictionaryChanged(object sender, NotifyDictionaryChangedEventArgs <int, NationalCountyStats> e)
        {
            switch (e.Action)
            {
            case NotifyDictionaryChangedAction.Clear:
                return;

            case NotifyDictionaryChangedAction.Add:
                NotifyDictionaryItemAddedEventArgs <int, NationalCountyStats> addEvent = e as NotifyDictionaryItemAddedEventArgs <int, NationalCountyStats>;

                long tmp = -1;

                if (this.statsDictionary.TryGetValue("totalDoctors", out tmp))
                {
                    this.statsDictionary["totalDoctors"]           += addEvent.Value.DoctorCount;
                    this.statsDictionary["totalPatientCount"]      += addEvent.Value.PatientCount;
                    this.statsDictionary["totalHealthReportCount"] += addEvent.Value.HealthReportCount;
                }
                else
                {
                    this.statsDictionary["totalDoctors"]           = addEvent.Value.DoctorCount;
                    this.statsDictionary["totalPatientCount"]      = addEvent.Value.PatientCount;
                    this.statsDictionary["totalHealthReportCount"] = addEvent.Value.HealthReportCount;
                }

                this.historyDictionary[addEvent.Key] = new DataSet(
                    addEvent.Value.DoctorCount,
                    addEvent.Value.PatientCount,
                    addEvent.Value.HealthReportCount);
                return;

            case NotifyDictionaryChangedAction.Update:
                NotifyDictionaryItemUpdatedEventArgs <int, NationalCountyStats> updateEvent =
                    e as NotifyDictionaryItemUpdatedEventArgs <int, NationalCountyStats>;
                this.statsDictionary["totalDoctors"]           += (updateEvent.Value.DoctorCount - this.historyDictionary[updateEvent.Key].totalDoctors);
                this.statsDictionary["totalPatientCount"]      += (updateEvent.Value.PatientCount - this.historyDictionary[updateEvent.Key].totalPatientCount);
                this.statsDictionary["totalHealthReportCount"] +=
                    (updateEvent.Value.HealthReportCount - this.historyDictionary[updateEvent.Key].totalHealthReportCount);
                this.historyDictionary[updateEvent.Key] = new DataSet(
                    updateEvent.Value.DoctorCount,
                    updateEvent.Value.PatientCount,
                    updateEvent.Value.HealthReportCount);
                return;

            case NotifyDictionaryChangedAction.Remove:
                return;

            default:
                break;
            }
        }
            private void OnCompletedNoCheck(bool committed)
            {
                bool   existsAfterTx;
                TValue valueToSignal;
                NotifyDictionaryChangedEventArgs <TKey, TValue> changeArgs = null;

                lock (_owner._syncLock)
                {
                    _owner._pendingTransactions.Remove(_key);
                    if (!committed)
                    {
                        if (Exists)
                        {
                            _owner._internalDictionary[_key] = OriginalValue;
                        }
                        else
                        {
                            _owner._internalDictionary.TryRemove(_key, out _);
                        }

                        existsAfterTx = _owner._internalDictionary.TryGetValue(_key, out valueToSignal);
                    }
                    else
                    {
                        existsAfterTx = _owner._internalDictionary.TryGetValue(_key, out valueToSignal);

                        if (Exists)
                        {
                            changeArgs = existsAfterTx
                                ? (NotifyDictionaryChangedEventArgs <TKey, TValue>)
                                         new NotifyDictionaryItemUpdatedEventArgs <TKey, TValue>(_transaction, _key, valueToSignal)
                                : new NotifyDictionaryItemRemovedEventArgs <TKey, TValue>(_transaction, _key);
                        }
                        else
                        {
                            changeArgs = new NotifyDictionaryItemAddedEventArgs <TKey, TValue>(_transaction, _key, valueToSignal);
                        }
                    }
                }

                while (_waiters?.Count > 0)
                {
                    var waiter = _waiters.Dequeue();
                    Task.Run(() => waiter.Signal(existsAfterTx, valueToSignal));
                }

                if (changeArgs != null)
                {
                    _owner.OnDictionaryChanged(changeArgs);
                }
            }
        public void JsonMessageConverterTest()
        {
            var transaction       = new TransactionMock(100, 200);
            var changes           = new List <ReliableCollectionChange>();
            var dictKey           = new Guid();
            var dictValue         = new User("ashish");
            var itemAddedEventArg = new NotifyDictionaryItemAddedEventArgs <Guid, User>(transaction, dictKey, dictValue);

            changes.Add(new ReliableCollectionChange("mydict", new List <EventArgs>()
            {
                itemAddedEventArg
            }));

            var notifyEvent = new NotifyTransactionAppliedEvent(transaction, changes);

            var jsonMessageConverter = new JsonMessageConverter(this.knownTypes);
            var encodedEvent         = jsonMessageConverter.Serialize(notifyEvent);
            var decodedEvent         = jsonMessageConverter.Deserialize <NotifyTransactionAppliedEvent>(encodedEvent);

            Assert.Equal(100, decodedEvent.Transaction.CommitSequenceNumber);
            Assert.Equal(200, decodedEvent.Transaction.TransactionId);
            Assert.Equal(1, decodedEvent.Changes.Count());

            var firstRCChange = decodedEvent.Changes.First();

            Assert.Equal("mydict", firstRCChange.CollectionName);
            Assert.Equal(1, firstRCChange.EventArgs.Count());

            var firstEventArg = firstRCChange.EventArgs.First();

            Assert.True(firstEventArg is NotifyDictionaryItemAddedEventArgs <Guid, User>);
            var decodedItemAddedArg = firstEventArg as NotifyDictionaryItemAddedEventArgs <Guid, User>;

            Assert.Equal(dictKey, decodedItemAddedArg.Key);
            Assert.Equal(dictValue.name, decodedItemAddedArg.Value.name);
        }
Example #7
0
        public MockReliableDictionary(Uri uri)
            : base(uri, null)
        {
            // Set the OnDictionaryChanged callback to fire the DictionaryChanged event.
            OnDictionaryChanged =
                (c) =>
            {
                if (DictionaryChanged != null)
                {
                    NotifyDictionaryChangedEventArgs <TKey, TValue> e;
                    switch (c.ChangeType)
                    {
                    case ChangeType.Added:
                        e = new NotifyDictionaryItemAddedEventArgs <TKey, TValue>(c.Transaction, c.Key, c.Added);
                        break;

                    case ChangeType.Removed:
                        e = new NotifyDictionaryItemRemovedEventArgs <TKey, TValue>(c.Transaction, c.Key);
                        break;

                    case ChangeType.Updated:
                        e = new NotifyDictionaryItemUpdatedEventArgs <TKey, TValue>(c.Transaction, c.Key, c.Added);
                        break;

                    default:
                        return(false);
                    }

                    DictionaryChanged.Invoke(this, e);
                }

                MockDictionaryChanged?.Invoke(this, c);

                return(true);
            };
        }
        public void ConstructorSetsValue()
        {
            var args = new NotifyDictionaryItemAddedEventArgs <string, string>(ExpectedKey, ExpectedValue);

            Assert.AreEqual(ExpectedValue, args.Value);
        }
        public void ConstructorSetsActionToAdd()
        {
            var args = new NotifyDictionaryItemAddedEventArgs <string, string>(ExpectedKey, ExpectedValue);

            Assert.AreEqual(NotifyDictionaryChangedAction.Add, args.Action);
        }