refactor: switch to using a hashmap based map WebAppFileMap for the webapp meta with path sorting

This commit is contained in:
Derrick Hammer 2024-03-03 03:27:15 -05:00
parent a059980ff0
commit cc5666ac1c
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 98 additions and 39 deletions

View File

@ -2,10 +2,9 @@ package metadata
import ( import (
"errors" "errors"
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/serialize" "git.lumeweb.com/LumeWeb/libs5-go/serialize"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/samber/lo" "github.com/emirpasic/gods/maps/linkedhashmap"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
"sort" "sort"
) )
@ -13,6 +12,7 @@ import (
var ( var (
_ Metadata = (*WebAppMetadata)(nil) _ Metadata = (*WebAppMetadata)(nil)
_ SerializableMetadata = (*WebAppMetadata)(nil) _ SerializableMetadata = (*WebAppMetadata)(nil)
_ SerializableMetadata = (*WebAppFileMap)(nil)
) )
type WebAppMetadata struct { type WebAppMetadata struct {
@ -21,10 +21,10 @@ type WebAppMetadata struct {
TryFiles []string `json:"tryFiles"` TryFiles []string `json:"tryFiles"`
ErrorPages map[int]string `json:"errorPages"` ErrorPages map[int]string `json:"errorPages"`
ExtraMetadata ExtraMetadata `json:"extraMetadata"` ExtraMetadata ExtraMetadata `json:"extraMetadata"`
Paths map[string]WebAppMetadataFileReference `json:"paths"` Paths WebAppFileMap `json:"paths"`
} }
func NewWebAppMetadata(name string, tryFiles []string, extraMetadata ExtraMetadata, errorPages map[int]string, paths map[string]WebAppMetadataFileReference) *WebAppMetadata { func NewWebAppMetadata(name string, tryFiles []string, extraMetadata ExtraMetadata, errorPages map[int]string, paths WebAppFileMap) *WebAppMetadata {
return &WebAppMetadata{ return &WebAppMetadata{
Name: name, Name: name,
TryFiles: tryFiles, TryFiles: tryFiles,
@ -43,21 +43,12 @@ func (wm *WebAppMetadata) EncodeMsgpack(enc *msgpack.Encoder) error {
return err return err
} }
keys := lo.Keys[string, WebAppMetadataFileReference](wm.Paths)
sort.Strings(keys)
paths := make([]WebAppMetadataFileReference, len(wm.Paths))
for i, v := range keys {
paths[i] = wm.Paths[v]
}
items := make([]interface{}, 5) items := make([]interface{}, 5)
items[0] = wm.Name items[0] = wm.Name
items[1] = wm.TryFiles items[1] = wm.TryFiles
items[2] = wm.ErrorPages items[2] = wm.ErrorPages
items[3] = paths items[3] = wm.Paths
items[4] = wm.ExtraMetadata items[4] = wm.ExtraMetadata
return enc.Encode(items) return enc.Encode(items)
@ -97,31 +88,11 @@ func (wm *WebAppMetadata) DecodeMsgpack(dec *msgpack.Decoder) error {
return err return err
} }
case 3: case 3:
paths, err := dec.DecodeSlice() err = dec.Decode(&wm.Paths)
if err != nil { if err != nil {
return err return err
} }
wm.Paths = make(map[string]WebAppMetadataFileReference, len(paths))
for _, v := range paths {
path := v.([]interface{})
parsedCid, err := encoding.CIDFromBytes(path[1].([]byte))
if err != nil {
return err
}
contentType := ""
if path[2] != nil {
contentType = path[2].(string)
}
wm.Paths[path[0].(string)] = WebAppMetadataFileReference{
Cid: parsedCid,
ContentType: contentType,
}
}
case 4: case 4:
err = dec.Decode(&wm.ExtraMetadata) err = dec.Decode(&wm.ExtraMetadata)
if err != nil { if err != nil {
@ -136,3 +107,91 @@ func (wm *WebAppMetadata) DecodeMsgpack(dec *msgpack.Decoder) error {
return nil return nil
} }
type WebAppFileMap struct {
linkedhashmap.Map
}
func NewWebAppFileMap() *WebAppFileMap {
return &WebAppFileMap{*linkedhashmap.New()}
}
func (wafm *WebAppFileMap) Put(key string, value WebAppMetadataFileReference) {
wafm.Map.Put(key, value)
}
func (wafm *WebAppFileMap) Get(key string) (WebAppMetadataFileReference, bool) {
value, found := wafm.Map.Get(key)
if !found {
return WebAppMetadataFileReference{}, false
}
return value.(WebAppMetadataFileReference), true
}
func (wafm *WebAppFileMap) Remove(key string) {
wafm.Map.Remove(key)
}
func (wafm *WebAppFileMap) Keys() []string {
keys := wafm.Map.Keys()
ret := make([]string, len(keys))
for i, key := range keys {
ret[i] = key.(string)
}
return ret
}
func (wafm *WebAppFileMap) Sort() {
keys := wafm.Keys()
newMap := NewWebAppFileMap()
sort.Strings(keys)
for _, key := range keys {
value, _ := wafm.Get(key)
newMap.Put(key, value)
}
wafm.Map = newMap.Map
}
func (wafm *WebAppFileMap) EncodeMsgpack(encoder *msgpack.Encoder) error {
wafm.Sort()
for _, key := range wafm.Keys() {
value, _ := wafm.Get(key)
err := encoder.EncodeString(key)
if err != nil {
return err
}
err = encoder.Encode(value)
if err != nil {
return err
}
}
return nil
}
func (wafm *WebAppFileMap) DecodeMsgpack(decoder *msgpack.Decoder) error {
arrLen, err := decoder.DecodeArrayLen()
if err != nil {
return err
}
for i := 0; i < arrLen; i++ {
key, err := decoder.DecodeString()
if err != nil {
return err
}
var value WebAppMetadataFileReference
err = decoder.Decode(&value)
if err != nil {
return err
}
wafm.Put(key, value)
}
wafm.Sort()
return nil
}