Ejemplo n.º 1
0
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            LeakReader lr = new LeakReader(3, @"D:\leaks");

            tsc = new CancellationTokenSource();
            InputProgress prog;

            using (LeakSaver ls = new LeakSaver(3, @"D:\leaks"))
            {
                li = new LeakInput(@"D:\dropbox", ls, lr);
                Task <InputProgress> inputTask = li.SortAllAsync(tsc.Token);
                Console.ReadLine();
                tsc.Cancel();
                Console.WriteLine("Stopping");
                prog = await inputTask;
                Console.WriteLine("Stopped inputing waiting for flush");
            }

            if (prog.IsDone)
            {
                System.Collections.Generic.IEnumerable <string> res = await lr.Lookup("ric");
            }
            else
            {
                Console.WriteLine("Progress: {0}%\t Done: {1}\tinMB: {2}", prog.Percentage * 100, prog.Completed, prog.Completed / 1000 / 1000);
                Console.Read();
            }
        }
Ejemplo n.º 2
0
        public async Task <bool> HasResult(string data)
        {
            string path = LeakSaver.GetFilePath(basePath, data, layers);

            try
            {
                using (StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read)))
                {
                    while (!sr.EndOfStream)
                    {
                        string line = await sr.ReadLineAsync();

                        if (line.StartsWith(data))
                        {
                            return(true);
                        }
                    }
                }
                return(false);
            }
            catch (DirectoryNotFoundException)
            {
                return(false);
            }
        }
Ejemplo n.º 3
0
        public async Task <IEnumerable <string> > Lookup(string data)
        {
            string path = LeakSaver.GetFilePath(basePath, data, layers);
            LinkedList <string> items = new LinkedList <string>();

            try
            {
                using (StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read)))
                {
                    while (!sr.EndOfStream)
                    {
                        string line = await sr.ReadLineAsync();

                        if (line.StartsWith(data, true, null))
                        {
                            items.AddLast(line);
                        }
                    }
                }
                return(items);
            }
            catch (DirectoryNotFoundException)
            {
                return(new LinkedList <string>());
            }
        }
Ejemplo n.º 4
0
 public LeakInput(string path, LeakSaver leakSaver)
 {
     BasePath       = path;
     this.leakSaver = leakSaver;
     streamReader   = new StreamReader(new FileStream(BasePath,
                                                      FileMode.Open, FileAccess.Read, FileShare.None,
                                                      bufferSize: 4096, useAsync: true));
 }
Ejemplo n.º 5
0
        public LeakInput(string path, LeakSaver leakSaver, LeakReader lr) : this(path, leakSaver)
        {
            //hack to get around no use of async await
            Task t = Task.Run(async() =>
            {
                //do a 'binary search' in the stream
                long lowerBound    = 0;
                long upperBound    = streamReader.BaseStream.Length;
                const long minDist = 5 * 1000;//start looking line by line at 5kb
                while (upperBound - lowerBound > minDist)
                {
                    long middle = (lowerBound + upperBound) / 2;

                    // print a nice graphic
                    const long glen         = 100;
                    long maxLen             = streamReader.BaseStream.Length;
                    long gLpos              = (glen * lowerBound) / maxLen;
                    long gMpos              = (glen * middle) / maxLen;
                    long gUpos              = (glen * upperBound) / maxLen;
                    char[] gdata            = new string(' ', (int)glen + 2).ToCharArray();
                    gdata[0]                = '[';
                    gdata[gdata.Length - 1] = ']';
                    if (gLpos != gMpos && gMpos != gUpos)
                    {
                        gdata[gLpos + 1] = '<';
                        gdata[gMpos + 1] = '.';
                        gdata[gUpos + 1] = '>';
                        Console.WriteLine(new string(gdata));
                    }


                    streamReader.BaseStream.Position = middle;
                    streamReader.DiscardBufferedData();
                    // read partial line
                    await streamReader.ReadLineAsync();
                    string line = await streamReader.ReadLineAsync();
                    if (await lr.HasResult(line))
                    {
                        lowerBound = middle - 1000; // offset because partial lines
                    }
                    else
                    {
                        upperBound = middle + 1000; // offset because partial lines
                    }
                }
                streamReader.BaseStream.Position = lowerBound;
                streamReader.DiscardBufferedData();
                await streamReader.ReadLineAsync();

                while (true)
                {
                    string line = await streamReader.ReadLineAsync();
                    if (!(await lr.HasResult(line)))
                    {
                        await leakSaver.SaveLine(line);
                        break;
                    }
                }
                Console.WriteLine("Skipped aprox. {0}MB", streamReader.BaseStream.Position / (1000 * 1000));
            });

            t.Wait();
        }