public override void Run(bool runChildren) { currentFileName = FileName.Convert(this); baseFileName = currentFileName; if (currentFileName.Length == 0) { ReportManage.ErrReport(this, "FileNameがありません"); return; } var f = CheckFileSizeAndCreate(System.IO.File.AppendText(currentFileName)); int count = 0; ActionBlock <string> actionBlock = new ActionBlock <string>((n) => { if (count % 10000 == 0) { f = CheckFileSizeAndCreate(f); } f.WriteLine(n); ReportManage.Report(this, "NextDataRow"); count++; }); inputBlock = new BufferBlock <string>(); inputBlock.LinkTo(actionBlock, new DataflowLinkOptions() { PropagateCompletion = true }); base.Run(runChildren); inputBlock.Complete(); actionBlock.Completion.Wait(); f.Close(); }
/// <summary> /// このクラスでの実行すること。 /// </summary> /// <param name="runChildren"></param> public override void Run(bool runChildren) { if (FileName != null) { try { string line = GetText(); if (string.IsNullOrEmpty(Line) == false) { line = Line.Convert(this); } System.IO.File.AppendAllText(FileName.Convert(this), line + "\n"); } catch (Exception e) { ReportManage.ErrReport(this, "ファイルの書き込みに失敗しました。" + e.Message); } } else { ReportManage.ErrReport(this, "ファイル名を指定してください"); } base.Run(runChildren); }
public async override void Run(bool runChildren) { fileReadCompleted = false; string file = FileName.Convert(this); if (string.IsNullOrEmpty(file)) { file = GetText(); } if (System.IO.File.Exists(file) == false) { ReportManage.ErrReport(this, "Fileが見つかりません「" + file + "」"); return; } cancellationTokenSource = new CancellationTokenSource(); var task = Task.Factory.StartNew(() => { List <string> list = new List <string>(BlockSize); foreach (var item in System.IO.File.ReadLines(FileName.Convert(this))) { if (cancellationTokenSource.Token.IsCancellationRequested) { break; } bool flag = true; do { if (q.Count < MaxBlock) { list.Add(item); if (list.Count > BlockSize) { q.Push(list.ToArray()); list.Clear(); } flag = false; break; } else { Task.Delay(3).Wait(); } }while (flag); } fileReadCompleted = true; }, cancellationTokenSource.Token); base.Run(runChildren); }
public override void Run(bool runChildren) { input = new BufferBlock <KeyValuePair <string, int> >(); BatchBlock <KeyValuePair <string, int> > batch = new BatchBlock <KeyValuePair <string, int> >(BatchSize); TransformBlock <KeyValuePair <string, int>[], Dictionary <string, int> > trans = new TransformBlock <KeyValuePair <string, int>[], Dictionary <string, int> >(l => { return(l.ToCountDictionary(n => n.Key, n => n.Value)); }); BatchBlock <Dictionary <string, int> > dicBatch = new BatchBlock <Dictionary <string, int> >(3); TransformBlock <Dictionary <string, int>[], Dictionary <string, int> > margeDic = new TransformBlock <Dictionary <string, int>[], Dictionary <string, int> >(n => { return(n.Marge()); }); bool retrunFlag = true; BufferBlock <Dictionary <string, int> > result = new BufferBlock <Dictionary <string, int> >(); ActionBlock <Dictionary <string, int> > returnAction = new ActionBlock <Dictionary <string, int> >(n => { if (retrunFlag) { dicBatch.Post(n); } else { result.Post(n); } }); input.LinkTo(batch, new DataflowLinkOptions() { PropagateCompletion = true }); batch.LinkTo(trans, new DataflowLinkOptions() { PropagateCompletion = true }); trans.LinkTo(dicBatch, new DataflowLinkOptions() { PropagateCompletion = true }); dicBatch.LinkTo(margeDic, new DataflowLinkOptions() { PropagateCompletion = true }); margeDic.LinkTo(returnAction, new DataflowLinkOptions() { PropagateCompletion = true }); SetText(GetText()); base.Run(runChildren); retrunFlag = false; input.Complete(); returnAction.Completion.Wait(); IList <Dictionary <string, int> > list; if (result.TryReceiveAll(out list)) { var r = list.Marge(); if (FileSave) { using (var f = System.IO.File.CreateText(FileName.Convert(this))) { foreach (var item in r.OrderByDescending(n => n.Value).Where(n => n.Value >= CutCount)) { f.WriteLine(item.Key + "\t" + item.Value); ReportManage.Report(this, item.Key + "\t" + item.Value, true, true); } ReportManage.Report(this, "FileWrite:" + FileName.Convert(this), true, true); } } var reduce = this.GetUpperRawler <Reduce>(); if (reduce != null) { foreach (var item in r.Where(n => n.Value >= CutCount)) { reduce.AddKeyValue(item); } } } else { ReportManage.ErrReport(this, "TryReceiveAll 失敗"); } }