Skip to content

Commit

Permalink
move convert schema to utils
Browse files Browse the repository at this point in the history
Signed-off-by: ryjiang <[email protected]>
  • Loading branch information
shanghaikid committed Dec 26, 2023
1 parent d369594 commit cb307e0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
37 changes: 11 additions & 26 deletions server/src/collections/collections.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ import {
CompactReq,
HasCollectionReq,
CountReq,
convertToDataType,
FieldSchema,
FieldType,
} from '@zilliz/milvus2-sdk-node';
import { Parser } from '@json2csv/plainjs';
import { throwErrorFromSDK, findKeyValue, genRows, ROW_COUNT } from '../utils';
import {
throwErrorFromSDK,
findKeyValue,
genRows,
ROW_COUNT,
convertFieldSchemaToFieldType,
} from '../utils';
import { QueryDto, ImportSampleDto, GetReplicasDto } from './dto';
import { CollectionData } from '../types';
import { SchemaService } from '../schema/schema.service';
Expand Down Expand Up @@ -374,33 +378,14 @@ export class CollectionsService {
enable_dynamic_field: !!collection.enable_dynamic_field,
};

if (collection.schema.fields.some((f: FieldSchema) => f.is_partition_key)) {
if (
collection.schema.fields.some((f: FieldSchema) => f.is_partition_key) &&
collection.num_partitions
) {
createCollectionParams.num_partitions = Number(collection.num_partitions);
}

console.dir(createCollectionParams, { depth: null });
return await this.createCollection(createCollectionParams);
}
}

function convertFieldSchemaToFieldType(fieldSchema: FieldSchema) {
const fieldType: FieldType = {
name: fieldSchema.name,
description: fieldSchema.description,
data_type: convertToDataType(fieldSchema.data_type),
element_type: convertToDataType(fieldSchema.element_type),
is_primary_key: fieldSchema.is_primary_key,
is_partition_key: fieldSchema.is_partition_key,
autoID: fieldSchema.autoID,
};

// Convert type_params from array to object
if (fieldSchema.type_params) {
fieldType.type_params = {};
for (const param of fieldSchema.type_params) {
fieldType.type_params[param.key] = param.value;
}
}

return fieldType;
}
29 changes: 28 additions & 1 deletion server/src/utils/Helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { KeyValuePair, FieldSchema } from '@zilliz/milvus2-sdk-node';
import {
KeyValuePair,
FieldSchema,
convertToDataType,
FieldType,
} from '@zilliz/milvus2-sdk-node';

export const findKeyValue = (obj: KeyValuePair[], key: string) =>
obj.find(v => v.key === key)?.value;
Expand Down Expand Up @@ -94,3 +99,25 @@ export const genRows = (
size: number,
enableDynamicField: boolean = false
) => Array.from({ length: size }, () => genRow(fields, enableDynamicField));

export const convertFieldSchemaToFieldType = (fieldSchema: FieldSchema) => {
const fieldType: FieldType = {
name: fieldSchema.name,
description: fieldSchema.description,
data_type: convertToDataType(fieldSchema.data_type),
element_type: convertToDataType(fieldSchema.element_type),
is_primary_key: fieldSchema.is_primary_key,
is_partition_key: fieldSchema.is_partition_key,
autoID: fieldSchema.autoID,
};

// Convert type_params from array to object
if (fieldSchema.type_params) {
fieldType.type_params = {};
for (const param of fieldSchema.type_params) {
fieldType.type_params[param.key] = param.value;
}
}

return fieldType;
};

0 comments on commit cb307e0

Please sign in to comment.