mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-12-14 22:56:36 +08:00
初步实现bfs原型(仅用于实验)
This commit is contained in:
48
internal/utils/bfs/meta_block.go
Normal file
48
internal/utils/bfs/meta_block.go
Normal file
@@ -0,0 +1,48 @@
|
||||
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package bfs
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type MetaAction = byte
|
||||
|
||||
const (
|
||||
MetaActionNew MetaAction = '+'
|
||||
MetaActionRemove MetaAction = '-'
|
||||
)
|
||||
|
||||
func EncodeMetaBlock(action MetaAction, hash string, data []byte) ([]byte, error) {
|
||||
var hl = len(hash)
|
||||
if hl != HashLen {
|
||||
return nil, errors.New("invalid hash length")
|
||||
}
|
||||
|
||||
var l = 1 /** Action **/ + hl /** Hash **/ + len(data)
|
||||
|
||||
var b = make([]byte, 4 /** Len **/ +l)
|
||||
binary.BigEndian.PutUint32(b, uint32(l))
|
||||
b[4] = action
|
||||
copy(b[5:], hash)
|
||||
copy(b[5+hl:], data)
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func DecodeMetaBlock(blockBytes []byte) (action MetaAction, hash string, data []byte, err error) {
|
||||
var dataOffset = 4 /** Len **/ + HashLen + 1 /** Action **/
|
||||
if len(blockBytes) < dataOffset {
|
||||
err = errors.New("decode failed: invalid block data")
|
||||
return
|
||||
}
|
||||
|
||||
action = blockBytes[4]
|
||||
hash = string(blockBytes[5 : 5+HashLen])
|
||||
|
||||
if action == MetaActionNew {
|
||||
data = blockBytes[dataOffset:]
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user