github.com/cyverse/go-irodsclient@v0.13.2/fs/cache_propagation.go (about)

     1  package fs
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  // used to sync caches between different fs instances
     8  
     9  var (
    10  	filesystemCacheEventHandlersMutex sync.RWMutex
    11  	filesystemCacheEventHandlers      map[string]FilesystemCacheEventHandler
    12  )
    13  
    14  func init() {
    15  	filesystemCacheEventHandlersMutex = sync.RWMutex{}
    16  	filesystemCacheEventHandlers = make(map[string]FilesystemCacheEventHandler)
    17  }
    18  
    19  // FileSystemCachePropagation manages filesystem cache propagation
    20  type FileSystemCachePropagation struct {
    21  	filesystem *FileSystem
    22  }
    23  
    24  // NewFileSystemCachePropagation creates a new FileSystemCachePropagation
    25  func NewFileSystemCachePropagation(fs *FileSystem) *FileSystemCachePropagation {
    26  	cachePropagation := &FileSystemCachePropagation{
    27  		filesystem: fs,
    28  	}
    29  
    30  	filesystemCacheEventHandlersMutex.Lock()
    31  	defer filesystemCacheEventHandlersMutex.Unlock()
    32  
    33  	filesystemCacheEventHandlers[fs.GetID()] = func(path string, eventType FilesystemCacheEventType) {
    34  		go cachePropagation.handle(path, eventType)
    35  	}
    36  
    37  	return cachePropagation
    38  }
    39  
    40  // Release releases resources
    41  func (propagation *FileSystemCachePropagation) Release() {
    42  	filesystemCacheEventHandlersMutex.Lock()
    43  	defer filesystemCacheEventHandlersMutex.Unlock()
    44  
    45  	delete(filesystemCacheEventHandlers, propagation.filesystem.GetID())
    46  }
    47  
    48  func (propagation *FileSystemCachePropagation) handle(path string, eventType FilesystemCacheEventType) {
    49  	switch eventType {
    50  	case FilesystemCacheFileCreateEvent:
    51  		propagation.filesystem.invalidateCacheForFileCreate(path)
    52  	case FilesystemCacheFileRemoveEvent:
    53  		propagation.filesystem.invalidateCacheForFileRemove(path)
    54  	case FilesystemCacheFileUpdateEvent:
    55  		propagation.filesystem.invalidateCacheForFileUpdate(path)
    56  	case FilesystemCacheDirCreateEvent:
    57  		propagation.filesystem.invalidateCacheForDirCreate(path)
    58  	case FilesystemCacheDirRemoveEvent:
    59  		propagation.filesystem.invalidateCacheForDirRemove(path, true)
    60  	case FilesystemCacheDirExtractEvent:
    61  		propagation.filesystem.invalidateCacheForDirExtract(path)
    62  	default:
    63  		// unhandled
    64  	}
    65  }
    66  
    67  // Propagate propagates fs cache update event
    68  func (propagation *FileSystemCachePropagation) Propagate(path string, eventType FilesystemCacheEventType) {
    69  	filesystemCacheEventHandlersMutex.RLock()
    70  	defer filesystemCacheEventHandlersMutex.RUnlock()
    71  
    72  	for fsID, handler := range filesystemCacheEventHandlers {
    73  		if fsID != propagation.filesystem.GetID() {
    74  			handler(path, eventType)
    75  		}
    76  	}
    77  }
    78  
    79  // PropagateDirCreate propagates fs cache update event for dir create
    80  func (propagation *FileSystemCachePropagation) PropagateDirCreate(path string) {
    81  	propagation.Propagate(path, FilesystemCacheDirCreateEvent)
    82  }
    83  
    84  // PropagateDirRemove propagates fs cache update event for dir remove
    85  func (propagation *FileSystemCachePropagation) PropagateDirRemove(path string) {
    86  	propagation.Propagate(path, FilesystemCacheDirRemoveEvent)
    87  }
    88  
    89  // PropagateDirExtract propagates fs cache update event for dir extract
    90  func (propagation *FileSystemCachePropagation) PropagateDirExtract(path string) {
    91  	propagation.Propagate(path, FilesystemCacheDirExtractEvent)
    92  }
    93  
    94  // PropagateFileCreate propagates fs cache update event for file create
    95  func (propagation *FileSystemCachePropagation) PropagateFileCreate(path string) {
    96  	propagation.Propagate(path, FilesystemCacheFileCreateEvent)
    97  }
    98  
    99  // PropagateFileRemove propagates fs cache update event for file remove
   100  func (propagation *FileSystemCachePropagation) PropagateFileRemove(path string) {
   101  	propagation.Propagate(path, FilesystemCacheFileRemoveEvent)
   102  }
   103  
   104  // PropagateFileUpdate propagates fs cache update event for file update
   105  func (propagation *FileSystemCachePropagation) PropagateFileUpdate(path string) {
   106  	propagation.Propagate(path, FilesystemCacheFileUpdateEvent)
   107  }