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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
package db
import (
"fmt"
"net"
"github.com/felix/dhtsearch/models"
"github.com/jackc/pgx"
"github.com/jackc/pgx/pgtype"
)
// Store is a store
type Store struct {
*pgx.ConnPool
}
// NewStore connects and initializes a new store
func NewStore(dsn string) (*Store, error) {
cfg, err := pgx.ParseURI(dsn)
if err != nil {
return nil, err
}
c, err := pgx.NewConnPool(pgx.ConnPoolConfig{ConnConfig: cfg, MaxConnections: 10})
if err != nil {
return nil, err
}
s := &Store{c}
// TODO
var upToDate bool
err = s.QueryRow(sqlCheckSchema).Scan(&upToDate)
if err != nil || !upToDate {
_, err = s.Exec(sqlSchema)
}
return s, err
}
// PendingInfohashes gets the next pending infohash from the store
func (s *Store) PendingInfohashes(n int) (peers []*models.Peer, err error) {
rows, err := s.Query(sqlSelectPendingInfohashes, n)
defer rows.Close()
if err != nil {
return nil, err
}
for rows.Next() {
var p models.Peer
var ih pgtype.Bytea
var addr string
err = rows.Scan(&addr, &ih)
if err != nil {
return nil, err
}
// TODO save peer network?
p.Addr, err = net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
ih.AssignTo(&p.Infohash)
peers = append(peers, &p)
}
return peers, nil
}
// SaveTorrent implements torrentStore
func (s *Store) SaveTorrent(t *models.Torrent) error {
tx, err := s.Begin()
if err != nil {
return err
}
defer tx.Rollback()
var torrentID int
err = tx.QueryRow(sqlInsertTorrent, t.Name, t.Infohash, t.Size).Scan(&torrentID)
if err != nil {
return fmt.Errorf("insertTorrent: %s", err)
}
// Write tags
for _, tag := range t.Tags {
tagID, err := s.SaveTag(tag)
if err != nil {
return fmt.Errorf("saveTag: %s", err)
}
_, err = tx.Exec(sqlInsertTagTorrent, tagID, torrentID)
if err != nil {
return fmt.Errorf("insertTagTorrent: %s", err)
}
}
// Write files
for _, f := range t.Files {
_, err := tx.Exec(sqlInsertFile, torrentID, f.Path, f.Size)
if err != nil {
return fmt.Errorf("insertFile: %s", err)
}
}
// Should this be outside the transaction?
_, err = tx.Exec(sqlUpdateFTSVectors, torrentID)
if err != nil {
return fmt.Errorf("updateVectors: %s", err)
}
return tx.Commit()
}
// SavePeer implements torrentStore
func (s *Store) SavePeer(p *models.Peer) (err error) {
_, err = s.Exec(sqlInsertPeer, p.Addr.String(), p.Infohash.Bytes())
return err
}
func (s *Store) RemovePeer(p *models.Peer) (err error) {
_, err = s.Exec(sqlRemovePeer, p.Addr.String())
return err
}
// TorrentsByHash implements torrentStore
func (s *Store) TorrentByHash(ih models.Infohash) (*models.Torrent, error) {
rows, err := s.Query(sqlGetTorrent, ih)
defer rows.Close()
if err != nil {
return nil, err
}
torrents, err := s.fetchTorrents(rows)
if err != nil {
return nil, err
}
return torrents[0], nil
}
// TorrentsByName implements torrentStore
func (s *Store) TorrentsByName(query string, offset int) ([]*models.Torrent, error) {
rows, err := s.Query(sqlSearchTorrents, fmt.Sprintf("%%%s%%", query), offset)
defer rows.Close()
if err != nil {
return nil, err
}
torrents, err := s.fetchTorrents(rows)
if err != nil {
return nil, err
}
return torrents, nil
}
// TorrentsByTag implements torrentStore
func (s *Store) TorrentsByTag(tag string, offset int) ([]*models.Torrent, error) {
rows, err := s.Query(sqlTorrentsByTag, tag, offset)
defer rows.Close()
if err != nil {
return nil, err
}
torrents, err := s.fetchTorrents(rows)
if err != nil {
return nil, err
}
return torrents, nil
}
// SaveTag implements tagStore interface
func (s *Store) SaveTag(tag string) (tagID int, err error) {
err = s.QueryRow(sqlInsertTag, tag).Scan(&tagID)
return tagID, err
}
func (s *Store) fetchTorrents(rows *pgx.Rows) (torrents []*models.Torrent, err error) {
for rows.Next() {
var t models.Torrent
/*
t := &models.Torrent{
Files: []models.File{},
Tags: []string{},
}
*/
err = rows.Scan(
&t.ID, &t.Infohash, &t.Name, &t.Size, &t.Created, &t.Updated,
)
if err != nil {
return nil, err
}
err = func() error {
rowsf, err := s.Query(sqlSelectFiles, t.ID)
defer rowsf.Close()
if err != nil {
return fmt.Errorf("failed to select files: %s", err)
}
for rowsf.Next() {
var f models.File
err = rowsf.Scan(&f.ID, &f.TorrentID, &f.Path, &f.Size)
if err != nil {
return fmt.Errorf("failed to build file: %s", err)
}
}
return nil
}()
if err != nil {
return nil, err
}
err = func() error {
rowst, err := s.Query(sqlSelectTags, t.ID)
defer rowst.Close()
if err != nil {
return fmt.Errorf("failed to select tags: %s", err)
}
for rowst.Next() {
var tg string
err = rowst.Scan(&tg)
if err != nil {
return fmt.Errorf("failed to build tag: %s", err)
}
t.Tags = append(t.Tags, tg)
}
return nil
}()
if err != nil {
return nil, err
}
torrents = append(torrents, &t)
}
return torrents, err
}
const (
sqlGetTorrent = `select *
from torrents
where infohash = $1 limit 1`
sqlInsertTorrent = `insert into torrents (
name, infohash, size, created, updated
) values (
$1, $2, $3, now(), now()
) on conflict (infohash) do
update set
name = $1,
size = $3,
updated = now()
returning id`
// peer.address
// torrent.infohash
sqlInsertPeer = `with save_peer as (
insert into peers
(address, created, updated) values ($1, now(), now())
returning id
), save_torrent as (
insert into torrents (infohash, created, updated)
values ($2, now(), now())
on conflict (infohash) do update set
updated = now()
returning id
) insert into peers_torrents
(peer_id, torrent_id)
select
sp.id, st.id
from save_peer sp, save_torrent st
on conflict do nothing`
sqlRemovePeer = `delete from peers
where address = $1`
sqlUpdateFTSVectors = `update torrents set
tsv = sub.tsv from (
select t.id,
setweight(to_tsvector(
translate(t.name, '._-', ' ')
), 'A')
|| setweight(to_tsvector(
translate(string_agg(coalesce(f.path, ''), ' '), './_-', ' ')
), 'B') as tsv
from torrents t
left join files f on t.id = f.torrent_id
where t.id = $1
group by t.id
) as sub
where sub.id = torrents.id`
sqlSelectPendingInfohashes = `with get_pending as (
select p.id
from peers p
join peers_torrents pt on p.id = pt.peer_id
join torrents t on pt.torrent_id = t.id
where t.name is null
order by p.updated asc
limit $1 for update
), stamp_peer as (
update peers set updated = now()
where id in (select id from get_pending)
) select
p.address, t.infohash
from peers p
join peers_torrents pt on p.id = pt.peer_id
join torrents t on pt.torrent_id = t.id
where p.id in (select id from get_pending)`
sqlSearchTorrents = `select
t.id, t.infohash, t.name, t.size, t.updated
from torrents t
where t.tsv @@ plainto_tsquery($1)
order by ts_rank(tsv, plainto_tsquery($1)) desc, t.updated desc
limit 50 offset $2`
sqlTorrentsByTag = `select
t.id, t.infohash, t.name, t.size, t.created, t.updated
from torrents t
inner join tags_torrents tt on t.id = tt.torrent_id
inner join tags ta on tt.tag_id = ta.id
where ta.name = $1 group by t.id
order by updated asc
limit 50 offset $2`
sqlSelectFiles = `select * from files
where torrent_id = $1
order by path asc`
sqlInsertFile = `insert into files
(torrent_id, path, size)
values
($1, $2, $3)`
sqlSelectTags = `select name
from tags t
inner join tags_torrents tt on t.id = tt.tag_id
where tt.torrent_id = $1`
sqlInsertTagTorrent = `insert into tags_torrents
(tag_id, torrent_id) values ($1, $2)
on conflict do nothing`
sqlInsertTag = `insert into tags (name) values ($1)
on conflict (name) do update set name = excluded.name returning id`
sqlCheckSchema = `select exists (
select 1 from pg_tables
where schemaname = 'public'
and tablename = 'settings'
)`
sqlSchema = `create table if not exists torrents (
id serial primary key,
infohash bytea unique,
size bigint,
name text,
created timestamp with time zone,
updated timestamp with time zone,
tsv tsvector
);
create index tsv_idx on torrents using gin(tsv);
create table if not exists files (
id serial not null primary key,
torrent_id integer not null references torrents on delete cascade,
path text,
size bigint
);
create table if not exists tags (
id serial primary key,
name character varying(50) unique
);
create table if not exists tags_torrents (
tag_id integer not null references tags (id) on delete cascade,
torrent_id integer not null references torrents (id) on delete cascade,
primary key (tag_id, torrent_id)
);
create table if not exists peers (
id serial primary key,
address character varying(50),
created timestamp with time zone,
updated timestamp with time zone
);
create table if not exists peers_torrents (
peer_id integer not null references peers (id) on delete cascade,
torrent_id integer not null references torrents (id) on delete cascade,
primary key (peer_id, torrent_id)
);
create table if not exists settings (
schema_version integer not null
);
insert into settings (schema_version) values (1);`
)
|