mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 13:36:09 +05:00
set disk cache as mem cache
This commit is contained in:
@@ -2,6 +2,7 @@ package torr
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
@@ -125,22 +126,19 @@ func SetTorrent(hashHex, title, poster, data string) *Torrent {
|
||||
|
||||
func RemTorrent(hashHex string) {
|
||||
hash := metainfo.NewHashFromHex(hashHex)
|
||||
bts.RemoveTorrent(hash)
|
||||
RemTorrentDB(hash)
|
||||
if sets.BTsets.UseDisk &&
|
||||
hashHex != "" &&
|
||||
hashHex != "/" &&
|
||||
sets.BTsets.TorrentsSavePath != "" &&
|
||||
sets.BTsets.TorrentsSavePath != "/" {
|
||||
|
||||
if sets.BTsets.UseDisk && hashHex != "" && hashHex != "/" {
|
||||
name := filepath.Join(sets.BTsets.TorrentsSavePath, hashHex)
|
||||
err := os.RemoveAll(name)
|
||||
ff, _ := ioutil.ReadDir(name)
|
||||
for _, f := range ff {
|
||||
os.Remove(filepath.Join(name, f.Name()))
|
||||
}
|
||||
err := os.Remove(name)
|
||||
if err != nil {
|
||||
log.TLogln("Error remove cache:", err)
|
||||
} else {
|
||||
log.TLogln("Remove cache from disk:", hashHex)
|
||||
}
|
||||
}
|
||||
bts.RemoveTorrent(hash)
|
||||
RemTorrentDB(hash)
|
||||
}
|
||||
|
||||
func ListTorrent() []*Torrent {
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -76,21 +74,6 @@ func (c *Cache) Init(info *metainfo.Info, hash metainfo.Hash) {
|
||||
for i := 0; i < c.pieceCount; i++ {
|
||||
c.pieces[i] = NewPiece(i, c)
|
||||
}
|
||||
|
||||
if settings.BTsets.UseDisk {
|
||||
name := filepath.Join(settings.BTsets.TorrentsSavePath, hash.HexString())
|
||||
fs, err := ioutil.ReadDir(name)
|
||||
if err == nil {
|
||||
for _, f := range fs {
|
||||
id, err := strconv.Atoi(f.Name())
|
||||
if err == nil {
|
||||
c.pieces[id].Size = f.Size()
|
||||
c.pieces[id].Complete = f.Size() == c.pieceLength
|
||||
c.pieces[id].Accessed = f.ModTime().Unix()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) SetTorrent(torr *torrent.Torrent) {
|
||||
@@ -274,68 +257,6 @@ func (c *Cache) isIdInFileBE(ranges []Range, id int) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// run only in cache on disk
|
||||
func (c *Cache) LoadPiecesOnDisk() {
|
||||
if c.torrent == nil || !settings.BTsets.UseDisk {
|
||||
return
|
||||
}
|
||||
|
||||
if c.isRemove {
|
||||
return
|
||||
}
|
||||
c.muRemove.Lock()
|
||||
if c.isRemove {
|
||||
c.muRemove.Unlock()
|
||||
return
|
||||
}
|
||||
c.isRemove = true
|
||||
defer func() { c.isRemove = false }()
|
||||
c.muRemove.Unlock()
|
||||
|
||||
ranges := make([]Range, 0)
|
||||
c.muReaders.Lock()
|
||||
for r, _ := range c.readers {
|
||||
ranges = append(ranges, r.getPiecesRange())
|
||||
}
|
||||
c.muReaders.Unlock()
|
||||
ranges = mergeRange(ranges)
|
||||
|
||||
for r, _ := range c.readers {
|
||||
pc := r.getReaderPiece()
|
||||
limit := 5
|
||||
|
||||
for limit > 0 {
|
||||
if c.pieces != nil && c.pieces[pc] != nil && !c.pieces[pc].Complete {
|
||||
if c.torrent.PieceState(pc).Priority == torrent.PiecePriorityNone {
|
||||
c.torrent.Piece(pc).SetPriority(torrent.PiecePriorityNormal)
|
||||
}
|
||||
limit--
|
||||
}
|
||||
pc++
|
||||
}
|
||||
}
|
||||
if len(c.readers) == 0 {
|
||||
limit := 5
|
||||
pc := 0
|
||||
end := c.pieceCount
|
||||
for pc <= end {
|
||||
if c.pieces != nil && c.pieces[pc] != nil && !c.pieces[pc].Complete {
|
||||
break
|
||||
}
|
||||
pc++
|
||||
}
|
||||
for pc <= end && limit > 0 {
|
||||
if c.pieces != nil && c.pieces[pc] != nil && !c.pieces[pc].Complete {
|
||||
if c.torrent.PieceState(pc).Priority == torrent.PiecePriorityNone {
|
||||
c.torrent.Piece(pc).SetPriority(torrent.PiecePriorityNormal)
|
||||
}
|
||||
limit--
|
||||
}
|
||||
pc++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// Reader section
|
||||
////////
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package torrstor
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
|
||||
"server/log"
|
||||
"server/settings"
|
||||
)
|
||||
@@ -14,29 +17,35 @@ import (
|
||||
type DiskPiece struct {
|
||||
piece *Piece
|
||||
|
||||
file *os.File
|
||||
name string
|
||||
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func NewDiskPiece(p *Piece) *DiskPiece {
|
||||
name := filepath.Join(settings.BTsets.TorrentsSavePath, p.cache.hash.HexString(), strconv.Itoa(p.Id))
|
||||
ff, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
log.TLogln("Error open file:", err)
|
||||
return nil
|
||||
ff, err := os.Stat(name)
|
||||
if err == nil {
|
||||
p.Size = ff.Size()
|
||||
p.Complete = ff.Size() == p.cache.pieceLength
|
||||
p.Accessed = ff.ModTime().Unix()
|
||||
}
|
||||
return &DiskPiece{piece: p, file: ff}
|
||||
return &DiskPiece{piece: p, name: name}
|
||||
}
|
||||
|
||||
func (p *DiskPiece) WriteAt(b []byte, off int64) (n int, err error) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
n, err = p.file.WriteAt(b, off)
|
||||
|
||||
go p.piece.cache.LoadPiecesOnDisk()
|
||||
ff, err := os.OpenFile(p.name, os.O_RDWR|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
log.TLogln("Error open file:", err)
|
||||
return 0, err
|
||||
}
|
||||
defer ff.Close()
|
||||
n, err = ff.WriteAt(b, off)
|
||||
|
||||
p.piece.Size += int64(n)
|
||||
if p.piece.Size > p.piece.cache.pieceLength {
|
||||
p.piece.Size = p.piece.cache.pieceLength
|
||||
}
|
||||
@@ -48,12 +57,33 @@ func (p *DiskPiece) ReadAt(b []byte, off int64) (n int, err error) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
n, err = p.file.ReadAt(b, off)
|
||||
ff, err := os.OpenFile(p.name, os.O_RDONLY, 0666)
|
||||
if os.IsNotExist(err) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
if err != nil {
|
||||
log.TLogln("Error open file:", err)
|
||||
return 0, err
|
||||
}
|
||||
defer ff.Close()
|
||||
|
||||
n, err = ff.ReadAt(b, off)
|
||||
|
||||
p.piece.Accessed = time.Now().Unix()
|
||||
if int64(len(b))+off >= p.piece.Size {
|
||||
go p.piece.cache.cleanPieces()
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (p *DiskPiece) Release() {
|
||||
p.file.Close()
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
p.piece.Size = 0
|
||||
p.piece.Complete = false
|
||||
|
||||
os.Remove(p.name)
|
||||
|
||||
p.piece.cache.torrent.Piece(p.piece.Id).SetPriority(torrent.PiecePriorityNone)
|
||||
}
|
||||
|
||||
@@ -109,7 +109,6 @@ func (t *Torrent) WaitInfo() bool {
|
||||
case <-t.Torrent.GotInfo():
|
||||
t.cache = t.bt.storage.GetCache(t.Hash())
|
||||
t.cache.SetTorrent(t.Torrent)
|
||||
go t.cache.LoadPiecesOnDisk()
|
||||
return true
|
||||
case <-t.closed:
|
||||
return false
|
||||
@@ -314,22 +313,32 @@ func (t *Torrent) Preload(index int, size int64) {
|
||||
|
||||
isComplete := true
|
||||
if readerPieceBefore >= pieceFileStart {
|
||||
limit := 5
|
||||
for i := pieceFileStart; i < readerPieceBefore; i++ {
|
||||
if !t.PieceState(i).Complete {
|
||||
isComplete = false
|
||||
if t.PieceState(i).Priority == torrent.PiecePriorityNone {
|
||||
t.Piece(i).SetPriority(torrent.PiecePriorityNormal)
|
||||
}
|
||||
limit--
|
||||
if limit <= 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if readerPieceAfter <= pieceFileEnd {
|
||||
limit := 5
|
||||
for i := readerPieceAfter; i <= pieceFileEnd; i++ {
|
||||
if !t.PieceState(i).Complete {
|
||||
isComplete = false
|
||||
if t.PieceState(i).Priority == torrent.PiecePriorityNone {
|
||||
t.Piece(i).SetPriority(torrent.PiecePriorityNormal)
|
||||
}
|
||||
limit--
|
||||
if limit <= 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user