MCPcopy
hub / github.com/openacid/slim / Unmarshal

Method Unmarshal

trie/slimtrie_marshal.go:35–95  ·  view source on GitHub ↗

Unmarshal a SlimTrie from a byte stream. Since 0.4.3

(buf []byte)

Source from the content-addressed store, hash-verified

33//
34// Since 0.4.3
35func (st *SlimTrie) Unmarshal(buf []byte) error {
36
37 st.inner = &Slim{}
38
39 reader := bytes.NewReader(buf)
40
41 _, h, err := pbcmpl.ReadHeader(reader)
42 if err != nil {
43 return errors.WithMessage(err, "failed to unmarshal header")
44 }
45
46 ver := h.GetVersion()
47 compatible := st.compatibleVersions()
48
49 if !vers.IsCompatible(ver, compatible) {
50 return errors.Wrapf(ErrIncompatible,
51 fmt.Sprintf(`version: "%s", compatible versions:"%s"`,
52 ver,
53 strings.Join(compatible, " || ")))
54 }
55
56 reader = bytes.NewReader(buf)
57
58 // 0.5.10 and 0.5.11 share the same protobuf format:
59
60 if vers.Check(ver, slimtrieVersion, "0.5.10") {
61 _, _, err := pbcmpl.Unmarshal(reader, st.inner)
62 if err != nil {
63 return errors.WithMessage(err, "failed to unmarshal inner")
64 }
65 return nil
66 }
67
68 // ver: "==1.0.0 || <0.5.10"
69
70 children := &array.Array32{}
71 steps := &array.U16{}
72 leaves := &array.Array{}
73 leaves.EltEncoder = st.encoder
74
75 _, _, err = pbcmpl.Unmarshal(reader, children)
76 if err != nil {
77 return errors.WithMessage(err, "failed to unmarshal children")
78 }
79
80 _, _, err = pbcmpl.Unmarshal(reader, steps)
81 if err != nil {
82 return errors.WithMessage(err, "failed to unmarshal steps")
83 }
84
85 _, _, err = pbcmpl.Unmarshal(reader, leaves)
86 if err != nil {
87 return errors.WithMessage(err, "failed to unmarshal leaves")
88 }
89
90 // backward compatible:
91
92 before000510(st, ver, children, steps, leaves)

Callers 15

TestU16EncodeDecodeFunction · 0.80
TestU16EncodeDecodeBigFunction · 0.80
TestU32EncodeDecodeFunction · 0.80
TestU32EncodeDecodeBigFunction · 0.80
TestU64EncodeDecodeFunction · 0.80
TestU64EncodeDecodeBigFunction · 0.80
TestI16EncodeDecodeFunction · 0.80
TestI16EncodeDecodeBigFunction · 0.80
TestI32EncodeDecodeFunction · 0.80
TestI32EncodeDecodeBigFunction · 0.80
TestI64EncodeDecodeFunction · 0.80
TestI64EncodeDecodeBigFunction · 0.80

Calls 3

compatibleVersionsMethod · 0.95
before000510Function · 0.85
GetVersionMethod · 0.45

Tested by 15

TestU16EncodeDecodeFunction · 0.64
TestU16EncodeDecodeBigFunction · 0.64
TestU32EncodeDecodeFunction · 0.64
TestU32EncodeDecodeBigFunction · 0.64
TestU64EncodeDecodeFunction · 0.64
TestU64EncodeDecodeBigFunction · 0.64
TestI16EncodeDecodeFunction · 0.64
TestI16EncodeDecodeBigFunction · 0.64
TestI32EncodeDecodeFunction · 0.64
TestI32EncodeDecodeBigFunction · 0.64
TestI64EncodeDecodeFunction · 0.64
TestI64EncodeDecodeBigFunction · 0.64