Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 1 | package stats |
| 2 | |
| 3 | import ( |
| 4 | "path" |
| 5 | "sort" |
| 6 | "time" |
| 7 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 8 | "veyron.io/veyron/veyron/lib/glob" |
Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | // Glob returns the name and (optionally) the value of all the objects that |
| 12 | // match the given pattern and have been updated since 'updatedSince'. The |
| 13 | // 'root' argument is the name of the object where the pattern starts. |
| 14 | // Example: |
| 15 | // a/b/c |
| 16 | // a/b/d |
| 17 | // b/e/f |
| 18 | // Glob("", "...", time.Time{}, true) will return "a/b/c", "a/b/d", "b/e/f" and |
| 19 | // their values. |
| 20 | // Glob("a/b", "*", time.Time{}, true) will return "c", "d" and their values. |
| 21 | func Glob(root string, pattern string, updatedSince time.Time, includeValues bool) *GlobIterator { |
| 22 | g, err := glob.Parse(pattern) |
| 23 | if err != nil { |
| 24 | return &GlobIterator{err: err} |
| 25 | } |
| 26 | lock.RLock() |
| 27 | defer lock.RUnlock() |
| 28 | node := findNodeLocked(root, false) |
| 29 | if node == nil { |
| 30 | return &GlobIterator{err: ErrNotFound} |
| 31 | } |
| 32 | var out []KeyValue |
| 33 | globStepLocked("", g, node, updatedSince, includeValues, &out) |
| 34 | sort.Sort(keyValueSort(out)) |
| 35 | return &GlobIterator{results: out} |
| 36 | } |
| 37 | |
| 38 | // globStepLocked applies a glob recursively. |
| 39 | func globStepLocked(prefix string, g *glob.Glob, n *node, updatedSince time.Time, includeValues bool, result *[]KeyValue) { |
Robin Thellend | 14a09ef | 2014-08-29 13:55:19 -0700 | [diff] [blame] | 40 | if g.Len() == 0 { |
| 41 | if updatedSince.IsZero() || (n.object != nil && !n.object.LastUpdate().Before(updatedSince)) { |
| 42 | var v interface{} |
| 43 | if includeValues && n.object != nil { |
| 44 | v = n.object.Value() |
| 45 | } |
| 46 | *result = append(*result, KeyValue{prefix, v}) |
Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 47 | } |
Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 48 | } |
| 49 | if g.Finished() { |
| 50 | return |
| 51 | } |
| 52 | for name, child := range n.children { |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 53 | if ok, _, left := g.MatchInitialSegment(name); ok { |
Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 54 | globStepLocked(path.Join(prefix, name), left, child, updatedSince, includeValues, result) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // KeyValue stores a Key and a Value. |
| 60 | type KeyValue struct { |
| 61 | Key string |
| 62 | Value interface{} |
| 63 | } |
| 64 | |
| 65 | // keyValueSort is used to sort a slice of KeyValue objects. |
| 66 | type keyValueSort []KeyValue |
| 67 | |
| 68 | func (s keyValueSort) Len() int { |
| 69 | return len(s) |
| 70 | } |
| 71 | |
| 72 | func (s keyValueSort) Less(i, j int) bool { |
| 73 | return s[i].Key < s[j].Key |
| 74 | } |
| 75 | |
| 76 | func (s keyValueSort) Swap(i, j int) { |
| 77 | s[i], s[j] = s[j], s[i] |
| 78 | } |
| 79 | |
| 80 | type GlobIterator struct { |
| 81 | results []KeyValue |
| 82 | next KeyValue |
| 83 | err error |
| 84 | } |
| 85 | |
| 86 | // Advance stages the next element so that the client can retrieve it with |
| 87 | // Value(). It returns true iff there is an element to retrieve. The client |
| 88 | // must call Advance() before calling Value(). Advance may block if an element |
| 89 | // is not immediately available. |
| 90 | func (i *GlobIterator) Advance() bool { |
| 91 | if len(i.results) == 0 { |
| 92 | return false |
| 93 | } |
| 94 | i.next = i.results[0] |
| 95 | i.results = i.results[1:] |
| 96 | return true |
| 97 | } |
| 98 | |
| 99 | // Value returns the element that was staged by Advance. Value does not block. |
| 100 | func (i GlobIterator) Value() KeyValue { |
| 101 | return i.next |
| 102 | } |
| 103 | |
| 104 | // Err returns a non-nil error iff the stream encountered any errors. Err does |
| 105 | // not block. |
| 106 | func (i GlobIterator) Err() error { |
| 107 | return i.err |
| 108 | } |