com.alachisoft.ncache.client.internal.caching.WebCacheEnumerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ncache-professional-client Show documentation
Show all versions of ncache-professional-client Show documentation
NCache Professional client for java.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.alachisoft.ncache.client.internal.caching;
import Alachisoft.NCache.Common.DataStructures.EnumerationDataChunk;
import Alachisoft.NCache.Common.DataStructures.EnumerationPointer;
import Alachisoft.NCache.Common.DataStructures.GroupEnumerationPointer;
import Alachisoft.NCache.Common.IDisposable;
import com.alachisoft.ncache.runtime.JSON.JsonValueBase;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
/**
* @author Basit Anwer
*/
class WebCacheEnumerator implements Enumeration, IDisposable {
private CacheImpl _cache;
private ArrayList _currentChunks;
private Iterator _currentChunkEnumerator;
private String _group;
private String _serializationContext;
private String _subGroup;
private Entry _de;
private Class serializationClass;
public WebCacheEnumerator(String serializationContext, String group, String subGroup, CacheImpl cache,Class> cls) {
_serializationContext = serializationContext;
_cache = cache;
_group = group;
_subGroup = subGroup;
_de = new Entry();
serializationClass=cls;
Initialize(_group, _subGroup);
}
/**
* @param group
* @param subGroup
*/
public void Initialize(String group, String subGroup) {
ArrayList pointers = new ArrayList();
if (group != null && !group.equals("")) {
pointers.add(new GroupEnumerationPointer(group, subGroup));
} else {
pointers.add(new EnumerationPointer());
}
_currentChunks = _cache.getNextChunk(pointers);
ArrayList data = new ArrayList();
for (int i = 0; i < _currentChunks.size(); i++) {
EnumerationDataChunk chunk = _currentChunks.get(i);
if (chunk != null && chunk.getData() != null) {
data.addAll(chunk.getData());
}
}
_currentChunkEnumerator = data.iterator();
}
///
/// Advance the enumerator to the next element of the collection
///
///
public boolean hasMoreElements() {
boolean result = false;
if (_currentChunkEnumerator != null) {
result = _currentChunkEnumerator.hasNext();
if (!result) {
if (_currentChunks != null && !isLastChunk(_currentChunks)) {
_currentChunks = _cache.getNextChunk(getPointerList(_currentChunks));
ArrayList data = new ArrayList();
for (int i = 0; i < _currentChunks.size(); i++) {
if (_currentChunks.get(i) != null && _currentChunks.get(i).getData() != null) {
data.addAll(_currentChunks.get(i).getData());
}
}
if (data != null && data.size() > 0) {
_currentChunkEnumerator = data.iterator();
result = _currentChunkEnumerator.hasNext();
}
} else if (_currentChunks != null && _currentChunks.size() > 0) {
ArrayList pointers = getPointerList(_currentChunks);
if (pointers.size() > 0) {
_cache.getNextChunk(pointers); //just an empty call to dispose enumerator for this particular list of pointer
}
}
}
}
return result;
}
public Object nextElement() {
String key = (String) getKey();
_de.setKey(key);
_de.setValue(getValue(key));
return _de;
}
///
/// Gets the key of the current dictionary entry
///
/**
* @return
*/
public Object getKey() {
Object key = null;
if (_currentChunkEnumerator != null) {
key = _currentChunkEnumerator.next();
}
return key;
}
///
/// Gets the value of the current dictionary entry
///
/**
* @param key
* @return
*/
public Object getValue(String key) {
Object value = null;
try {
value = _cache.get(key, serializationClass);
} catch (Exception ex) {
if (ex.getMessage().startsWith("Connection with server lost")) {
try {
value = _cache.get(key, serializationClass);
} catch (Exception inner) {
throw new RuntimeException(inner);
}
}
throw new RuntimeException(ex);
}
return value;
}
private boolean isLastChunk(List chunks) {
for (int i = 0; i < chunks.size(); i++) {
if (!chunks.get(i).isLastChunk()) {
return false;
}
}
return true;
}
private ArrayList getPointerList(ArrayList chunks) {
ArrayList pointers = new ArrayList();
for (int i = 0; i < chunks.size(); i++) {
if (!chunks.get(i).isLastChunk()) {
pointers.add(chunks.get(i).getPointer());
}
}
return pointers;
}
private ArrayList getChunk(ArrayList pointer) {
ArrayList chunks = null;
try {
chunks = _cache.getNextChunk(pointer);
} catch (Exception ex) {
//this is a empty call just to dispose the enumeration pointers for this particular enumerator
//on all the nodes.
for (int i = 0; i < pointer.size(); i++) {
pointer.get(i).setDisposable(true);
}
try {
_cache.getNextChunk(pointer);
} catch (Exception exc) {
}
// if (exceptionsEnabled)
// {
// throw ex;
// }
}
return chunks;
}
@Override
public void dispose() {
if (_cache != null && _currentChunks != null) {
ArrayList pointerlist = getPointerList(_currentChunks);
if (pointerlist.size() > 0) {
_cache.getNextChunk(pointerlist); //just an empty call to dispose enumerator for this particular pointer
}
//_cache.GetNextChunk(GetPointerList(_currentChunks));
}
_serializationContext = null;
_cache = null;
_group = null;
_subGroup = null;
}
}