com.hazelcast.cache.impl.ClusterWideIterator Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
*
* 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.
*/
package com.hazelcast.cache.impl;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.spi.InternalCompletableFuture;
import com.hazelcast.spi.Operation;
import com.hazelcast.spi.OperationService;
import com.hazelcast.spi.serialization.SerializationService;
import java.util.Iterator;
import java.util.List;
import javax.cache.Cache;
/**
* Cluster-wide iterator for {@link com.hazelcast.cache.ICache}.
*
*
* This implementation is used for server or embedded mode.
*
* Note: For more information on the iterator details, see {@link AbstractClusterWideIterator}.
*
* @param the type of key.
* @param the type of value.
* @see AbstractClusterWideIterator
*/
public class ClusterWideIterator
extends AbstractClusterWideIterator
implements Iterator> {
private final SerializationService serializationService;
private final CacheProxy cacheProxy;
public ClusterWideIterator(CacheProxy cache, boolean prefetchValues) {
this(cache, DEFAULT_FETCH_SIZE, prefetchValues);
}
public ClusterWideIterator(CacheProxy cache, int fetchSize, boolean prefetchValues) {
super(cache, cache.getNodeEngine().getPartitionService().getPartitionCount(), fetchSize, prefetchValues);
this.cacheProxy = cache;
this.serializationService = cache.getNodeEngine().getSerializationService();
advance();
}
public ClusterWideIterator(CacheProxy cache, int fetchSize, int partitionId, boolean prefetchValues) {
super(cache, cache.getNodeEngine().getPartitionService().getPartitionCount(), fetchSize, prefetchValues);
this.cacheProxy = cache;
this.serializationService = cache.getNodeEngine().getSerializationService();
this.partitionIndex = partitionId;
advance();
}
protected List fetch() {
final OperationService operationService = cacheProxy.getNodeEngine().getOperationService();
if (prefetchValues) {
Operation operation = cacheProxy.operationProvider.createEntryIteratorOperation(lastTableIndex, fetchSize);
final InternalCompletableFuture f = operationService
.invokeOnPartition(CacheService.SERVICE_NAME, operation, partitionIndex);
CacheEntryIterationResult iteratorResult = f.join();
if (iteratorResult != null) {
setLastTableIndex(iteratorResult.getEntries(), iteratorResult.getTableIndex());
return iteratorResult.getEntries();
}
} else {
Operation operation = cacheProxy.operationProvider.createKeyIteratorOperation(lastTableIndex, fetchSize);
final InternalCompletableFuture f = operationService
.invokeOnPartition(CacheService.SERVICE_NAME, operation, partitionIndex);
CacheKeyIterationResult iteratorResult = f.join();
if (iteratorResult != null) {
setLastTableIndex(iteratorResult.getKeys(), iteratorResult.getTableIndex());
return iteratorResult.getKeys();
}
}
return null;
}
@Override
protected Data toData(Object obj) {
return serializationService.toData(obj);
}
@Override
protected T toObject(Object data) {
return serializationService.toObject(data);
}
}