Use the append paradigm for the delta iterator as well

This commit is contained in:
Kovid Goyal 2023-07-06 09:19:08 +05:30
parent 579ab8551e
commit c2ad662bed
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 46 additions and 36 deletions

View file

@ -44,30 +44,38 @@ type Operation struct {
var bin = binary.LittleEndian
func (self Operation) Serialize() []byte {
if self.serialized_repr != nil {
return self.serialized_repr
}
var ans []byte
func (self Operation) SerializeSize() int {
switch self.Type {
case OpBlock:
return 9
case OpBlockRange:
return 13
case OpHash:
return 3 + len(self.Data)
case OpData:
return 5 + len(self.Data)
}
return -1
}
func (self Operation) Serialize(ans []byte) {
if self.serialized_repr != nil {
copy(ans, self.serialized_repr)
}
switch self.Type {
case OpBlock:
ans = make([]byte, 9)
bin.PutUint64(ans[1:], self.BlockIndex)
case OpBlockRange:
ans = make([]byte, 13)
bin.PutUint64(ans[1:], self.BlockIndex)
bin.PutUint32(ans[9:], uint32(self.BlockIndexEnd-self.BlockIndex))
case OpHash:
ans = make([]byte, 3+len(self.Data))
bin.PutUint16(ans[1:], uint16(len(self.Data)))
copy(ans[3:], self.Data)
case OpData:
ans = make([]byte, 5+len(self.Data))
bin.PutUint32(ans[1:], uint32(len(self.Data)))
copy(ans[5:], self.Data)
}
ans[0] = byte(self.Type)
return ans
}
func (self *Operation) Unserialize(data []byte) (n int, err error) {