diff options
| author | chrislu <chris.lu@gmail.com> | 2022-02-13 19:14:34 -0800 |
|---|---|---|
| committer | chrislu <chris.lu@gmail.com> | 2022-02-13 19:14:34 -0800 |
| commit | bb9919b07a52c8af3295074d797792b0ed5d42de (patch) | |
| tree | 4d6efb360c742ea249dcde098125e1bede023e1c /weed/mount/filehandle_map.go | |
| parent | 072f923a9b22693750452deeebe5f45cad98160a (diff) | |
| download | seaweedfs-bb9919b07a52c8af3295074d797792b0ed5d42de.tar.xz seaweedfs-bb9919b07a52c8af3295074d797792b0ed5d42de.zip | |
add open release, refactor
Diffstat (limited to 'weed/mount/filehandle_map.go')
| -rw-r--r-- | weed/mount/filehandle_map.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/weed/mount/filehandle_map.go b/weed/mount/filehandle_map.go new file mode 100644 index 000000000..ca010dabb --- /dev/null +++ b/weed/mount/filehandle_map.go @@ -0,0 +1,79 @@ +package mount + +import ( + "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" + "sync" +) + +type FileHandleId uint64 + +type FileHandleToInode struct { + sync.RWMutex + nextFh FileHandleId + inode2fh map[uint64]*FileHandle + fh2inode map[FileHandleId]uint64 +} +type FileHandle struct { + fh FileHandleId + counter int64 + entry *filer_pb.Entry + inode uint64 +} + +func NewFileHandleToInode() *FileHandleToInode { + return &FileHandleToInode{ + inode2fh: make(map[uint64]*FileHandle), + fh2inode: make(map[FileHandleId]uint64), + nextFh: 0, + } +} + +func (i *FileHandleToInode) GetFileHandle(inode uint64) *FileHandle { + i.Lock() + defer i.Unlock() + fh, found := i.inode2fh[inode] + if !found { + fh = &FileHandle{ + fh: i.nextFh, + counter: 1, + inode: inode, + } + i.nextFh++ + i.inode2fh[inode] = fh + i.fh2inode[fh.fh] = inode + } else { + fh.counter++ + } + return fh +} + +func (i *FileHandleToInode) ReleaseByInode(inode uint64) { + i.Lock() + defer i.Unlock() + fh, found := i.inode2fh[inode] + if found { + fh.counter-- + if fh.counter <= 0 { + delete(i.inode2fh, inode) + delete(i.fh2inode, fh.fh) + } + } +} +func (i *FileHandleToInode) ReleaseByHandle(fh FileHandleId) { + i.Lock() + defer i.Unlock() + inode, found := i.fh2inode[fh] + if found { + fhHandle, fhFound := i.inode2fh[inode] + if !fhFound { + delete(i.fh2inode, fh) + } else { + fhHandle.counter-- + if fhHandle.counter <= 0 { + delete(i.inode2fh, inode) + delete(i.fh2inode, fhHandle.fh) + } + } + + } +} |
