2021-12-19 12:05:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
|
|
|
|
import "encoding/binary"
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func PrepareKeyToString (_namespace string, _key uint64) (string, error) {
|
|
|
|
if _key_0, _error := PrepareKey (_namespace, _key); _error == nil {
|
|
|
|
return EncodeKeyToString (_namespace, _key_0)
|
|
|
|
} else {
|
|
|
|
return "", _error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func EncodeKeyToString (_namespace string, _key uint64) (string, error) {
|
|
|
|
var _buffer [8]byte
|
|
|
|
if _error := EncodeKeyToBytes_0 (_namespace, _key, _buffer[:]); _error == nil {
|
|
|
|
return string (_buffer[:]), nil
|
|
|
|
} else {
|
|
|
|
return "", _error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func EncodeKeyToBytes (_namespace string, _key uint64) ([]byte, error) {
|
|
|
|
_buffer := make ([]byte, 8)
|
|
|
|
_error := EncodeKeyToBytes_0 (_namespace, _key, _buffer)
|
|
|
|
return _buffer, _error
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func EncodeKeyToBytes_0 (_namespace string, _key uint64, _buffer []byte) (error) {
|
|
|
|
if len (_buffer) != 8 {
|
|
|
|
return fmt.Errorf ("[890eef13] invalid key buffer length!")
|
|
|
|
}
|
2021-12-19 17:18:22 +00:00
|
|
|
_prefix := KeyNamespacePrefix (_namespace)
|
2021-12-19 12:05:22 +00:00
|
|
|
if _prefix == 0 {
|
2021-12-19 17:19:36 +00:00
|
|
|
return fmt.Errorf ("[8c6ba154] invalid key namespace `%s`!", _namespace)
|
2021-12-19 12:05:22 +00:00
|
|
|
}
|
|
|
|
_keyPrefix := byte (_key >> 56)
|
|
|
|
if _keyPrefix != _prefix {
|
|
|
|
return fmt.Errorf ("[85a5c362] invalid key prefix `%0x` for `%d`!", _key, _keyPrefix)
|
|
|
|
}
|
2021-12-19 17:18:22 +00:00
|
|
|
if (_key << 8) == 0 {
|
|
|
|
return fmt.Errorf ("[c8fa7817] invalid key zero!")
|
|
|
|
}
|
2021-12-19 12:05:22 +00:00
|
|
|
binary.BigEndian.PutUint64 (_buffer, _key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func PrepareKey (_namespace string, _key uint64) (uint64, error) {
|
|
|
|
if _key == 0 {
|
|
|
|
return 0, fmt.Errorf ("[355f64d0] invalid key zero!")
|
|
|
|
}
|
|
|
|
if _key >= (1 << 24) {
|
|
|
|
return 0, fmt.Errorf ("[aba09b4d] invalid key value `%d`!", _key)
|
|
|
|
}
|
2021-12-19 17:18:22 +00:00
|
|
|
_prefix := KeyNamespacePrefix (_namespace)
|
2021-12-19 12:05:22 +00:00
|
|
|
if _prefix == 0 {
|
|
|
|
return 0, fmt.Errorf ("[feece73b] invalid key namespace `%s`!", _namespace)
|
|
|
|
}
|
|
|
|
_keyString := fmt.Sprintf ("%08x", _key)
|
|
|
|
_keyBytes := []byte (_keyString)
|
|
|
|
_keyBytes[0] = _prefix
|
|
|
|
_keyValue := binary.BigEndian.Uint64 (_keyBytes)
|
|
|
|
return _keyValue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-12-19 17:18:22 +00:00
|
|
|
func KeyNamespacePrefix (_namespace string) (byte) {
|
2021-12-19 12:05:22 +00:00
|
|
|
switch _namespace {
|
2021-12-19 17:18:22 +00:00
|
|
|
case NamespaceFilesContent : return NamespaceFilesContentPrefix
|
|
|
|
case NamespaceFilesIndex : return NamespaceFilesIndexPrefix
|
|
|
|
case NamespaceFoldersContent : return NamespaceFoldersContentPrefix
|
|
|
|
case NamespaceFoldersIndex : return NamespaceFoldersIndexPrefix
|
2022-08-26 11:15:31 +00:00
|
|
|
case NamespaceRedirectsContent : return NamespaceRedirectsContentPrefix
|
|
|
|
case NamespaceRedirectsIndex : return NamespaceRedirectsIndexPrefix
|
2021-12-19 17:18:22 +00:00
|
|
|
case NamespaceDataContent : return NamespaceDataContentPrefix
|
|
|
|
case NamespaceDataMetadata : return NamespaceDataMetadataPrefix
|
|
|
|
case NamespaceHeaderName : return NamespaceHeaderNamePrefix
|
|
|
|
case NamespaceHeaderValue : return NamespaceHeaderValuePrefix
|
2021-12-19 12:05:22 +00:00
|
|
|
default : return '0'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func EncodeKeysPairToString (_namespace1 string, _key1 uint64, _namespace2 string, _key2 uint64) (string, error) {
|
|
|
|
var _buffer [16]byte
|
2021-12-19 17:18:22 +00:00
|
|
|
if _error := EncodeKeysPairToBytes_0 (_namespace1, _key1, _namespace2, _key2, _buffer[:]); _error == nil {
|
|
|
|
return string (_buffer[:]), nil
|
|
|
|
} else {
|
2021-12-19 12:05:22 +00:00
|
|
|
return "", _error
|
|
|
|
}
|
2021-12-19 17:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func EncodeKeysPairToBytes_0 (_namespace1 string, _key1 uint64, _namespace2 string, _key2 uint64, _buffer []byte) (error) {
|
|
|
|
if len (_buffer) != 16 {
|
|
|
|
return fmt.Errorf ("[c6f09bfb] invalid keys buffer length!")
|
|
|
|
}
|
|
|
|
if _error := EncodeKeyToBytes_0 (_namespace1, _key1, _buffer[0:8]); _error != nil {
|
|
|
|
return _error
|
|
|
|
}
|
2021-12-19 12:05:22 +00:00
|
|
|
if _error := EncodeKeyToBytes_0 (_namespace2, _key2, _buffer[8:16]); _error != nil {
|
2021-12-19 17:18:22 +00:00
|
|
|
return _error
|
2021-12-19 12:05:22 +00:00
|
|
|
}
|
2021-12-19 17:18:22 +00:00
|
|
|
return nil
|
2021-12-19 12:05:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func DecodeKeysPair (_buffer []byte) (uint64, uint64, error) {
|
|
|
|
if len (_buffer) != 16 {
|
|
|
|
return 0, 0, fmt.Errorf ("[dd6ba461] invalid key buffer length!")
|
|
|
|
}
|
|
|
|
_key1 := binary.BigEndian.Uint64 (_buffer[0:8])
|
|
|
|
_key2 := binary.BigEndian.Uint64 (_buffer[8:16])
|
|
|
|
return _key1, _key2, nil
|
|
|
|
}
|
|
|
|
|
2021-12-19 17:18:22 +00:00
|
|
|
|
|
|
|
func DecodeKey (_namespace string, _buffer []byte) (uint64, error) {
|
|
|
|
if len (_buffer) != 8 {
|
|
|
|
return 0, fmt.Errorf ("[5096a85e] invalid key buffer length!")
|
|
|
|
}
|
|
|
|
_prefix := KeyNamespacePrefix (_namespace)
|
|
|
|
if _prefix == 0 {
|
|
|
|
return 0, fmt.Errorf ("[7fcffef8] invalid key namespace `%s`!", _namespace)
|
|
|
|
}
|
|
|
|
_key := DecodeKey_9 (_buffer)
|
|
|
|
_keyPrefix := byte (_key >> 56)
|
|
|
|
if _keyPrefix != _prefix {
|
|
|
|
return 0, fmt.Errorf ("[a8a94763] invalid key prefix `%0x` for `%d`!", _key, _keyPrefix)
|
|
|
|
}
|
|
|
|
if (_key << 8) == 0 {
|
|
|
|
return 0, fmt.Errorf ("[481159b4] invalid key zero!")
|
|
|
|
}
|
|
|
|
return _key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func DecodeKey_9 (_buffer []byte) (uint64) {
|
|
|
|
_key := binary.BigEndian.Uint64 (_buffer)
|
|
|
|
return _key
|
|
|
|
}
|
|
|
|
|