1919
2020use arrow_array:: RecordBatch ;
2121use arrow_ipc:: writer:: StreamWriter ;
22- use lazy_static :: lazy_static ;
22+ use once_cell :: sync :: Lazy ;
2323use std:: borrow:: Borrow ;
2424use std:: collections:: HashMap ;
25+ use std:: fmt:: { self , Debug , Formatter } ;
2526use std:: fs:: { File , OpenOptions } ;
2627use std:: io:: Write ;
28+ use std:: ops:: { Deref , DerefMut } ;
2729use std:: sync:: { Mutex , RwLock } ;
2830
2931use crate :: storage:: StorageDir ;
@@ -33,21 +35,44 @@ use self::errors::StreamWriterError;
3335type ArrowWriter < T > = StreamWriter < T > ;
3436type LocalWriter < T > = Mutex < Option < ArrowWriter < T > > > ;
3537
36- lazy_static ! {
37- #[ derive( Default ) ]
38- pub static ref STREAM_WRITERS : RwLock <WriterTable <String , String , File >> = RwLock :: new( WriterTable :: new( ) ) ;
38+ pub static STREAM_WRITERS : Lazy < InnerStreamWriter > =
39+ Lazy :: new ( || InnerStreamWriter ( RwLock :: new ( WriterTable :: new ( ) ) ) ) ;
40+
41+ /*
42+ A wrapper type for global struct to implement methods over
43+ */
44+ pub struct InnerStreamWriter ( RwLock < WriterTable < String , String , File > > ) ;
45+
46+ impl Deref for InnerStreamWriter {
47+ type Target = RwLock < WriterTable < String , String , File > > ;
48+ fn deref ( & self ) -> & Self :: Target {
49+ & self . 0
50+ }
51+ }
52+ impl DerefMut for InnerStreamWriter {
53+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
54+ & mut self . 0
55+ }
56+ }
57+ /*
58+ Manually implmenting for the Type
59+ since it depends on the types which are missing it
60+ */
61+ impl Debug for InnerStreamWriter {
62+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
63+ f. write_str ( "InnerStreamWriter { __private_field: () }" )
64+ }
3965}
4066
41- impl STREAM_WRITERS {
67+ impl InnerStreamWriter {
4268 // append to a existing stream
4369 pub fn append_to_local (
70+ & self ,
4471 stream : & str ,
4572 schema_key : & str ,
4673 record : & RecordBatch ,
4774 ) -> Result < ( ) , StreamWriterError > {
48- let hashmap_guard = STREAM_WRITERS
49- . read ( )
50- . map_err ( |_| StreamWriterError :: RwPoisoned ) ?;
75+ let hashmap_guard = self . read ( ) . map_err ( |_| StreamWriterError :: RwPoisoned ) ?;
5176
5277 match hashmap_guard. get ( stream, schema_key) {
5378 Some ( localwriter) => {
@@ -71,7 +96,7 @@ impl STREAM_WRITERS {
7196 None => {
7297 // this requires mutable borrow of the map so we drop this read lock and wait for write lock
7398 drop ( hashmap_guard) ;
74- STREAM_WRITERS :: create_entry ( stream. to_owned ( ) , schema_key. to_owned ( ) , record) ?;
99+ self . create_entry ( stream. to_owned ( ) , schema_key. to_owned ( ) , record) ?;
75100 }
76101 } ;
77102 Ok ( ( ) )
@@ -80,13 +105,12 @@ impl STREAM_WRITERS {
80105 // create a new entry with new stream_writer
81106 // Only create entry for valid streams
82107 fn create_entry (
108+ & self ,
83109 stream : String ,
84110 schema_key : String ,
85111 record : & RecordBatch ,
86112 ) -> Result < ( ) , StreamWriterError > {
87- let mut hashmap_guard = STREAM_WRITERS
88- . write ( )
89- . map_err ( |_| StreamWriterError :: RwPoisoned ) ?;
113+ let mut hashmap_guard = self . write ( ) . map_err ( |_| StreamWriterError :: RwPoisoned ) ?;
90114
91115 let writer = init_new_stream_writer_file ( & stream, & schema_key, record) ?;
92116
@@ -95,14 +119,12 @@ impl STREAM_WRITERS {
95119 Ok ( ( ) )
96120 }
97121
98- pub fn delete_stream ( stream : & str ) {
99- STREAM_WRITERS . write ( ) . unwrap ( ) . delete_stream ( stream) ;
122+ pub fn delete_stream ( & self , stream : & str ) {
123+ self . write ( ) . unwrap ( ) . delete_stream ( stream) ;
100124 }
101125
102- pub fn unset_all ( ) -> Result < ( ) , StreamWriterError > {
103- let table = STREAM_WRITERS
104- . read ( )
105- . map_err ( |_| StreamWriterError :: RwPoisoned ) ?;
126+ pub fn unset_all ( & self ) -> Result < ( ) , StreamWriterError > {
127+ let table = self . read ( ) . map_err ( |_| StreamWriterError :: RwPoisoned ) ?;
106128
107129 for writer in table. iter ( ) {
108130 if let Some ( mut streamwriter) = writer
0 commit comments