1 // modernc.org/sqlite depends on modernc.org/libc which doesn't work for JS (and probably wasm but I
2 // think JS is the stronger signal).
4 //go:build cgo && !nosqlite
5 // +build cgo,!nosqlite
15 "crawshaw.io/sqlite/sqlitex"
17 "github.com/anacrolix/torrent/metainfo"
20 // sqlite is always the default when available.
21 func NewDefaultPieceCompletionForDir(dir string) (PieceCompletion, error) {
22 return NewSqlitePieceCompletion(dir)
25 type sqlitePieceCompletion struct {
31 var _ PieceCompletion = (*sqlitePieceCompletion)(nil)
33 func NewSqlitePieceCompletion(dir string) (ret *sqlitePieceCompletion, err error) {
34 p := filepath.Join(dir, ".torrent.db")
35 db, err := sqlite.OpenConn(p, 0)
39 err = sqlitex.ExecScript(db, `create table if not exists piece_completion(infohash, "index", complete, unique(infohash, "index"))`)
44 ret = &sqlitePieceCompletion{db: db}
48 func (me *sqlitePieceCompletion) Get(pk metainfo.PieceKey) (c Completion, err error) {
52 me.db, `select complete from piece_completion where infohash=? and "index"=?`,
53 func(stmt *sqlite.Stmt) error {
54 c.Complete = stmt.ColumnInt(0) != 0
58 pk.InfoHash.HexString(), pk.Index)
62 func (me *sqlitePieceCompletion) Set(pk metainfo.PieceKey, b bool) error {
66 return errors.New("closed")
70 `insert or replace into piece_completion(infohash, "index", complete) values(?, ?, ?)`,
72 pk.InfoHash.HexString(), pk.Index, b)
75 func (me *sqlitePieceCompletion) Close() (err error) {