-
Notifications
You must be signed in to change notification settings - Fork 29
Plain text ingestion WIP: read frames of input with trailing lines #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Copyright 2018 Datalust Pty Ltd | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| namespace SeqCli.PlainText | ||
| { | ||
| struct Frame | ||
| { | ||
| public bool HasValue { get; set; } | ||
| public bool IsOrphan { get; set; } | ||
| public string Value { get; set; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // Copyright 2018 Datalust Pty Ltd | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Superpower; | ||
| using Superpower.Model; | ||
|
|
||
| namespace SeqCli.PlainText | ||
| { | ||
| class FrameReader | ||
| { | ||
| readonly TextReader _source; | ||
| readonly TimeSpan _trailingLineArrivalDeadline; | ||
| readonly TextParser<TextSpan> _frameStart; | ||
|
|
||
| string _unconsumedFirstLine; | ||
| Task<string> _unawaitedNextLine; | ||
|
|
||
| public FrameReader(TextReader source, TextParser<TextSpan> frameStart, TimeSpan trailingLineArrivalDeadline) | ||
| { | ||
| _source = source ?? throw new ArgumentNullException(nameof(source)); | ||
| _frameStart = frameStart ?? throw new ArgumentNullException(nameof(frameStart)); | ||
| _trailingLineArrivalDeadline = trailingLineArrivalDeadline; | ||
| } | ||
|
|
||
| public async Task<Frame> TryReadAsync() | ||
| { | ||
| var valueBuilder = new StringBuilder(); | ||
| var hasValue = false; | ||
|
|
||
| if (_unconsumedFirstLine != null) | ||
| { | ||
| valueBuilder.AppendLine(_unconsumedFirstLine); | ||
| _unconsumedFirstLine = null; | ||
| hasValue = true; | ||
| } | ||
| else if (_unawaitedNextLine != null) | ||
| { | ||
| var line = await _unawaitedNextLine; | ||
| _unawaitedNextLine = null; | ||
| if (line == null) | ||
| return new Frame(); | ||
|
|
||
| valueBuilder.AppendLine(line); | ||
| hasValue = true; | ||
|
|
||
| if (!IsFrameStart(line)) | ||
| return new Frame {HasValue = true, IsOrphan = true, Value = valueBuilder.ToString()}; | ||
| } | ||
|
|
||
| Task<string> readLine = null; | ||
| while (true) | ||
| { | ||
| readLine = readLine ?? Task.Run(_source.ReadLineAsync); | ||
| var index = Task.WaitAny(new Task[] {readLine}, _trailingLineArrivalDeadline); | ||
| if (index == -1) // Timeout | ||
| { | ||
| if (hasValue) | ||
| { | ||
| _unawaitedNextLine = readLine; | ||
| return new Frame {HasValue = true, Value = valueBuilder.ToString()}; | ||
| } | ||
|
|
||
| // else, around we go! | ||
| } | ||
| else | ||
| { | ||
| var line = await readLine; | ||
| readLine = null; | ||
| if (line == null) | ||
| { | ||
| if (hasValue) | ||
| { | ||
| return new Frame {HasValue = true, Value = valueBuilder.ToString()}; | ||
| } | ||
|
|
||
| return new Frame(); | ||
| } | ||
|
|
||
| if (IsFrameStart(line)) | ||
| { | ||
| if (hasValue) | ||
| { | ||
| _unconsumedFirstLine = line; | ||
| return new Frame {HasValue = true, Value = valueBuilder.ToString()}; | ||
| } | ||
|
|
||
| valueBuilder.AppendLine(line); | ||
| hasValue = true; | ||
| } | ||
| else | ||
| { | ||
| if (!hasValue) | ||
| { | ||
| valueBuilder.AppendLine(line); | ||
| return new Frame {HasValue = true, Value = valueBuilder.ToString(), IsOrphan = true}; | ||
| } | ||
|
|
||
| valueBuilder.AppendLine(line); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bool IsFrameStart(string line) | ||
| { | ||
| if (line == null) throw new ArgumentNullException(nameof(line)); | ||
| var result = _frameStart(new TextSpan(line)); | ||
| return result.HasValue && result.Value.Length > 0; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using SeqCli.PlainText; | ||
| using Superpower; | ||
| using Superpower.Model; | ||
| using Superpower.Parsers; | ||
| using Xunit; | ||
|
|
||
| namespace SeqCli.Tests.PlainText | ||
| { | ||
| public class FrameReaderTests | ||
| { | ||
| static TextParser<TextSpan> SpanMatchedBy<T>(TextParser<T> parser) | ||
| { | ||
| return i => | ||
| { | ||
| var result = parser(i); | ||
|
|
||
| if (!result.HasValue) | ||
| return Result.CastEmpty<T, TextSpan>(result); | ||
|
|
||
| return Result.Value( | ||
| i.Until(result.Remainder), | ||
| i, | ||
| result.Remainder); | ||
| }; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task SplitsLinesIntoFrames() | ||
| { | ||
| var source = new StringBuilder(); | ||
| source.AppendLine("first"); | ||
| source.AppendLine("second"); | ||
|
|
||
| var reader = new FrameReader( | ||
| new StringReader(source.ToString()), | ||
| SpanMatchedBy(Character.Letter), | ||
| TimeSpan.FromMilliseconds(1)); | ||
|
|
||
| var first = await reader.TryReadAsync(); | ||
| Assert.True(first.HasValue); | ||
| Assert.Equal("first" + Environment.NewLine, first.Value); | ||
|
|
||
| var second = await reader.TryReadAsync(); | ||
| Assert.True(second.HasValue); | ||
| Assert.Equal("second" + Environment.NewLine, second.Value); | ||
|
|
||
| var empty = await reader.TryReadAsync(); | ||
| Assert.False(empty.HasValue); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TerminatesWhenNoLinesArePresent() | ||
| { | ||
| var reader = new FrameReader( | ||
| new StringReader(""), | ||
| SpanMatchedBy(Character.Letter), | ||
| TimeSpan.FromMilliseconds(1)); | ||
|
|
||
| var none = await reader.TryReadAsync(); | ||
| Assert.False(none.HasValue); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CollectsTrailingLines() | ||
| { | ||
| var source = new StringBuilder(); | ||
| source.AppendLine("first"); | ||
| source.AppendLine(" some more"); | ||
| source.AppendLine(" and more"); | ||
| source.AppendLine("second"); | ||
| source.AppendLine("third"); | ||
| source.AppendLine(" and yet more"); | ||
|
|
||
| var frames = await ReadAllFrames(source.ToString(), SpanMatchedBy(Character.Letter)); | ||
| Assert.Equal(3, frames.Length); | ||
| Assert.StartsWith("first", frames[0].Value); | ||
| Assert.EndsWith("and more" + Environment.NewLine, frames[0].Value); | ||
| } | ||
|
|
||
| static async Task<Frame[]> ReadAllFrames(string source, TextParser<TextSpan> frameStart) | ||
| { | ||
| var reader = new FrameReader( | ||
| new StringReader(source), | ||
| frameStart, | ||
| TimeSpan.FromMilliseconds(1)); | ||
|
|
||
| var result = new List<Frame>(); | ||
|
|
||
| var frame = await reader.TryReadAsync(); | ||
| while (frame.HasValue) | ||
| { | ||
| result.Add(frame); | ||
| frame = await reader.TryReadAsync(); | ||
| } | ||
|
|
||
| return result.ToArray(); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was necessary to deal with the change from
While()toWithoutAny()a few lines earlier. The former created zero-length matches, while the latter doesn't.We can revert to the earlier approach when something like
Span.MatchedBy(Character.Except('"').Many())can be expressed in Superpower.