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

com.volcengine.service.vikingDB.Collection Maven / Gradle / Ivy

There is a newer version: 1.0.192
Show newest version
package com.volcengine.service.vikingDB;

import com.google.gson.internal.LinkedTreeMap;
import com.volcengine.service.vikingDB.common.DataObject;
import com.volcengine.service.vikingDB.common.Field;
import com.volcengine.service.vikingDB.common.ID;

import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
public class Collection {
    private String collectionName;
    private List fields;
    private VikingDBService vikingDBService;
    private String primaryKey;
    private List indexes = null; 
    private String description="";
    private HashMap stat=null;
    private String createTime=null;
    private String updateTime=null;
    private String updatePerson=null;

    public Collection(String collectionName, List fields, VikingDBService vikingDBService, String primaryKey){
        this.collectionName = collectionName;
        this.fields = fields;
        this.vikingDBService = vikingDBService;
        this.primaryKey = primaryKey;
    }
    public Collection(){}
    public void upsertData(DataObject dataObject) throws Exception{
        if(dataObject.getIsBuild() == 0){
            VikingDBException vikingDBException = new VikingDBException(1000031, null, "Param dose not build");
            throw vikingDBException.getErrorCodeException(1000031, null, "Param dose not build");
        }

        List> fieldList = new ArrayList<>();
        fieldList.add(dataObject.getFields());
        Integer ttl = null;
        if(dataObject.getTTL() != null){
            ttl = dataObject.getTTL();
        }
        HashMap params = new HashMap();
        params.put("collection_name", collectionName);
        params.put("fields", fieldList);
        params.put("ttl", ttl);
        // System.out.println(params);
        vikingDBService.doRequest("UpsertData", null, params);

    }
    public void upsertData(List dataObjects) throws Exception{
        List> fieldList = new ArrayList<>();
        HashMap> record = new HashMap<>();
        for(DataObject item: dataObjects){
            if(item.getIsBuild() == 0){
                VikingDBException vikingDBException = new VikingDBException(1000031, null, "Param dose not build");
                throw vikingDBException.getErrorCodeException(1000031, null, "Param dose not build");
            }

            if(record.get(item.getTTL()) != null){
                ArrayList fields = record.get(item.getTTL());
                fields.add(item.getFields());
                record.put(item.getTTL(), fields);
            } else {
                ArrayList fields = new ArrayList<>();
                fields.add(item.getFields());
                record.put(item.getTTL(), fields);
            }
        }

        for (Integer key : record.keySet()) {
            HashMap params = new HashMap();
            params.put("collection_name", collectionName);
            params.put("fields", record.get(key));
            params.put("ttl", key);
            // System.out.println(params);
            vikingDBService.doRequest("UpsertData", null, params);
        }


    }
    public void deleteData(T id) throws Exception{
        HashMap params = new HashMap();
        params.put("collection_name", collectionName);
        params.put("primary_keys", id);
        vikingDBService.doRequest("DeleteData",null, params);
    }
    public DataObject fetchData(T id) throws Exception{
        DataObject dataObject;
        HashMap params = new HashMap();
        params.put("collection_name", collectionName);
        params.put("primary_keys", id);
        LinkedTreeMap resData = vikingDBService.doRequest("FetchData",null, params);
        // System.out.println(resData);
        @SuppressWarnings("unchecked")
        ArrayList> res = (ArrayList>)resData.get("data");
        // System.out.println(res);
        HashMap hashMap = vikingDBService.convertLinkedTreeMapToHashMap(res.get(0));
        // System.out.println(hashMap);
        dataObject = new DataObject()
                    .setFields(hashMap)
                    .setId(id);
        return dataObject;
    }
    public List fetchData(List ids) throws Exception{
        List dataObjects = new ArrayList<>();
        HashMap params = new HashMap();
        params.put("collection_name", collectionName);
        params.put("primary_keys", ids);
        LinkedTreeMap resData = vikingDBService.doRequest("FetchData",null, params);
        // System.out.println(resData);
        @SuppressWarnings("unchecked")
        ArrayList> res = (ArrayList>)resData.get("data");
        // System.out.println(res);
        for(LinkedTreeMap linkedTreeMap: res){
            HashMap hashMap = vikingDBService.convertLinkedTreeMapToHashMap(linkedTreeMap);
            // System.out.println(hashMap);
            DataObject dataObject = new DataObject()
                            .setFields(hashMap)
                            .setId(hashMap.get(primaryKey));
            dataObjects.add(dataObject);
        }
        return dataObjects;
    }
}