Class: FSEvent::WatchSet

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/fsevent/watchset.rb

Overview

watchset.rb — set of watches

Copyright © 2014 National Institute of Advanced Industrial Science and Technology (AIST)

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Instance Method Summary collapse

Methods included from Util

nested_hash, nonempty_hash, prefixpat_match, reaction_immediate_at_beginning?, reaction_immediate_at_subsequent?, valid_device_name_for_read?, valid_device_name_for_write?, valid_device_name_pat_for_read?, valid_status_name_for_read?, valid_status_name_for_write?, valid_status_name_pat_for_read?

Constructor Details

#initializeWatchSet

Returns a new instance of WatchSet.



21
22
23
24
25
26
27
28
29
# File 'lib/fsevent/watchset.rb', line 21

def initialize
  # valid values of reaction: :immediate, :schedule
  @watch_defs = nested_hash(3) # watcher_device_name -> watchee_device_name_pat -> status_name_pat -> reaction

  @watch_exact_exact = nested_hash(3) # watchee_device_name_exact -> status_name_exact -> watcher_device_name -> reaction
  @watch_exact_prefix = nested_hash(3) # watchee_device_name_exact -> status_name_prefix -> watcher_device_name -> reaction
  @watch_prefix_exact = nested_hash(3) # watchee_device_name_prefix -> status_name_exact -> watcher_device_name -> reaction
  @watch_prefix_prefix = nested_hash(3) # watchee_device_name_prefix -> status_name_prefix -> watcher_device_name -> reaction
end

Instance Method Details

#add(watchee_device_name_pat, status_name_pat, watcher_device_name, reaction) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fsevent/watchset.rb', line 31

def add(watchee_device_name_pat, status_name_pat, watcher_device_name, reaction)
  @watch_defs[watcher_device_name][watchee_device_name_pat][status_name_pat] = reaction
  if /\*\z/ =~ watchee_device_name_pat
    watchee_device_name_prefix = $`
    if /\*\z/ =~ status_name_pat
      status_name_prefix = $`
      @watch_prefix_prefix[watchee_device_name_prefix][status_name_prefix][watcher_device_name] = reaction
    else
      @watch_prefix_exact[watchee_device_name_prefix][status_name_pat][watcher_device_name] = reaction
    end
  else
    if /\*\z/ =~ status_name_pat
      status_name_prefix = $`
      @watch_exact_prefix[watchee_device_name_pat][status_name_prefix][watcher_device_name] = reaction
    else
      @watch_exact_exact[watchee_device_name_pat][status_name_pat][watcher_device_name] = reaction
    end
  end
end

#del(watchee_device_name_pat, status_name_pat, watcher_device_name) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fsevent/watchset.rb', line 51

def del(watchee_device_name_pat, status_name_pat, watcher_device_name)
  @watch_defs[watcher_device_name][watchee_device_name_pat].delete status_name_pat
  if /\*\z/ =~ watchee_device_name_pat
    watchee_device_name_prefix = $`
    if /\*\z/ =~ status_name_pat
      status_name_prefix = $`
      @watch_prefix_prefix[watchee_device_name_prefix][status_name_prefix].delete watcher_device_name
    else
      @watch_prefix_exact[watchee_device_name_prefix][status_name_pat].delete watcher_device_name
    end
  else
    if /\*\z/ =~ status_name_pat
      status_name_prefix = $`
      @watch_exact_prefix[watchee_device_name_pat][status_name_prefix].delete watcher_device_name
    else
      @watch_exact_exact[watchee_device_name_pat][status_name_pat].delete watcher_device_name
    end
  end
end

#delete_watcher(watcher_device_name) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fsevent/watchset.rb', line 119

def delete_watcher(watcher_device_name)
  @watch_defs.delete watcher_device_name

  [@watch_exact_exact,
   @watch_exact_prefix,
   @watch_prefix_exact,
   @watch_prefix_prefix].each {|h0|
    h0.each {|watchee_device_name, h1|
      h1.each {|status_name, h2|
        h2.delete watcher_device_name
      }
    }
  }
end

#lookup_watchers(watchee_device_name, status_name) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fsevent/watchset.rb', line 71

def lookup_watchers(watchee_device_name, status_name)
  # needs cache for performance?
  result = []
  if @watch_exact_exact.has_key?(watchee_device_name) &&
     @watch_exact_exact[watchee_device_name].has_key?(status_name)
    @watch_exact_exact[watchee_device_name][status_name].each {|watcher_device_name, reaction|
      result << [watcher_device_name, reaction]
    }
  end
  if @watch_exact_prefix.has_key?(watchee_device_name)
    @watch_exact_prefix[watchee_device_name].each {|status_name_prefix, h| # linear search.  can be slow.  binary search is better.
      if status_name.start_with? status_name_prefix
        h.each {|watcher_device_name, reaction|
          result << [watcher_device_name, reaction]
        }
      end
    }
  end
  @watch_prefix_exact.each {|watchee_device_name_prefix, h1| # linear search.  can be slow.  binary search is better.
    next unless watchee_device_name.start_with? watchee_device_name_prefix
    if @watch_prefix_exact[watchee_device_name_prefix].has_key?(status_name)
      @watch_prefix_exact[watchee_device_name_prefix][status_name].each {|watcher_device_name, reaction|
        result << [watcher_device_name, reaction]
      }
    end
  }
  @watch_prefix_prefix.each {|watchee_device_name_prefix, h1| # linear search.  can be slow.  binary search is better.
    next unless watchee_device_name.start_with? watchee_device_name_prefix
    @watch_prefix_prefix[watchee_device_name_prefix].each {|status_name_prefix, h| # linear search.  can be slow.  binary search is better.
      if status_name.start_with? status_name_prefix
        h.each {|watcher_device_name, reaction|
          result << [watcher_device_name, reaction]
        }
      end
    }
  }
  result
end

#watcher_each(watcher_device_name) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/fsevent/watchset.rb', line 110

def watcher_each(watcher_device_name)
  return unless @watch_defs.has_key? watcher_device_name
  @watch_defs[watcher_device_name].each {|watchee_device_name_pat, h|
    h.each {|status_name_pat, reaction|
      yield watchee_device_name_pat, status_name_pat, reaction
    }
  }
end