Encode writes the custom encoding of v to the stream. It returns an error if the encoded size of v is greater than 1MB. Any error encountered during the write is also returned.
(v *TimedWALMessage)
| 298 | // the encoded size of v is greater than 1MB. Any error encountered |
| 299 | // during the write is also returned. |
| 300 | func (enc *WALEncoder) Encode(v *TimedWALMessage) error { |
| 301 | pbMsg, err := WALToProto(v.Msg) |
| 302 | if err != nil { |
| 303 | return err |
| 304 | } |
| 305 | pv := tmcons.TimedWALMessage{ |
| 306 | Time: v.Time, |
| 307 | Msg: pbMsg, |
| 308 | } |
| 309 | |
| 310 | data, err := proto.Marshal(&pv) |
| 311 | if err != nil { |
| 312 | panic(fmt.Errorf("encode timed wall message failure: %w", err)) |
| 313 | } |
| 314 | |
| 315 | crc := crc32.Checksum(data, crc32c) |
| 316 | length := uint32(len(data)) |
| 317 | if length > maxMsgSizeBytes { |
| 318 | return fmt.Errorf("msg is too big: %d bytes, max: %d bytes", length, maxMsgSizeBytes) |
| 319 | } |
| 320 | totalLength := 8 + int(length) |
| 321 | |
| 322 | msg := make([]byte, totalLength) |
| 323 | binary.BigEndian.PutUint32(msg[0:4], crc) |
| 324 | binary.BigEndian.PutUint32(msg[4:8], length) |
| 325 | copy(msg[8:], data) |
| 326 | |
| 327 | _, err = enc.wr.Write(msg) |
| 328 | return err |
| 329 | } |
| 330 | |
| 331 | // IsDataCorruptionError returns true if data has been corrupted inside WAL. |
| 332 | func IsDataCorruptionError(err error) bool { |