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

com.alachisoft.ncache.client.internal.communication.NodeRequestStatus Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
package com.alachisoft.ncache.client.internal.communication;

//  Copyright (c) 2020 Alachisoft
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//     http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License


class NodeRequestStatus {
    private final Object syncRoot = new Object();
    private java.util.HashSet requestSet;
    private java.util.HashSet acknowledgedRequestSet;
    private long lastRequestId;
    private long lastAcknowledgeId;

    public NodeRequestStatus() {
        requestSet = new java.util.HashSet();
        acknowledgedRequestSet = new java.util.HashSet();
    }

    public final void RegisterRequest(long id) {
        if (!requestSet.contains(id)) {
            requestSet.add(id);
        }
        lastRequestId = id;
    }

    public final long getLastAcknowledged() {
        if (acknowledgedRequestSet.size() != 0) {
            return GetMinValue(acknowledgedRequestSet);
        }
        return -1;
    }

    public final Object getSyncRoot() {
        return syncRoot;
    }

    public final void Acknowledge(long Id) {
        if (requestSet.contains(Id)) {
            requestSet.remove(Id);
            acknowledgedRequestSet.add(Id);
            lastAcknowledgeId = Id;
        }
    }

    public final void Clean() {
        long minimum = requestSet.size() != 0 ? GetMinValue(requestSet) : lastRequestId;
        synchronized (acknowledgedRequestSet) {
            long lastAcknowledged = acknowledgedRequestSet.size() != 0 ? GetMaxValue(acknowledgedRequestSet) : lastAcknowledgeId;
            long[] ids = GetArray(acknowledgedRequestSet);
            for (int i = 0; i < ids.length; i++) {
                if (ids[i] < minimum) {
                    acknowledgedRequestSet.remove(ids[i]);
                }
            }
            if (!acknowledgedRequestSet.contains(lastAcknowledged)) {
                acknowledgedRequestSet.add(lastAcknowledged);
            }
        }
    }

    private long GetMinValue(java.util.HashSet collection) {
        long minVal = Long.MAX_VALUE;
        java.util.Iterator ie = collection.iterator();
        while (ie.hasNext()) {
            long current = ie.next();
            if (minVal > current) {
                minVal = current;
            }
        }
        return minVal == Long.MAX_VALUE ? -1 : minVal;
    }

    private long GetMaxValue(java.util.HashSet collection) {
        long maxVal = Long.MIN_VALUE;
        java.util.Iterator ie = collection.iterator();
        while (ie.hasNext()) {
            long current = ie.next();
            if (maxVal < current) {
                maxVal = current;
            }
        }
        return maxVal;
    }

    private long[] GetArray(java.util.HashSet collection) {
        long[] longtypeArray = new long[collection.size()];
        int index = 0;
        for (long value : collection) {
            longtypeArray[index] = value;
            index++;
        }
        return longtypeArray;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy