Пример #1
0
        internal void Add(long key, object value)
        {
            if (_index.Count == 0)
            {
                _head = key;
            }

            int  add = 0, remove = 0;
            bool incrementKeyCount = true;

            if (_index.Contains(key))
            {
                EvictionIndexEntry indexEntry = (EvictionIndexEntry)_index[key];
                if (indexEntry != null)
                {
                    remove = indexEntry.InMemorySize;

                    if (indexEntry.Contains(value))
                    {
                        incrementKeyCount = false;
                    }


                    indexEntry.Insert(value);

                    add = indexEntry.InMemorySize;
                }
            }
            else
            {
                EvictionIndexEntry indexEntry = new EvictionIndexEntry();
                indexEntry.Insert(value);

                add = indexEntry.InMemorySize;

                _index[key] = indexEntry;

                EvictionIndexEntry prevEntry = _index[_tail] as EvictionIndexEntry;

                if (prevEntry != null)
                {
                    prevEntry.Next = key;
                }
                indexEntry.Previous = _tail;
            }
            if (key > _tail)
            {
                _tail = key;
            }

            _evictionIndexEntriesSize -= remove;
            _evictionIndexEntriesSize += add;

            if (incrementKeyCount)
            {
                _keysCount++;
            }
        }
Пример #2
0
        /// <summary>
        /// Add method only adds the new node at the tail...
        /// Insert method can add the new nodes in between also....
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        internal void Insert(long key, object value, long previous, long next)
        {
            EvictionIndexEntry nextEntry = _index[next] as EvictionIndexEntry;
            EvictionIndexEntry prevEntry = _index[previous] as EvictionIndexEntry;

            if (_index.Count == 0 || key < _head)
            {
                _head = key;
            }


            int  addSize = 0, removeSize = 0;
            bool incrementKeyCount = true;


            if (_index.Contains(key))
            {
                EvictionIndexEntry indexEntry = (EvictionIndexEntry)_index[key];
                if (indexEntry != null)
                {
                    removeSize = indexEntry.InMemorySize;

                    if (indexEntry.Contains(value))
                    {
                        incrementKeyCount = false;
                    }

                    indexEntry.Insert(value);

                    addSize = indexEntry.InMemorySize;
                }
            }
            else
            {
                EvictionIndexEntry indexEntry = new EvictionIndexEntry();
                indexEntry.Insert(value);

                addSize = indexEntry.InMemorySize;

                _index[key] = indexEntry;

                //very first node
                if (prevEntry == null && nextEntry == null)
                {
                    indexEntry.Next     = -1;;
                    indexEntry.Previous = -1;
                }
                //insert at begining
                else if (prevEntry == null && nextEntry != null)
                {
                    indexEntry.Next     = next;
                    indexEntry.Previous = -1;
                    nextEntry.Previous  = key;
                }
                //insert at end
                else if (prevEntry != null && nextEntry == null)
                {
                    indexEntry.Previous = previous;
                    indexEntry.Next     = -1;
                    prevEntry.Next      = key;
                }
                //insert in between the two nodes
                else
                {
                    indexEntry.Previous = previous;
                    indexEntry.Next     = next;
                    prevEntry.Next      = key;
                    nextEntry.Previous  = key;
                }
            }
            if (key > _tail)
            {
                _tail = key;
            }

            _evictionIndexEntriesSize -= removeSize;
            _evictionIndexEntriesSize += addSize;

            if (incrementKeyCount)
            {
                _keysCount++;
            }
        }