All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.DataStructures.SlidingIndex Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Alachisoft.NCache.Common.DataStructures;

public class SlidingIndex implements AutoCloseable {
    private static final int MAX_OBSERVATION_INTERVAL = 300;
    private static final int MIN_OBSERVATION_INTERVAL = 1;
    java.util.ArrayList> _mainIndex = new java.util.ArrayList>();
    private int _observationInterval = MIN_OBSERVATION_INTERVAL; //in seocnds;
    private long _entryIdSequence;
    private boolean _checkDuplication;

    public SlidingIndex(int interval) {
        this(interval, false);
    }

    public SlidingIndex(int interval, boolean checkDuplication) {
        if (interval > MIN_OBSERVATION_INTERVAL) //&& interval < MAX_OBSERVATION_INTERVAL)
        {
            _observationInterval = interval;
        }
        _checkDuplication = checkDuplication;
        Alachisoft.NCache.Common.Stats.Clock.StartClock();
    }

    public int GetInterval() {
        return _observationInterval;
    }

    public final boolean AddToIndex(T indexValue) {
        synchronized (this) {
            long currentTime = Alachisoft.NCache.Common.Stats.Clock.getCurrentTimeInSeconds();
            long entryId = _entryIdSequence++;
            InstantaneousIndex indexEntry = null;

            if (_mainIndex.size() == 0) {
                indexEntry = new InstantaneousIndex();
                indexEntry.setEnableDuplicationCheck(_checkDuplication);
                indexEntry.setClockTime(currentTime);
                _mainIndex.add(indexEntry);
            } else {
                if (_checkDuplication && CheckForDuplication(indexValue)) {
                    return false;
                }

                ExpireOldEnteries(currentTime);

                InstantaneousIndex matchEntry = null;
                for (InstantaneousIndex match : _mainIndex) {
                    if (match.getClockTime() == currentTime) {
                        matchEntry = match;
                        break;
                    }
                }

                boolean newEntry = false;
                if (matchEntry != null) {
                    indexEntry = matchEntry;
                } else {
                    newEntry = true;
                    indexEntry = new InstantaneousIndex();
                    indexEntry.setEnableDuplicationCheck(_checkDuplication);
                    indexEntry.setClockTime(currentTime);
                    _mainIndex.add(indexEntry);
                }

            }
            indexEntry.AddEntry(entryId, indexValue);
        }
        return true;
    }

    private boolean CheckForDuplication(T activity) {
        if (!_checkDuplication) {
            return false;
        }

        for (InstantaneousIndex currentIndexEntry : _mainIndex) {
            if (currentIndexEntry.CheckDuplication(activity)) {
                return true;
            }
        }
        return false;
    }

    public final java.util.Iterator GetCurrentData() {
        synchronized (this) {
            java.util.Iterator enumerator = new Alachisoft.NCache.Common.DataStructures.Enumerator(this);
            return enumerator;
        }
    }

    public final Enumerator GetCurrentData(tangible.RefObject startTime, boolean getReplicatData)
    {
        synchronized (this)
        {
            Enumerator enumerator = new Enumerator(this, startTime, getReplicatData);
            return enumerator;
        }

    }


    private void ExpireOldEnteries(long currentTime) {
        synchronized (this) {

            java.util.ArrayList> enteriesTobeRemoved = new java.util.ArrayList>();
            for (InstantaneousIndex currentIndexEntry : _mainIndex) {
                long windowStartTime = currentTime - _observationInterval;
                if (windowStartTime > 0 && currentIndexEntry.getClockTime() < windowStartTime) {
                    enteriesTobeRemoved.add(currentIndexEntry);
                } else {
                    break;
                }
            }
            for (InstantaneousIndex currentIndexEntry : enteriesTobeRemoved) {
                _mainIndex.remove(currentIndexEntry);
            }
        }
    }

    @Override
    public void close() throws Exception {
        Alachisoft.NCache.Common.Stats.Clock.StopClock();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy