package rotater

import (
	"sync"
	"sync/atomic"
	"amuz.es/src/infra/goutils/logger"
)

/**
logger set
 */
type RotateSyncerSet struct {
	storage sync.Map
}

func (s *RotateSyncerSet) Delete(key logger.RotateSyncer) {
	s.storage.Delete(key)
}
func (s *RotateSyncerSet) Exist(key logger.RotateSyncer) (ok bool) {
	_, ok = s.storage.Load(key)
	return
}
func (s *RotateSyncerSet) SetNx(key logger.RotateSyncer) (bool) {
	_, exist := s.storage.LoadOrStore(key, 0)
	return !exist
}
func (s *RotateSyncerSet) Range(f func(key logger.RotateSyncer)) {
	s.storage.Range(s.rangeWrap(f))
}
func (s *RotateSyncerSet) Store(key logger.RotateSyncer) {
	s.storage.Store(key, 0)
}
func (s *RotateSyncerSet) rangeWrap(f func(key logger.RotateSyncer)) func(key, value interface{}) bool {
	ok := true
	return func(key, value interface{}) bool {
		f(key.(logger.RotateSyncer))
		return ok
	}
}

func (s *RotateSyncerSet) Len() int {
	var count uint64
	s.Range(func(conn logger.RotateSyncer) {
		atomic.AddUint64(&count, 1)
	})
	return int(count)
}
