entry_reader.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 rafthttp
  14. import (
  15. "encoding/binary"
  16. "io"
  17. "github.com/coreos/etcd/raft/raftpb"
  18. )
  19. type entryReader struct {
  20. r io.Reader
  21. }
  22. func (er *entryReader) readEntries() ([]raftpb.Entry, error) {
  23. var l uint64
  24. if err := binary.Read(er.r, binary.BigEndian, &l); err != nil {
  25. return nil, err
  26. }
  27. ents := make([]raftpb.Entry, int(l))
  28. for i := 0; i < int(l); i++ {
  29. if err := er.readEntry(&ents[i]); err != nil {
  30. return nil, err
  31. }
  32. }
  33. return ents, nil
  34. }
  35. func (er *entryReader) readEntry(ent *raftpb.Entry) error {
  36. var l uint64
  37. if err := binary.Read(er.r, binary.BigEndian, &l); err != nil {
  38. return err
  39. }
  40. buf := make([]byte, int(l))
  41. if _, err := io.ReadFull(er.r, buf); err != nil {
  42. return err
  43. }
  44. return ent.Unmarshal(buf)
  45. }