47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package checkpoint
|
|
|
|
import "sync"
|
|
|
|
// MemoryStore 是进程内检查点存储,线程安全。用于单元测试与零依赖本地运行。
|
|
type MemoryStore struct {
|
|
mu sync.RWMutex
|
|
logOffsets map[string]int64
|
|
ackedSeq map[string]int64
|
|
}
|
|
|
|
// NewMemoryStore 创建空的进程内检查点存储。
|
|
func NewMemoryStore() *MemoryStore {
|
|
return &MemoryStore{
|
|
logOffsets: map[string]int64{},
|
|
ackedSeq: map[string]int64{},
|
|
}
|
|
}
|
|
|
|
func (s *MemoryStore) GetLogOffset(file string) (int64, error) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.logOffsets[file], nil
|
|
}
|
|
|
|
func (s *MemoryStore) SetLogOffset(file string, offset int64) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.logOffsets[file] = offset
|
|
return nil
|
|
}
|
|
|
|
func (s *MemoryStore) GetAckedSeq(hostID string) (int64, error) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.ackedSeq[hostID], nil
|
|
}
|
|
|
|
func (s *MemoryStore) SetAckedSeq(hostID string, seq int64) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.ackedSeq[hostID] = seq
|
|
return nil
|
|
}
|
|
|
|
func (s *MemoryStore) Close() error { return nil }
|