2021-12-27 10:14:50 +08:00

204 lines
7.6 KiB
Java

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 io.milvus.param.partition;
import io.milvus.exception.ParamException;
import io.milvus.param.Constant;
import io.milvus.param.ParamUtils;
import lombok.Getter;
import lombok.NonNull;
import java.util.ArrayList;
import java.util.List;
/**
* Parameters for <code>loadPartition</code> interface.
*/
@Getter
public class LoadPartitionsParam {
private final String collectionName;
private final List<String> partitionNames;
private final boolean syncLoad;
private final long syncLoadWaitingInterval;
private final long syncLoadWaitingTimeout;
private LoadPartitionsParam(@NonNull Builder builder) {
this.collectionName = builder.collectionName;
this.partitionNames = builder.partitionNames;
this.syncLoad = builder.syncLoad;
this.syncLoadWaitingInterval = builder.syncLoadWaitingInterval;
this.syncLoadWaitingTimeout = builder.syncLoadWaitingTimeout;
}
public static Builder newBuilder() {
return new Builder();
}
/**
* Builder for <code>LoadPartitionsParam</code> class.
*/
public static final class Builder {
private String collectionName;
private final List<String> partitionNames = new ArrayList<>();
// syncLoad:
// Default behavior is sync loading, loadPartition() return after partition finish loading.
private Boolean syncLoad = Boolean.TRUE;
// syncLoadWaitingDuration:
// When syncLoad is ture, loadPartition() will wait until partition finish loading,
// this value control the waiting interval. Unit: millisecond. Default value: 500 milliseconds.
private Long syncLoadWaitingInterval = 500L;
// syncLoadWaitingTimeout:
// When syncLoad is ture, loadPartition() will wait until partition finish loading,
// this value control the waiting timeout. Unit: second. Default value: 60 seconds.
private Long syncLoadWaitingTimeout = 60L;
private Builder() {
}
/**
* Set collection name. Collection name cannot be empty or null.
*
* @param collectionName collection name
* @return <code>Builder</code>
*/
public Builder withCollectionName(@NonNull String collectionName) {
this.collectionName = collectionName;
return this;
}
/**
* Set partition names list. Partition names list cannot be null or empty.
*
* @param partitionNames partition names list
* @return <code>Builder</code>
*/
public Builder withPartitionNames(@NonNull List<String> partitionNames) {
partitionNames.forEach(this::addPartitionName);
return this;
}
/**
* Add a partition name. Partition name cannot be empty or null.
*
* @param partitionName partition name
* @return <code>Builder</code>
*/
public Builder addPartitionName(@NonNull String partitionName) {
if (!this.partitionNames.contains(partitionName)) {
this.partitionNames.add(partitionName);
}
return this;
}
/**
* Set load action to sync mode.
* With sync mode, the client side will keep waiting until all segments of the partition successfully loaded.
*
* If not sync mode, client will return at once after the loadPartitions() is called.
*
* @param syncLoad <code>Boolean.TRUE</code> is sync mode, Boolean.FALSE is not
* @return <code>Builder</code>
*/
public Builder withSyncLoad(@NonNull Boolean syncLoad) {
this.syncLoad = syncLoad;
return this;
}
/**
* Set waiting interval in sync mode. In sync mode, the client will constantly check partition load state by interval.
* Interval must be larger than zero, and cannot be larger than Constant.MAX_WAITING_LOADING_INTERVAL.
* @see Constant
*
* @param milliseconds interval
* @return <code>Builder</code>
*/
public Builder withSyncLoadWaitingInterval(@NonNull Long milliseconds) {
this.syncLoadWaitingInterval = milliseconds;
return this;
}
/**
* Set time out value for sync mode.
* Time out value must be larger than zero, and cannot be larger than Constant.MAX_WAITING_LOADING_TIMEOUT.
* @see Constant
*
* @param seconds time out value for sync mode
* @return <code>Builder</code>
*/
public Builder withSyncLoadWaitingTimeout(@NonNull Long seconds) {
this.syncLoadWaitingTimeout = seconds;
return this;
}
/**
* Verify parameters and create a new <code>LoadPartitionsParam</code> instance.
*
* @return <code>LoadPartitionsParam</code>
*/
public LoadPartitionsParam build() throws ParamException {
ParamUtils.CheckNullEmptyString(collectionName, "Collection name");
if (partitionNames.isEmpty()) {
throw new ParamException("Partition names cannot be empty");
}
for (String name : partitionNames) {
ParamUtils.CheckNullEmptyString(name, "Partition name");
}
if (syncLoad == Boolean.TRUE) {
if (syncLoadWaitingInterval <= 0) {
throw new ParamException("Sync load waiting interval must be larger than zero");
} else if (syncLoadWaitingInterval > Constant.MAX_WAITING_LOADING_INTERVAL) {
throw new ParamException("Sync load waiting interval cannot be larger than "
+ Constant.MAX_WAITING_LOADING_INTERVAL.toString() + " milliseconds");
}
if (syncLoadWaitingTimeout <= 0) {
throw new ParamException("Sync load waiting interval must be larger than zero");
} else if (syncLoadWaitingTimeout > Constant.MAX_WAITING_LOADING_TIMEOUT) {
throw new ParamException("Sync load waiting interval cannot be larger than "
+ Constant.MAX_WAITING_LOADING_TIMEOUT.toString() + " seconds");
}
}
return new LoadPartitionsParam(this);
}
}
/**
* Construct a <code>String</code> by <code>LoadPartitionsParam</code> instance.
*
* @return <code>String</code>
*/
@Override
public String toString() {
return "LoadPartitionsParam{" +
"collectionName='" + collectionName + '\'' +
", partitionName='" + partitionNames.toString() + '\'' +
", syncLoad=" + syncLoad +
", syncLoadWaitingInterval=" + syncLoadWaitingInterval +
'}';
}
}