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

     1  package fs
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/rs/xid"
     7  )
     8  
     9  // FilesystemCacheEventType defines cache
    10  type FilesystemCacheEventType string
    11  
    12  const (
    13  	// FilesystemCacheFileCreateEvent is an event type for file creation
    14  	FilesystemCacheFileCreateEvent FilesystemCacheEventType = "file create"
    15  	// FilesystemCacheFileRemoveEvent is an event type for file removal
    16  	FilesystemCacheFileRemoveEvent FilesystemCacheEventType = "file remove"
    17  	// FilesystemCacheFileUpdateEvent is an event type for file update
    18  	FilesystemCacheFileUpdateEvent FilesystemCacheEventType = "file update"
    19  	// FilesystemCacheDirCreateEvent is an event type for dir creation
    20  	FilesystemCacheDirCreateEvent FilesystemCacheEventType = "dir create"
    21  	// FilesystemCacheDirRemoveEvent is an event type for dir removal
    22  	FilesystemCacheDirRemoveEvent FilesystemCacheEventType = "dir remove"
    23  	// FilesystemCacheDirExtractEvent is an event type for dir extract
    24  	FilesystemCacheDirExtractEvent FilesystemCacheEventType = "dir extract"
    25  )
    26  
    27  // FilesystemCacheEventHandler is a cache event handler type
    28  type FilesystemCacheEventHandler func(path string, eventType FilesystemCacheEventType)
    29  
    30  // FilesystemCacheEventHandlerMap manages FilesystemCacheEventHandler
    31  type FilesystemCacheEventHandlerMap struct {
    32  	mutex    sync.RWMutex
    33  	handlers map[string]FilesystemCacheEventHandler // ID-handler mapping
    34  }
    35  
    36  // NewFilesystemCacheEventHandlerMap creates a new FilesystemCacheEventHandlerMap
    37  func NewFilesystemCacheEventHandlerMap() *FilesystemCacheEventHandlerMap {
    38  	return &FilesystemCacheEventHandlerMap{
    39  		mutex:    sync.RWMutex{},
    40  		handlers: map[string]FilesystemCacheEventHandler{},
    41  	}
    42  }
    43  
    44  // Release releases resources
    45  func (handlerMap *FilesystemCacheEventHandlerMap) Release() {
    46  	handlerMap.mutex.Lock()
    47  	defer handlerMap.mutex.Unlock()
    48  
    49  	handlerMap.handlers = map[string]FilesystemCacheEventHandler{}
    50  }
    51  
    52  // AddEventHandler adds cache eventh handler
    53  func (handlerMap *FilesystemCacheEventHandlerMap) AddEventHandler(handler FilesystemCacheEventHandler) string {
    54  	handlerID := xid.New().String()
    55  
    56  	handlerMap.mutex.Lock()
    57  	defer handlerMap.mutex.Unlock()
    58  
    59  	handlerMap.handlers[handlerID] = handler
    60  
    61  	return handlerID
    62  }
    63  
    64  // RemoveEventHandler removes cache eventh handler
    65  func (handlerMap *FilesystemCacheEventHandlerMap) RemoveEventHandler(handlerID string) {
    66  	handlerMap.mutex.Lock()
    67  	defer handlerMap.mutex.Unlock()
    68  
    69  	delete(handlerMap.handlers, handlerID)
    70  }
    71  
    72  // GetEventHandler returns event handler for the id
    73  func (handlerMap *FilesystemCacheEventHandlerMap) GetEventHandler(handlerID string) FilesystemCacheEventHandler {
    74  	handlerMap.mutex.RLock()
    75  	defer handlerMap.mutex.RUnlock()
    76  
    77  	return handlerMap.handlers[handlerID]
    78  }
    79  
    80  // GetEventHandlers returns all event handlers
    81  func (handlerMap *FilesystemCacheEventHandlerMap) GetEventHandlers() []FilesystemCacheEventHandler {
    82  	handlerMap.mutex.RLock()
    83  	defer handlerMap.mutex.RUnlock()
    84  
    85  	handlers := []FilesystemCacheEventHandler{}
    86  	for _, handler := range handlerMap.handlers {
    87  		handlers = append(handlers, handler)
    88  	}
    89  
    90  	return handlers
    91  }
    92  
    93  // GetEventHandlerIDs returns all event handler ids
    94  func (handlerMap *FilesystemCacheEventHandlerMap) GetEventHandlerIDs() []string {
    95  	handlerMap.mutex.RLock()
    96  	defer handlerMap.mutex.RUnlock()
    97  
    98  	handlerIDs := []string{}
    99  	for handlerID := range handlerMap.handlers {
   100  		handlerIDs = append(handlerIDs, handlerID)
   101  	}
   102  
   103  	return handlerIDs
   104  }
   105  
   106  // SendEvent sends event
   107  func (handlerMap *FilesystemCacheEventHandlerMap) SendEvent(path string, eventType FilesystemCacheEventType) {
   108  	handlerMap.mutex.RLock()
   109  	defer handlerMap.mutex.RUnlock()
   110  
   111  	for _, handler := range handlerMap.handlers {
   112  		handler(path, eventType)
   113  	}
   114  }
   115  
   116  // SendFileCreateEvent sends file create event
   117  func (handlerMap *FilesystemCacheEventHandlerMap) SendFileCreateEvent(path string) {
   118  	handlerMap.SendEvent(path, FilesystemCacheFileCreateEvent)
   119  }
   120  
   121  // SendFileRemoveEvent sends file remove event
   122  func (handlerMap *FilesystemCacheEventHandlerMap) SendFileRemoveEvent(path string) {
   123  	handlerMap.SendEvent(path, FilesystemCacheFileRemoveEvent)
   124  }
   125  
   126  // SendFileUpdateEvent sends file update event
   127  func (handlerMap *FilesystemCacheEventHandlerMap) SendFileUpdateEvent(path string) {
   128  	handlerMap.SendEvent(path, FilesystemCacheFileUpdateEvent)
   129  }
   130  
   131  // SendDirCreateEvent sends dir create event
   132  func (handlerMap *FilesystemCacheEventHandlerMap) SendDirCreateEvent(path string) {
   133  	handlerMap.SendEvent(path, FilesystemCacheDirCreateEvent)
   134  }
   135  
   136  // SendDirRemoveEvent sends dir remove event
   137  func (handlerMap *FilesystemCacheEventHandlerMap) SendDirRemoveEvent(path string) {
   138  	handlerMap.SendEvent(path, FilesystemCacheDirRemoveEvent)
   139  }
   140  
   141  // SendDirExtractEvent sends dir extract event
   142  func (handlerMap *FilesystemCacheEventHandlerMap) SendDirExtractEvent(path string) {
   143  	handlerMap.SendEvent(path, FilesystemCacheDirExtractEvent)
   144  }