decoder.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package wal
  15. import (
  16. "bufio"
  17. "encoding/binary"
  18. "hash"
  19. "io"
  20. "github.com/coreos/etcd/pkg/crc"
  21. "github.com/coreos/etcd/pkg/pbutil"
  22. "github.com/coreos/etcd/raft/raftpb"
  23. "github.com/coreos/etcd/wal/walpb"
  24. )
  25. type decoder struct {
  26. br *bufio.Reader
  27. c io.Closer
  28. crc hash.Hash32
  29. }
  30. func newDecoder(rc io.ReadCloser) *decoder {
  31. return &decoder{
  32. br: bufio.NewReader(rc),
  33. c: rc,
  34. crc: crc.New(0, crcTable),
  35. }
  36. }
  37. func (d *decoder) decode(rec *walpb.Record) error {
  38. rec.Reset()
  39. l, err := readInt64(d.br)
  40. if err != nil {
  41. return err
  42. }
  43. data := make([]byte, l)
  44. if _, err = io.ReadFull(d.br, data); err != nil {
  45. return err
  46. }
  47. if err := rec.Unmarshal(data); err != nil {
  48. return err
  49. }
  50. // skip crc checking if the record type is crcType
  51. if rec.Type == crcType {
  52. return nil
  53. }
  54. d.crc.Write(rec.Data)
  55. return rec.Validate(d.crc.Sum32())
  56. }
  57. func (d *decoder) updateCRC(prevCrc uint32) {
  58. d.crc = crc.New(prevCrc, crcTable)
  59. }
  60. func (d *decoder) lastCRC() uint32 {
  61. return d.crc.Sum32()
  62. }
  63. func (d *decoder) close() error {
  64. return d.c.Close()
  65. }
  66. func mustUnmarshalEntry(d []byte) raftpb.Entry {
  67. var e raftpb.Entry
  68. pbutil.MustUnmarshal(&e, d)
  69. return e
  70. }
  71. func mustUnmarshalState(d []byte) raftpb.HardState {
  72. var s raftpb.HardState
  73. pbutil.MustUnmarshal(&s, d)
  74. return s
  75. }
  76. func readInt64(r io.Reader) (int64, error) {
  77. var n int64
  78. err := binary.Read(r, binary.LittleEndian, &n)
  79. return n, err
  80. }