dispatch/storage/channel_index.go

169 lines
3.2 KiB
Go
Raw Normal View History

2019-01-23 06:34:39 +00:00
package storage
import (
"sort"
"strings"
"sync"
"time"
)
type ChannelListItem struct {
Name string
UserCount int
Topic string
}
type ChannelListIndex interface {
Add(item *ChannelListItem)
Finish()
Search(q string) []*ChannelListItem
SearchN(q string, start, n int) []*ChannelListItem
2020-05-11 12:13:36 +00:00
len() int
2019-01-23 06:34:39 +00:00
}
type MapChannelListIndex struct {
channels chanList
m map[string][]*ChannelListItem
}
func NewMapChannelListIndex() *MapChannelListIndex {
return &MapChannelListIndex{
m: map[string][]*ChannelListItem{},
}
}
func (idx *MapChannelListIndex) Add(item *ChannelListItem) {
idx.channels = append(idx.channels, item)
}
func (idx *MapChannelListIndex) Finish() {
sort.Sort(idx.channels)
for _, ch := range idx.channels {
key := strings.TrimLeft(strings.ToLower(ch.Name), "#")
for i := 1; i <= len(key); i++ {
k := key[:i]
if _, ok := idx.m[k]; ok {
idx.m[k] = append(idx.m[k], ch)
} else {
idx.m[k] = chanList{ch}
}
}
}
}
func (idx *MapChannelListIndex) Search(q string) []*ChannelListItem {
if q == "" {
return idx.channels
}
return idx.m[q]
}
func (idx *MapChannelListIndex) SearchN(q string, start, n int) []*ChannelListItem {
if q == "" {
if start >= len(idx.channels) {
return nil
}
return idx.channels[start:min(start+n, len(idx.channels))]
}
res := idx.m[q]
if start >= len(res) {
return nil
}
return res[start:min(start+n, len(res))]
}
2020-05-11 12:13:36 +00:00
func (idx *MapChannelListIndex) len() int {
return len(idx.channels)
}
2019-01-23 06:34:39 +00:00
func min(x, y int) int {
if x < y {
return x
}
return y
}
type chanList []*ChannelListItem
func (c chanList) Len() int {
return len(c)
}
func (c chanList) Less(i, j int) bool {
return c[i].UserCount > c[j].UserCount ||
(c[i].UserCount == c[j].UserCount &&
strings.ToLower(c[i].Name) < strings.ToLower(c[j].Name))
}
func (c chanList) Swap(i, j int) {
ch := c[i]
c[i] = c[j]
c[j] = ch
}
const ChannelListUpdateInterval = time.Hour * 24
2019-01-25 10:24:57 +00:00
const ChannelListUpdateTimeout = time.Minute * 5
2019-01-23 06:34:39 +00:00
type ChannelIndexManager struct {
indexes map[string]*managedChannelIndex
lock sync.Mutex
}
func NewChannelIndexManager() *ChannelIndexManager {
return &ChannelIndexManager{
indexes: map[string]*managedChannelIndex{},
}
}
type managedChannelIndex struct {
index ChannelListIndex
updatedAt time.Time
updating bool
}
2020-06-15 08:58:51 +00:00
func (m *ChannelIndexManager) Get(network string) (ChannelListIndex, bool) {
2019-01-23 06:34:39 +00:00
m.lock.Lock()
defer m.lock.Unlock()
2020-06-15 08:58:51 +00:00
idx, ok := m.indexes[network]
2019-01-23 06:34:39 +00:00
if !ok {
2020-06-15 08:58:51 +00:00
m.indexes[network] = &managedChannelIndex{
2019-01-23 06:34:39 +00:00
updating: true,
}
2020-06-15 08:58:51 +00:00
go m.timeoutUpdate(network)
2019-01-23 06:34:39 +00:00
return nil, true
}
if !idx.updating && time.Since(idx.updatedAt) > ChannelListUpdateInterval {
idx.updating = true
2020-06-15 08:58:51 +00:00
go m.timeoutUpdate(network)
2019-01-23 06:34:39 +00:00
return idx.index, true
}
return idx.index, false
}
2020-06-15 08:58:51 +00:00
func (m *ChannelIndexManager) Set(network string, index ChannelListIndex) {
2020-05-11 12:13:36 +00:00
if index.len() > 0 {
m.lock.Lock()
2020-06-15 08:58:51 +00:00
m.indexes[network] = &managedChannelIndex{
2020-05-11 12:13:36 +00:00
index: index,
updatedAt: time.Now(),
}
m.lock.Unlock()
2019-01-23 06:34:39 +00:00
}
}
2019-01-25 10:24:57 +00:00
2020-06-15 08:58:51 +00:00
func (m *ChannelIndexManager) timeoutUpdate(network string) {
2019-01-25 10:24:57 +00:00
time.Sleep(ChannelListUpdateTimeout)
m.lock.Lock()
2020-06-15 08:58:51 +00:00
if m.indexes[network].updating {
m.indexes[network].updating = false
2019-01-25 10:24:57 +00:00
}
m.lock.Unlock()
}