2018-02-19 04:26:01 +00:00
|
|
|
package counters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"sync"
|
2018-09-08 01:32:24 +00:00
|
|
|
"sync/atomic"
|
2018-02-19 04:26:01 +00:00
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2018-10-27 03:21:02 +00:00
|
|
|
"github.com/Azareal/Gosora/query_gen"
|
2018-02-19 04:26:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var TopicViewCounter *DefaultTopicViewCounter
|
|
|
|
|
|
|
|
// TODO: Use two odd-even maps for now, and move to something more concurrent later, maybe a sharded map?
|
|
|
|
type DefaultTopicViewCounter struct {
|
|
|
|
oddTopics map[int]*RWMutexCounterBucket // map[tid]struct{counter,sync.RWMutex}
|
|
|
|
evenTopics map[int]*RWMutexCounterBucket
|
|
|
|
oddLock sync.RWMutex
|
|
|
|
evenLock sync.RWMutex
|
|
|
|
|
|
|
|
update *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultTopicViewCounter() (*DefaultTopicViewCounter, error) {
|
2018-08-04 11:46:36 +00:00
|
|
|
acc := qgen.NewAcc()
|
2018-02-19 04:26:01 +00:00
|
|
|
counter := &DefaultTopicViewCounter{
|
|
|
|
oddTopics: make(map[int]*RWMutexCounterBucket),
|
|
|
|
evenTopics: make(map[int]*RWMutexCounterBucket),
|
|
|
|
update: acc.Update("topics").Set("views = views + ?").Where("tid = ?").Prepare(),
|
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
c.AddScheduledFifteenMinuteTask(counter.Tick) // Who knows how many topics we have queued up, we probably don't want this running too frequently
|
|
|
|
//c.AddScheduledSecondTask(counter.Tick)
|
|
|
|
c.AddShutdownTask(counter.Tick)
|
2018-02-19 04:26:01 +00:00
|
|
|
return counter, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (counter *DefaultTopicViewCounter) Tick() error {
|
2018-02-26 09:07:00 +00:00
|
|
|
// TODO: Fold multiple 1 view topics into one query
|
|
|
|
|
2018-02-19 04:26:01 +00:00
|
|
|
counter.oddLock.RLock()
|
|
|
|
oddTopics := counter.oddTopics
|
|
|
|
counter.oddLock.RUnlock()
|
|
|
|
for topicID, topic := range oddTopics {
|
|
|
|
var count int
|
|
|
|
topic.RLock()
|
|
|
|
count = topic.counter
|
|
|
|
topic.RUnlock()
|
|
|
|
// TODO: Only delete the bucket when it's zero to avoid hitting popular topics?
|
|
|
|
counter.oddLock.Lock()
|
|
|
|
delete(counter.oddTopics, topicID)
|
|
|
|
counter.oddLock.Unlock()
|
|
|
|
err := counter.insertChunk(count, topicID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
counter.evenLock.RLock()
|
|
|
|
evenTopics := counter.evenTopics
|
|
|
|
counter.evenLock.RUnlock()
|
|
|
|
for topicID, topic := range evenTopics {
|
|
|
|
var count int
|
|
|
|
topic.RLock()
|
|
|
|
count = topic.counter
|
|
|
|
topic.RUnlock()
|
|
|
|
// TODO: Only delete the bucket when it's zero to avoid hitting popular topics?
|
|
|
|
counter.evenLock.Lock()
|
|
|
|
delete(counter.evenTopics, topicID)
|
|
|
|
counter.evenLock.Unlock()
|
|
|
|
err := counter.insertChunk(count, topicID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Optimise this further. E.g. Using IN() on every one view topic. Rinse and repeat for two views, three views, four views and five views.
|
|
|
|
func (counter *DefaultTopicViewCounter) insertChunk(count int, topicID int) error {
|
|
|
|
if count == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-11 12:21:15 +00:00
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
c.DebugLogf("Inserting %d views into topic %d", count, topicID)
|
2018-02-19 04:26:01 +00:00
|
|
|
_, err := counter.update.Exec(count, topicID)
|
2018-09-08 01:32:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-11 12:21:15 +00:00
|
|
|
|
2018-09-08 01:32:24 +00:00
|
|
|
// TODO: Add a way to disable this for extra speed ;)
|
2019-04-19 06:36:26 +00:00
|
|
|
tcache := c.Topics.GetCache()
|
2018-09-11 12:21:15 +00:00
|
|
|
if tcache != nil {
|
|
|
|
topic, err := tcache.Get(topicID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
atomic.AddInt64(&topic.ViewCount, int64(count))
|
2018-09-08 01:32:24 +00:00
|
|
|
}
|
2018-09-11 12:21:15 +00:00
|
|
|
|
2018-09-08 01:32:24 +00:00
|
|
|
return nil
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (counter *DefaultTopicViewCounter) Bump(topicID int) {
|
|
|
|
// Is the ID even?
|
|
|
|
if topicID%2 == 0 {
|
|
|
|
counter.evenLock.RLock()
|
|
|
|
topic, ok := counter.evenTopics[topicID]
|
|
|
|
counter.evenLock.RUnlock()
|
|
|
|
if ok {
|
|
|
|
topic.Lock()
|
|
|
|
topic.counter++
|
|
|
|
topic.Unlock()
|
|
|
|
} else {
|
|
|
|
counter.evenLock.Lock()
|
|
|
|
counter.evenTopics[topicID] = &RWMutexCounterBucket{counter: 1}
|
|
|
|
counter.evenLock.Unlock()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
counter.oddLock.RLock()
|
|
|
|
topic, ok := counter.oddTopics[topicID]
|
|
|
|
counter.oddLock.RUnlock()
|
|
|
|
if ok {
|
|
|
|
topic.Lock()
|
|
|
|
topic.counter++
|
|
|
|
topic.Unlock()
|
|
|
|
} else {
|
|
|
|
counter.oddLock.Lock()
|
|
|
|
counter.oddTopics[topicID] = &RWMutexCounterBucket{counter: 1}
|
|
|
|
counter.oddLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|