You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
833 B
Go

2 years ago
package mgr_user
import "math/rand"
const period = 1000000 //7位
const array = 100
func NewRIDCreator(lsid int32) *RIDCreator {
creator := &RIDCreator{sids: make(map[int32]int64, 1024)}
for rid := int64(lsid * period); rid < int64(lsid*period*10); rid++ {
idx := rid % array
if creator.ids[idx] == nil {
creator.ids[idx] = make(map[int64]bool)
}
creator.ids[idx][rid] = true
}
return creator
}
type RIDCreator struct {
ids [array]map[int64]bool
sids map[int32]int64
}
func (this *RIDCreator) Add(rid int64) {
idx := rid % array
if this.ids[idx] != nil {
delete(this.ids[idx], rid)
}
}
func (this *RIDCreator) ID(sid int32) int64 {
for {
idx := rand.Intn(array)
for rid := range this.ids[idx] {
if this.sids[sid]%10 == rid%10 {
this.sids[sid]++
this.Add(rid)
return rid
}
}
}
}