Alachisoft.NCache.Common.DataReader.PartitionRSEnumerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nc-common Show documentation
Show all versions of nc-common Show documentation
Internal package of Alachisoft.
package Alachisoft.NCache.Common.DataReader;
import com.alachisoft.ncache.runtime.exceptions.InvalidReaderException;
import com.alachisoft.ncache.runtime.exceptions.OperationFailedException;
import com.alachisoft.ncache.runtime.exceptions.runtime.OperationFailedRuntimeException;
import java.io.IOException;
import java.util.UUID;
// 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 PartitionRSEnumerator implements RecordSetEnumerator, IPartitionInfo {
private RecordSetEnumerator _recordSetEnumerator;
private String _nodeIP;
private String _readerId;
private int _nextIndex;
private RecordSetLoader _cacheImpl;
private boolean isValid = true;
private int _port;
private String uniqueId = "";
public PartitionRSEnumerator(RecordSetEnumerator recordSetEnumerator, String readerId, String nodeIP, int nextIdex, RecordSetLoader cacheImpl) {
_recordSetEnumerator = recordSetEnumerator;
_readerId = readerId;
_nextIndex = nextIdex;
_cacheImpl = cacheImpl;
uniqueId = UUID.randomUUID().toString();
parseAddress(nodeIP);
}
public final String getServer() {
return _nodeIP;
}
private void parseAddress(String address) {
if (!address.isEmpty() && address != null) {
String[] hostPort = address.split("[:]", -1);
_nodeIP = hostPort[0];
if (hostPort.length > 1) {
try {
_port = Integer.parseInt(hostPort[1]);
} catch (RuntimeException e) {
}
}
}
}
private String getReciepent() {
return _port > 0 ? _nodeIP + ":" + _port : _nodeIP;
}
public final String getReaderId() {
return _readerId;
}
public final void setIsValid(boolean value) {
isValid = value;
}
public final boolean getNextRecordSetChunk() throws InvalidReaderException {
ReaderResultSet readerChunk = null;
if (_cacheImpl != null) {
if (!isValid) {
throw new InvalidReaderException("Reader state has been lost.");
}
try {
readerChunk = _cacheImpl.getRecordSet(_readerId, _nextIndex, _nodeIP, getReciepent());
} catch (OperationFailedException e) {
throw new InvalidReaderException("Reader state has been lost.");
}
}
if (readerChunk != null && readerChunk.getRecordSet() != null && readerChunk.getRecordSet().getRowCount() > 0) {
_recordSetEnumerator = new RecordSetEnumeratorImpl(readerChunk.getRecordSet());
_nextIndex = readerChunk.getNextIndex();
return _recordSetEnumerator.moveNext();
}
return false;
}
///#region-----------------IRecordSetEnumerator---------------------
public final RecordRow getCurrent() {
return _recordSetEnumerator.getCurrent();
}
public final ColumnCollection getColumnCollection() {
return _recordSetEnumerator.getColumnCollection();
}
public final boolean moveNext() {
boolean next = _recordSetEnumerator.moveNext();
if (!next) {
try {
next = getNextRecordSetChunk();
} catch (InvalidReaderException e) {
throw new RuntimeException(e);
}
}
return next;
}
///#endregion
@Override
public int hashCode() {
return uniqueId.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
PartitionRSEnumerator pr = obj instanceof PartitionRSEnumerator ? (PartitionRSEnumerator) obj : null;
if (pr == null) {
return false;
}
return uniqueId.equals(pr.uniqueId);
}
@Override
public void close() throws Exception {
try {
if (_cacheImpl != null) {
_cacheImpl.disposeReader(_readerId, _nodeIP);
}
_recordSetEnumerator.close();
_cacheImpl = null;
} catch (InvalidReaderException e) {
throw new InvalidReaderException("Reader state has been lost.");
}
}
}