CommonUtils.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.v1;
  20. import io.milvus.param.R;
  21. import java.nio.ByteBuffer;
  22. import java.util.*;
  23. public class CommonUtils {
  24. public static void handleResponseStatus(R<?> r) {
  25. if (r.getStatus() != R.Status.Success.getCode()) {
  26. throw new RuntimeException(r.getMessage());
  27. }
  28. }
  29. public static List<Float> generateFloatVector(int dimension) {
  30. Random ran = new Random();
  31. List<Float> vector = new ArrayList<>();
  32. for (int i = 0; i < dimension; ++i) {
  33. vector.add(ran.nextFloat());
  34. }
  35. return vector;
  36. }
  37. public static List<Float> generateFloatVector(int dimension, Float initValue) {
  38. List<Float> vector = new ArrayList<>();
  39. for (int i = 0; i < dimension; ++i) {
  40. vector.add(initValue);
  41. }
  42. return vector;
  43. }
  44. public static List<List<Float>> generateFloatVectors(int dimension, int count) {
  45. List<List<Float>> vectors = new ArrayList<>();
  46. for (int n = 0; n < count; ++n) {
  47. List<Float> vector = generateFloatVector(dimension);
  48. vectors.add(vector);
  49. }
  50. return vectors;
  51. }
  52. public static List<List<Float>> generateFixFloatVectors(int dimension, int count) {
  53. List<List<Float>> vectors = new ArrayList<>();
  54. for (int n = 0; n < count; ++n) {
  55. List<Float> vector = generateFloatVector(dimension, (float)n);
  56. vectors.add(vector);
  57. }
  58. return vectors;
  59. }
  60. /////////////////////////////////////////////////////////////////////////////////////////////////////
  61. public static ByteBuffer generateBinaryVector(int dimension) {
  62. Random ran = new Random();
  63. int byteCount = dimension / 8;
  64. // binary vector doesn't care endian since each byte is independent
  65. ByteBuffer vector = ByteBuffer.allocate(byteCount);
  66. for (int i = 0; i < byteCount; ++i) {
  67. vector.put((byte) ran.nextInt(Byte.MAX_VALUE));
  68. }
  69. return vector;
  70. }
  71. public static List<ByteBuffer> generateBinaryVectors(int dimension, int count) {
  72. List<ByteBuffer> vectors = new ArrayList<>();
  73. for (int n = 0; n < count; ++n) {
  74. ByteBuffer vector = generateBinaryVector(dimension);
  75. vectors.add(vector);
  76. }
  77. return vectors;
  78. }
  79. }