1 Star 1 Fork 0

凡卡/libp2parea

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
logic_id.go 3.57 KB
一键复制 编辑 原始数据 按行查看 历史
凡卡 提交于 2023-11-29 11:01 . first commit
package nodeStore
import (
"bytes"
"math/big"
"sync"
"gitee.com/prestonTao/utils"
)
type Ids struct {
level uint //id位数量
root []byte //
ids [][]byte //
count int64 //
logicNumBuider *LogicNumBuider //
lock *sync.RWMutex //
}
/*
添加一个id
@return bool 是否是自己需要的节点
*/
func (this *Ids) AddId(id []byte) (ok bool, removeIDs [][]byte) {
// engine.Log.Info("AddId: 1111111111111")
if this.count <= 0 {
for i := 0; i < len(this.ids); i++ {
this.ids[i] = id
}
this.count++
ok = true
return
}
// engine.Log.Info("AddId: 1111111111111")
//非逻辑节点不要添加
netIDs := this.logicNumBuider.GetNodeNetworkNum()
delId := make([][]byte, 0)
for i, one := range this.ids {
// engine.Log.Info("AddId: 1111111111111")
kl := NewKademlia(2)
kl.Add(new(big.Int).SetBytes(one))
kl.Add(new(big.Int).SetBytes(id))
nearId := kl.Get(new(big.Int).SetBytes(*netIDs[i]))
// fmt.Println(hex.EncodeToString(nearId[0].Bytes()))
nearIdBs := nearId[0].Bytes()
nearIdNewBs := utils.FullHighPositionZero(&nearIdBs, int(this.level/8))
// engine.Log.Info("AddId: 1111111111111")
// if hex.EncodeToString(*one) == hex.EncodeToString(nearId[0].Bytes()) {
if bytes.Equal(one, *nearIdNewBs) {
// engine.Log.Info("AddId: 1111111111111")
continue
}
// fmt.Println("删除的节点id", i, one, "替换", node.IdInfo.Id)
delId = append(delId, one)
// netNodes[i] = node.IdInfo.Id
this.ids[i] = id
ok = true
// engine.Log.Info("AddId: 1111111111111")
}
// engine.Log.Info("AddId: 1111111111111")
//找到删除的节点
removeIDs = make([][]byte, 0)
for _, one := range delId {
// engine.Log.Info("AddId: 1111111111111")
find := false
for _, netOne := range this.ids {
if bytes.Equal(one, netOne) {
find = true
break
}
}
if !find {
removeIDs = append(removeIDs, one)
}
}
// engine.Log.Info("AddId: 1111111111111")
if ok {
// engine.Log.Info("AddId: 1111111111111")
this.count++
}
// engine.Log.Info("AddId: 1111111111111")
return
}
/*
删除一个id
*/
func (this *Ids) RemoveId(id []byte) {
have := false
netIDs := this.logicNumBuider.GetNodeNetworkNum()
for i, one := range this.ids {
if bytes.Equal(one, id) {
continue
}
ids := this.GetIds()
kl := NewKademlia(len(ids))
for _, one := range ids {
kl.Add(new(big.Int).SetBytes(one))
}
nearId := kl.Get(new(big.Int).SetBytes(*netIDs[i]))
// mhbs, _ := utils.Encode(nearId[1].Bytes(), gconfig.HashCode)
// idmh := utils.Multihash(mhbs)
idAddr := nearId[1].Bytes()
nearIdNewBs := utils.FullHighPositionZero(&idAddr, int(this.level/8))
this.ids[i] = *nearIdNewBs
have = true
}
if have {
this.count--
}
}
/*
获取所有id
*/
func (this *Ids) GetIds() [][]byte {
if this.count <= 0 {
// engine.Log.Info("GetIds 000000000")
return make([][]byte, 0)
}
//去重复
m := make(map[string][]byte)
for _, one := range this.ids {
// m[hex.EncodeToString(one)] = one
m[utils.Bytes2string(one)] = one
}
//组装成数组
ids := make([][]byte, 0)
for _, v := range m {
// engine.Log.Info("GetIds 1111111111111")
ids = append(ids, v)
}
// engine.Log.Info("GetIds 222222222222")
return ids
}
/*
通过下标获取id
*/
func (this *Ids) GetIndex(index int) []byte {
return this.ids[index]
}
func NewIds(id []byte, level uint) *Ids {
lb := NewLogicNumBuider(id, level)
return &Ids{
level: level,
root: id,
ids: make([][]byte, level),
logicNumBuider: lb,
lock: new(sync.RWMutex),
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/prestonTao/libp2parea.git
git@gitee.com:prestonTao/libp2parea.git
prestonTao
libp2parea
libp2parea
3aaa451ef873

搜索帮助