Exemple #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;
                _tail = key;
            }

            _evictionIndexEntriesSize -= remove;
            _evictionIndexEntriesSize += add;

            if (incrementKeyCount)
            {
                _keysCount++;
            }
        }
Exemple #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 currentKey)
        {
            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 currentEntry = (EvictionIndexEntry)_index[currentKey];
                EvictionIndexEntry indexEntry   = new EvictionIndexEntry();
                indexEntry.Insert(value);

                addSize = indexEntry.InMemorySize;

                _index[key] = indexEntry;

                if (currentEntry != null)
                {
                    EvictionIndexEntry nextEntry = _index[currentEntry.Next] as EvictionIndexEntry;

                    if (nextEntry != null)
                    {
                        indexEntry.Next     = currentEntry.Next;
                        indexEntry.Previous = currentKey;

                        currentEntry.Next  = key;
                        nextEntry.Previous = key;
                    }
                    else
                    {
                        currentEntry.Next   = key;
                        indexEntry.Previous = currentKey;

                        if (currentKey == _tail)
                        {
                            _tail = key;
                        }
                    }
                }
                else
                {
                    if (_head == -1)
                    {
                        _head = key;

                        if (_tail == -1)
                        {
                            _tail = key;
                        }
                    }
                    else
                    {
                        EvictionIndexEntry headEntry = (EvictionIndexEntry)_index[_head];

                        indexEntry.Next    = _head;
                        headEntry.Previous = key;
                        _head = key;
                    }
                }
            }

            _evictionIndexEntriesSize -= removeSize;
            _evictionIndexEntriesSize += addSize;

            if (incrementKeyCount)
            {
                _keysCount++;
            }
        }