github.com/kula/etcd@v0.2.1-0.20131226070625-e96234382ac0/store/watcher.go (about)

     1  /*
     2  Copyright 2013 CoreOS Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package store
    18  
    19  type watcher struct {
    20  	eventChan  chan *Event
    21  	recursive  bool
    22  	sinceIndex uint64
    23  }
    24  
    25  // notify function notifies the watcher. If the watcher interests in the given path,
    26  // the function will return true.
    27  func (w *watcher) notify(e *Event, originalPath bool, deleted bool) bool {
    28  	// watcher is interested the path in three cases and under one condition
    29  	// the condition is that the event happens after the watcher's sinceIndex
    30  
    31  	// 1. the path at which the event happens is the path the watcher is watching at.
    32  	// For example if the watcher is watching at "/foo" and the event happens at "/foo",
    33  	// the watcher must be interested in that event.
    34  
    35  	// 2. the watcher is a recursive watcher, it interests in the event happens after
    36  	// its watching path. For example if watcher A watches at "/foo" and it is a recursive
    37  	// one, it will interest in the event happens at "/foo/bar".
    38  
    39  	// 3. when we delete a directory, we need to force notify all the watchers who watches
    40  	// at the file we need to delete.
    41  	// For example a watcher is watching at "/foo/bar". And we deletes "/foo". The watcher
    42  	// should get notified even if "/foo" is not the path it is watching.
    43  	if (w.recursive || originalPath || deleted) && e.Index() >= w.sinceIndex {
    44  		w.eventChan <- e
    45  		return true
    46  	}
    47  	return false
    48  }