com.hivemq.extensions.iteration.Chunker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hivemq-community-edition-embedded Show documentation
Show all versions of hivemq-community-edition-embedded Show documentation
HiveMQ CE is a Java-based open source MQTT broker that fully supports MQTT 3.x and MQTT 5
The newest version!
/*
* Copyright 2019-present HiveMQ GmbH
*
* 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.hivemq.extensions.iteration;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.hivemq.configuration.service.InternalConfigurations;
import com.hivemq.extension.sdk.api.annotations.NotNull;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* @author Georg Held
*/
@Singleton
public class Chunker {
private final int bucketCount;
@Inject
public Chunker() {
bucketCount = InternalConfigurations.PERSISTENCE_BUCKET_COUNT.get();
}
public @NotNull ListenableFuture>> getAllLocalChunk(
final @NotNull ChunkCursor cursor,
final int maxChunkSize,
final @NotNull SingleWriterCall singleWriterCall) {
try {
checkNotNull(cursor, "Cursor must not be null");
checkNotNull(singleWriterCall, "Single writer call must not be null");
final ImmutableList.Builder>>> builder =
ImmutableList.builder();
final int maxResults = maxChunkSize / (bucketCount - cursor.getFinishedBuckets().size());
for (int i = 0; i < bucketCount; i++) {
//skip already finished buckets
if (!cursor.getFinishedBuckets().contains(i)) {
final String lastKey = cursor.getLastKeys().get(i);
builder.add(singleWriterCall.call(i, lastKey, maxResults));
}
}
return Futures.transform(Futures.allAsList(builder.build()), allBucketsResult -> {
Preconditions.checkNotNull(allBucketsResult, "Iteration result from all buckets cannot be null");
final ImmutableMap.Builder>> resultBuilder =
ImmutableMap.builder();
for (final BucketChunkResult
© 2015 - 2025 Weber Informatics LLC | Privacy Policy