
/*=============================================================================================
	author:ZhaoJun
	version:1.2
	email:2732810281@qq.com
=============================================================================================*/


//严格模式
/*-------------------------------------------------------------------------------------------*/	
'use strict';
/*-------------------------------------------------------------------------------------------*/

// test初始操作
/*-------------------------------------------------------------------------------------------*/	
    class distr
    {
        static async config(type='file')
        {
            let curObj=$ExtendHash.hashTable[type]
            if(!curObj.url)$Sys.err({message:"配置文件地址未填写"});
            let data=await $Sys.model('/request/http','get')(curObj.url);
            try 
            {
                curObj.table=JSON.parse(data.data)
            } 
            catch (error) 
            {
                console.error('[hash分布式]获取配置文件失败',error)
                $Sys.err({message:"配置文件获取失败"});
            }
        }

        static order(type='file')
        {
            let curObj=$ExtendHash.hashTable[type]
            curObj.list=[]
            curObj.listAble=[]
            for (const key in curObj.table) 
            {
                curObj.list.push(key)
                if(curObj.table[key].stat)curObj.listAble.push(key)
            }
            curObj.list=curObj.list.sort((a,b)=>{return a - b})
            curObj.listAble=curObj.listAble.sort((a,b)=>{return a - b})
        }

        static getHash(key,type='file')
        {
            let curObj=$ExtendHash.hashTable[type]
            if(!key)$Sys.err({message:"没有节点"});
            let crc=$Sys.model('/fun/com','crc16',[key]);
            for (let index = 0; index < curObj.listAble.length; index++) 
            {
                if(curObj.listAble[index]>crc)
                {
                    return curObj.listAble[index+1]?curObj.listAble[index+1]:curObj.listAble[0]
                }
            }
            return curObj.listAble[0]
        }        

        static getHashAll(key,type='file')
        {
            let curObj=$ExtendHash.hashTable[type]
            if(!key)$Sys.err({message:"没有节点"});
            let crc=$Sys.model('/fun/com','crc16',[key]);
            for (let index = 0; index < curObj.list.length; index++) 
            {
                if(curObj.list[index]>crc)
                {
                    return curObj.list[index+1]?curObj.list[index+1]:curObj.list[0]
                }
            }
            return curObj.list[0]
        }
    }
    module.exports=distr;
/*-------------------------------------------------------------------------------------------*/ 
