示例#1
0
    static void Main(string [] args)
    {
        Mode      mode        = Mode.Uris;
        bool      show_counts = true;
        ArrayList index_dirs  = new ArrayList();
        ArrayList index_names = new ArrayList();
        ArrayList uris        = new ArrayList();

        foreach (string arg in args)
        {
            switch (arg)
            {
            case "--help":
                PrintUsage();
                Environment.Exit(0);
                break;

            case "--uris":
                mode = Mode.Uris;
                break;

            case "--properties":
            case "--props":
                mode = Mode.Properties;
                break;

            case "--term-frequencies":
            case "--term-freqs":
                mode = Mode.TermFrequencies;
                break;

            case "--hide-counts":
                show_counts = false;
                break;

            case "--show-counts":
                show_counts = false;
                break;

            case "--fields":
                mode = Mode.Fields;
                break;

            default:
                if (arg.StartsWith("--indexdir="))
                {
                    index_dirs.Add(arg.Remove(0, 11));
                }
                else if (arg.StartsWith("--index="))
                {
                    index_names.Add(arg.Remove(0, 8));
                }
                else
                {
                    Uri uri;

                    try {
                        uri = UriFu.UserUritoEscapedUri(arg);
                    } catch (UriFormatException) {
                        uri = UriFu.PathToFileUri(arg);
                    }

                    uris.Add(uri);
                }
                break;
            }
        }

        if (uris.Count > 0 && (mode == Mode.TermFrequencies || mode == Mode.Fields))
        {
            Console.WriteLine("ERROR: --term-frequencies and --fields do not make sense with files or URIs.");
            Environment.Exit(1);
        }

        ArrayList indexes = new ArrayList();

        // If no --index or --indexdir options, get all the default indexes.
        if (index_dirs.Count == 0 && index_names.Count == 0)
        {
            foreach (DirectoryInfo subdir in DirectoryWalker.GetDirectoryInfos(PathFinder.IndexDir))
            {
                indexes.Add(new IndexInfo(subdir.Name));
            }
        }
        else
        {
            foreach (string name in index_names)
            {
                DirectoryInfo info = new DirectoryInfo(Path.Combine(PathFinder.IndexDir, name));

                if (!info.Exists)
                {
                    Console.WriteLine("ERROR: No index named '{0}'", name);
                    Environment.Exit(1);
                }

                indexes.Add(new IndexInfo(info.Name));
            }

            foreach (string dir in index_dirs)
            {
                indexes.Add(new IndexInfo(dir));
            }
        }

        indexes.Sort();

        if (mode == Mode.Uris || mode == Mode.Properties)
        {
            DumpIndexInformation(indexes, uris, mode == Mode.Properties, show_counts);
        }
        else if (mode == Mode.TermFrequencies)
        {
            DumpIndexTermFreqs(indexes);
        }
        else if (mode == Mode.Fields)
        {
            DumpIndexFields(indexes);
        }
    }
示例#2
0
        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);
                    part.Uri   = UriFu.UserUritoEscapedUri(text);
                    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);
            }

            if ((pos = text.IndexOf('*')) >= 0)
            {
                QueryPart_Wildcard wild = new QueryPart_Wildcard();
                wild.QueryString  = text;
                wild.PropertyOnly = true;
                return(wild);
            }

            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);
        }