
com.hazelcast.connector.map.impl.AsyncMapImpl Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2023 Hazelcast Inc.
*
* Licensed under the Hazelcast Community License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://hazelcast.com/hazelcast-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.connector.map.impl;
import com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl;
import com.hazelcast.client.impl.protocol.ClientMessage;
import com.hazelcast.client.impl.protocol.codec.MapPutAllCodec;
import com.hazelcast.client.proxy.ClientMapProxy;
import com.hazelcast.client.proxy.NearCachedClientMapProxy;
import com.hazelcast.client.spi.ClientPartitionService;
import com.hazelcast.client.spi.impl.ClientInvocation;
import com.hazelcast.connector.map.AsyncMap;
import com.hazelcast.core.ExecutionCallback;
import com.hazelcast.core.IMap;
import com.hazelcast.internal.nearcache.NearCache;
import com.hazelcast.internal.serialization.InternalSerializationService;
import com.hazelcast.nio.serialization.Data;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
import static com.hazelcast.util.Preconditions.checkNotNull;
import static java.util.concurrent.CompletableFuture.completedFuture;
class AsyncMapImpl implements AsyncMap {
private final IMap map;
AsyncMapImpl(IMap map) {
this.map = map;
}
@Override
public CompletionStage getAsync(@Nonnull K key) {
return Hz3ImplUtil.toCompletableFuture(map.getAsync(key));
}
@SuppressWarnings("unchecked")
public CompletionStage putAllAsync(
Map extends K, ? extends V> items
) {
ClientMapProxy targetMap = (ClientMapProxy) map;
if (items.isEmpty()) {
return completedFuture(null);
}
checkNotNull(targetMap, "Null argument map is not allowed");
ClientPartitionService partitionService = targetMap.getContext().getPartitionService();
int partitionCount = partitionService.getPartitionCount();
Map>> entryMap = new HashMap<>(partitionCount);
InternalSerializationService serializationService = targetMap.getContext().getSerializationService();
for (Entry extends K, ? extends V> entry : items.entrySet()) {
checkNotNull(entry.getKey(), "Null key is not allowed");
checkNotNull(entry.getValue(), "Null value is not allowed");
Data keyData = serializationService.toData(entry.getKey());
int partitionId = partitionService.getPartitionId(keyData);
entryMap
.computeIfAbsent(partitionId, k -> new ArrayList<>())
.add(new AbstractMap.SimpleEntry<>(keyData, serializationService.toData(entry.getValue())));
}
HazelcastClientInstanceImpl client = (HazelcastClientInstanceImpl) targetMap.getContext().getHazelcastInstance();
CompletableFuture resultFuture = new CompletableFuture<>();
ExecutionCallback callback = createPutAllCallback(
entryMap.size(),
targetMap instanceof NearCachedClientMapProxy ? ((NearCachedClientMapProxy) targetMap).getNearCache()
: null,
items.keySet(),
entryMap.values().stream().flatMap(List::stream).map(Entry::getKey),
resultFuture);
for (Entry>> partitionEntries : entryMap.entrySet()) {
Integer partitionId = partitionEntries.getKey();
// use setAsync if there's only one entry
if (partitionEntries.getValue().size() == 1) {
Entry onlyEntry = partitionEntries.getValue().get(0);
// cast to raw so that we can pass serialized key and value
((IMap) targetMap).setAsync(onlyEntry.getKey(), onlyEntry.getValue())
.andThen(callback);
} else {
ClientMessage request = MapPutAllCodec.encodeRequest(targetMap.getName(), partitionEntries.getValue());
new ClientInvocation(client, request, targetMap.getName(), partitionId).invoke()
.andThen(callback);
}
}
return resultFuture;
}
private ExecutionCallback
© 2015 - 2025 Weber Informatics LLC | Privacy Policy