All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.milvus.param.collection.CreateCollectionParam Maven / Gradle / Ivy

There is a newer version: 2.4.8
Show newest version
/*
 * 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.collection;

import io.milvus.common.clientenum.ConsistencyLevelEnum;
import io.milvus.exception.ParamException;
import io.milvus.param.ParamUtils;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * Parameters for createCollection interface.
 */
@Getter
@ToString
public class CreateCollectionParam {
    private final String collectionName;
    private final int shardsNum;
    private final String description;
    private final int partitionsNum;
    private final ConsistencyLevelEnum consistencyLevel;
    private final String databaseName;

    private final List fieldTypes;
    private final boolean enableDynamicField;

    private CreateCollectionParam(@NonNull Builder builder) {
        this.collectionName = builder.collectionName;
        this.shardsNum = builder.shardsNum;
        this.description = builder.description;
        this.fieldTypes = builder.fieldTypes;
        this.partitionsNum = builder.partitionsNum;
        this.consistencyLevel = builder.consistencyLevel;
        this.databaseName = builder.databaseName;
        this.enableDynamicField = builder.enableDynamicField;
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    /**
     * Builder for {@link CreateCollectionParam} class.
     */
    public static final class Builder {
        private String collectionName;
        private int shardsNum = 0; // default to 0, let server decide the value
        private String description = "";
        private List fieldTypes = new ArrayList<>();
        private int partitionsNum = 0;
        private ConsistencyLevelEnum consistencyLevel = ConsistencyLevelEnum.BOUNDED;
        private String databaseName;
        private CollectionSchemaParam schema;

        private boolean enableDynamicField;
        private Builder() {
        }

        /**
         * Sets the collection name. Collection name cannot be empty or null.
         *
         * @param collectionName collection name
         * @return Builder
         */
        public Builder withCollectionName(@NonNull String collectionName) {
            this.collectionName = collectionName;
            return this;
        }

        /**
         * Sets the database name. database name can be nil.
         *
         * @param databaseName database name
         * @return Builder
         */
        public Builder withDatabaseName(String databaseName) {
            this.databaseName = databaseName;
            return this;
        }

        /**
         * Sets the shards number. The number must be greater or equal to zero.
         * The default value is 0, which means letting the server decide the value.
         * The server set this value to 1 if user didn't specify it.
         *
         * @param shardsNum shards number to distribute insert data into multiple data nodes and query nodes.
         * @return Builder
         */
        public Builder withShardsNum(int shardsNum) {
            this.shardsNum = shardsNum;
            return this;
        }

        /**
         * Sets the collection if enableDynamicField.
         * @deprecated Use {@link #withSchema(CollectionSchemaParam)} repace
         *
         * @param enableDynamicField enableDynamicField of the collection
         * @return Builder
         */
        @Deprecated
        public Builder withEnableDynamicField(boolean enableDynamicField) {
            this.enableDynamicField = enableDynamicField;
            return this;
        }

        /**
         * Sets the collection description. The description can be empty. The default is "".
         *
         * @param description description of the collection
         * @return Builder
         */
        public Builder withDescription(@NonNull String description) {
            this.description = description;
            return this;
        }

        /**
         * Sets the schema of the collection. The schema cannot be empty or null.
         * @see FieldType
         * @deprecated Use {@link #withSchema(CollectionSchemaParam)} repace
         *
         * @param fieldTypes a List of {@link FieldType}
         * @return Builder
         */
        @Deprecated
        public Builder withFieldTypes(@NonNull List fieldTypes) {
            this.fieldTypes.addAll(fieldTypes);
            return this;
        }

        /**
         * Adds a field schema.
         * @see FieldType
         * @deprecated Use {@link #withSchema(CollectionSchemaParam)} repace
         *
         * @param fieldType a {@link FieldType} object
         * @return Builder
         */
        @Deprecated
        public Builder addFieldType(@NonNull FieldType fieldType) {
            this.fieldTypes.add(fieldType);
            return this;
        }

        /**
         * Sets the consistency level. The default value is {@link ConsistencyLevelEnum#BOUNDED}.
         * @see ConsistencyLevelEnum
         *
         * @param consistencyLevel consistency level
         * @return Builder
         */
        public Builder withConsistencyLevel(@NonNull ConsistencyLevelEnum consistencyLevel) {
            this.consistencyLevel = consistencyLevel;
            return this;
        }

        /**
         * Sets the partitions number if there is partition key field. The number must be greater than zero.
         * The default value is 64(defined in server side). The upper limit is 4096(defined in server side).
         * Not allow to set this value if none of field is partition key.
         * Only one partition key field is allowed in a collection.
         *
         * @param partitionsNum partitions number
         * @return Builder
         */
        public Builder withPartitionsNum(int partitionsNum) {
            this.partitionsNum = partitionsNum;
            return this;
        }

        /**
         * Sets the schema of collection.
         *
         *
         * @param schema the schema of collection
         * @return Builder
         */
        public Builder withSchema(@NonNull CollectionSchemaParam schema) {
            this.schema = schema;
            return this;
        }

        /**
         * Verifies parameters and creates a new {@link CreateCollectionParam} instance.
         *
         * @return {@link CreateCollectionParam}
         */
        public CreateCollectionParam build() throws ParamException {
            ParamUtils.CheckNullEmptyString(collectionName, "Collection name");

            if (shardsNum < 0) {
                throw new ParamException("ShardNum must be larger or equal to 0");
            }

            if (!fieldTypes.isEmpty() && schema != null) {
                throw new ParamException("Please use either withFieldTypes(), addFieldType(), or withSchema(), and do not use them simultaneously.");
            }

            if (schema != null) {
                fieldTypes = schema.getFieldTypes();
                enableDynamicField = schema.isEnableDynamicField();
            }

            if (fieldTypes.isEmpty()) {
                throw new ParamException("Field numbers must be larger than 0");
            }

            boolean hasPartitionKey = false;
            for (FieldType fieldType : fieldTypes) {
                if (fieldType == null) {
                    throw new ParamException("Collection field cannot be null");
                }

                if (fieldType.isPartitionKey()) {
                    if (hasPartitionKey) {
                        throw new ParamException("Only one partition key field is allowed in a collection");
                    }
                    hasPartitionKey = true;
                }
            }

            if (partitionsNum > 0) {
                if (!hasPartitionKey) {
                    throw new ParamException("None of fields is partition key, not allow to define partition number");
                }
            }

            return new CreateCollectionParam(this);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy