R.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package io.milvus.param;
  2. import java.util.Arrays;
  3. import java.util.Optional;
  4. public class R<T> {
  5. private Exception exception;
  6. private Integer status;
  7. private T data;
  8. public Exception getException() {
  9. return exception;
  10. }
  11. public void setException(Exception exception) {
  12. this.exception = exception;
  13. }
  14. public Integer getStatus() {
  15. return status;
  16. }
  17. public void setStatus(Integer status) {
  18. this.status = status;
  19. }
  20. public T getData() {
  21. return data;
  22. }
  23. public void setData(T data) {
  24. this.data = data;
  25. }
  26. public static <T> R<T> failed(Exception exception){
  27. R<T> r = new R<>();
  28. r.setStatus(Status.Unknown.getCode());
  29. r.setException(exception);
  30. return r;
  31. }
  32. public static <T> R<T> failed(Status statusCode){
  33. R<T> r = new R<>();
  34. r.setStatus(statusCode.getCode());
  35. r.setException(new Exception(statusCode.name()));
  36. return r;
  37. }
  38. public static <T> R<T> success(){
  39. R<T> r = new R<>();
  40. r.setStatus(Status.Success.getCode());
  41. return r;
  42. }
  43. public static <T> R<T> success(T data){
  44. R<T> r = new R<>();
  45. r.setStatus(Status.Success.getCode());
  46. r.setData(data);
  47. return r;
  48. }
  49. /** Represents server and client side status code */
  50. public enum Status {
  51. // Server side error
  52. Success(0),
  53. UnexpectedError(1),
  54. ConnectFailed(2),
  55. PermissionDenied(3),
  56. CollectionNotExists(4),
  57. IllegalArgument(5),
  58. IllegalDimension(7),
  59. IllegalIndexType(8),
  60. IllegalCollectionName(9),
  61. IllegalTOPK(10),
  62. IllegalRowRecord(11),
  63. IllegalVectorID(12),
  64. IllegalSearchResult(13),
  65. FileNotFound(14),
  66. MetaFailed(15),
  67. CacheFailed(16),
  68. CannotCreateFolder(17),
  69. CannotCreateFile(18),
  70. CannotDeleteFolder(19),
  71. CannotDeleteFile(20),
  72. BuildIndexError(21),
  73. IllegalNLIST(22),
  74. IllegalMetricType(23),
  75. OutOfMemory(24),
  76. IndexNotExist(25),
  77. EmptyCollection(26),
  78. // internal error code.
  79. DDRequestRace(1000),
  80. // Client side error
  81. RpcError(-1),
  82. ClientNotConnected(-2),
  83. Unknown(-3),
  84. VersionMismatch(-4),
  85. ParamError(-5);
  86. private final int code;
  87. Status(int code) {
  88. this.code = code;
  89. }
  90. public static Status valueOf(int val) {
  91. Optional<Status> search =
  92. Arrays.stream(values()).filter(status -> status.code == val).findFirst();
  93. return search.orElse(Unknown);
  94. }
  95. public int getCode() {
  96. return code;
  97. }
  98. }
  99. @Override
  100. public String toString() {
  101. if(exception != null){
  102. return "R{" +
  103. "exception=" + exception.getMessage() +
  104. ", status=" + status +
  105. ", data=" + data +
  106. '}';
  107. }else{
  108. return "R{" +
  109. "status=" + status +
  110. ", data=" + data +
  111. '}';
  112. }
  113. }
  114. }