1
1
//! The Writer is the primary interface for writing values in avro encoded format.
2
2
3
- use rand:: { thread_rng, Rng } ;
4
-
5
3
use crate :: codec:: Codec ;
6
4
use crate :: schema:: Schema ;
7
5
use crate :: value:: Value ;
8
- use serde:: Serialize ;
9
-
10
6
use crate :: config:: { DEFAULT_FLUSH_INTERVAL , MAGIC_BYTES , SYNC_MARKER_SIZE } ;
11
7
use crate :: error:: { AvrowErr , AvrowResult } ;
12
8
use crate :: schema:: Registry ;
13
9
use crate :: schema:: Variant ;
14
10
use crate :: serde_avro;
15
11
use crate :: util:: { encode_long, encode_raw_bytes} ;
16
12
use crate :: value:: Map ;
13
+ use serde:: Serialize ;
14
+ use rand:: { thread_rng, Rng } ;
17
15
use std:: collections:: HashMap ;
18
16
use std:: default:: Default ;
19
17
use std:: io:: Write ;
@@ -34,7 +32,7 @@ pub struct WriterBuilder<'a, W> {
34
32
}
35
33
36
34
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.
38
36
pub fn new ( ) -> Self {
39
37
WriterBuilder {
40
38
metadata : Default :: default ( ) ,
@@ -52,35 +50,34 @@ impl<'a, W: Write> WriterBuilder<'a, W> {
52
50
self
53
51
}
54
52
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.
56
54
pub fn set_codec ( mut self , codec : Codec ) -> Self {
57
55
self . codec = codec;
58
56
self
59
57
}
60
58
61
- /// Provide the writer with a reference to the schema file
59
+ /// Provide the writer with a reference to the schema file.
62
60
pub fn set_schema ( mut self , schema : & ' a Schema ) -> Self {
63
61
self . schema = Some ( schema) ;
64
62
self
65
63
}
66
64
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.
68
66
pub fn set_datafile ( mut self , w : W ) -> Self {
69
67
self . datafile = Some ( w) ;
70
68
self
71
69
}
72
70
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).
75
74
pub fn set_flush_interval ( mut self , interval : usize ) -> Self {
76
75
self . flush_interval = interval;
77
76
self
78
77
}
79
78
80
- /// Builds the Writer instance consuming this builder.
79
+ /// Builds the ` Writer` instance consuming this builder.
81
80
pub fn build ( self ) -> AvrowResult < Writer < ' a , W > > {
82
- // write the metadata
83
- // Writer::with_codec(&self.schema, self.datafile, self.codec)
84
81
let mut writer = Writer {
85
82
out_stream : self . datafile . ok_or ( AvrowErr :: WriterBuildFailed ) ?,
86
83
schema : self . schema . ok_or ( AvrowErr :: WriterBuildFailed ) ?,
@@ -103,7 +100,7 @@ impl<'a, W: Write> Default for WriterBuilder<'a, W> {
103
100
104
101
/// The Writer is the primary interface for writing values to an avro datafile or a byte container (say a `Vec<u8>`).
105
102
/// 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
107
104
/// implementing the [Write](https://doc.rust-lang.org/std/io/trait.Write.html) trait.
108
105
pub struct Writer < ' a , W > {
109
106
out_stream : W ,
@@ -116,7 +113,8 @@ pub struct Writer<'a, W> {
116
113
}
117
114
118
115
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).
120
118
pub fn new ( schema : & ' a Schema , out_stream : W ) -> AvrowResult < Self > {
121
119
let mut writer = Writer {
122
120
out_stream,
@@ -131,8 +129,8 @@ impl<'a, W: Write> Writer<'a, W> {
131
129
Ok ( writer)
132
130
}
133
131
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 .
136
134
/// Supported codecs as per spec are:
137
135
/// * null (default): No compression is applied.
138
136
/// * [snappy](https://en.wikipedia.org/wiki/Snappy_(compression)) (`--features snappy`)
@@ -157,7 +155,9 @@ impl<'a, W: Write> Writer<'a, W> {
157
155
/// Appends a value to the buffer.
158
156
/// Before a value gets written, it gets validated with the schema referenced
159
157
/// 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
161
161
/// buffer may not reflect values immediately.
162
162
/// Call [`flush`](struct.Writer.html#method.flush) to explicitly write all buffered data.
163
163
/// Alternatively calling [`into_inner`](struct.Writer.html#method.into_inner) on the writer
@@ -194,7 +194,6 @@ impl<'a, W: Write> Writer<'a, W> {
194
194
}
195
195
196
196
/// Sync/flush any buffered data to the underlying buffer.
197
- /// Note: This method is called to ensure that all
198
197
pub fn flush ( & mut self ) -> AvrowResult < ( ) > {
199
198
// bail if no data is written or it has already been flushed before
200
199
if self . block_count == 0 {
@@ -248,8 +247,8 @@ impl<'a, W: Write> Writer<'a, W> {
248
247
Ok ( ( ) )
249
248
}
250
249
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.
253
252
pub fn into_inner ( mut self ) -> AvrowResult < W > {
254
253
self . flush ( ) ?;
255
254
Ok ( self . out_stream )
0 commit comments