Пример #1
0
        /// <summary>
        /// Invoke the passed <see cref="IEntryProcessor"/> against the
        /// entries specified by the passed cache and entries.
        /// </summary>
        /// <remarks>
        /// The invocation is made thread safe by locking the corresponding
        /// keys on the cache. If an attempt to lock all the entries at once
        /// fails, they will be processed individually one-by-one.
        /// </remarks>
        /// <param name="cache">
        /// The <see cref="IConcurrentCache"/> that the
        /// <b>IEntryProcessor</b> works against.
        /// </param>
        /// <param name="entries">
        /// A collection of <see cref="IInvocableCacheEntry"/> objects to
        /// process.
        /// </param>
        /// <param name="agent">
        /// The <b>IEntryProcessor</b> to use to process the specified keys.
        /// </param>
        /// <returns>
        /// An <b>IDictionary</b> containing the results of invoking the
        /// <b>IEntryProcessor</b> against each of the specified entry.
        /// </returns>
        public static IDictionary InvokeAllLocked(IConcurrentCache cache,
                                                  ICollection entries, IEntryProcessor agent)
        {
            ICollection keys = ConverterCollections.GetCollection(entries,
                                                                  ENTRY_TO_KEY_CONVERTER, NullImplementation.GetConverter());

            // try to lock them all at once
            var listLocked = LockAll(cache, keys, 0);

            if (listLocked == null)
            {
                // the attempt failed; do it one-by-one
                var result = new HashDictionary(entries.Count);
                foreach (IInvocableCacheEntry entry in entries)
                {
                    result[entry.Key] = InvokeLocked(cache, entry, agent);
                }
                return(result);
            }
            try
            {
                return(agent.ProcessAll(entries));
            }
            finally
            {
                UnlockAll(cache, listLocked);
            }
        }
Пример #2
0
        /// <summary>
        /// Process a set of <see cref="IInvocableCacheEntry"/> objects
        /// in order to produce an aggregated result.
        /// </summary>
        /// <param name="entries">
        /// A collection of read-only <b>IInvocableCacheEntry</b>
        /// objects to aggregate.
        /// </param>
        /// <returns>
        /// The aggregated result from processing the entries.
        /// </returns>
        public virtual object Aggregate(ICollection entries)
        {
            PartialResult resultPartial = new PartialResult(Comparer);
            IConverter    converter     = new ExtractingConverter(Extractor);

            AddToResult(ConverterCollections.GetCollection(entries, converter, converter), resultPartial);

            return(IsParallel
                   ? (object)resultPartial
                   : (object)FinalizeResult(resultPartial));
        }
        public void ConverterCollectionTests()
        {
            ArrayList  list  = new ArrayList();
            IConverter cDown = new ConvertDown();
            IConverter cUp   = new ConvertUp();

            list.Add(cDown.Convert(1));
            list.Add(cDown.Convert(2));
            list.Add(cDown.Convert(3));

            ICollection convCol = ConverterCollections.GetCollection(list, cUp, cDown);

            Assert.IsNotNull(convCol);
            Assert.AreEqual(convCol.Count, list.Count);
            Assert.AreEqual(convCol.IsSynchronized, list.IsSynchronized);
            Assert.AreEqual(convCol.SyncRoot, list.SyncRoot);

            object[] a = new object[convCol.Count];
            convCol.CopyTo(a, 0);
            Assert.AreEqual(a.Length, convCol.Count);
            for (int i = 0; i < convCol.Count; i++)
            {
                Assert.AreEqual(a[i], cUp.Convert(list[i]));
            }

            foreach (object o in convCol)
            {
                Assert.IsTrue(list.Contains(cDown.Convert(o)));
            }

            Assert.IsInstanceOf(typeof(ConverterCollections.ConverterCollection), convCol);
            ConverterCollections.ConverterCollection cc = convCol as ConverterCollections.ConverterCollection;
            Assert.IsNotNull(cc);
            Assert.AreEqual(cc.Collection, list);
            Assert.AreEqual(cc.ConverterDown, cDown);
            Assert.AreEqual(cc.ConverterUp, cUp);

            cc.Invalidate();
            Assert.IsNull(cc.Collection);
            Assert.IsNull(cc.ConverterDown);
            Assert.IsNull(cc.ConverterUp);
        }