You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
926 B
55 lines
926 B
package unknownnicks
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/globalsign/mgo/bson"
|
|
)
|
|
|
|
var updateMutex sync.Mutex
|
|
var updateMap map[string]int
|
|
|
|
// Add adds a nick as unknown.
|
|
func Add(nick string) {
|
|
updateMap[nick] = updateMap[nick] + 1
|
|
}
|
|
|
|
// BeginUpdate starts an add operation
|
|
func BeginUpdate() error {
|
|
updateMutex.Lock()
|
|
|
|
_, err := collection.RemoveAll(bson.M{})
|
|
if err != nil {
|
|
updateMutex.Unlock()
|
|
return err
|
|
}
|
|
|
|
if updateMap == nil {
|
|
updateMap = make(map[string]int)
|
|
}
|
|
|
|
for key := range updateMap {
|
|
delete(updateMap, key)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CancelUpdate cancels an add operation
|
|
func CancelUpdate() {
|
|
updateMutex.Unlock()
|
|
}
|
|
|
|
// CommitUpdate commits an add operation to the database.
|
|
func CommitUpdate() error {
|
|
defer updateMutex.Unlock()
|
|
|
|
for nick, score := range updateMap {
|
|
_, err := collection.UpsertId(nick, bson.M{"$set": bson.M{"score": score}})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|