数据库异步查询和处理

时间:2016-03-22 15:54:53

标签: c# asynchronous pipeline sharpsvn tpl-dataflow

让我重新说一下。

我有一个方法,在给定一个起始字符串(路径)后生成字符串(路径) 如果这些路径是针对目录的,我想将其排入方法的输入中。

在同步处理路径之后,我想获取数据并将其异步克隆到管道的多个路径中,每个路径都需要获取数据块。因此广播块是不可能的(它不能在块之前发送阻塞信号), 加入块,加入结果相对简单。

总结一下 数据流块中是否有块,我可以从代理访问输入队列,如果何时,如何? 是否有一个类似于广播块的构造但可以阻止它之前的块?

我尝试通过全能的谷歌:

class subversion
    {
        private static string repo;
        private static string user;
        private static string pw;
        private static DateTime start;
        private static DateTime end;

        private static List<parserObject> output;
        public static List<parserObject> svnOutputList
        {
            get {return output; }
        }

        private static List<string> extension_whitelist;

        public async void run(string link, string i_user, string i_pw, DateTime i_start, DateTime i_end)
        {
            repo = link;
            user = i_user;
            pw = i_pw;
            start = i_start;
            end = i_end;


            output = new List<parserObject>();
        BufferBlock<string> crawler_que = new BufferBlock<string>();
        BufferBlock<svnFile> parser_que = new BufferBlock<svnFile>();

       var svn = crawl(crawler_que, parser_que);

        var broadcaster = new ActionBlock<svnFile>(async file =>
        {//tried to addapt the code from this ensure always send broadcastblock -> see link below
            List<Task> todo = new List<Task>();
            todo.Add(mLoc);//error cannot convert methodgroup to task

            foreach (var task in todo)//error: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement?

            {
                task.SendAsync(file);//error cannot convert task to targetblock

            }
            await Task.WhenAll(todo.ToArray());
        });

        parser_que.LinkTo(broadcaster);
        await Task.WhenAll(broadcaster, svn);//error cannot convert actionblock to task
    }

    private static async Task crawl(BufferBlock<string> in_queue, BufferBlock<svnFile> out_queue)
    {
        SvnClient client = new SvnClient();
        client.Authentication.ForceCredentials(user, pw);

        SvnListArgs arg = new SvnListArgs
        {
            Depth = SvnDepth.Children,
            RetrieveEntries = SvnDirEntryItems.AllFieldsV15
        };

        while (await in_queue.OutputAvailableAsync())
        {
            string buffer_author = null;
            string prev_author = null;
            System.Collections.ObjectModel.Collection<SvnListEventArgs> contents;

            string link = await in_queue.ReceiveAsync();
            if (client.GetList(new Uri(link), arg, out contents))
            {
                foreach (SvnListEventArgs item in contents)
                {
                    if (item.Entry.NodeKind == SvnNodeKind.Directory)
                    {
                        in_queue.Post(item.Path);
                    }
                    else if (item.Entry.NodeKind == SvnNodeKind.File)
                    {
                        try
                        {
                            int length = item.Name.LastIndexOf(".");
                            if (length <= 0)
                            {
                                continue;
                            }
                            string ext = item.Name.Substring(length);
                            if (extension_whitelist.Contains(ext))
                            {
                                Uri target = new Uri((repo + link));
                                SvnRevisionRange range;
                                SvnBlameArgs args = new SvnBlameArgs
                                {
                                    Start = start.AddDays(-1),
                                    End = end
                                };
                                try
                                {
                                    svnFile file_instance = new svnFile();
                                    client.Blame(target, args, delegate(object sender3, SvnBlameEventArgs e)
                                    {
                                        if (e.Author != null)
                                        {
                                            buffer_author = e.Author;
                                            prev_author = e.Author;
                                        }
                                        else
                                        {
                                            buffer_author = prev_author;
                                        }
                                        file_instance.lines.Add(new svnLine(buffer_author, e.Line));
                                    });
                                    out_queue.Post(file_instance);
                                }
                                catch (Exception a) { Console.WriteLine("exception:" + a.Message);}
                            }
                        }
                        catch (Exception a)
                        {
                        }
                    }
                }
            }
        }
    }
    private static async Task mLoc(svnFile file)
    {
        List<parserPart> parts = new List<parserPart>();
        int find;
        foreach (svnLine line in file.lines)
        {
            if ((find = parts.FindIndex(x => x.uploader_id == line.author)) > 0)
            {
                parts[find].count += 1;
            }
            else
            {
                parts.Add(new parserPart(line.author));
            }
            find = 0;
        }
        parserObject ret = new parserObject(parts, "mLoc");
        await output.Add(ret);
        return;
    }
}

广播块回答:Alternate to Dataflow BroadcastBlock with guaranteed delivery

0 个答案:

没有答案