memory_sequencer.go 655 B

123456789101112131415161718192021222324252627282930313233343536
  1. package sequence
  2. import (
  3. "sync"
  4. )
  5. // just for testing
  6. type MemorySequencer struct {
  7. counter uint64
  8. sequenceLock sync.Mutex
  9. }
  10. func NewMemorySequencer() (m *MemorySequencer) {
  11. m = &MemorySequencer{counter: 1}
  12. return
  13. }
  14. func (m *MemorySequencer) NextFileId(count uint64) (uint64, uint64) {
  15. m.sequenceLock.Lock()
  16. defer m.sequenceLock.Unlock()
  17. ret := m.counter
  18. m.counter += uint64(count)
  19. return ret, count
  20. }
  21. func (m *MemorySequencer) SetMax(seenValue uint64) {
  22. m.sequenceLock.Lock()
  23. defer m.sequenceLock.Unlock()
  24. if m.counter <= seenValue {
  25. m.counter = seenValue + 1
  26. }
  27. }
  28. func (m *MemorySequencer) Peek() uint64 {
  29. return m.counter
  30. }