Skip to content

Commit e842201

Browse files
committed
chore: writer docs
1 parent 1df4982 commit e842201

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ The Avro specification provides two kinds of encoding:
6464

6565
This crate implements only the binary encoding as that's the format practically used for performance and storage reasons.
6666

67-
## Features.
67+
## Features
6868

6969
* Full support for recursive self-referential schemas with Serde serialization/deserialization.
7070
* All compressions codecs (`deflate`, `bzip2`, `snappy`, `xz`, `zstd`) supported as per spec.
@@ -139,7 +139,7 @@ fn main() -> Result<(), Error> {
139139

140140
```
141141

142-
A more involved self-referential recursive schema example:
142+
### Self-referential recursive schema example
143143

144144
```rust
145145
use anyhow::Error;
@@ -202,7 +202,7 @@ fn main() -> Result<(), Error> {
202202

203203
```
204204

205-
An example of writing a json object with a confirming schema. The json object maps to an `avrow::Record` type.
205+
### An example of writing a json object with a confirming schema. The json object maps to the `avrow::Record` type.
206206

207207
```rust
208208
use anyhow::Error;

src/writer.rs

+20-21
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
//! The Writer is the primary interface for writing values in avro encoded format.
22
3-
use rand::{thread_rng, Rng};
4-
53
use crate::codec::Codec;
64
use crate::schema::Schema;
75
use crate::value::Value;
8-
use serde::Serialize;
9-
106
use crate::config::{DEFAULT_FLUSH_INTERVAL, MAGIC_BYTES, SYNC_MARKER_SIZE};
117
use crate::error::{AvrowErr, AvrowResult};
128
use crate::schema::Registry;
139
use crate::schema::Variant;
1410
use crate::serde_avro;
1511
use crate::util::{encode_long, encode_raw_bytes};
1612
use crate::value::Map;
13+
use serde::Serialize;
14+
use rand::{thread_rng, Rng};
1715
use std::collections::HashMap;
1816
use std::default::Default;
1917
use std::io::Write;
@@ -34,7 +32,7 @@ pub struct WriterBuilder<'a, W> {
3432
}
3533

3634
impl<'a, W: Write> WriterBuilder<'a, W> {
37-
/// Creates a builder instance to construct a Writer
35+
/// Creates a builder instance to construct a Writer.
3836
pub fn new() -> Self {
3937
WriterBuilder {
4038
metadata: Default::default(),
@@ -52,35 +50,34 @@ impl<'a, W: Write> WriterBuilder<'a, W> {
5250
self
5351
}
5452

55-
/// Set one of the available codec. This requires the respective code feature flags to be enabled.
53+
/// Set one of the available codecs. This requires the respective feature flags to be enabled.
5654
pub fn set_codec(mut self, codec: Codec) -> Self {
5755
self.codec = codec;
5856
self
5957
}
6058

61-
/// Provide the writer with a reference to the schema file
59+
/// Provide the writer with a reference to the schema file.
6260
pub fn set_schema(mut self, schema: &'a Schema) -> Self {
6361
self.schema = Some(schema);
6462
self
6563
}
6664

67-
/// Set the underlying output stream. This can be anything which implements the Write trait.
65+
/// Set the underlying output stream. This can be any type that implements the `Write` trait.
6866
pub fn set_datafile(mut self, w: W) -> Self {
6967
self.datafile = Some(w);
7068
self
7169
}
7270

73-
/// Set the flush interval (bytes) for the internal block buffer. It's the amount of bytes post which
74-
/// the internal buffer is written to the underlying datafile. Defaults to [DEFAULT_FLUSH_INTERVAL].
71+
/// Set the flush interval (in bytes) for the internal buffer. It's the amount of bytes post which
72+
/// the internal buffer is written to the underlying datafile or output stream..
73+
/// Defaults to [`DEFAULT_FLUSH_INTERVAL`](config/constant.DEFAULT_FLUSH_INTERVAL.html).
7574
pub fn set_flush_interval(mut self, interval: usize) -> Self {
7675
self.flush_interval = interval;
7776
self
7877
}
7978

80-
/// Builds the Writer instance consuming this builder.
79+
/// Builds the `Writer` instance consuming this builder.
8180
pub fn build(self) -> AvrowResult<Writer<'a, W>> {
82-
// write the metadata
83-
// Writer::with_codec(&self.schema, self.datafile, self.codec)
8481
let mut writer = Writer {
8582
out_stream: self.datafile.ok_or(AvrowErr::WriterBuildFailed)?,
8683
schema: self.schema.ok_or(AvrowErr::WriterBuildFailed)?,
@@ -103,7 +100,7 @@ impl<'a, W: Write> Default for WriterBuilder<'a, W> {
103100

104101
/// The Writer is the primary interface for writing values to an avro datafile or a byte container (say a `Vec<u8>`).
105102
/// It takes a reference to the schema for validating the values being written
106-
/// and an output stream W which can be any type
103+
/// and an output stream `W` which can be any type
107104
/// implementing the [Write](https://doc.rust-lang.org/std/io/trait.Write.html) trait.
108105
pub struct Writer<'a, W> {
109106
out_stream: W,
@@ -116,7 +113,8 @@ pub struct Writer<'a, W> {
116113
}
117114

118115
impl<'a, W: Write> Writer<'a, W> {
119-
/// Creates a new avro Writer instance taking a reference to a `Schema` and and a `Write`.
116+
/// Creates a new avro `Writer` instance taking a reference to a `Schema`
117+
/// and a type implementing [`Write`](https://doc.rust-lang.org/std/io/trait.Write.html).
120118
pub fn new(schema: &'a Schema, out_stream: W) -> AvrowResult<Self> {
121119
let mut writer = Writer {
122120
out_stream,
@@ -131,8 +129,8 @@ impl<'a, W: Write> Writer<'a, W> {
131129
Ok(writer)
132130
}
133131

134-
/// Same as the new method, but additionally takes a `Codec` as parameter.
135-
/// Codecs can be used to compress the encoded data being written in avro format.
132+
/// Same as the `new` method, but additionally takes a `Codec` as parameter.
133+
/// Codecs can be used to compress the encoded data being written in an avro datafile.
136134
/// Supported codecs as per spec are:
137135
/// * null (default): No compression is applied.
138136
/// * [snappy](https://en.wikipedia.org/wiki/Snappy_(compression)) (`--features snappy`)
@@ -157,7 +155,9 @@ impl<'a, W: Write> Writer<'a, W> {
157155
/// Appends a value to the buffer.
158156
/// Before a value gets written, it gets validated with the schema referenced
159157
/// by this writer.
160-
/// **Note**: writes are buffered internally as per the flush interval and the underlying
158+
///
159+
/// # Note:
160+
/// writes are buffered internally as per the flush interval (for performance) and the underlying
161161
/// buffer may not reflect values immediately.
162162
/// Call [`flush`](struct.Writer.html#method.flush) to explicitly write all buffered data.
163163
/// Alternatively calling [`into_inner`](struct.Writer.html#method.into_inner) on the writer
@@ -194,7 +194,6 @@ impl<'a, W: Write> Writer<'a, W> {
194194
}
195195

196196
/// Sync/flush any buffered data to the underlying buffer.
197-
/// Note: This method is called to ensure that all
198197
pub fn flush(&mut self) -> AvrowResult<()> {
199198
// bail if no data is written or it has already been flushed before
200199
if self.block_count == 0 {
@@ -248,8 +247,8 @@ impl<'a, W: Write> Writer<'a, W> {
248247
Ok(())
249248
}
250249

251-
/// Consumes self and yields the inner Write instance.
252-
/// Additionally calls flush if no flush has happened before this call.
250+
/// Consumes self and yields the inner `Write` instance.
251+
/// Additionally calls `flush` if no flush has happened before this call.
253252
pub fn into_inner(mut self) -> AvrowResult<W> {
254253
self.flush()?;
255254
Ok(self.out_stream)

0 commit comments

Comments
 (0)