mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 07:40:56 +08:00 
			
		
		
		
	bfs: bfs.FS增加锁 (experimental)
This commit is contained in:
		@@ -4,6 +4,7 @@ package bfs
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
 | 
			
		||||
	"log"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
@@ -19,20 +20,29 @@ type FS struct {
 | 
			
		||||
	isClosed bool
 | 
			
		||||
 | 
			
		||||
	syncTicker *time.Ticker
 | 
			
		||||
 | 
			
		||||
	locker *fsutils.Locker
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewFS(dir string, options *FSOptions) *FS {
 | 
			
		||||
func OpenFS(dir string, options *FSOptions) (*FS, error) {
 | 
			
		||||
	options.EnsureDefaults()
 | 
			
		||||
 | 
			
		||||
	var locker = fsutils.NewLocker(dir + "/fs")
 | 
			
		||||
	err := locker.Lock()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var fs = &FS{
 | 
			
		||||
		dir:        dir,
 | 
			
		||||
		bMap:       map[string]*BlocksFile{},
 | 
			
		||||
		mu:         &sync.RWMutex{},
 | 
			
		||||
		opt:        options,
 | 
			
		||||
		syncTicker: time.NewTicker(1 * time.Second),
 | 
			
		||||
		locker:     locker,
 | 
			
		||||
	}
 | 
			
		||||
	go fs.init()
 | 
			
		||||
	return fs
 | 
			
		||||
	return fs, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *FS) init() {
 | 
			
		||||
@@ -47,7 +57,7 @@ func (this *FS) OpenFileWriter(hash string, bodySize int64, isPartial bool) (*Fi
 | 
			
		||||
		return nil, errors.New("invalid body size for partial content")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	bFile, err := this.openBFileForWriting(hash)
 | 
			
		||||
	bFile, err := this.openBFileForHashWriting(hash)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -55,7 +65,7 @@ func (this *FS) OpenFileWriter(hash string, bodySize int64, isPartial bool) (*Fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *FS) OpenFileReader(hash string, isPartial bool) (*FileReader, error) {
 | 
			
		||||
	bFile, err := this.openBFileForReading(hash)
 | 
			
		||||
	bFile, err := this.openBFileForHashReading(hash)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -63,7 +73,7 @@ func (this *FS) OpenFileReader(hash string, isPartial bool) (*FileReader, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *FS) ExistFile(hash string) (bool, error) {
 | 
			
		||||
	bFile, err := this.openBFileForReading(hash)
 | 
			
		||||
	bFile, err := this.openBFileForHashReading(hash)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -71,7 +81,7 @@ func (this *FS) ExistFile(hash string) (bool, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *FS) RemoveFile(hash string) error {
 | 
			
		||||
	bFile, err := this.openBFileForWriting(hash)
 | 
			
		||||
	bFile, err := this.openBFileForHashWriting(hash)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
@@ -90,6 +100,12 @@ func (this *FS) Close() error {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	this.mu.Unlock()
 | 
			
		||||
 | 
			
		||||
	err := this.locker.Release()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		lastErr = err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return lastErr
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -139,7 +155,7 @@ func (this *FS) syncLoop() {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *FS) openBFileForWriting(hash string) (*BlocksFile, error) {
 | 
			
		||||
func (this *FS) openBFileForHashWriting(hash string) (*BlocksFile, error) {
 | 
			
		||||
	err := CheckHashErr(hash)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@@ -160,7 +176,7 @@ func (this *FS) openBFileForWriting(hash string) (*BlocksFile, error) {
 | 
			
		||||
	return this.openBFile(bPath, bName)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *FS) openBFileForReading(hash string) (*BlocksFile, error) {
 | 
			
		||||
func (this *FS) openBFileForHashReading(hash string) (*BlocksFile, error) {
 | 
			
		||||
	err := CheckHashErr(hash)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user