DescIndexResponseWrapper.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package io.milvus.response;
  2. import io.milvus.grpc.IndexDescription;
  3. import io.milvus.grpc.DescribeIndexResponse;
  4. import io.milvus.param.Constant;
  5. import io.milvus.param.IndexType;
  6. import io.milvus.param.MetricType;
  7. import lombok.Getter;
  8. import lombok.NonNull;
  9. import java.util.*;
  10. /**
  11. * Util class to wrap response of <code>describeIndex</code> interface.
  12. */
  13. public class DescIndexResponseWrapper {
  14. private final DescribeIndexResponse response;
  15. public DescIndexResponseWrapper(@NonNull DescribeIndexResponse response) {
  16. this.response = response;
  17. }
  18. /**
  19. * Get index description of fields.
  20. *
  21. * @return List of IndexDesc, index description of fields
  22. */
  23. public List<IndexDesc> getIndexDescriptions() {
  24. List<IndexDesc> results = new ArrayList<>();
  25. List<IndexDescription> descriptions = response.getIndexDescriptionsList();
  26. descriptions.forEach((desc)->{
  27. IndexDesc res = new IndexDesc(desc.getFieldName(), desc.getIndexName(), desc.getIndexID());
  28. desc.getParamsList().forEach((kv)-> res.addParam(kv.getKey(), kv.getValue()));
  29. results.add(res);
  30. });
  31. return results;
  32. }
  33. /**
  34. * Get index description by field name.
  35. * Return null if the field doesn't exist
  36. *
  37. * @param fieldName field name to get index description
  38. * @return {@link IndexDesc} description of the index
  39. */
  40. public IndexDesc getIndexDescByFieldName(@NonNull String fieldName) {
  41. for (int i = 0; i < response.getIndexDescriptionsCount(); ++i) {
  42. IndexDescription desc = response.getIndexDescriptions(i);
  43. if (fieldName.compareTo(desc.getFieldName()) == 0) {
  44. IndexDesc res = new IndexDesc(desc.getFieldName(), desc.getIndexName(), desc.getIndexID());
  45. desc.getParamsList().forEach((kv)-> res.addParam(kv.getKey(), kv.getValue()));
  46. return res;
  47. }
  48. }
  49. return null;
  50. }
  51. /**
  52. * Internal-use class to wrap response of <code>describeIndex</code> interface.
  53. */
  54. @Getter
  55. public static final class IndexDesc {
  56. private final String fieldName;
  57. private final String indexName;
  58. private final long id;
  59. private final Map<String, String> params = new HashMap<>();
  60. public IndexDesc(@NonNull String fieldName, @NonNull String indexName, long id) {
  61. this.fieldName = fieldName;
  62. this.indexName = indexName;
  63. this.id = id;
  64. }
  65. public void addParam(@NonNull String key, @NonNull String value) {
  66. this.params.put(key, value);
  67. }
  68. public IndexType getIndexType() {
  69. if (this.params.containsKey(Constant.INDEX_TYPE)) {
  70. // may throw IllegalArgumentException
  71. return IndexType.valueOf(params.get(Constant.INDEX_TYPE).toUpperCase(Locale.ROOT));
  72. }
  73. return IndexType.None;
  74. }
  75. public MetricType getMetricType() {
  76. if (this.params.containsKey(Constant.METRIC_TYPE)) {
  77. // may throw IllegalArgumentException
  78. return MetricType.valueOf(params.get(Constant.METRIC_TYPE));
  79. }
  80. return MetricType.None;
  81. }
  82. public String getExtraParam() {
  83. if (this.params.containsKey(Constant.PARAMS)) {
  84. // may throw IllegalArgumentException
  85. return params.get(Constant.PARAMS);
  86. }
  87. return "";
  88. }
  89. @Override
  90. public String toString() {
  91. return "IndexDesc(fieldName: " + getFieldName() + " indexName: " + getIndexName() +
  92. " id: " + getId() + " indexType: " + getIndexType().name() + " metricType: " +
  93. getMetricType().name() + " extraParam: " + getExtraParam() + ")";
  94. }
  95. }
  96. /**
  97. * Construct a <code>String</code> by {@link DescIndexResponseWrapper} instance.
  98. *
  99. * @return <code>String</code>
  100. */
  101. @Override
  102. public String toString() {
  103. return "Index description{" +
  104. getIndexDescriptions().toString() +
  105. '}';
  106. }
  107. }