MCPcopy Create free account
hub / github.com/ByteStorage/FlyDB / MarshalBinary

Method MarshalBinary

structure/zset.go:1164–1201  ·  view source on GitHub ↗

MarshalBinary serializes the FZSet instance into a byte slice. It uses MessagePack format for serialization Returns the serialized byte slice and an error if the encoding fails.

()

Source from the content-addressed store, hash-verified

1162// It uses MessagePack format for serialization
1163// Returns the serialized byte slice and an error if the encoding fails.
1164func (fzs *FZSet) MarshalBinary() (_ []byte, err error) {
1165
1166 // Initializing the MessagePackEncoder
1167 enc := encoding.NewMessagePackEncoder()
1168
1169 // Encoding the size attribute of d (i.e., d.size). The operation could fail, thus we check for an error.
1170 // An error, if occurred, will be returned immediately, hence the flow of execution stops here.
1171 err = enc.Encode(fzs.size)
1172 if err != nil {
1173 return nil, err
1174 }
1175
1176 // This is the start of a loop going over all the nodes in d's skip list from the tail of the
1177 // list to the head.
1178 // The tail and head pointers refer to the last and first element of the list, respectively,
1179 // and are maintained for efficient traversing of the list.
1180 // we do that to get the elements in reverse order from biggest to the smallest for the best
1181 // insertion efficiency as it makes the insertion O(1), because each new element to be inserted is
1182 // the smallest yet.
1183 x := fzs.skipList.tail
1184 // as long as there are elements in the SkipList continue
1185 for x != nil {
1186 // Encoding the value of the current node in the skip list
1187 // Again, if an error occurs it gets immediately returned, thus breaking the loop.
1188 err = enc.Encode(x.value)
1189 if err != nil {
1190 return nil, err
1191 }
1192
1193 // Move to the previous node in the skip list.
1194 x = x.prev
1195 }
1196
1197 // After the traversal of the skip list, the encoder should now hold the serialized representation of the
1198 // FZSet. Now, we return the bytes from the encoder along with any error that might have occurred
1199 // during the encoding (should be nil if everything went fine).
1200 return enc.Bytes(), err
1201}
1202
1203// UnmarshalBinary de-serializes the given byte slice into ZSetValue instance
1204// It uses the MessagePack format for de-serialization

Callers

nothing calls this directly

Calls 3

NewMessagePackEncoderFunction · 0.92
EncodeMethod · 0.45
BytesMethod · 0.45

Tested by

no test coverage detected