snapshot_store.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 rafthttp
  15. import (
  16. "io"
  17. )
  18. // snapshotStore is the store of snapshot. Caller could put one
  19. // snapshot into the store, and get it later.
  20. // snapshotStore stores at most one snapshot at a time, or it panics.
  21. type snapshotStore struct {
  22. rc io.ReadCloser
  23. // index of the stored snapshot
  24. // index is 0 if and only if there is no snapshot stored.
  25. index uint64
  26. }
  27. func (s *snapshotStore) put(rc io.ReadCloser, index uint64) {
  28. if s.index != 0 {
  29. plog.Panicf("unexpected put when there is one snapshot stored")
  30. }
  31. s.rc, s.index = rc, index
  32. }
  33. func (s *snapshotStore) get(index uint64) io.ReadCloser {
  34. if s.index == index {
  35. // set index to 0 to indicate no snapshot stored
  36. s.index = 0
  37. return s.rc
  38. }
  39. return nil
  40. }