-
Notifications
You must be signed in to change notification settings - Fork 46
implementation of streaming yaml parsing #106
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 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bcf4bb2
draft implementation of streaming yaml parsing
jsvd d576b09
remove instance variable and add yaml_load_strategy
jsvd e16be4d
fix tests and fix non quoted events
jsvd bbb8547
add docs for yaml_load_strategy
jsvd ace3d23
[skip ci] bump to 3.5.0
jsvd c2c4586
remove global java_imports and remove debugging puts
jsvd 3e342fd
Update spec/fixtures/dict.yml
jsvd d0c92ba
Update lib/logstash/filters/dictionary/streaming_yaml_parser.rb
jsvd 226ac12
Update lib/logstash/filters/dictionary/streaming_yaml_parser.rb
jsvd 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
110 changes: 110 additions & 0 deletions
110
lib/logstash/filters/dictionary/streaming_yaml_parser.rb
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,110 @@ | ||
| module LogStash module Filters module Dictionary | ||
| class StreamingYamlDictParser | ||
| def snakeYamlEngineV2 | ||
| Java::org.snakeyaml.engine.v2 | ||
| end | ||
|
|
||
| def snakeYamlEngineV2Events | ||
| Java::org.snakeyaml.engine.v2.events | ||
| end | ||
|
|
||
| def initialize(filename, yaml_code_point_limit) | ||
| settings = snakeYamlEngineV2.api.LoadSettings.builder | ||
| .set_code_point_limit(yaml_code_point_limit) | ||
| .build | ||
|
|
||
| stream = Java::java.io.FileInputStream.new(filename) | ||
| reader = Java::java.io.InputStreamReader.new(stream, Java::java.nio.charset.StandardCharsets::UTF_8) | ||
| stream_reader = snakeYamlEngineV2.scanner.StreamReader.new(reader, settings) | ||
|
|
||
| @parser = snakeYamlEngineV2.parser.ParserImpl.new(stream_reader, settings) | ||
|
|
||
| skip_until(snakeYamlEngineV2Events.MappingStartEvent) | ||
| end | ||
|
|
||
|
|
||
| def each_pair | ||
| while peek_event && !peek_event.is_a?(snakeYamlEngineV2Events.MappingEndEvent) | ||
| key = parse_node | ||
| value = parse_node | ||
| yield(key, value) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def next_event | ||
| @parser.next | ||
| ensure | ||
| nil | ||
| end | ||
|
|
||
| def peek_event | ||
| @parser.peek_event | ||
| end | ||
|
|
||
| def skip_until(event_class) | ||
| while @parser.has_next | ||
| evt = @parser.next | ||
| return if event_class === evt | ||
| end | ||
| end | ||
|
|
||
| def parse_node | ||
| event = next_event | ||
|
|
||
| case event | ||
| when snakeYamlEngineV2Events.ScalarEvent | ||
| parse_scalar(event) | ||
| when snakeYamlEngineV2Events.MappingStartEvent | ||
| parse_mapping | ||
| when snakeYamlEngineV2Events.SequenceStartEvent | ||
| parse_sequence | ||
| else | ||
| raise "Unexpected event: #{event.class}" | ||
| end | ||
| end | ||
|
|
||
| def parse_mapping | ||
| hash = {} | ||
| while peek_event && !peek_event.is_a?(snakeYamlEngineV2Events.MappingEndEvent) | ||
| key = parse_node | ||
| value = parse_node | ||
| hash[key] = value | ||
| end | ||
| next_event | ||
| hash | ||
| end | ||
|
|
||
| def parse_sequence | ||
| array = [] | ||
| while peek_event && !peek_event.is_a?(snakeYamlEngineV2Events.SequenceEndEvent) | ||
| array << parse_node | ||
| end | ||
| next_event | ||
| array | ||
| end | ||
| def parse_scalar(scalar) | ||
jsvd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| value = scalar.value | ||
| # return quoted scalars as they are | ||
| # e.g. don't convert "true" to true | ||
| return value unless scalar.is_plain | ||
|
|
||
| # otherwise let's do some checking and conversions | ||
| case value | ||
| when 'null', '', '~' then nil | ||
| when 'true' then true | ||
| when 'false' then false | ||
| else | ||
| # Try to convert to integer or float | ||
| if value.match?(/\A-?\d+\z/) | ||
| value.to_i | ||
| elsif value.match?(/\A-?\d+\.\d+\z/) | ||
| value.to_f | ||
| else | ||
| value | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end end end | ||
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
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
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| a : 1 | ||
| b : 2 | ||
| c : 3 | ||
| d : { "e": [1, "hello", true, "false", "1", "1.1"] } |
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.
Uh oh!
There was an error while loading. Please reload this page.