//////////////////////////////////////////////////////// public static void ReadKeywordMappings() { property_table = new Hashtable(); // FIXME: No need for a SerializerFactory here, since we need the serializer // only once XmlSerializerFactory xsf = new XmlSerializerFactory(); XmlSerializer xs = xsf.CreateSerializer(typeof(QueryMapping), new Type[] { typeof(QueryKeywordMapping) }); QueryMapping query_mapping = null; // <keyword name, can override> Dictionary <string, bool> mapping_override = new Dictionary <string, bool> (); using (Stream s = File.OpenRead(Path.Combine(PathFinder.ConfigDataDir, "query-mapping.xml"))) { try { query_mapping = (QueryMapping)xs.Deserialize(s); foreach (QueryKeywordMapping mapping in query_mapping.Mappings) { PropertyKeywordFu.RegisterMapping(mapping); mapping_override [mapping.Keyword] = true; } } catch (XmlException e) { Logger.Log.Error(e, "Unable to parse global query-mapping.xml"); } } // Override global mappings by local mappings if (!File.Exists(Path.Combine(PathFinder.StorageDir, "query-mapping.xml"))) { return; } using (Stream s = File.OpenRead(Path.Combine(PathFinder.StorageDir, "query-mapping.xml"))) { try { query_mapping = (QueryMapping)xs.Deserialize(s); foreach (QueryKeywordMapping mapping in query_mapping.Mappings) { if (mapping_override.ContainsKey(mapping.Keyword)) { property_table.Remove(mapping.Keyword); mapping_override [mapping.Keyword] = false; } PropertyKeywordFu.RegisterMapping(mapping); } } catch (XmlException e) { Logger.Log.Error(e, "Unable to parse local query-mapping.xml"); } } }
// gmcs QueryStringParser.cs -r:../Util/Util.dll -r:../BeagleClient/Beagle.dll PropertyKeywordFu.cs public static void Main() { PropertyKeywordFu.ReadKeywordMappings(); while (true) { string input = Console.ReadLine(); if (input == String.Empty) { continue; } Console.WriteLine("Parsing query string '{0}'", input); foreach (QueryPart part in Parse(input)) { Console.WriteLine(part.ToString()); } } }
static public void Start() { // Only add the executing assembly if we haven't already loaded it. if (assemblies.IndexOf(Assembly.GetExecutingAssembly()) == -1) { assemblies.Add(Assembly.GetExecutingAssembly()); } foreach (Assembly assembly in assemblies) { ScanAssemblyForQueryables(assembly); // This allows backends to define their // own executors. Server.ScanAssemblyForExecutors(assembly); } assemblies = null; PropertyKeywordFu.ReadKeywordMappings(); LoadSystemIndexes(); LoadStaticQueryables(); if (indexing_delay < 0) { return; } if (indexing_delay == 0 || Environment.GetEnvironmentVariable("BEAGLE_EXERCISE_THE_DOG") != null) { StartQueryables(); } else { Logger.Log.Debug("Waiting {0} seconds before starting queryables", indexing_delay); GLib.Timeout.Add((uint)indexing_delay * 1000, new GLib.TimeoutHandler(StartQueryables)); } }
static private QueryPart MatchToQueryPart(Match m) { // Looping over all Matches we have got: // m.Groups["pm"] plus or minus sign // m.Groups["key"] keyname // m.Groups["quote"] quoted string // m.Groups["midquote1"] + m.Groups["midquote2"] quoted midway string also represents unquoted string string query = m.ToString(); // Either quote is set or midquote1 and (optionally) midquote2 is set string text = m.Groups ["quote"].ToString() + m.Groups ["midquote1"].ToString() + m.Groups ["midquote2"].ToString(); string key = m.Groups ["key"].ToString(); bool IsProhibited = (m.Groups ["pm"].ToString() == "-"); // check for file extensions // if match starts with *. or . and only contains letters we assume it's a file extension if (extension_re.Match(text).Success || key.ToLower() == "ext" || key.ToLower() == "extension") { QueryPart_Property query_part = new QueryPart_Property(); query_part.Key = Property.FilenameExtensionPropKey; if (text.StartsWith("*.")) { query_part.Value = text.Substring(1).ToLower(); } else if (text.StartsWith(".")) { query_part.Value = text.ToLower(); } else { query_part.Value = "." + text.ToLower(); } query_part.Type = PropertyType.Keyword; query_part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required); Logger.Log.Debug("Extension query: {0}", query_part.Value); return(query_part); } if (key == String.Empty) { Logger.Log.Debug("Parsed query '{0}' as text_query", text); return(StringToQueryPart(text, IsProhibited)); } // FIXME: i18n-izing "date" if (key == "date") { try { QueryPart part = DateQueryToQueryPart(text); part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required); return(part); } catch (FormatException) { Log.Warn("Could not parse [{0}] as date query. Assuming text.", text); return(StringToQueryPart(text, IsProhibited)); } } // FIXME: i18n-izing "uri" if (key == "uri") { try { QueryPart_Uri part = new QueryPart_Uri(); part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required); // uri: queries require the raw Uri as present in the index part.Uri = new Uri(text, true); return(part); } catch (System.UriFormatException) { Log.Warn("Could not parse [{0}] as uri query. Assuming text.", text); return(StringToQueryPart(text, IsProhibited)); } } // Special case if (key == "inuri") { QueryPart_Property inuri_part = new QueryPart_Property(); inuri_part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required); inuri_part.Key = "inuri"; inuri_part.Value = text; inuri_part.Type = PropertyType.Keyword; Log.Debug("Handing special query 'inuri:{0}'", text); return(inuri_part); } // Non-keyword queries by directly using property names // Query of form property:namespace:name=value // which is translated to a non-keyword query // namespace:name=value int pos; if (key == "property" && ((pos = text.IndexOf('=')) != -1)) { QueryPart_Property part = new QueryPart_Property(); part.Key = text.Substring(0, pos); part.Value = text.Substring(pos + 1); part.Type = PropertyType.Text; part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required); Logger.Log.Debug("Parsed query '" + query + "' as prop query:key=" + part.Key + ", value=" + part.Value + " and property type=" + part.Type); return(part); } // keyword queries by directly using property names // Query of form keyword:namespace:name=value // which is translated to a keyword query // namespace:name=value if (key == "keyword" && ((pos = text.IndexOf('=')) != -1)) { QueryPart_Property part = new QueryPart_Property(); part.Key = text.Substring(0, pos); part.Value = text.Substring(pos + 1); part.Type = PropertyType.Keyword; part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required); Logger.Log.Debug("Parsed query '" + query + "' as prop query:key=" + part.Key + ", value=" + part.Value + " and property type=" + part.Type); return(part); } string[] prop_string = null; bool is_present; PropertyType[] prop_type; int num; is_present = PropertyKeywordFu.GetMapping(key, out num, out prop_string, out prop_type); // if key is not present in the mapping, assume the query is a text query // i.e. if token is foo:bar and there is no mappable property named foo, // assume "foo:bar" as text query // FIXME the analyzer changes the text query "foo:bar" to "foo bar" // which might not be the right thing to do if (!is_present) { Logger.Log.Warn("Could not find property, parsed query '{0}' as text_query", query); return(StringToQueryPart(query, IsProhibited)); } if (num == 1) { QueryPart_Property query_part_prop = new QueryPart_Property(); query_part_prop.Key = prop_string [0]; query_part_prop.Value = text; query_part_prop.Type = prop_type [0]; query_part_prop.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required); Logger.Log.Debug("Parsed query '" + query + "' as prop query:key=" + query_part_prop.Key + ", value=" + query_part_prop.Value + " and property type=" + query_part_prop.Type); return(query_part_prop); } // Multiple property queries are mapped to this keyword query // Create an OR query from them // FIXME: Would anyone want an AND query ? QueryPart_Or query_part_or = new QueryPart_Or(); query_part_or.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required); Logger.Log.Debug("Parsed query '{0}' as OR of {1} queries:", query, num); for (int i = 0; i < num; ++i) { QueryPart_Property query_part_prop = new QueryPart_Property(); query_part_prop.Key = prop_string [i]; query_part_prop.Value = text; query_part_prop.Type = prop_type [i]; query_part_prop.Logic = QueryPartLogic.Required; Log.Debug("\t:key={0}, value={1} and property type={2}", query_part_prop.Key, query_part_prop.Value, query_part_prop.Type); query_part_or.Add(query_part_prop); } return(query_part_or); }