2021-12-07 10:07:58 +08:00
|
|
|
// modernc.org/sqlite depends on modernc.org/libc which doesn't work for JS (and probably wasm but I
|
|
|
|
// think JS is the stronger signal).
|
|
|
|
//go:build !js && !nosqlite
|
|
|
|
// +build !js,!nosqlite
|
2020-07-15 14:13:26 +08:00
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2021-09-02 18:54:22 +08:00
|
|
|
"errors"
|
2020-07-15 14:13:26 +08:00
|
|
|
"path/filepath"
|
2021-05-14 13:45:54 +08:00
|
|
|
"sync"
|
2020-07-15 14:13:26 +08:00
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2021-11-19 13:18:41 +08:00
|
|
|
"zombiezen.com/go/sqlite"
|
|
|
|
"zombiezen.com/go/sqlite/sqlitex"
|
2020-07-15 14:13:26 +08:00
|
|
|
)
|
|
|
|
|
2021-12-07 10:07:58 +08:00
|
|
|
// sqlite is always the default when available.
|
|
|
|
func NewDefaultPieceCompletionForDir(dir string) (PieceCompletion, error) {
|
|
|
|
return NewSqlitePieceCompletion(dir)
|
|
|
|
}
|
|
|
|
|
2020-07-15 14:13:26 +08:00
|
|
|
type sqlitePieceCompletion struct {
|
2021-09-02 18:54:22 +08:00
|
|
|
mu sync.Mutex
|
|
|
|
closed bool
|
|
|
|
db *sqlite.Conn
|
2020-07-15 14:13:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ PieceCompletion = (*sqlitePieceCompletion)(nil)
|
|
|
|
|
|
|
|
func NewSqlitePieceCompletion(dir string) (ret *sqlitePieceCompletion, err error) {
|
|
|
|
p := filepath.Join(dir, ".torrent.db")
|
|
|
|
db, err := sqlite.OpenConn(p, 0)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = sqlitex.ExecScript(db, `create table if not exists piece_completion(infohash, "index", complete, unique(infohash, "index"))`)
|
|
|
|
if err != nil {
|
|
|
|
db.Close()
|
|
|
|
return
|
|
|
|
}
|
2021-05-14 13:45:54 +08:00
|
|
|
ret = &sqlitePieceCompletion{db: db}
|
2020-07-15 14:13:26 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *sqlitePieceCompletion) Get(pk metainfo.PieceKey) (c Completion, err error) {
|
2021-05-14 13:45:54 +08:00
|
|
|
me.mu.Lock()
|
|
|
|
defer me.mu.Unlock()
|
2020-07-15 14:13:26 +08:00
|
|
|
err = sqlitex.Exec(
|
|
|
|
me.db, `select complete from piece_completion where infohash=? and "index"=?`,
|
|
|
|
func(stmt *sqlite.Stmt) error {
|
|
|
|
c.Complete = stmt.ColumnInt(0) != 0
|
|
|
|
c.Ok = true
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
pk.InfoHash.HexString(), pk.Index)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *sqlitePieceCompletion) Set(pk metainfo.PieceKey, b bool) error {
|
2021-05-14 13:45:54 +08:00
|
|
|
me.mu.Lock()
|
|
|
|
defer me.mu.Unlock()
|
2021-09-02 18:54:22 +08:00
|
|
|
if me.closed {
|
|
|
|
return errors.New("closed")
|
|
|
|
}
|
2020-07-15 14:13:26 +08:00
|
|
|
return sqlitex.Exec(
|
|
|
|
me.db,
|
|
|
|
`insert or replace into piece_completion(infohash, "index", complete) values(?, ?, ?)`,
|
|
|
|
nil,
|
|
|
|
pk.InfoHash.HexString(), pk.Index, b)
|
|
|
|
}
|
|
|
|
|
2021-05-14 13:45:54 +08:00
|
|
|
func (me *sqlitePieceCompletion) Close() (err error) {
|
|
|
|
me.mu.Lock()
|
|
|
|
defer me.mu.Unlock()
|
2021-09-02 18:54:22 +08:00
|
|
|
if me.closed {
|
|
|
|
return
|
2021-05-14 13:45:54 +08:00
|
|
|
}
|
2021-09-02 18:54:22 +08:00
|
|
|
err = me.db.Close()
|
|
|
|
me.db = nil
|
|
|
|
me.closed = true
|
2021-05-14 13:45:54 +08:00
|
|
|
return
|
2020-07-15 14:13:26 +08:00
|
|
|
}
|