This project has retired. For details please refer to its
Attic page.
Filter xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.util;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.regex.Pattern;
23 import java.util.regex.PatternSyntaxException;
24 import org.apache.commons.lang.ArrayUtils;
25 import org.apache.hadoop.chukwa.Chunk;
26 import org.apache.hadoop.chukwa.extraction.engine.RecordUtil;
27 import org.apache.hadoop.chukwa.util.RegexUtil.CheckedPatternSyntaxException;
28 import org.apache.log4j.Logger;
29
30
31 public class Filter {
32
33 static Logger log = Logger.getLogger(Filter.class);
34
35 private static final String[] SEARCH_TARGS =
36 {"datatype", "name", "host", "cluster", "content"};
37 static final String SEPARATOR="&";
38
39 private static class SearchRule {
40 Pattern p;
41 String targ;
42
43 SearchRule(Pattern p, String t) {
44 this.p = p;
45 this.targ = t;
46 }
47
48 boolean matches(Chunk chunk) {
49 if(targ.equals("datatype")) {
50 return p.matcher(chunk.getDataType()).matches();
51 } else if(targ.equals("name")) {
52 return p.matcher(chunk.getStreamName()).matches();
53 } else if(targ.equals("host")) {
54 return p.matcher(chunk.getSource()).matches();
55 } else if(targ.equals("cluster")) {
56 String cluster = RecordUtil.getClusterName(chunk);
57 return p.matcher(cluster).matches();
58 } else if(targ.equals("content")) {
59 String content = new String(chunk.getData());
60 return p.matcher(content).matches();
61 } else if(targ.startsWith("tags.")) {
62 String tagName = targ.substring("tags.".length());
63 if (!RegexUtil.isRegex(tagName)) {
64 log.warn("Error parsing 'tagName' as a regex: "
65 + RegexUtil.regexError(tagName));
66 return false;
67 }
68 String tagVal = chunk.getTag(tagName);
69 if(tagVal == null)
70 return false;
71 return p.matcher(tagVal).matches();
72 } else {
73 assert false: "unknown target: " +targ;
74 return false;
75 }
76 }
77
78 public String toString() {
79 return targ + "=" +p.toString();
80 }
81 }
82
83 List<SearchRule> compiledPatterns;
84
85 public Filter(String listOfPatterns) throws CheckedPatternSyntaxException {
86 compiledPatterns = new ArrayList<SearchRule>();
87
88 String[] patterns = listOfPatterns.split(SEPARATOR);
89 for(String p: patterns) {
90 int equalsPos = p.indexOf('=');
91
92 if(equalsPos < 0 || equalsPos > (p.length() -2)) {
93 throw new CheckedPatternSyntaxException(
94 "pattern must be of form targ=pattern", p, -1);
95 }
96
97 String targ = p.substring(0, equalsPos);
98 if(!targ.startsWith("tags.") && !ArrayUtils.contains(SEARCH_TARGS, targ)) {
99 throw new CheckedPatternSyntaxException(
100 "pattern doesn't start with recognized search target", p, -1);
101 }
102
103 String regex = p.substring(equalsPos+1);
104 if (!RegexUtil.isRegex(regex)) {
105 throw new CheckedPatternSyntaxException(RegexUtil.regexException(regex));
106 }
107
108 Pattern pat = Pattern.compile(regex, Pattern.DOTALL);
109 compiledPatterns.add(new SearchRule(pat, targ));
110 }
111 }
112
113 public boolean matches(Chunk chunk) {
114 for(SearchRule r: compiledPatterns) {
115 if(!r.matches(chunk))
116 return false;
117 }
118 return true;
119 }
120
121 int size() {
122 return compiledPatterns.size();
123 }
124
125 public String toString() {
126 StringBuilder sb = new StringBuilder();
127 sb.append(compiledPatterns.get(0));
128 for(int i=1; i < compiledPatterns.size(); ++i) {
129 sb.append(" & ");
130 sb.append(compiledPatterns.get(i));
131 }
132 return sb.toString();
133 }
134
135 private static final class MatchAll extends Filter {
136 public MatchAll() throws CheckedPatternSyntaxException {
137 super("datatype=.*");
138 }
139
140 public boolean matches(Chunk c) {
141 return true;
142 }
143
144 public String toString() {
145 return "ALL";
146 }
147 }
148
149 public static final Filter ALL = newMatchAll();
150 private static Filter newMatchAll() {
151 try {
152 return new MatchAll();
153 } catch (CheckedPatternSyntaxException e) {
154 throw new RuntimeException("Illegal MatchAll regular expression.", e);
155 }
156 }
157
158 }