-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrepo.go
154 lines (135 loc) · 3.55 KB
/
repo.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
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
type SqliteRepo struct {
db *sql.DB
}
func OpenDatabase(filepath string) (*SqliteRepo, error) {
db, err := sql.Open("sqlite3", filepath+"?cache=shared")
if err != nil {
return nil, err
}
db.SetMaxOpenConns(1)
// Clear all taken markets
_, err = db.Exec("UPDATE files SET taken = false WHERE taken = true")
if err != nil {
return nil, err
}
return &SqliteRepo{
db,
}, nil
}
func (repo *SqliteRepo) GetOverview() (IndexOverview, error) {
var overview IndexOverview
err := repo.db.QueryRow("SELECT name, total_files, total_size, base_url FROM overview LIMIT 1").
Scan(&overview.Name, &overview.TotalFiles, &overview.TotalSize, &overview.BaseUrl)
if err != nil {
return overview, err
}
return overview, nil
}
func (repo *SqliteRepo) GetTotalDownloadedSize() (int64, error) {
var total int64
err := repo.db.QueryRow("SELECT IFNULL(sum(size), 0) FROM files WHERE done = true").
Scan(&total)
if err != nil {
return 0, err
}
return total, nil
}
func (repo *SqliteRepo) GetTotalDownladedFiles() (int64, error) {
var total int64
err := repo.db.QueryRow("SELECT COUNT(*) FROM files WHERE done = true").
Scan(&total)
if err != nil {
return 0, err
}
return total, nil
}
func (repo *SqliteRepo) MarkFileDone(file *IndexedFile) error {
_, err := repo.db.Exec("UPDATE files SET done = true WHERE path = ?", file.Filepath)
return err
}
func (repo *SqliteRepo) GetNextEmptyDir() (string, error) {
var d string
err := repo.db.QueryRow(`UPDATE empty_dirs SET done = true
WHERE rowid = (
SELECT MIN(rowid)
FROM empty_dirs
WHERE done = false
) RETURNING path`).Scan(&d)
if err != nil {
return "", err
}
return d, err
}
func (repo *SqliteRepo) Close() error {
err := repo.db.Close()
return err
}
func (repo *SqliteRepo) GetNextFile() (*IndexedFile, error) {
var f IndexedFile
f.RetryCount = 0
err := repo.db.QueryRow(`UPDATE files SET taken = true
WHERE rowid = (
SELECT MIN(rowid)
FROM files
WHERE done = false AND taken = false
) RETURNING path, size, crc32`).Scan(&f.Filepath, &f.Size, &f.CRC32)
if err != nil {
return nil, err
}
return &f, nil
}
// GetNextFileBatch Cannot be safely executed in parallel
func (repo *SqliteRepo) GetNextFileBatch(limit int64) ([]*IndexedFile, error) {
rows, err := repo.db.Query("SELECT path, size, crc32 FROM files WHERE done = false AND taken = false LIMIT ?", limit)
if err != nil {
return nil, err
}
defer rows.Close()
files := make([]*IndexedFile, 0)
for rows.Next() {
var f IndexedFile
f.RetryCount = 0
err = rows.Scan(&f.Filepath, &f.Size, &f.CRC32)
if err != nil {
return nil, err
}
files = append(files, &f)
}
err = rows.Close()
if err != nil {
return nil, err
}
for _, f := range files {
_, updateErr := repo.db.Exec("UPDATE files SET taken = true WHERE path = ?", f.Filepath)
if updateErr != nil {
// Handle the update error if needed
return nil, updateErr
}
}
return files, nil
}
func (repo *SqliteRepo) ResetDownloadState() error {
_, err := repo.db.Exec("UPDATE files SET done = false")
if err != nil {
return err
}
_, err = repo.db.Exec("UPDATE files SET taken = false")
if err != nil {
return err
}
_, err = repo.db.Exec("UPDATE empty_dirs SET done = false")
return err
}
func (repo *SqliteRepo) ClearTakenAll() error {
_, err := repo.db.Exec("UPDATE files SET taken = false")
return err
}
func (repo *SqliteRepo) ClearTaken(f *IndexedFile) error {
_, err := repo.db.Exec("UPDATE files SET taken = false WHERE path = ?", f.Filepath)
return err
}