Esempio n. 1
0
        /// <summary>
        /// 停止计时器队列的运行
        /// </summary>
        /// <param name="queue">计时器队列</param>
        public void Cancel(ITimerQueue queue)
        {
            var timerQueue = queue as TimerQueue;

            Guard.NotNull(timerQueue, "timerQueue");
            executeList.Remove(timerQueue);
        }
Esempio n. 2
0
        public void RemoveTest()
        {
            int num  = 50000;
            var list = new SortSet <int, int>();
            var lst  = new List <int>();
            var rand = new System.Random();

            for (var i = 0; i < num; i++)
            {
                var val = rand.Next();
                lst.Add(val);
                list.Add(val, val);
            }

            foreach (int n in lst)
            {
                list.Remove(n);
            }

            foreach (var val in list)
            {
                Assert.Fail();
                if (val < 0)
                {
                }
            }

            if (list.Count != 0)
            {
                Assert.Fail();
            }
        }
Esempio n. 3
0
        public void TestRandValue()
        {
            var sortSets = new SortSet <int, int>();
            var random   = new Random();

            for (var i = 1000; i >= 1; i--)
            {
                sortSets.Add(i, random.Next(0, 1000));
            }

            for (var i = 1; i <= 1000; i++)
            {
                if (sortSets.Remove(i))
                {
                    continue;
                }

                Assert.Fail("can not remove i : " + i);
            }

            Assert.AreEqual(0, sortSets.Count);
            foreach (var sortSet in sortSets)
            {
                Assert.Fail();
            }
        }
Esempio n. 4
0
        public void TestIterationsDeleted()
        {
            var master = new SortSet <int, int>(0.25, 32);

            for (var i = 0; i < 100; i++)
            {
                master.Add(i, i);
            }

            var assertList = new List <int>()
            {
                0, 10, 20, 30, 40, 50, 60, 70, 80, 90
            };
            var tmpList = new List <int>();

            foreach (var i in master)
            {
                tmpList.Add(i);
                master.Remove(i);
                master.Remove(i + 1);
                master.Remove(i + 2);
                master.Remove(i + 3);
                master.Remove(i + 4);
                master.Remove(i + 5);
                master.Remove(i + 6);
                master.Remove(i + 7);
                master.Remove(i + 8);
                master.Remove(i + 9);
            }

            var n = 0;

            foreach (var v in tmpList)
            {
                Assert.AreEqual(assertList[n++], v);
            }

            Assert.AreEqual(assertList.Count, tmpList.Count);
        }
Esempio n. 5
0
        public void RemoveNotExistsElementTest()
        {
            var master = new SortSet <int, int>(0.25, 32);

            master.Add(10, 10);
            master.Add(90, 10);
            master.Add(20, 10);
            master.Add(80, 10);

            if (master.Remove(8888))
            {
                Assert.Fail();
            }
        }
Esempio n. 6
0
        /// <inheritdoc />
        public bool GC(int ttl, int maxSize)
        {
            if (!Enable)
            {
                return(false);
            }

            cacheCollected = true;

            var candidates  = new SortSet <IMetaData, DateTime>();
            var directories = new Queue <string>();

            void AddCandidate(string file)
            {
                var meta = fileSystem.GetMetaData(file);

                candidates.Add(meta, meta.LastAccessTime);
            }

            if (!fileSystem.Exists(CacheDirectory, FileSystemOptions.Directory))
            {
                return(true);
            }

            var contents = fileSystem.GetContents(CacheDirectory);

            Array.ForEach(contents.GetDirectories(), directories.Enqueue);
            Array.ForEach(contents.GetFiles(), AddCandidate);

            while (directories.Count > 0)
            {
#pragma warning disable S4158 // bug: Empty collections should not be accessed or iterated
                contents = fileSystem.GetContents(directories.Dequeue());
#pragma warning restore S4158

                Array.ForEach(contents.GetDirectories(), directories.Enqueue);
                Array.ForEach(contents.GetFiles(), AddCandidate);
            }

            var freeSpace    = 0L;
            var deletedFiles = 0;

            // gc with ttl.
            var expire = DateTime.Now.AddSeconds(-ttl);
            foreach (var candidate in candidates)
            {
                if (candidate.LastAccessTime >= expire)
                {
                    // The sorset will have sorted the modification time.
                    break;
                }

                fileSystem.Delete(candidate.Path);
                candidates.Remove(candidate);
                freeSpace += candidate.Size;
                deletedFiles++;
            }

            void PromptFree()
            {
                io.WriteError($"Cache garbage collection completed, delete {deletedFiles} files, free {AbstractHelper.FormatMemory(freeSpace)} space.");
            }

            // gc with maxSize
            var totalSize = fileSystem.GetMetaData(CacheDirectory).Size;
            if (totalSize < maxSize)
            {
                PromptFree();
                return(true);
            }

            foreach (var candidate in candidates)
            {
                if (totalSize < maxSize)
                {
                    break;
                }

                fileSystem.Delete(candidate.Path);
                totalSize -= candidate.Size;
                freeSpace += candidate.Size;
                deletedFiles++;
            }

            PromptFree();
            return(true);
        }
Esempio n. 7
0
        /// <summary>
        /// 转换到指定目标并且删除
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="sortset">有序集</param>
        /// <param name="obj">对象</param>
        /// <returns>是否成功</returns>
        private bool ConvertAndRemove <T>(SortSet <T, int> sortset, object obj) where T : class
        {
            var ele = obj as T;

            return(ele != null && sortset.Remove(ele));
        }