2024-01-07 09:30:03 +00:00
|
|
|
package storage
|
2024-01-06 14:45:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
2024-01-07 08:13:35 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
2024-01-06 14:45:00 +00:00
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-01-07 09:33:40 +00:00
|
|
|
_ msgpack.CustomDecoder = (*StorageLocationMap)(nil)
|
2024-01-07 08:13:35 +00:00
|
|
|
|
2024-01-07 09:33:40 +00:00
|
|
|
_ msgpack.CustomEncoder = (*StorageLocationMap)(nil)
|
2024-01-07 08:13:35 +00:00
|
|
|
_ interfaces.StorageLocation = (*StorageLocationImpl)(nil)
|
2024-01-06 14:45:00 +00:00
|
|
|
)
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
type StorageLocationImpl struct {
|
|
|
|
kind int
|
|
|
|
parts []string
|
|
|
|
binaryParts [][]byte
|
|
|
|
expiry int64
|
|
|
|
providerMessage []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageLocationImpl) Type() int {
|
|
|
|
return s.kind
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageLocationImpl) Parts() []string {
|
|
|
|
//TODO implement me
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageLocationImpl) BinaryParts() [][]byte {
|
|
|
|
return s.binaryParts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageLocationImpl) Expiry() int64 {
|
|
|
|
return s.expiry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageLocationImpl) SetType(t int) {
|
|
|
|
s.kind = t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageLocationImpl) SetParts(p []string) {
|
|
|
|
s.parts = p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageLocationImpl) SetBinaryParts(bp [][]byte) {
|
|
|
|
s.binaryParts = bp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageLocationImpl) SetExpiry(e int64) {
|
|
|
|
s.expiry = e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageLocationImpl) SetProviderMessage(msg []byte) {
|
|
|
|
s.providerMessage = msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageLocationImpl) ProviderMessage() []byte {
|
|
|
|
return s.providerMessage
|
2024-01-06 14:45:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 08:56:05 +00:00
|
|
|
func NewStorageLocation(Type int, Parts []string, Expiry int64) interfaces.StorageLocation {
|
|
|
|
return &StorageLocationImpl{
|
2024-01-07 08:13:35 +00:00
|
|
|
kind: Type,
|
|
|
|
parts: Parts,
|
|
|
|
expiry: Expiry,
|
2024-01-06 14:45:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
func (s *StorageLocationImpl) BytesURL() string {
|
|
|
|
return s.parts[0]
|
2024-01-06 14:45:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
func (s *StorageLocationImpl) OutboardBytesURL() string {
|
|
|
|
if len(s.parts) == 1 {
|
|
|
|
return s.parts[0] + ".obao"
|
2024-01-06 14:45:00 +00:00
|
|
|
}
|
2024-01-07 08:13:35 +00:00
|
|
|
return s.parts[1]
|
2024-01-06 14:45:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
func (s *StorageLocationImpl) String() string {
|
|
|
|
expiryDate := time.Unix(s.expiry, 0)
|
|
|
|
return "StorageLocationImpl(" + strconv.Itoa(s.Type()) + ", " + fmt.Sprint(s.parts) + ", expiry: " + expiryDate.Format(time.RFC3339) + ")"
|
2024-01-06 14:45:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
type SignedStorageLocationImpl struct {
|
2024-01-06 14:45:00 +00:00
|
|
|
NodeID encoding.NodeId
|
2024-01-07 08:13:35 +00:00
|
|
|
Location StorageLocationImpl
|
2024-01-06 14:45:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
func NewSignedStorageLocation(NodeID encoding.NodeId, Location StorageLocationImpl) *SignedStorageLocationImpl {
|
|
|
|
return &SignedStorageLocationImpl{
|
2024-01-06 14:45:00 +00:00
|
|
|
NodeID: NodeID,
|
|
|
|
Location: Location,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
func (ssl *SignedStorageLocationImpl) String() string {
|
2024-01-06 14:45:00 +00:00
|
|
|
nodeString, _ := ssl.NodeID.ToString()
|
|
|
|
|
|
|
|
if nodeString == "" {
|
|
|
|
nodeString = "failed to decode node id"
|
|
|
|
}
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
return "SignedStorageLocationImpl(" + ssl.Location.String() + ", " + nodeString + ")"
|
2024-01-06 14:45:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 09:33:40 +00:00
|
|
|
type StorageLocationMap map[int]NodeStorage
|
|
|
|
type NodeStorage map[string]NodeDetailsStorage
|
|
|
|
type NodeDetailsStorage map[int]interface{}
|
2024-01-06 14:45:00 +00:00
|
|
|
|
2024-01-07 09:33:40 +00:00
|
|
|
func (s *StorageLocationMap) DecodeMsgpack(dec *msgpack.Decoder) error {
|
2024-01-06 14:45:00 +00:00
|
|
|
temp, err := dec.DecodeUntypedMap()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if *s == nil {
|
2024-01-07 09:33:40 +00:00
|
|
|
*s = make(map[int]NodeStorage)
|
2024-01-06 14:45:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 09:33:40 +00:00
|
|
|
tempMap, ok := interface{}(temp).(StorageLocationMap)
|
2024-01-06 14:45:00 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected data format from msgpack decoding")
|
|
|
|
}
|
|
|
|
|
|
|
|
*s = tempMap
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-07 09:33:40 +00:00
|
|
|
func (s StorageLocationMap) EncodeMsgpack(enc *msgpack.Encoder) error {
|
2024-01-06 14:45:00 +00:00
|
|
|
// Create a temporary map to hold the encoded data
|
2024-01-06 15:53:20 +00:00
|
|
|
tempMap := make(map[int]map[string]map[int]interface{})
|
2024-01-06 14:45:00 +00:00
|
|
|
|
|
|
|
// Populate the temporary map with data from storageLocationMap
|
|
|
|
for storageKey, nodeStorages := range s {
|
2024-01-06 15:53:20 +00:00
|
|
|
tempNodeStorages := make(map[string]map[int]interface{})
|
2024-01-06 14:45:00 +00:00
|
|
|
for nodeId, nodeDetails := range nodeStorages {
|
|
|
|
tempNodeStorages[nodeId] = nodeDetails
|
|
|
|
}
|
|
|
|
tempMap[storageKey] = tempNodeStorages
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode the temporary map using MessagePack
|
|
|
|
return enc.Encode(tempMap)
|
|
|
|
}
|
|
|
|
|
2024-01-07 09:33:40 +00:00
|
|
|
func NewStorageLocationMap() StorageLocationMap {
|
|
|
|
return StorageLocationMap{}
|
2024-01-06 14:45:00 +00:00
|
|
|
}
|