-
Notifications
You must be signed in to change notification settings - Fork 35
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
Adds lazy reader support for blobs #629
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
e0a83d8
Top-level nulls, bools, ints
zslayton 89f79aa
Consolidate impls of AsUtf8 w/helper fn
zslayton 840be4d
Improved TextBufferView docs, removed DataSource
zslayton 5db1ff0
Adds lazy text floats
zslayton 07d4a70
Adds LazyRawTextReader support for comments
zslayton 181e0a5
Adds LazyRawTextReader support for reading strings
zslayton 357ca8f
clippy fixes
zslayton 716ff34
Fix a couple of unit tests
zslayton e29fec5
Less ambitious float eq comparison
zslayton 8f79a36
Adds LazyRawTextReader support for reading symbols
zslayton 4cb9b2b
Adds more doc comments
zslayton 54470d2
More doc comments
zslayton 78014e7
Adds `LazyRawTextReader` support for reading lists
zslayton a6a3aa8
Adds `LazyRawTextReader` support for structs
zslayton 4fc9078
More doc comments
zslayton 11174ac
Adds `LazyRawTextReader` support for reading IVMs
zslayton 719dbaa
Initial impl of a LazyRawAnyReader
zslayton f603872
Improved comments.
zslayton 4696ca5
Adds LazyRawTextReader support for annotations
zslayton c7129ac
Adds lazy reader support for timestamps
zslayton 44435ea
Lazy reader support for s-expressions
zslayton d50e05b
Fixed doc comments
zslayton 8283422
Fix internal doc link
zslayton 0f01099
Adds lazy reader support for decimals
zslayton b60f1fe
Fixed bad unit test example case
zslayton 915c83a
clippy fixes
zslayton fe922ff
Adds lazy reader support for blobs
zslayton 4b53bb3
Merge remote-tracking branch 'origin/main' into lazy-timestamps
zslayton 60d5a17
Incorporates review feedback
zslayton db9718d
Matcher recognizes +00:00 as Zulu
zslayton 37264a3
Merge remote-tracking branch 'origin/lazy-timestamps' into lazy-sexps
zslayton 74b8baf
Merge remote-tracking branch 'origin/lazy-sexps' into lazy-decimals
zslayton d716c9f
Merge remote-tracking branch 'origin/lazy-decimals' into lazy-blobs
zslayton 8cecdda
Allow blobs with interleaved whitespace
zslayton b28df56
clippy suggestions
zslayton 8ea5c9d
Merge remote-tracking branch 'origin/main' into lazy-blobs
zslayton 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 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 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,123 @@ | ||
use crate::text::text_formatter::IonValueFormatter; | ||
use crate::Bytes; | ||
use std::borrow::Cow; | ||
use std::fmt::{Debug, Display, Formatter}; | ||
use std::ops::Deref; | ||
|
||
pub struct BytesRef<'data> { | ||
data: Cow<'data, [u8]>, | ||
} | ||
|
||
impl<'data> Deref for BytesRef<'data> { | ||
type Target = [u8]; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.data.as_ref() | ||
} | ||
} | ||
|
||
impl<'data> BytesRef<'data> { | ||
pub fn to_owned(&self) -> Bytes { | ||
Bytes::from(self.as_ref()) | ||
} | ||
|
||
pub fn into_owned(self) -> Bytes { | ||
Bytes::from(self) | ||
} | ||
|
||
pub fn data(&self) -> &[u8] { | ||
self.as_ref() | ||
} | ||
} | ||
|
||
impl<'data> From<BytesRef<'data>> for Bytes { | ||
fn from(value: BytesRef<'data>) -> Self { | ||
match value.data { | ||
Cow::Borrowed(bytes) => Bytes::from(bytes), | ||
Cow::Owned(bytes) => Bytes::from(bytes), | ||
} | ||
} | ||
} | ||
|
||
impl<'data, const N: usize> From<&'data [u8; N]> for BytesRef<'data> { | ||
fn from(bytes: &'data [u8; N]) -> Self { | ||
BytesRef { | ||
data: Cow::from(bytes.as_ref()), | ||
} | ||
} | ||
} | ||
|
||
impl<'data> From<&'data [u8]> for BytesRef<'data> { | ||
fn from(bytes: &'data [u8]) -> Self { | ||
BytesRef { | ||
data: Cow::from(bytes), | ||
} | ||
} | ||
} | ||
|
||
impl<'data> From<Vec<u8>> for BytesRef<'data> { | ||
fn from(bytes: Vec<u8>) -> Self { | ||
BytesRef { | ||
data: Cow::from(bytes), | ||
} | ||
} | ||
} | ||
|
||
impl<'data> From<&'data str> for BytesRef<'data> { | ||
fn from(text: &'data str) -> Self { | ||
BytesRef { | ||
data: Cow::from(text.as_bytes()), | ||
} | ||
} | ||
} | ||
|
||
impl<'data> PartialEq<[u8]> for BytesRef<'data> { | ||
fn eq(&self, other: &[u8]) -> bool { | ||
self.data() == other | ||
} | ||
} | ||
|
||
impl<'data> PartialEq<&[u8]> for BytesRef<'data> { | ||
fn eq(&self, other: &&[u8]) -> bool { | ||
self.data() == *other | ||
} | ||
} | ||
|
||
impl<'data> PartialEq<BytesRef<'data>> for [u8] { | ||
fn eq(&self, other: &BytesRef<'data>) -> bool { | ||
self == other.data() | ||
} | ||
} | ||
|
||
impl<'a, 'b> PartialEq<BytesRef<'a>> for BytesRef<'b> { | ||
fn eq(&self, other: &BytesRef<'a>) -> bool { | ||
self == other.data() | ||
} | ||
} | ||
|
||
impl<'data> Display for BytesRef<'data> { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
let mut formatter = IonValueFormatter { output: f }; | ||
formatter | ||
.format_blob(self.data()) | ||
.map_err(|_| std::fmt::Error) | ||
} | ||
} | ||
|
||
impl<'data> Debug for BytesRef<'data> { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
const NUM_BYTES_TO_SHOW: usize = 32; | ||
let data = self.data.as_ref(); | ||
// Shows up to the first 32 bytes in hex | ||
write!(f, "BytesRef: [")?; | ||
for byte in data.iter().copied().take(NUM_BYTES_TO_SHOW) { | ||
write!(f, "{:x} ", byte)?; | ||
} | ||
if data.len() > NUM_BYTES_TO_SHOW { | ||
write!(f, "...{} more", (data.len() - NUM_BYTES_TO_SHOW))?; | ||
} | ||
write!(f, "]")?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains 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 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 @@ | ||
use crate::lazy::bytes_ref::BytesRef; | ||
use crate::lazy::decoder::LazyDecoder; | ||
use crate::lazy::str_ref::StrRef; | ||
use crate::result::IonFailure; | ||
|
@@ -18,7 +19,7 @@ pub enum RawValueRef<'data, D: LazyDecoder<'data>> { | |
Timestamp(Timestamp), | ||
String(StrRef<'data>), | ||
Symbol(RawSymbolTokenRef<'data>), | ||
Blob(&'data [u8]), | ||
Blob(BytesRef<'data>), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗺️ |
||
Clob(&'data [u8]), | ||
SExp(D::SExp), | ||
List(D::List), | ||
|
@@ -140,7 +141,7 @@ impl<'data, D: LazyDecoder<'data>> RawValueRef<'data, D> { | |
} | ||
} | ||
|
||
pub fn expect_blob(self) -> IonResult<&'data [u8]> { | ||
pub fn expect_blob(self) -> IonResult<BytesRef<'data>> { | ||
if let RawValueRef::Blob(b) = self { | ||
Ok(b) | ||
} else { | ||
|
@@ -247,7 +248,7 @@ mod tests { | |
); | ||
assert_eq!( | ||
reader.next()?.expect_value()?.read()?.expect_blob()?, | ||
&[0x06, 0x5A, 0x1B] // Base64-decoded "Blob" | ||
[0x06u8, 0x5A, 0x1B].as_ref() // Base64-decoded "Blob" | ||
); | ||
assert_eq!( | ||
reader.next()?.expect_value()?.read()?.expect_clob()?, | ||
|
This file contains 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 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
Oops, something went wrong.
Oops, something went wrong.
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.
🗺️ When there was only a binary reader, methods that read blobs could always return a
&[u8]
--a slice of the input buffer. Now that we also have a text reader, we need to accommodate base64-encoded blobs, which always require a newVec
to be allocated to hold the decoded data.BytesRef
can hold either a borrowed&[u8]
or an ownedVec<u8>
, allowing it to be used in either situation.This type is analogous to
StrRef
andSymbolRef
but for blobs.