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

Alachisoft.NCache.Common.DataReader.DistributedRSEnumerator Maven / Gradle / Ivy

package Alachisoft.NCache.Common.DataReader;

import com.alachisoft.ncache.runtime.exceptions.InvalidReaderException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

//  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

public class DistributedRSEnumerator implements RecordSetEnumerator {
    protected ArrayList _partitionRecordSets = null;
    protected RecordRow _current = null;
    private RecordSetEnumerator _currentRecordSet = null;
    private int _counter = 0;
    private boolean _next = false;
    private HashMap> _validReaders;

    public DistributedRSEnumerator(ArrayList partitionRecordSets, HashMap> validReaders) {
        _partitionRecordSets = partitionRecordSets;
        _validReaders = validReaders;
    }

    ///#region-----------------IRecordSetEnumerator---------------------
    public final RecordRow getCurrent() {
        return _current;
    }

    public final ColumnCollection getColumnCollection() {
        return _partitionRecordSets.size() > 0 ? _partitionRecordSets.get(0).getColumnCollection() : null;
    }

    public boolean moveNext() {
        try {
            setCurrentEnumerator();
        } catch (InvalidReaderException e) {
            throw new RuntimeException(e);
        }
        return _next;
    }

    ///#endregion
    private void setCurrentEnumerator() throws InvalidReaderException //for each move next,call pick one of partition record set.
    {
        if (_partitionRecordSets.size() == 0) {
            return;
        }
        boolean hasNext = false;
        do {
            try {
                if (_partitionRecordSets.size() <= _counter) {
                    throw new InvalidReaderException("Data reader has lost its state");
                }

                _currentRecordSet = _partitionRecordSets.get(_counter);

                hasNext = _currentRecordSet.moveNext();
            } catch (InvalidReaderException e) {
                try {
                    this.close();
                } catch (Exception ex) {
                    throw new InvalidReaderException("Data reader has lost its state");
                }
            }

            if (hasNext) {
                _current = _currentRecordSet.getCurrent();
            } else {
                _partitionRecordSets.remove(_currentRecordSet);
                removeFromValidReaders(_currentRecordSet);
            }
            _counter++;
            if (_counter >= _partitionRecordSets.size()) {
                _counter = 0;
            }
        } while (!hasNext && _partitionRecordSets.size() > 0);
        _next = hasNext;

    }

    public final void removeFromValidReaders(RecordSetEnumerator pe) {
        IPartitionInfo info = pe instanceof IPartitionInfo ? (IPartitionInfo) pe : null;

        if (info != null) {
            HashMap readers = null;

            synchronized (_validReaders) {
                readers = _validReaders.get(info.getServer());

                if (readers != null) {
                    readers.remove(pe);
                }
            }
        }
    }

    @Override
    public void close() throws Exception {
        if (_partitionRecordSets != null) {
            for (RecordSetEnumerator pe : _partitionRecordSets) {
              pe.close();
              removeFromValidReaders(pe);
            }
            _partitionRecordSets = null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy