2
0

InsertParam.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package io.milvus.param.dml;
  20. import io.milvus.exception.ParamException;
  21. import io.milvus.param.ParamUtils;
  22. import lombok.Getter;
  23. import lombok.NonNull;
  24. import java.util.List;
  25. /**
  26. * Parameters for <code>insert</code> interface.
  27. */
  28. @Getter
  29. public class InsertParam {
  30. private final List<Field> fields;
  31. private final String collectionName;
  32. private final String partitionName;
  33. private final int rowCount;
  34. private InsertParam(@NonNull Builder builder) {
  35. this.collectionName = builder.collectionName;
  36. this.partitionName = builder.partitionName;
  37. this.fields = builder.fields;
  38. this.rowCount = builder.rowCount;
  39. }
  40. public static Builder newBuilder() {
  41. return new Builder();
  42. }
  43. /**
  44. * Builder for {@link InsertParam} class.
  45. */
  46. public static class Builder {
  47. private String collectionName;
  48. private String partitionName = "_default";
  49. private List<InsertParam.Field> fields;
  50. private int rowCount;
  51. private Builder() {
  52. }
  53. /**
  54. * Sets the collection name. Collection name cannot be empty or null.
  55. *
  56. * @param collectionName collection name
  57. * @return <code>Builder</code>
  58. */
  59. public Builder withCollectionName(@NonNull String collectionName) {
  60. this.collectionName = collectionName;
  61. return this;
  62. }
  63. /**
  64. * Set partition name (Optional).
  65. *
  66. * @param partitionName partition name
  67. * @return <code>Builder</code>
  68. */
  69. public Builder withPartitionName(@NonNull String partitionName) {
  70. this.partitionName = partitionName;
  71. return this;
  72. }
  73. /**
  74. * Sets the data to insert. The field list cannot be empty.
  75. * @see InsertParam.Field
  76. *
  77. * @param fields insert data
  78. * @return <code>Builder</code>
  79. */
  80. public Builder withFields(@NonNull List<InsertParam.Field> fields) {
  81. this.fields = fields;
  82. return this;
  83. }
  84. /**
  85. * Verifies parameters and creates a new {@link InsertParam} instance.
  86. *
  87. * @return {@link InsertParam}
  88. */
  89. public InsertParam build() throws ParamException {
  90. ParamUtils.CheckNullEmptyString(collectionName, "Collection name");
  91. if (fields.isEmpty()) {
  92. throw new ParamException("Fields cannot be empty");
  93. }
  94. for (InsertParam.Field field : fields) {
  95. if (field == null) {
  96. throw new ParamException("Field cannot be null." +
  97. " If the field is auto-id, just ignore it from withFields()");
  98. }
  99. ParamUtils.CheckNullEmptyString(field.getName(), "Field name");
  100. if (field.getValues() == null || field.getValues().isEmpty()) {
  101. throw new ParamException("Field value cannot be empty." +
  102. " If the field is auto-id, just ignore it from withFields()");
  103. }
  104. }
  105. // check row count
  106. int count = fields.get(0).getValues().size();
  107. for (InsertParam.Field field : fields) {
  108. if (field.getValues().size() != count) {
  109. throw new ParamException("Row count of fields must be equal");
  110. }
  111. }
  112. this.rowCount = count;
  113. if (count == 0) {
  114. throw new ParamException("Zero row count is not allowed");
  115. }
  116. // this method doesn't check data type, the insert() api will do this work
  117. return new InsertParam(this);
  118. }
  119. }
  120. /**
  121. * Constructs a <code>String</code> by {@link InsertParam} instance.
  122. *
  123. * @return <code>String</code>
  124. */
  125. @Override
  126. public String toString() {
  127. return "InsertParam{" +
  128. "collectionName='" + collectionName + '\'' +
  129. ", partitionName='" + partitionName + '\'' +
  130. ", row_count=" + rowCount +
  131. ", fields=" + fields +
  132. '}';
  133. }
  134. /**
  135. * Internal class for insert data.
  136. * If dataType is Bool, values is List of Boolean
  137. * If dataType is Int64, values is List of Long
  138. * If dataType is Float, values is List of Float
  139. * If dataType is Double, values is List of Double
  140. * If dataType is Varchar, values is List of String
  141. * If dataType is FloatVector, values is List of List Float
  142. * If dataType is BinaryVector, values is List of ByteBuffer
  143. *
  144. * Note:
  145. * If dataType is Int8/Int16/Int32, values is List of Integer or Short
  146. * (why? because the rpc proto only support int32/int64 type, actually Int8/Int16/Int32 use int32 type to encode/decode)
  147. *
  148. */
  149. public static class Field {
  150. private final String name;
  151. private final List<?> values;
  152. public Field(String name, List<?> values) {
  153. this.name = name;
  154. this.values = values;
  155. }
  156. /**
  157. * Return name of the field.
  158. *
  159. * @return <code>String</code>
  160. */
  161. public String getName() {
  162. return name;
  163. }
  164. /**
  165. * Return data of the field, in column-base.
  166. *
  167. * @return <code>List</code>
  168. */
  169. public List<?> getValues() {
  170. return values;
  171. }
  172. /**
  173. * Constructs a <code>String</code> by {@link InsertParam.Field} instance.
  174. *
  175. * @return <code>String</code>
  176. */
  177. @Override
  178. public String toString() {
  179. return "Field{" +
  180. "fieldName='" + name + '\'' +
  181. ", row_count=" + values.size() +
  182. '}';
  183. }
  184. }
  185. }