示例#1
0
        static void Main(string[] args)
        {
            // В проекте CompositeKeyCollection имеется 3 реализации класса коллекций с композитными (составными) ключами:
            // 1. CompositeKeyCollection<TKey1, Tkey2, TData> - обычная обобщенная реализация класса коллекции (не потокобезопасная)
            // 2. ThreadSafeCompositeKeyCollection<Tkey1, Tkey2, TData> - потокобезопасная реализация класса CompositeKeyCollection<TKey1, TKey2, TData>
            // 3. TransactionalCompositeKeyCollection<TKey1, TKey2, TData> - класс коллекции с комопозитными ключами и поддержкой транзакционности на операции вставки и удаления записей в коллекцию (PS.: пока еще не потокобезопасная)

            // Примеры использования коллекций:
            // 1. CompositeKeyCollection<TKey1, Tkey2, TData>
            var currentDate          = DateTime.Today;
            var tasksDoneCollection1 = new CompositeKeyCollection <UserType, string, int>();

            tasksDoneCollection1.Add(new UserType(currentDate), "Developers", 10);
            tasksDoneCollection1.Add(new UserType(currentDate), "Managers", 12);
            tasksDoneCollection1.Add(new UserType(currentDate.AddDays(1)), "Developers", 7);
            PrintCollection(tasksDoneCollection1);

            var key1 = new UserType(currentDate);
            var key2 = "Developers";

            tasksDoneCollection1.Remove(key1, key2);
            PrintCollection(tasksDoneCollection1);

            key1 = new UserType(currentDate.AddDays(1));
            var items = tasksDoneCollection1.Find(key1, key2);

            Console.WriteLine("Items found: {0}", items.Count);

            var value = tasksDoneCollection1[key1, key2];

            Console.WriteLine("Value in dictionary: {0}", value);

            tasksDoneCollection1.Clear();


            // 2. ThreadSafeCompositeKeyCollection<Tkey1, Tkey2, TData>
            _collection2 = new ThreadSafeCompositeKeyCollection <UserType, string, int>();
            Task.Run(() => RunFirstWriteThread());
            Task.Run(() => RunSecondWriteThread());
            Task.Run(() => RunShowKeyCountThread());


            // 3.TransactionalDictionary<TKey1, TKey2, TData>
            var collection3 = new TransactionalCompositeKeyCollection <UserType, string, int>();

            collection3.Add(new UserType(DateTime.Today), "Managers", 10);
            collection3.Add(new UserType(DateTime.Today), "Analitics", 15);

            // adding an invalid object to the collection
            try
            {
                collection3.Add(null, "Developers", 20);
            }
            catch (ArgumentNullException ex) { }

            Console.Read();
        }
        public void UpdateCollectionConcurentlyTest()
        {
            _concurentCollection = new ThreadSafeCompositeKeyCollection <UserType, string, int>();

            Task.Run(() => RunFirstWriteThread()).Wait();
            Task.Run(() => RunSecondWriteThread()).Wait();
            Task.Run(() => RunThirdWriteThread()).Wait();
            Task.Run(() => RunFirstReadThread()).Wait();
            Task.Run(() => RunSecondReadThread()).Wait();
            Task.Run(() => RunFirstRemovingThread()).Wait();
        }
        public void LinqComparisonTest()
        {
            _thSafeCollection = new ThreadSafeCompositeKeyCollection <int, DateTime, string>();
            _kvpDictionary    = new Dictionary <KeyValuePair <int, DateTime>, string>();

            Random   rnd = new Random();
            DateTime dt  = DateTime.Today;

            string value = "Developers";

            for (int idx = 0; idx < 100000; idx++)
            {
                var datetime = dt.AddDays(-idx);
                _kvpDictionary.Add(new KeyValuePair <int, DateTime>(idx, datetime), value);
                _thSafeCollection.Add(idx, datetime, value);
            }

            int      key1 = 55000;
            DateTime key2 = DateTime.Today.AddDays(-55000);
            var      customCollectionSearchTime     = CountSearchTimeOnCustomCollection(key1, key2);
            var      dictionaryCollectionSearchTime = CountSearchTimeOnDictionary(key1, key2);


            dt = new DateTime(1900, 01, 01);
            for (int idx = 0; idx < 100000; idx++)
            {
                var datetime = dt.AddDays(idx);
                value = "Managers";
                _kvpDictionary.Add(new KeyValuePair <int, DateTime>(idx, datetime), value);
                _thSafeCollection.Add(idx, datetime, value);
            }

            var customCollectionSearchTime2     = CountSearchTimeOnCustomCollection(key1, key2);
            var dictionaryCollectionSearchTime2 = CountSearchTimeOnDictionary(key1, key2);


            dt = new DateTime(1500, 01, 01);
            for (int idx = 0; idx < 100000; idx++)
            {
                var datetime = dt.AddMinutes(idx);
                value = "Testers";
                _kvpDictionary.Add(new KeyValuePair <int, DateTime>(idx, datetime), value);
                _thSafeCollection.Add(idx, datetime, value);
            }

            var customCollectionSearchTime3     = CountSearchTimeOnCustomCollection(key1, key2);
            var dictionaryCollectionSearchTime3 = CountSearchTimeOnDictionary(key1, key2);

            dt = new DateTime(2200, 01, 01);
            for (int idx = 0; idx < 100000; idx++)
            {
                var datetime = dt.AddMinutes(idx);
                value = "Analytics";
                _kvpDictionary.Add(new KeyValuePair <int, DateTime>(idx, datetime), value);
                _thSafeCollection.Add(idx, datetime, value);
            }

            var customCollectionSearchTime4     = CountSearchTimeOnCustomCollection(key1, key2);
            var dictionaryCollectionSearchTime4 = CountSearchTimeOnDictionary(key1, key2);

            Debug.WriteLine("customCollectionSearchTime = {0}", customCollectionSearchTime);
            Debug.WriteLine("dictionaryCollectionSearchTime = {0}", dictionaryCollectionSearchTime);
            Debug.WriteLine("customCollectionSearchTime2 = {0}", customCollectionSearchTime2);
            Debug.WriteLine("dictionaryCollectionSearchTime2 = {0}", dictionaryCollectionSearchTime2);
            Debug.WriteLine("customCollectionSearchTime3 = {0}", customCollectionSearchTime3);
            Debug.WriteLine("dictionaryCollectionSearchTime3 = {0}", dictionaryCollectionSearchTime3);
            Debug.WriteLine("customCollectionSearchTime4 = {0}", customCollectionSearchTime4);
            Debug.WriteLine("dictionaryCollectionSearchTime4 = {0}", dictionaryCollectionSearchTime4);
        }