decoder.go 2.0 KB

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