Skip to content

Commit

Permalink
trie: implements vlc coder
Browse files Browse the repository at this point in the history
  • Loading branch information
qianbin committed Dec 30, 2023
1 parent e5d353f commit c46fc27
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions trie/internal/vlc/buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2023 The VeChainThor developers

// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>

// Package vlc implements variable length coding.
//
// It's much simpler and a bit faster than RLP.
// Trie nodes stored in database are encoded using VLC.
package vlc

import "encoding/binary"

// Buffer is essentially a byte slice. It holds encoded elements.
type Buffer []byte

// Truncate truncates the buffer to size.
func (b *Buffer) Truncate(size int) {
*b = (*b)[:size]
}

// PutUint puts the uint value.
func (b *Buffer) PutUint(i uint64) {
*b = binary.AppendUvarint(*b, i)
}

// PutString puts the string value.
func (b *Buffer) PutString(str []byte) {
*b = binary.AppendUvarint(*b, uint64(len(str)))
*b = append(*b, str...)
}

// PutFunc puts the string value returned by func build.
// The build func should not modify existing content of buf,
// and should follow the behavior like append.
func (b *Buffer) PutFunc(build func(buf []byte) []byte) {
offset := len(*b)
*b = append(*b, make([]byte, binary.MaxVarintLen64)...)
*b = build(*b)

str := (*b)[offset+binary.MaxVarintLen64:]
b.Truncate(offset)
b.PutString(str)
}

// SplitString extracts a string and returns rest bytes.
// It'll panic if errored.
func SplitString(buf []byte) (str []byte, rest []byte) {
size, n := binary.Uvarint(buf)
buf = buf[n:]
return buf[:size], buf[size:]
}

// SplitUint extracts uint64 and returns rest bytes.
// It'll panic if errored.
func SplitUint(buf []byte) (uint64, []byte) {
v, n := binary.Uvarint(buf)
return v, buf[n:]
}

0 comments on commit c46fc27

Please sign in to comment.