-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[AVRO-3506] [rust] Single object writer #1672
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
Changes from 16 commits
1bcdadb
afee9fd
a89010d
a67a35c
d955204
8141d2e
6821a02
de03d26
b0e3e22
15b91c5
cf9f394
b4663b3
2b91fcf
061b89d
8a971d2
7588433
8041112
3aeceae
c6ac535
65f8d0e
3aa2c37
db855f6
7679228
0d5a022
b465b39
7f82a81
328f590
16efcc3
9556eb4
bfc5165
bd3307f
2fff83d
6ede153
25c43c1
73d22fa
8c9f3fa
7e7e286
80ecb27
9cb9275
faa355c
1d59139
d0b679c
7d060ab
b44b520
19bd662
95643ae
31c4bc3
cddea87
ddbb8cc
7ab95ec
0aa6da2
4731dee
b63eacb
8f9dd6f
7735001
5f109d6
18ddaf5
6545a4b
91f75be
adc8add
01600ec
94a4be0
8640187
77de905
651f769
47d0f82
a31a195
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
|
|
||
| package org.apache.avro.message; | ||
|
|
||
| import org.apache.avro.Schema; | ||
| import org.apache.avro.generic.GenericData; | ||
| import org.apache.avro.generic.GenericRecordBuilder; | ||
| import org.junit.AfterClass; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.FileOutputStream; | ||
| import java.io.FileReader; | ||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.file.Files; | ||
| import java.util.Arrays; | ||
| import java.util.logging.Logger; | ||
|
|
||
| public class TestInteropMessageData { | ||
| private static final String inDir = System.getProperty("share.dir", "../../../share") + "/test/data/messageV1"; | ||
| private static final File SCHEMA_FILE = new File(inDir + "/test_schema.avsc"); | ||
| private static final File MESSAGE_FILE = new File(inDir + "/test_message.bin"); | ||
| private static Schema SCHEMA; | ||
| private static GenericRecordBuilder BUILDER; | ||
|
|
||
| @BeforeClass | ||
| public static void setup() throws IOException { | ||
| final FileInputStream fileInputStream = new FileInputStream(SCHEMA_FILE); | ||
| SCHEMA = new Schema.Parser().parse(fileInputStream); | ||
| BUILDER = new GenericRecordBuilder(SCHEMA); | ||
| fileInputStream.close(); | ||
| } | ||
|
|
||
| @Test | ||
| public void sanity_check() throws IOException { | ||
| MessageEncoder<GenericData.Record> encoder = new BinaryMessageEncoder<>(GenericData.get(), SCHEMA); | ||
| ByteBuffer buffer = encoder.encode( | ||
| BUILDER.set("id", 42L).set("name", "Bill").set("tags", Arrays.asList("dog_lover", "cat_hater")).build()); | ||
| byte[] fileBuffer = Files.readAllBytes(MESSAGE_FILE.toPath()); | ||
| Assert.assertArrayEquals(fileBuffer, buffer.array()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you 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. | ||
|
|
||
| use apache_avro::{schema::AvroSchema, types::Value}; | ||
|
|
||
| struct InteropMessage; | ||
|
|
||
| impl AvroSchema for InteropMessage { | ||
| fn get_schema() -> apache_avro::Schema { | ||
| let schema = std::fs::read_to_string("../../share/test/data/messageV1/test_schema.avsc") | ||
| .expect("File should exist with schema inside"); | ||
| apache_avro::Schema::parse_str(schema.as_str()) | ||
| .expect("File should exist with schema inside") | ||
| } | ||
| } | ||
|
|
||
| impl From<InteropMessage> for Value { | ||
| fn from(_: InteropMessage) -> Value { | ||
| Value::Record(vec![ | ||
| ("id".into(), 42i64.into()), | ||
| ("name".into(), "Bill".into()), | ||
| ( | ||
| "tags".into(), | ||
| Value::Array( | ||
| vec!["dog_lover", "cat_hater"] | ||
| .into_iter() | ||
| .map(|s| s.into()) | ||
| .collect(), | ||
| ), | ||
| ), | ||
| ]) | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| let file_message = std::fs::read("../../share/test/data/messageV1/test_message.bin") | ||
| .expect("File not found or error reading"); | ||
| let mut generated_encoding: Vec<u8> = Vec::new(); | ||
| apache_avro::SingleObjectWriter::<InteropMessage>::with_capacity(1024) | ||
| .expect("resolve expected") | ||
| .write_value(InteropMessage, &mut generated_encoding) | ||
| .expect("Should encode"); | ||
| assert_eq!(file_message, generated_encoding) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -434,6 +434,86 @@ impl<'s> ResolvedSchema<'s> { | |
| } | ||
| } | ||
|
|
||
| pub(crate) struct ResolvedOwnedSchema { | ||
|
Member
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. Can we avoid the code duplication somehow ?
Contributor
Author
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. Im not sure. The ResolvedSchema has that lifetime that Im not sure how to associate with another field in the struct. |
||
| names: Names, | ||
| root_schema: Schema, | ||
| } | ||
|
|
||
| impl<'s> TryFrom<Schema> for ResolvedOwnedSchema { | ||
| type Error = Error; | ||
|
|
||
| fn try_from(schema: Schema) -> AvroResult<Self> { | ||
| let names = HashMap::new(); | ||
| let mut rs = ResolvedOwnedSchema { | ||
| names, | ||
| root_schema: schema, | ||
| }; | ||
| Self::from_internal(&rs.root_schema, &mut rs.names, &None)?; | ||
| Ok(rs) | ||
| } | ||
| } | ||
|
|
||
| impl ResolvedOwnedSchema { | ||
| pub(crate) fn get_root_schema(&self) -> &Schema { | ||
| &self.root_schema | ||
| } | ||
| pub(crate) fn get_names(&self) -> &Names { | ||
| &self.names | ||
| } | ||
|
|
||
| fn from_internal( | ||
| schema: &Schema, | ||
| names: &mut Names, | ||
| enclosing_namespace: &Namespace, | ||
| ) -> AvroResult<()> { | ||
| match schema { | ||
| Schema::Array(schema) | Schema::Map(schema) => { | ||
| Self::from_internal(schema, names, enclosing_namespace) | ||
| } | ||
| Schema::Union(UnionSchema { schemas, .. }) => { | ||
| for schema in schemas { | ||
| Self::from_internal(schema, names, enclosing_namespace)? | ||
| } | ||
| Ok(()) | ||
| } | ||
| Schema::Enum { name, .. } | Schema::Fixed { name, .. } => { | ||
| let fully_qualified_name = name.fully_qualified_name(enclosing_namespace); | ||
| if names | ||
| .insert(fully_qualified_name.clone(), schema.clone()) | ||
| .is_some() | ||
| { | ||
| Err(Error::AmbiguousSchemaDefinition(fully_qualified_name)) | ||
| } else { | ||
| Ok(()) | ||
| } | ||
| } | ||
| Schema::Record { name, fields, .. } => { | ||
| let fully_qualified_name = name.fully_qualified_name(enclosing_namespace); | ||
| if names | ||
| .insert(fully_qualified_name.clone(), schema.clone()) | ||
| .is_some() | ||
| { | ||
| Err(Error::AmbiguousSchemaDefinition(fully_qualified_name)) | ||
| } else { | ||
| let record_namespace = fully_qualified_name.namespace; | ||
| for field in fields { | ||
| Self::from_internal(&field.schema, names, &record_namespace)? | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
| Schema::Ref { name } => { | ||
| let fully_qualified_name = name.fully_qualified_name(enclosing_namespace); | ||
| names | ||
| .get(&fully_qualified_name) | ||
| .map(|_| ()) | ||
| .ok_or(Error::SchemaResolutionError(fully_qualified_name)) | ||
| } | ||
| _ => Ok(()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Represents a `field` in a `record` Avro schema. | ||
| #[derive(Clone, Debug, PartialEq)] | ||
| pub struct RecordField { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.