Skip to content

Commit

Permalink
Rework cache invalidation code
Browse files Browse the repository at this point in the history
  • Loading branch information
iychoi committed Aug 31, 2021
1 parent 2f87fdf commit b1223b3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 50 deletions.
4 changes: 1 addition & 3 deletions fs/file_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/cyverse/go-irodsclient/irods/connection"
irods_fs "github.com/cyverse/go-irodsclient/irods/fs"
"github.com/cyverse/go-irodsclient/irods/types"
"github.com/cyverse/go-irodsclient/irods/util"
)

// FileHandle ...
Expand Down Expand Up @@ -39,8 +38,7 @@ func (handle *FileHandle) Close() error {
defer handle.FileSystem.Session.ReturnConnection(handle.Connection)

if handle.IsWriteMode() {
handle.FileSystem.invalidateCachePath(handle.Entry.Path)
handle.FileSystem.invalidateCachePath(util.GetIRODSPathDirname(handle.Entry.Path))
handle.FileSystem.invalidateCachePathRecursively(handle.Entry.Path)
}

return irods_fs.CloseDataObject(handle.Connection, handle.IRODSHandle)
Expand Down
82 changes: 35 additions & 47 deletions fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func (fs *FileSystem) RemoveDir(path string, recurse bool, force bool) error {
return err
}

fs.removeCachePath(irodsPath)
fs.invalidateCachePathRecursively(irodsPath)
return nil
}

Expand All @@ -469,7 +469,7 @@ func (fs *FileSystem) RemoveFile(path string, force bool) error {
return err
}

fs.removeCachePath(irodsPath)
fs.invalidateCachePathRecursively(irodsPath)
return nil
}

Expand Down Expand Up @@ -504,15 +504,8 @@ func (fs *FileSystem) RenameDirToDir(srcPath string, destPath string) error {
return err
}

if util.GetIRODSPathDirname(irodsSrcPath) == util.GetIRODSPathDirname(irodsDestPath) {
// from the same dir
fs.invalidateCachePath(util.GetIRODSPathDirname(irodsSrcPath))

} else {
fs.removeCachePath(irodsSrcPath)
fs.invalidateCachePath(util.GetIRODSPathDirname(irodsDestPath))
}

fs.invalidateCachePathRecursively(irodsSrcPath)
fs.invalidateCachePathRecursively(irodsDestPath)
return nil
}

Expand Down Expand Up @@ -547,14 +540,8 @@ func (fs *FileSystem) RenameFileToFile(srcPath string, destPath string) error {
return err
}

if util.GetIRODSPathDirname(irodsSrcPath) == util.GetIRODSPathDirname(irodsDestPath) {
// from the same dir
fs.invalidateCachePath(util.GetIRODSPathDirname(irodsSrcPath))
} else {
fs.removeCachePath(irodsSrcPath)
fs.invalidateCachePath(util.GetIRODSPathDirname(irodsDestPath))
}

fs.invalidateCachePathRecursively(irodsSrcPath)
fs.invalidateCachePathRecursively(irodsDestPath)
return nil
}

Expand All @@ -573,8 +560,7 @@ func (fs *FileSystem) MakeDir(path string, recurse bool) error {
return err
}

fs.invalidateCachePath(util.GetIRODSPathDirname(irodsPath))

fs.invalidateCachePathRecursively(irodsPath)
return nil
}

Expand Down Expand Up @@ -609,8 +595,7 @@ func (fs *FileSystem) CopyFileToFile(srcPath string, destPath string) error {
return err
}

fs.invalidateCachePath(util.GetIRODSPathDirname(irodsDestPath))

fs.invalidateCachePathRecursively(irodsDestPath)
return nil
}

Expand All @@ -634,7 +619,6 @@ func (fs *FileSystem) TruncateFile(path string, size int64) error {
}

fs.invalidateCachePath(irodsPath)

return nil
}

Expand All @@ -648,7 +632,13 @@ func (fs *FileSystem) ReplicateFile(path string, resource string, update bool) e
}
defer fs.Session.ReturnConnection(conn)

return irods_fs.ReplicateDataObject(conn, irodsPath, resource, update, false)
err = irods_fs.ReplicateDataObject(conn, irodsPath, resource, update, false)
if err != nil {
return err
}

fs.invalidateCachePath(irodsPath)
return nil
}

// DownloadFile downloads a file to local
Expand Down Expand Up @@ -816,8 +806,7 @@ func (fs *FileSystem) UploadFile(localPath string, irodsPath string, resource st
return err
}

fs.invalidateCachePath(util.GetIRODSPathDirname(irodsFilePath))

fs.invalidateCachePathRecursively(irodsFilePath)
return nil
}

Expand Down Expand Up @@ -863,8 +852,7 @@ func (fs *FileSystem) UploadFileParallel(localPath string, irodsPath string, res
return err
}

fs.invalidateCachePath(util.GetIRODSPathDirname(irodsFilePath))

fs.invalidateCachePathRecursively(irodsFilePath)
return nil
}

Expand Down Expand Up @@ -925,8 +913,7 @@ func (fs *FileSystem) UploadFileParallelInBlocksAsync(localPath string, irodsPat
}

outputChan2, errChan2 := irods_fs.UploadDataObjectParallelInBlockAsync(fs.Session, localSrcPath, irodsFilePath, resource, srcStat.Size(), blockLength, taskNum, replicate)
fs.invalidateCachePath(util.GetIRODSPathDirname(irodsFilePath))

fs.invalidateCachePathRecursively(irodsFilePath)
return outputChan2, errChan2
}

Expand Down Expand Up @@ -1349,8 +1336,6 @@ func (fs *FileSystem) ListMetadata(path string) ([]*types.IRODSMeta, error) {
func (fs *FileSystem) AddMetadata(irodsPath string, attName string, attValue string, attUnits string) error {
irodsCorrectPath := util.GetCorrectIRODSPath(irodsPath)

fs.Cache.RemoveMetadataCache(irodsCorrectPath)

metadata := &types.IRODSMeta{
Name: attName,
Value: attValue,
Expand All @@ -1375,15 +1360,14 @@ func (fs *FileSystem) AddMetadata(irodsPath string, attName string, attValue str
}
}

fs.Cache.RemoveMetadataCache(irodsCorrectPath)
return nil
}

// DeleteMetadata deletes a metadata for the path
func (fs *FileSystem) DeleteMetadata(irodsPath string, attName string, attValue string, attUnits string) error {
irodsCorrectPath := util.GetCorrectIRODSPath(irodsPath)

fs.Cache.RemoveMetadataCache(irodsCorrectPath)

metadata := &types.IRODSMeta{
Name: attName,
Value: attValue,
Expand All @@ -1408,10 +1392,11 @@ func (fs *FileSystem) DeleteMetadata(irodsPath string, attName string, attValue
}
}

fs.Cache.RemoveMetadataCache(irodsCorrectPath)
return nil
}

// InvalidateCachePath invalidates cache with the given path
// invalidateCachePath invalidates cache with the given path
func (fs *FileSystem) invalidateCachePath(path string) {
fs.Cache.RemoveEntryCache(path)
fs.Cache.RemoveDirCache(path)
Expand All @@ -1420,27 +1405,30 @@ func (fs *FileSystem) invalidateCachePath(path string) {
fs.Cache.RemoveMetadataCache(path)
}

func (fs *FileSystem) removeCachePath(path string) {
func (fs *FileSystem) invalidateCachePathRecursively(path string) {
// if path is directory, recursively
entry := fs.Cache.GetEntryCache(path)
if entry != nil {
fs.Cache.RemoveEntryCache(path)
fs.Cache.RemoveFileACLsCache(path)
fs.Cache.RemoveEntryCache(path)
fs.Cache.RemoveFileACLsCache(path)
fs.Cache.RemoveMetadataCache(path)

if entry != nil {
if entry.Type == DirectoryEntry {
dirEntries := fs.Cache.GetDirCache(path)
if dirEntries != nil {
for _, dirEntry := range dirEntries {
// do it recursively
fs.removeCachePath(dirEntry)
}
fs.Cache.RemoveDirCache(path)
fs.Cache.RemoveDirACLsCache(path)
for _, dirEntry := range dirEntries {
// do it recursively
fs.invalidateCachePathRecursively(dirEntry)
}
}

fs.Cache.RemoveDirCache(path)
fs.Cache.RemoveDirACLsCache(path)

fs.Cache.RemoveDirCache(util.GetIRODSPathDirname(path))
fs.Cache.RemoveDirACLsCache(util.GetIRODSPathDirname(path))
} else {
fs.Cache.RemoveDirCache(path)
fs.Cache.RemoveDirACLsCache(path)
}
}

Expand Down

0 comments on commit b1223b3

Please sign in to comment.