memory_sequencer.go 568 B

1234567891011121314151617181920212223242526272829303132
  1. package sequence
  2. import (
  3. "sync"
  4. )
  5. // default Sequencer
  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 {
  15. m.sequenceLock.Lock()
  16. defer m.sequenceLock.Unlock()
  17. ret := m.counter
  18. m.counter += count
  19. return ret
  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. }