
nstream.persist.kv.KvAdapterConfigForm Maven / Gradle / Ivy
// Copyright 2015-2024 Nstream, inc.
//
// 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 nstream.persist.kv;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Slot;
import swim.structure.Value;
final class KvAdapterConfigForm extends Form {
private static final String MIN_COMMIT_INTERVAL_FIELD = "minCommitInterval";
private static final String MAX_COMMIT_INTERVAL_FIELD = "maxCommitInterval";
private static final String MIN_COMMIT_SIZE_FIELD = "minCommitSize";
private static final String SHUTDOWN_TIMEOUT_FIELD = "shutdownTimeout";
private static final String MAX_BATCH_SIZE_FIELD = "maxBatchSize";
private static final String IMPL_NAME_FIELD = "implName";
private static final String PARAMS_FIELD = "parameters";
@Override
public Class> type() {
return KvAdapterConfigForm.class;
}
@Override
public Item mold(KvAdapterConfig object) {
final var builder = Value.builder();
builder.add(Slot.of(MIN_COMMIT_INTERVAL_FIELD, object.getMinCommitInterval()));
object.getMaxCommitInterval().ifPresent((interval) -> builder.add(Slot.of(MAX_COMMIT_INTERVAL_FIELD, interval)));
builder.add(Slot.of(MIN_COMMIT_SIZE_FIELD, object.getMinCommitSize()));
builder.add(Slot.of(SHUTDOWN_TIMEOUT_FIELD, object.getShutdownTimeout()));
builder.add(Slot.of(MAX_BATCH_SIZE_FIELD, object.getMaxBatchSize()));
object.getImplName().ifPresent((name) -> builder.add(Slot.of(IMPL_NAME_FIELD, name)));
object.getParameters().ifPresent((params) -> builder.add(Slot.of(PARAMS_FIELD, params)));
return builder.bind();
}
private static final long DEFAULT_MIN_COMMIT_INTERVAL = 60_000;
private static final long DEFAULT_MIN_COMMIT_SIZE = 4 * 1024 * 1024;
private static final long DEFAULT_MAX_BATCH_SIZE = 1024 * 1024;
private static final long DEFAULT_SHUTDOWN_TIMEOUT = 10 * 1000;
private static long posLongParam(Value value, String name, long defaultValue) {
long param = value.get(name).longValue(defaultValue);
if (param <= 0) {
param = defaultValue;
}
return param;
}
@Override
public KvAdapterConfig cast(Item item) {
try {
final Value value = item.toValue();
final long minCommitInterval = posLongParam(value, MIN_COMMIT_INTERVAL_FIELD, DEFAULT_MIN_COMMIT_INTERVAL);
final long maxCommitInterval = posLongParam(value, MAX_COMMIT_INTERVAL_FIELD, -1);
final long minCommitSize = posLongParam(value, MIN_COMMIT_SIZE_FIELD, DEFAULT_MIN_COMMIT_SIZE);
final long shutdownTimeout = posLongParam(value, SHUTDOWN_TIMEOUT_FIELD, DEFAULT_SHUTDOWN_TIMEOUT);
final long maxBatchSize = posLongParam(value, MAX_BATCH_SIZE_FIELD, DEFAULT_MAX_BATCH_SIZE);
final Value parameters;
if (value.containsKey(PARAMS_FIELD)) {
parameters = value.get(PARAMS_FIELD);
} else {
parameters = null;
}
final String implName = value.get(IMPL_NAME_FIELD).stringValue(null);
return new KvAdapterConfig(minCommitInterval, maxCommitInterval, minCommitSize, shutdownTimeout, maxBatchSize, implName, parameters);
} catch (UnsupportedOperationException | IllegalArgumentException ex) {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy