mirror of
https://github.com/chrislusf/seaweedfs
synced 2024-09-18 23:10:35 +02:00
simplify file handle management
This commit is contained in:
parent
dc08e4098f
commit
bd56172b82
|
@ -55,8 +55,7 @@ type WFS struct {
|
||||||
|
|
||||||
// contains all open handles, protected by handlesLock
|
// contains all open handles, protected by handlesLock
|
||||||
handlesLock sync.Mutex
|
handlesLock sync.Mutex
|
||||||
handles []*FileHandle
|
handles map[uint64]*FileHandle
|
||||||
pathToHandleIndex map[util.FullPath]int
|
|
||||||
|
|
||||||
bufPool sync.Pool
|
bufPool sync.Pool
|
||||||
|
|
||||||
|
@ -76,7 +75,7 @@ func NewSeaweedFileSystem(option *Option) *WFS {
|
||||||
wfs := &WFS{
|
wfs := &WFS{
|
||||||
option: option,
|
option: option,
|
||||||
listDirectoryEntriesCache: ccache.New(ccache.Configure().MaxSize(option.DirListCacheLimit * 3).ItemsToPrune(100)),
|
listDirectoryEntriesCache: ccache.New(ccache.Configure().MaxSize(option.DirListCacheLimit * 3).ItemsToPrune(100)),
|
||||||
pathToHandleIndex: make(map[util.FullPath]int),
|
handles: make(map[uint64]*FileHandle),
|
||||||
bufPool: sync.Pool{
|
bufPool: sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return make([]byte, option.ChunkSizeLimit)
|
return make([]byte, option.ChunkSizeLimit)
|
||||||
|
@ -117,26 +116,15 @@ func (wfs *WFS) AcquireHandle(file *File, uid, gid uint32) (fileHandle *FileHand
|
||||||
wfs.handlesLock.Lock()
|
wfs.handlesLock.Lock()
|
||||||
defer wfs.handlesLock.Unlock()
|
defer wfs.handlesLock.Unlock()
|
||||||
|
|
||||||
index, found := wfs.pathToHandleIndex[fullpath]
|
inodeId := file.fullpath().AsInode()
|
||||||
if found && wfs.handles[index] != nil {
|
existingHandle, found := wfs.handles[inodeId]
|
||||||
glog.V(2).Infoln(fullpath, "found fileHandle id", index)
|
if found && existingHandle != nil {
|
||||||
return wfs.handles[index]
|
return existingHandle
|
||||||
}
|
}
|
||||||
|
|
||||||
fileHandle = newFileHandle(file, uid, gid)
|
fileHandle = newFileHandle(file, uid, gid)
|
||||||
for i, h := range wfs.handles {
|
wfs.handles[inodeId] = fileHandle
|
||||||
if h == nil {
|
fileHandle.handle = inodeId
|
||||||
wfs.handles[i] = fileHandle
|
|
||||||
fileHandle.handle = uint64(i)
|
|
||||||
wfs.pathToHandleIndex[fullpath] = i
|
|
||||||
glog.V(4).Infof("%s reuse fh %d", fullpath, fileHandle.handle)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
wfs.handles = append(wfs.handles, fileHandle)
|
|
||||||
fileHandle.handle = uint64(len(wfs.handles) - 1)
|
|
||||||
wfs.pathToHandleIndex[fullpath] = int(fileHandle.handle)
|
|
||||||
glog.V(4).Infof("%s new fh %d", fullpath, fileHandle.handle)
|
glog.V(4).Infof("%s new fh %d", fullpath, fileHandle.handle)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -147,10 +135,8 @@ func (wfs *WFS) ReleaseHandle(fullpath util.FullPath, handleId fuse.HandleID) {
|
||||||
defer wfs.handlesLock.Unlock()
|
defer wfs.handlesLock.Unlock()
|
||||||
|
|
||||||
glog.V(4).Infof("%s ReleaseHandle id %d current handles length %d", fullpath, handleId, len(wfs.handles))
|
glog.V(4).Infof("%s ReleaseHandle id %d current handles length %d", fullpath, handleId, len(wfs.handles))
|
||||||
delete(wfs.pathToHandleIndex, fullpath)
|
|
||||||
if int(handleId) < len(wfs.handles) {
|
delete(wfs.handles, fullpath.AsInode())
|
||||||
wfs.handles[int(handleId)] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue