mirror of
https://github.com/garethgeorge/backrest.git
synced 2026-09-28 19:05:33 +00:00
chore: use storm for indexing on top boltdb
This commit is contained in:
@@ -1,53 +0,0 @@
|
||||
package serializationutil
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var ErrInvalidLength = errors.New("invalid length")
|
||||
|
||||
func Itob(v int64) []byte {
|
||||
b := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(b, uint64(v))
|
||||
return b
|
||||
}
|
||||
|
||||
func Btoi(b []byte) (int64, error) {
|
||||
if len(b) != 8 {
|
||||
return 0, ErrInvalidLength
|
||||
}
|
||||
return int64(binary.BigEndian.Uint64(b)), nil
|
||||
}
|
||||
|
||||
func Stob(v string) []byte {
|
||||
var b []byte
|
||||
b = append(b, Itob(int64(len(v)))...)
|
||||
b = append(b, []byte(v)...)
|
||||
return b
|
||||
}
|
||||
|
||||
func Btos(b []byte) (string, int64, error) {
|
||||
if len(b) < 8 {
|
||||
return "", 0, ErrInvalidLength
|
||||
}
|
||||
length, _ := Btoi(b[:8])
|
||||
if int64(len(b)) < 8+length {
|
||||
return "", 0, ErrInvalidLength
|
||||
}
|
||||
return string(b[8:8+length]), 8+length, nil
|
||||
}
|
||||
|
||||
func BytesToKey(b []byte) []byte {
|
||||
var key []byte
|
||||
key = append(key, Itob(int64(len(b)))...)
|
||||
key = append(key, b...)
|
||||
return key
|
||||
}
|
||||
|
||||
func NormalizeSnapshotId(id string) string {
|
||||
if len(id) < 8 {
|
||||
return id
|
||||
}
|
||||
return id[:8]
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package serializationutil
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestItoa(t *testing.T) {
|
||||
nums := []int64{0, 1, 2, 3, 4, 1 << 32, int64(1) << 62}
|
||||
for _, num := range nums {
|
||||
b := Itob(num)
|
||||
if v, _ := Btoi(b); v != num {
|
||||
t.Errorf("itob/btoi failed for %d", num)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStob(t *testing.T) {
|
||||
strs := []string{"", "a", "ab", "abc", "abcd", "abcde", "abcdef"}
|
||||
for _, str := range strs {
|
||||
b := Stob(str)
|
||||
if val, _, _ := Btos(b); val != str {
|
||||
t.Errorf("stob/btos failed for %s", str)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user