All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dromara.milvus.plus.service.MilvusService Maven / Gradle / Ivy

The newest version!
package org.dromara.milvus.plus.service;

import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.vector.response.DeleteResp;
import io.milvus.v2.service.vector.response.GetResp;
import io.milvus.v2.service.vector.response.InsertResp;
import io.milvus.v2.service.vector.response.UpsertResp;
import org.dromara.milvus.plus.cache.ConversionCache;
import org.dromara.milvus.plus.cache.MilvusCache;
import org.dromara.milvus.plus.converter.SearchRespConverter;
import org.dromara.milvus.plus.core.conditions.LambdaDeleteWrapper;
import org.dromara.milvus.plus.core.conditions.LambdaInsertWrapper;
import org.dromara.milvus.plus.core.conditions.LambdaQueryWrapper;
import org.dromara.milvus.plus.core.conditions.LambdaUpdateWrapper;
import org.dromara.milvus.plus.core.mapper.BaseMilvusMapper;
import org.dromara.milvus.plus.model.MilvusEntity;
import org.dromara.milvus.plus.model.vo.MilvusResp;
import org.dromara.milvus.plus.model.vo.MilvusResult;
import org.dromara.milvus.plus.util.SpringUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Service
public class MilvusService implements IAMService,ICMService,IVecMService{

    @Override
    public MilvusClientV2 getClient() {
        return SpringUtils.getBean(MilvusClientV2.class);
    }

    public  MilvusResp>> getById(Class entityClass,Serializable... ids) {
        ConversionCache conversionCache = MilvusCache.milvusCache.get(entityClass.getName());
        MilvusEntity milvusEntity = conversionCache.getMilvusEntity();
        GetResp getResp = getByIds(milvusEntity.getCollectionName(),Arrays.asList(ids));
        MilvusResp>> milvusResp = SearchRespConverter.convertGetRespToMilvusResp(getResp, entityClass);
        return milvusResp;
    }

    public MilvusResp removeById(Class entityClass,Serializable ... ids){
        ConversionCache conversionCache = MilvusCache.milvusCache.get(entityClass.getName());
        DeleteResp deleteResp = deleteByIds(conversionCache.getCollectionName(), Arrays.asList(ids));
        MilvusResp resp = new MilvusResp<>();
        resp.setData(deleteResp);
        resp.setSuccess(true);
        return resp;
    }
    public MilvusResp insert(T ... entities){
        Class entityClass = getEntityClass(entities[0]);
        BaseMilvusMapper mapper=getBaseMilvusMapper();
        LambdaInsertWrapper lambda = mapper.lambda(entityClass, new LambdaInsertWrapper<>());
        return lambda.insert(entities);
    }
    public MilvusResp insert(Collection entities){
        T entity = entities.iterator().next();
        Class entityClass = getEntityClass(entity);
        BaseMilvusMapper mapper=getBaseMilvusMapper();
        LambdaInsertWrapper lambda = mapper.lambda(entityClass, new LambdaInsertWrapper<>());
        return lambda.insert(entities.iterator());
    }
    public MilvusResp updateById(T... entities) {
        Class entityClass = getEntityClass(entities[0]);
        BaseMilvusMapper mapper=getBaseMilvusMapper();
        LambdaUpdateWrapper lambda = mapper.lambda(entityClass, new LambdaUpdateWrapper<>());
        return lambda.updateById(entities);
    }
    public MilvusResp updateById(Collection entities) {
        T entity = entities.iterator().next();
        Class entityClass = getEntityClass(entity);
        BaseMilvusMapper mapper=getBaseMilvusMapper();
        LambdaUpdateWrapper lambda = mapper.lambda(entityClass, new LambdaUpdateWrapper<>());
        return lambda.updateById(entities.iterator());
    }
    public LambdaUpdateWrapper ofUpdate(Class entityClass){
        BaseMilvusMapper mapper=getBaseMilvusMapper();
        LambdaUpdateWrapper lambda = mapper.lambda(entityClass, new LambdaUpdateWrapper<>());
        return lambda;
    }
    public LambdaInsertWrapper ofInsert(Class entityClass){
        BaseMilvusMapper mapper=getBaseMilvusMapper();
        LambdaInsertWrapper lambda = mapper.lambda(entityClass, new LambdaInsertWrapper<>());
        return lambda;
    }
    public LambdaDeleteWrapper ofDelete(Class entityClass){
        BaseMilvusMapper mapper=getBaseMilvusMapper();
        LambdaDeleteWrapper lambda = mapper.lambda(entityClass, new LambdaDeleteWrapper<>());
        return lambda;
    }
    public  LambdaQueryWrapper ofQuery(Class entityClass){
        BaseMilvusMapper mapper=getBaseMilvusMapper();
        LambdaQueryWrapper lambda = mapper.lambda(entityClass, new LambdaQueryWrapper<>());
        return lambda;
    }

    private  BaseMilvusMapper getBaseMilvusMapper() {
        MilvusClientV2 client = getClient();
        return new BaseMilvusMapper() {
            @Override
            public MilvusClientV2 getClient() {
                return client;
            }
        };
    }
    private  Class getEntityClass(T entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity must not be null");
        }
        return (Class) entity.getClass();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy