2013-11-10 17:31:50 +08:00
|
|
|
package sequence
|
|
|
|
|
2014-04-17 14:43:27 +08:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
2013-11-10 17:31:50 +08:00
|
|
|
|
2024-04-30 12:51:00 +08:00
|
|
|
// default Sequencer
|
2013-11-10 17:31:50 +08:00
|
|
|
type MemorySequencer struct {
|
2014-04-17 14:43:27 +08:00
|
|
|
counter uint64
|
|
|
|
sequenceLock sync.Mutex
|
2013-11-10 17:31:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemorySequencer() (m *MemorySequencer) {
|
|
|
|
m = &MemorySequencer{counter: 1}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-30 15:49:58 +08:00
|
|
|
func (m *MemorySequencer) NextFileId(count uint64) uint64 {
|
2014-04-17 14:43:27 +08:00
|
|
|
m.sequenceLock.Lock()
|
|
|
|
defer m.sequenceLock.Unlock()
|
2013-11-10 17:31:50 +08:00
|
|
|
ret := m.counter
|
2019-10-30 15:49:58 +08:00
|
|
|
m.counter += count
|
|
|
|
return ret
|
2013-11-10 17:31:50 +08:00
|
|
|
}
|
2014-04-17 14:43:27 +08:00
|
|
|
|
|
|
|
func (m *MemorySequencer) SetMax(seenValue uint64) {
|
|
|
|
m.sequenceLock.Lock()
|
|
|
|
defer m.sequenceLock.Unlock()
|
|
|
|
if m.counter <= seenValue {
|
|
|
|
m.counter = seenValue + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemorySequencer) Peek() uint64 {
|
|
|
|
return m.counter
|
|
|
|
}
|