Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2008-2020, 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.map.impl.operation;
import com.hazelcast.config.InMemoryFormat;
import com.hazelcast.core.EntryEventType;
import com.hazelcast.core.ManagedContext;
import com.hazelcast.internal.serialization.Data;
import com.hazelcast.internal.serialization.SerializationService;
import com.hazelcast.map.EntryProcessor;
import com.hazelcast.map.impl.MapDataSerializerHook;
import com.hazelcast.map.impl.MapEntries;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.query.Predicate;
import com.hazelcast.query.impl.Indexes;
import com.hazelcast.query.impl.QueryableEntry;
import com.hazelcast.query.impl.predicates.QueryOptimizer;
import com.hazelcast.spi.impl.operationservice.BackupAwareOperation;
import com.hazelcast.spi.impl.operationservice.MutatingOperation;
import com.hazelcast.spi.impl.operationservice.Operation;
import com.hazelcast.spi.impl.operationservice.PartitionAwareOperation;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import static com.hazelcast.internal.util.ToHeapDataConverter.toHeapData;
import static com.hazelcast.map.impl.operation.EntryOperator.operator;
/**
* GOTCHA: This operation does NOT load missing keys from map-store for now.
*/
public class PartitionWideEntryOperation extends MapOperation
implements MutatingOperation, PartitionAwareOperation, BackupAwareOperation {
protected MapEntries responses;
protected EntryProcessor entryProcessor;
protected transient EntryOperator operator;
protected transient Set keysFromIndex;
protected transient QueryOptimizer queryOptimizer;
public PartitionWideEntryOperation() {
}
public PartitionWideEntryOperation(String name, EntryProcessor entryProcessor) {
super(name);
this.entryProcessor = entryProcessor;
}
@Override
public void innerBeforeRun() throws Exception {
super.innerBeforeRun();
SerializationService serializationService = getNodeEngine().getSerializationService();
ManagedContext managedContext = serializationService.getManagedContext();
managedContext.initialize(entryProcessor);
keysFromIndex = null;
queryOptimizer = mapServiceContext.getQueryOptimizer();
}
protected Predicate getPredicate() {
return null;
}
@Override
protected void runInternal() {
if (mapContainer.getMapConfig().getInMemoryFormat() == InMemoryFormat.NATIVE) {
runForNative();
} else {
runWithPartitionScan();
}
}
private void runForNative() {
if (runWithIndex()) {
return;
}
runWithPartitionScanForNative();
}
/**
* @return {@code true} if index has been used and the EP
* has been executed on its keys, {@code false} otherwise
*/
private boolean runWithIndex() {
// here we try to query the partitioned-index
Predicate predicate = getPredicate();
if (predicate == null) {
return false;
}
// we use the partitioned-index to operate on the selected keys only
Indexes indexes = mapContainer.getIndexes(getPartitionId());
Set entries = indexes.query(queryOptimizer.optimize(predicate, indexes), 1);
if (entries == null) {
return false;
}
responses = new MapEntries(entries.size());
// when NATIVE we can pass null as predicate since it's all
// happening on partition thread so no data-changes may occur
operator = operator(this, entryProcessor, null);
keysFromIndex = new HashSet<>(entries.size());
for (QueryableEntry entry : entries) {
keysFromIndex.add(entry.getKeyData());
Data response = operator.operateOnKey(entry.getKeyData()).doPostOperateOps().getResult();
if (response != null) {
responses.add(entry.getKeyData(), response);
}
}
return true;
}
private void runWithPartitionScan() {
responses = new MapEntries(recordStore.size());
operator = operator(this, entryProcessor, getPredicate());
recordStore.forEach((dataKey, record) -> {
Data response = operator.operateOnKey(dataKey).doPostOperateOps().getResult();
if (response != null) {
responses.add(dataKey, response);
}
}, false);
}
// TODO unify this method with `runWithPartitionScan`
private void runWithPartitionScanForNative() {
// if we reach here, it means we didn't manage to leverage index and we fall-back to full-partition scan
int totalEntryCount = recordStore.size();
responses = new MapEntries(totalEntryCount);
Queue