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 (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
@@ -125,22 +126,19 @@ func SetTorrent(hashHex, title, poster, data string) *Torrent {
|
|||||||
|
|
||||||
func RemTorrent(hashHex string) {
|
func RemTorrent(hashHex string) {
|
||||||
hash := metainfo.NewHashFromHex(hashHex)
|
hash := metainfo.NewHashFromHex(hashHex)
|
||||||
bts.RemoveTorrent(hash)
|
if sets.BTsets.UseDisk && hashHex != "" && hashHex != "/" {
|
||||||
RemTorrentDB(hash)
|
|
||||||
if sets.BTsets.UseDisk &&
|
|
||||||
hashHex != "" &&
|
|
||||||
hashHex != "/" &&
|
|
||||||
sets.BTsets.TorrentsSavePath != "" &&
|
|
||||||
sets.BTsets.TorrentsSavePath != "/" {
|
|
||||||
|
|
||||||
name := filepath.Join(sets.BTsets.TorrentsSavePath, 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 {
|
if err != nil {
|
||||||
log.TLogln("Error remove cache:", err)
|
log.TLogln("Error remove cache:", err)
|
||||||
} else {
|
|
||||||
log.TLogln("Remove cache from disk:", hashHex)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
bts.RemoveTorrent(hash)
|
||||||
|
RemTorrentDB(hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListTorrent() []*Torrent {
|
func ListTorrent() []*Torrent {
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package torrstor
|
package torrstor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -76,21 +74,6 @@ func (c *Cache) Init(info *metainfo.Info, hash metainfo.Hash) {
|
|||||||
for i := 0; i < c.pieceCount; i++ {
|
for i := 0; i < c.pieceCount; i++ {
|
||||||
c.pieces[i] = NewPiece(i, c)
|
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) {
|
func (c *Cache) SetTorrent(torr *torrent.Torrent) {
|
||||||
@@ -274,68 +257,6 @@ func (c *Cache) isIdInFileBE(ranges []Range, id int) bool {
|
|||||||
return false
|
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
|
// Reader section
|
||||||
////////
|
////////
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
package torrstor
|
package torrstor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/anacrolix/torrent"
|
||||||
|
|
||||||
"server/log"
|
"server/log"
|
||||||
"server/settings"
|
"server/settings"
|
||||||
)
|
)
|
||||||
@@ -14,29 +17,35 @@ import (
|
|||||||
type DiskPiece struct {
|
type DiskPiece struct {
|
||||||
piece *Piece
|
piece *Piece
|
||||||
|
|
||||||
file *os.File
|
name string
|
||||||
|
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDiskPiece(p *Piece) *DiskPiece {
|
func NewDiskPiece(p *Piece) *DiskPiece {
|
||||||
name := filepath.Join(settings.BTsets.TorrentsSavePath, p.cache.hash.HexString(), strconv.Itoa(p.Id))
|
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)
|
ff, err := os.Stat(name)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
log.TLogln("Error open file:", err)
|
p.Size = ff.Size()
|
||||||
return nil
|
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) {
|
func (p *DiskPiece) WriteAt(b []byte, off int64) (n int, err error) {
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
defer p.mu.Unlock()
|
defer p.mu.Unlock()
|
||||||
|
|
||||||
n, err = p.file.WriteAt(b, off)
|
ff, err := os.OpenFile(p.name, os.O_RDWR|os.O_CREATE, 0666)
|
||||||
|
if err != nil {
|
||||||
go p.piece.cache.LoadPiecesOnDisk()
|
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 {
|
if p.piece.Size > p.piece.cache.pieceLength {
|
||||||
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()
|
p.mu.Lock()
|
||||||
defer p.mu.Unlock()
|
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()
|
p.piece.Accessed = time.Now().Unix()
|
||||||
|
if int64(len(b))+off >= p.piece.Size {
|
||||||
|
go p.piece.cache.cleanPieces()
|
||||||
|
}
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *DiskPiece) Release() {
|
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():
|
case <-t.Torrent.GotInfo():
|
||||||
t.cache = t.bt.storage.GetCache(t.Hash())
|
t.cache = t.bt.storage.GetCache(t.Hash())
|
||||||
t.cache.SetTorrent(t.Torrent)
|
t.cache.SetTorrent(t.Torrent)
|
||||||
go t.cache.LoadPiecesOnDisk()
|
|
||||||
return true
|
return true
|
||||||
case <-t.closed:
|
case <-t.closed:
|
||||||
return false
|
return false
|
||||||
@@ -314,22 +313,32 @@ func (t *Torrent) Preload(index int, size int64) {
|
|||||||
|
|
||||||
isComplete := true
|
isComplete := true
|
||||||
if readerPieceBefore >= pieceFileStart {
|
if readerPieceBefore >= pieceFileStart {
|
||||||
|
limit := 5
|
||||||
for i := pieceFileStart; i < readerPieceBefore; i++ {
|
for i := pieceFileStart; i < readerPieceBefore; i++ {
|
||||||
if !t.PieceState(i).Complete {
|
if !t.PieceState(i).Complete {
|
||||||
isComplete = false
|
isComplete = false
|
||||||
if t.PieceState(i).Priority == torrent.PiecePriorityNone {
|
if t.PieceState(i).Priority == torrent.PiecePriorityNone {
|
||||||
t.Piece(i).SetPriority(torrent.PiecePriorityNormal)
|
t.Piece(i).SetPriority(torrent.PiecePriorityNormal)
|
||||||
}
|
}
|
||||||
|
limit--
|
||||||
|
if limit <= 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if readerPieceAfter <= pieceFileEnd {
|
if readerPieceAfter <= pieceFileEnd {
|
||||||
|
limit := 5
|
||||||
for i := readerPieceAfter; i <= pieceFileEnd; i++ {
|
for i := readerPieceAfter; i <= pieceFileEnd; i++ {
|
||||||
if !t.PieceState(i).Complete {
|
if !t.PieceState(i).Complete {
|
||||||
isComplete = false
|
isComplete = false
|
||||||
if t.PieceState(i).Priority == torrent.PiecePriorityNone {
|
if t.PieceState(i).Priority == torrent.PiecePriorityNone {
|
||||||
t.Piece(i).SetPriority(torrent.PiecePriorityNormal)
|
t.Piece(i).SetPriority(torrent.PiecePriorityNormal)
|
||||||
}
|
}
|
||||||
|
limit--
|
||||||
|
if limit <= 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user