您的位置:首页 > 其它

正则表达式进行大文件查找时的超时处理

2010-06-07 09:51 218 查看
应用系统服务器端的日志查找有很多的实现方式,限于软件、资源的问题,决定自己开发个远程日志单点查看工具,实现原理:
服务端职责:提供消息接收、监听,提供日志搜索查找与反馈
客户端职责:发送查询指令,接收查询结果
UI:提供关键字(支持正则表达式)、全字、大小写查找,可设定开始时间、可搜索的机器及其目录……
其中C/S通过Socket的方式连接通讯。

因为提供到正则表达式支持,所以在不正确的输入正则表达式时,会造成搜索查找非常耗时,所以在附加正则时,需要进行超时处理,具体如何实现,且看下文:

public class AsynchronousRegex
    {
        private MatchCollection mc;
        private bool isMatch = false;
        private int _timeout;
        private int sleepCounter = 0;
        private int sleepInterval = 100;

        public bool IsTimeout
        {
            get;
            set;
        }

        public AsynchronousRegex(int timeout)
        {
            this._timeout = timeout;
            this.IsTimeout = false;
            this.mc = null;
        }

        /// <summary>
        /// get matches items
        /// </summary>
        /// <param name="input"></param>
        /// <param name="regex"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public MatchCollection Matches(string input, string regex, RegexOptions options)
        {
            Reg r = new Reg(regex, input, options);
            r.OnMatchComplete += new Reg.MatchCompleteHandler(this.MatchCompleteHandler);
            Thread t = new Thread(new ThreadStart(r.Matchs));
            t.Start();

            this.Sleep(t);

            t = null;
            return mc;
        }

        /// <summary>
        /// check match 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="regex"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public bool IsMatch(string input, string regex, RegexOptions options)
        {
            Reg r = new Reg(regex, input, options);
            r.OnIsMatchComplete += new Reg.IsMatchCompleteHandler(this.IsMatchCompleteHandler);

            Thread t = new Thread(new ThreadStart(r.IsMatch));
            t.Start();

            this.Sleep(t);

            t = null;
            return isMatch;
        }

        private void Sleep(Thread t)
        {
            if (t != null && t.IsAlive)
            {
                Thread.Sleep(TimeSpan.FromMilliseconds(this.sleepInterval));
                this.sleepCounter++;
                if (this.sleepCounter * this.sleepInterval >= this._timeout)
                {
                    t.Abort();
                    this.IsTimeout = true;
                }
                else
                {
                    this.Sleep(t);
                }
            }
        }

        private void MatchCompleteHandler(MatchCollection mc)
        {
            this.mc = mc;
        }

        private void IsMatchCompleteHandler(bool _IsMatch)
        {
            this.isMatch = _IsMatch;
        }

        class Reg
        {
            internal delegate void MatchCompleteHandler(MatchCollection mc);
            internal delegate void IsMatchCompleteHandler(bool isMatch);
            internal event MatchCompleteHandler OnMatchComplete;
            internal event IsMatchCompleteHandler OnIsMatchComplete;

            public Reg(string regex, string input, RegexOptions options)
            {
                this.Regexs = regex;
                this.Inputs = input;
                this.Options = options;
            }

            /// <summary>
            /// 内容
            /// </summary>
            public string Inputs
            {
                get;
                set;
            }

            /// <summary>
            /// 规则
            /// </summary>
            public string Regexs
            {
                get;
                set;
            }

            public RegexOptions Options
            {
                get;
                set;
            }

            internal void Matchs()
            {
                MatchCollection mc = Regex.Matches(this.Inputs, Regexs, Options);
                if (mc != null && mc.Count > 0)    // 这里有可能造成cpu资源耗尽
                {
                    this.OnMatchComplete(mc);
                }
            }

            internal void IsMatch()
            {
                Regex regex = new Regex(Regexs, Options);
                if (regex.IsMatch(this.Inputs))
                {
                    this.OnIsMatchComplete(true);
                }
            }
        }
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: