Skip to content
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

partial parsing #28

Merged
merged 3 commits into from
Dec 13, 2022
Merged

Conversation

poncito
Copy link
Contributor

@poncito poncito commented Nov 25, 2022

I'd like to be able to partially parse a document.
I would need to read a header before knowing the type I should use to parse the payload.
Partial parsing would allow me to avoid type instabillity.

struct MyStruct{T}
    field_encoding_the_type::Int
    payload::T
end

x = reader[MyStruct{AbstractBSONReader}]
if x.field_encoding_the_type == 0
    payload = x.payload[T] # using the corresponding type
    ...
elseif....
end

@@ -147,7 +147,25 @@ end
writer[] = x
close(writer)
reader = BSONReader(buf)
reader[typeof(x)] == x
@test reader[typeof(x)] == x
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing @test?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it

@codecov-commenter
Copy link

codecov-commenter commented Nov 25, 2022

Codecov Report

Merging #28 (78f582c) into master (69e129a) will decrease coverage by 1.04%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #28      +/-   ##
==========================================
- Coverage   89.88%   88.84%   -1.05%     
==========================================
  Files          13       13              
  Lines         811      753      -58     
==========================================
- Hits          729      669      -60     
- Misses         82       84       +2     
Flag Coverage Δ
unittest 88.84% <100.00%> (-1.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/reader.jl 91.08% <100.00%> (-0.73%) ⬇️
src/object_id.jl 80.00% <0.00%> (-13.03%) ⬇️
src/indexed_reader.jl 92.00% <0.00%> (-4.00%) ⬇️
src/validator.jl 94.44% <0.00%> (-0.91%) ⬇️
src/writer.jl 88.38% <0.00%> (-0.44%) ⬇️
src/index.jl 94.44% <0.00%> (-0.43%) ⬇️
src/convenience.jl 79.31% <0.00%> (+2.64%) ⬆️
src/representations.jl 93.18% <0.00%> (+4.05%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@poncito
Copy link
Contributor Author

poncito commented Nov 25, 2022

julia> struct S{T}
           header::Int
           payload::T
       end
       
       testf(reader) = reader[S{typeof(reader)}].header
       
       buf = UInt8[]
       writer = BSONWriter(buf)
       writer[] = (; header = 1, payload = 45)
       close(writer)
       
       reader = BSONReader(buf)
       @benchmark testf($reader)
       
BenchmarkTools.Trial: 10000 samples with 997 evaluations.
 Range (min  max):  20.186 ns  46.640 ns  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     20.270 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   20.492 ns ±  0.879 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▄█▇▇▅▃▄▁       ▁▃▁▂▁ ▁           ▂▁ ▁▁                      ▂
  ████████▅██▅██▄█████▇██▅▇▆▄▇▇▅▆█▆██▆██▇██▆█▇▅▆▅▄▄▅▃▆▅▁▃▄▁▁▃ █
  20.2 ns      Histogram: log(frequency) by time      22.7 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

@poncito
Copy link
Contributor Author

poncito commented Dec 12, 2022

Hi @ancapdev,
Would you have time to look into this small feature?
Thank you,

@@ -147,7 +147,25 @@ end
writer[] = x
close(writer)
reader = BSONReader(buf)
reader[typeof(x)] == x
@test reader[typeof(x)] == x
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it

src/reader.jl Outdated Show resolved Hide resolved
@ancapdev
Copy link
Owner

Hi @ancapdev, Would you have time to look into this small feature? Thank you,

Been a little snowed under, but this looks good to me, just a tiny formatting change

Co-authored-by: Christian Rorvik <christian.rorvik@gmail.com>
@ancapdev ancapdev merged commit 727e92a into ancapdev:master Dec 13, 2022
@ancapdev
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants