github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/storage/codec.go (about) 1 package storage 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/pyroscope-io/pyroscope/pkg/storage/dict" 8 "github.com/pyroscope-io/pyroscope/pkg/storage/dimension" 9 "github.com/pyroscope-io/pyroscope/pkg/storage/segment" 10 "github.com/pyroscope-io/pyroscope/pkg/storage/tree" 11 ) 12 13 type treeCodec struct{ *Storage } 14 15 func (treeCodec) New(_ string) interface{} { return tree.New() } 16 17 func (c treeCodec) Serialize(w io.Writer, k string, v interface{}) error { 18 key := segment.FromTreeToDictKey(k) 19 d, err := c.dicts.GetOrCreate(key) 20 if err != nil { 21 return err 22 } 23 err = v.(*tree.Tree).SerializeTruncate(d.(*dict.Dict), c.config.maxNodesSerialization, w) 24 if err != nil { 25 return err 26 } 27 c.dicts.Put(key, d) 28 return nil 29 } 30 31 func (c treeCodec) Deserialize(r io.Reader, k string) (interface{}, error) { 32 key := segment.FromTreeToDictKey(k) 33 d, err := c.dicts.GetOrCreate(key) 34 if err != nil { 35 return nil, fmt.Errorf("dicts cache for %v: %w", key, err) 36 } 37 return tree.Deserialize(d.(*dict.Dict), r) 38 } 39 40 type dictionaryCodec struct{} 41 42 func (dictionaryCodec) New(_ string) interface{} { return dict.New() } 43 44 func (dictionaryCodec) Serialize(w io.Writer, _ string, v interface{}) error { 45 return v.(*dict.Dict).Serialize(w) 46 } 47 48 func (dictionaryCodec) Deserialize(r io.Reader, _ string) (interface{}, error) { 49 return dict.Deserialize(r) 50 } 51 52 type segmentCodec struct{} 53 54 func (segmentCodec) New(_ string) interface{} { return segment.New() } 55 56 func (segmentCodec) Serialize(w io.Writer, _ string, v interface{}) error { 57 return v.(*segment.Segment).Serialize(w) 58 } 59 60 func (segmentCodec) Deserialize(r io.Reader, _ string) (interface{}, error) { 61 return segment.Deserialize(r) 62 } 63 64 type dimensionCodec struct{} 65 66 func (dimensionCodec) New(_ string) interface{} { return dimension.New() } 67 68 func (dimensionCodec) Serialize(w io.Writer, _ string, v interface{}) error { 69 return v.(*dimension.Dimension).Serialize(w) 70 } 71 72 func (dimensionCodec) Deserialize(r io.Reader, _ string) (interface{}, error) { 73 return dimension.Deserialize(r) 74 }