예제 #1
0
파일: UrlQueue.cs 프로젝트: Wrath7/testrepo
        /// <summary>
        /// Return the first i Urls on the queue but don't remove them.
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public new UrlQueueEntity[] Peek(int i)
        {
            UrlQueueEntity[] toReturn;
            if (this.Count > i)
            {
                toReturn = new UrlQueueEntity[i];
            }
            else
            {
                if (this.Count > 0)
                {
                    toReturn = new UrlQueueEntity[this.Count];
                }
                else
                {
                    return new UrlQueueEntity[0];
                }
            }

            int rrPoint = _roundRobinId;
            int rrStop = rrPoint;
            bool first = true;

            int j = 0;
            while ((first || (rrPoint != rrStop)) && j < toReturn.Length)
            {
                first = false;
                if (_roundRobinQueues[rrPoint].Count > 0)
                {
                    try
                    {
                        toReturn[j] = _roundRobinQueues[rrPoint].Peek();
                        j++;
                    }
                    catch
                    {
                        //State changed between when we checked and when we peeked.
                    }
                }
                rrPoint = (rrPoint + 1) % _roundRobinQueues.Count;
            }

            while (j < toReturn.Length)
            {
                UrlQueueEntity uqe = new UrlQueueEntity("Value Hidden In queue", new DateTime(), 1);
                toReturn[j] = uqe;
            }
            return toReturn;
        }
예제 #2
0
파일: UrlQueue.cs 프로젝트: Wrath7/testrepo
        /// <summary>
        /// Insert a new UrlEntity Into the queue.
        /// </summary>
        /// <param name="newEnt"></param>
        /// <returns></returns>
        public bool Insert(UrlQueueEntity newEnt)
        {
            Queue<UrlQueueEntity> siteQueue;
            if (!_siteQueues.TryGetValue(newEnt.SiteId, out siteQueue))
            {
                lock (_wholeQueueLock)
                {
                    if (!_siteQueues.TryGetValue(newEnt.SiteId, out siteQueue))
                    {
                        Dictionary<int, Queue<UrlQueueEntity>> newSiteQueues = new Dictionary<int,Queue<UrlQueueEntity>>();
                        foreach (KeyValuePair<int, Queue<UrlQueueEntity>> kvp in _siteQueues)
                        {
                            newSiteQueues.Add(kvp.Key, kvp.Value);
                        }
                        siteQueue = new Queue<UrlQueueEntity>();
                        newSiteQueues.Add(newEnt.SiteId, siteQueue);

                        _roundRobinQueues.Add(siteQueue);
                        _siteQueues = newSiteQueues;
                    }
                }

            }
            lock (siteQueue)
            {
                siteQueue.Enqueue(newEnt);
            }
            return true;
        }