Ejemplo n.º 1
0
        public TopN <string> Match(string s, int count)
        {
            TopN <string> matches = new TopN <string>(count);

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            LockFreeQueue <string> queue = new LockFreeQueue <string>();
            int queuecount;

            lock (_paths) {
                int i = 0;
                while (i < _paths.Count)
                {
                    queue.Enqueue(_paths[i++]);
                }
                queuecount = _paths.Count;
            }

            ManualResetEvent mre  = new ManualResetEvent(false);
            WaitCallback     work = delegate {
                string        path;
                List <string> outs = new List <string>();
                object        obj  = null;
                int           score;
                while (queue.Dequeue(out path))
                {
                    if (DoMatch(path, s, out score, ref obj, outs))
                    {
                        lock (matches) {
                            foreach (string o in outs)
                            {
                                matches.Add(score, o);
                            }
                        }
                    }
                    if (Interlocked.Decrement(ref queuecount) == 0)
                    {
                        mre.Set();
                    }
                    else
                    {
                        outs.Clear();
                    }
                }
            };

            if (queuecount != 0)
            {
                int cpu = 0;
                while (cpu++ < Environment.ProcessorCount)
                {
                    ThreadPool.QueueUserWorkItem(work);
                }
                mre.WaitOne();
            }

            // Logger.Trace("{0}ms elapsed", sw.ElapsedMilliseconds);
            return(matches);
        }
Ejemplo n.º 2
0
        void ev_client()
        {
            try {
                _logger.Trace("listening");
                using (Stream stream = _client.GetStream()) {
                    using (StreamWriter wtr = new StreamWriter(stream, Encoding.ASCII)) {
                        using (StreamReader rdr = new StreamReader(stream)) {
                            while (true)
                            {
                                string line = rdr.ReadLine();
                                if (line == null)
                                {
                                    return;
                                }
                                // _logger.Trace("got cmd {0}", line);

                                line = Regex.Replace(line, @"^\s*#.*", "");
                                if (String.IsNullOrWhiteSpace(line))
                                {
                                    continue;
                                }
                                string[] s = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

                                if (s[0] == "config")
                                {
                                    _UpdateConfig(new ConfigParser().LoadConfig(s[1]));
                                }
                                else if (s[0] == "grep" && s[1] == "match")
                                {
                                    s = line.Split(new char[] { ' ', '\t' }, 3, StringSplitOptions.RemoveEmptyEntries);

                                    var combinedMatches = new TopN <string>(200);
                                    foreach (Matcher matcher in _matchers.Values)
                                    {
                                        foreach (var match in matcher.GrepMatch(line.Substring(line.IndexOf("match") + 6), 200))
                                        {
                                            combinedMatches.Add(match.Score, match.Item);
                                        }
                                    }

                                    StringBuilder sb = new StringBuilder();
                                    foreach (var match in combinedMatches)
                                    {
                                        sb.Append(match.Item);
                                        sb.Append("\n");
                                    }
                                    wtr.Write(sb.ToString());
                                    wtr.Write("\n");
                                }
                                else if (s[0] == "find" && s[1] == "match")
                                {
                                    s = line.Split(new char[] { ' ', '\t' }, 3, StringSplitOptions.RemoveEmptyEntries);

                                    var combinedMatches = new TopN <string>(200);
                                    foreach (Matcher matcher in _matchers.Values)
                                    {
                                        foreach (var match in matcher.PathMatch(line.Substring(line.IndexOf("match") + 6).ToLowerInvariant(), 200))
                                        {
                                            combinedMatches.Add(match.Score, match.Item);
                                        }
                                    }

                                    StringBuilder sb = new StringBuilder();
                                    foreach (var match in combinedMatches)
                                    {
                                        sb.Append(match.Item);
                                        sb.Append("\n");
                                    }
                                    wtr.Write(sb.ToString());
                                    wtr.Write("\n");
                                }
                                else if (s[0] == "nop")
                                {
                                    wtr.Write("nop\n");
                                }
                                else if (s[0] == "quit")
                                {
                                    return;
                                }
                                else
                                {
                                }

                                // _logger.Trace("done cmd {0}", line);
                                wtr.Flush();
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                Console.WriteLine("got exception {0}", ex.ToString());
            } finally {
                if (_client != null)
                {
                    try { _client.Close(); } catch { }
                    _client = null;
                }
                lock (__matchercache) {
                    foreach (var kvp in _matchers)
                    {
                        if (kvp.Value.Free())
                        {
                            __matchercache.Remove(kvp.Key);
                        }
                    }
                    _matchers = null;
                    Logger.TraceFrom("cache", "size: " + __matchercache.Count);
                }
            }
        }