-
Notifications
You must be signed in to change notification settings - Fork 7
/
pre.go
157 lines (122 loc) · 3.47 KB
/
pre.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"database/sql"
"fmt"
"log"
"strconv"
"strings"
)
type preRow struct {
ID int `json:"id"`
Name string `json:"name"`
Team string `json:"team"`
Cat string `json:"cat"`
Genre string `json:"genre"`
URL string `json:"url"`
Size float64 `json:"size"`
Files int `json:"files"`
PreAt int `json:"preAt"`
Nuke *nuke `json:"nuke"`
}
var replacer = strings.NewReplacer("(", "\\(", ")", "\\)")
const preColumns = "id, name, team, cat, genre, url, size, files, pre_at"
const defaultMaxMatches = 1000
func (p *preRow) proc() {
p.Size /= 1000
}
func scanPresRows(rows *sql.Rows, appendNukes bool) []preRow {
res := make([]preRow, 0)
for rows.Next() {
var r preRow
err := rows.Scan(&r.ID, &r.Name, &r.Team, &r.Cat, &r.Genre, &r.URL, &r.Size, &r.Files, &r.PreAt)
if err != nil {
log.Print(err)
continue
}
r.proc()
if appendNukes {
r.fetchNuke(mysql)
}
res = append(res, r)
}
return res
}
func getPre(db *sql.DB, preID int, withNuke bool) (*preRow, error) {
sqlQuery := fmt.Sprintf("SELECT %s FROM %s WHERE id = ?", preColumns, sphinxTable)
var r preRow
err := db.QueryRow(sqlQuery, preID).Scan(&r.ID, &r.Name, &r.Team, &r.Cat, &r.Genre, &r.URL, &r.Size, &r.Files, &r.PreAt)
if err != nil {
return nil, err
}
r.proc()
if withNuke {
r.fetchNuke(mysql)
}
return &r, nil
}
func getPresById(tx *sql.Tx, preID int, withNuke bool) ([]preRow, error) {
sqlQuery := fmt.Sprintf("SELECT %s FROM %s WHERE id = ?", preColumns, sphinxTable)
var r preRow
err := tx.QueryRow(sqlQuery, preID).Scan(&r.ID, &r.Name, &r.Team, &r.Cat, &r.Genre, &r.URL, &r.Size, &r.Files, &r.PreAt)
if err != nil {
return nil, err
}
r.proc()
if withNuke {
r.fetchNuke(mysql)
}
return []preRow{r}, nil
}
func searchPres(tx *sql.Tx, q string, offsetInt, countInt int, withNukes bool) ([]preRow, error) {
sqlQuery := fmt.Sprintf("SELECT %s FROM %s WHERE MATCH(?) ORDER BY id DESC LIMIT %d,%d", preColumns, sphinxTable, offsetInt, countInt)
if offsetInt+countInt > defaultMaxMatches {
sqlQuery += ", max_matches = " + strconv.Itoa(offsetInt+countInt)
}
sqlRows, err := tx.Query(sqlQuery, replacer.Replace(q))
if err != nil {
return nil, err
}
defer sqlRows.Close()
return scanPresRows(sqlRows, withNukes), nil
}
func latestPres(tx *sql.Tx, offsetInt, countInt int, withNukes bool) ([]preRow, error) {
sqlQuery := fmt.Sprintf("SELECT %s FROM %s ORDER BY id DESC LIMIT %d,%d", preColumns, sphinxTable, offsetInt, countInt)
if offsetInt+countInt > defaultMaxMatches {
sqlQuery += ", max_matches = " + strconv.Itoa(offsetInt+countInt)
}
sqlRows, err := tx.Query(sqlQuery)
if err != nil {
return nil, err
}
defer sqlRows.Close()
return scanPresRows(sqlRows, withNukes), nil
}
func (p *preRow) fetchNuke(db *sql.DB) {
n, err := getNuke(db, p.ID)
if err != nil {
return
}
p.setNuke(n)
}
func (p *preRow) setNuke(n *nuke) {
if n != nil {
n.setType()
p.Nuke = n
}
}
func (p *preRow) insertOrUpdateIndex() (int64, error) {
sqlQuery := fmt.Sprintf("REPLACE INTO %s (%s) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", realTimeIndex, preColumns)
res, err := sphinx.Exec(sqlQuery, p.ID, p.Name, p.Team, p.Cat, p.Genre, p.URL, p.Size, p.Files, p.PreAt)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
func (p *preRow) deleteFromIndex() (int64, error) {
sqlQuery := fmt.Sprintf("DELETE FROM %s WHERE id = ?", realTimeIndex)
res, err := sphinx.Exec(sqlQuery, p.ID)
if err != nil {
return 0, err
}
return res.RowsAffected()
}