schema.service.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import {
  2. CreateIndexReq,
  3. DescribeIndexReq,
  4. DropIndexReq,
  5. DescribeIndexResponse,
  6. } from '@zilliz/milvus2-sdk-node';
  7. import { throwErrorFromSDK } from '../utils/Error';
  8. import { clientCache } from '../app';
  9. export class SchemaService {
  10. async createIndex(clientId: string, data: CreateIndexReq) {
  11. const { milvusClient, indexCache } = clientCache.get(clientId);
  12. const res = await milvusClient.createIndex(data);
  13. const key = data.collection_name;
  14. // clear cache;
  15. indexCache.delete(key);
  16. throwErrorFromSDK(res);
  17. return res;
  18. }
  19. /**
  20. * This function is used to describe an index in Milvus.
  21. * It first checks if the index description is cached, if so, it returns the cached value.
  22. * If not, it calls the Milvus SDK's describeIndex function to get the index description.
  23. * If the index is finished building, it caches the index description for future use.
  24. * If the index is not finished building, it deletes any cached value for this index.
  25. * @param data - The request data for describing an index. It contains the collection name.
  26. * @returns - The response from the Milvus SDK's describeIndex function or the cached index description.
  27. */
  28. async describeIndex(clientId: string, data: DescribeIndexReq) {
  29. const { milvusClient, indexCache } = clientCache.get(clientId);
  30. // Get the collection name from the request data
  31. const key = data.collection_name;
  32. // Try to get the index description from the cache
  33. const value: DescribeIndexResponse = indexCache.get(key);
  34. // If the index description is in the cache, return it
  35. if (value) {
  36. return value;
  37. } else {
  38. // If the index description is not in the cache, call the Milvus SDK's describeIndex function
  39. const res = await milvusClient.describeIndex(data);
  40. // If the index is finished building and there is at least one index description,
  41. // cache the index description for future use
  42. if (
  43. (res.index_descriptions?.length > 0 &&
  44. res.index_descriptions.every(i => i.state === 'Finished')) ||
  45. res.index_descriptions.length === 0
  46. ) {
  47. indexCache.set(key, res);
  48. } else {
  49. // If the index is not finished building, delete any cached value for this index
  50. indexCache.delete(key);
  51. }
  52. // Return the response from the Milvus SDK's describeIndex function
  53. return res;
  54. }
  55. }
  56. async dropIndex(clientId: string, data: DropIndexReq) {
  57. const { milvusClient, indexCache } = clientCache.get(clientId);
  58. const res = await milvusClient.dropIndex(data);
  59. const key = data.collection_name;
  60. // clear cache;
  61. indexCache.delete(key);
  62. throwErrorFromSDK(res);
  63. return res;
  64. }
  65. async clearCache(clientId: string) {
  66. const { indexCache } = clientCache.get(clientId);
  67. return indexCache.clear();
  68. }
  69. }