2013-09-07 00:39:14 +02:00
|
|
|
// Copyright 2013 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gazelle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Conn) flushTorrents() {
|
|
|
|
var query bytes.Buffer
|
|
|
|
c.waitGroup.Add(1)
|
|
|
|
defer c.waitGroup.Done()
|
|
|
|
var count int
|
|
|
|
|
|
|
|
for {
|
|
|
|
length := len(c.torrentChannel)
|
|
|
|
query.Reset()
|
|
|
|
|
|
|
|
query.WriteString("INSERT INTO torrents (ID, Snatched, Seeders, Leechers, last_action) VALUES\n")
|
|
|
|
|
|
|
|
for count = 0; count < length; count++ {
|
2013-09-07 00:51:15 +02:00
|
|
|
s := <-c.torrentChannel
|
|
|
|
if s == "" {
|
2013-09-07 00:39:14 +02:00
|
|
|
break
|
|
|
|
}
|
2013-09-07 00:51:15 +02:00
|
|
|
query.WriteString(s)
|
2013-09-07 00:39:14 +02:00
|
|
|
|
|
|
|
if count != length-1 {
|
|
|
|
query.WriteRune(',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.terminate {
|
|
|
|
log.Printf("[torrents] Flushing %d\n", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
if count > 0 {
|
|
|
|
query.WriteString("\nON DUPLICATE KEY UPDATE Snatched = Snatched + VALUES(Snatched), " +
|
|
|
|
"Seeders = VALUES(Seeders), Leechers = VALUES(Leechers), " +
|
|
|
|
"last_action = IF(last_action < VALUES(last_action), VALUES(last_action), last_action);")
|
|
|
|
|
2013-09-07 01:08:06 +02:00
|
|
|
c.Exec(query.String())
|
2013-09-07 00:39:14 +02:00
|
|
|
|
|
|
|
if length < cap(c.torrentChannel)/2 {
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
}
|
|
|
|
} else if c.terminate {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) flushUsers() {}
|
|
|
|
func (c *Conn) flushTransferHistory() {}
|
|
|
|
func (c *Conn) flushTransferIps() {}
|
|
|
|
func (c *Conn) flushSnatches() {}
|