@@ -3,7 +3,9 @@ mod serde;
33
44use std:: collections:: BTreeSet ;
55use std:: collections:: hash_map:: Entry ;
6+ use std:: io;
67use std:: path:: Path ;
8+ use std:: string:: FromUtf8Error ;
79
810use :: serde:: de:: { self , Deserializer , Error as _} ;
911use :: serde:: ser:: { SerializeSeq , Serializer } ;
@@ -95,21 +97,28 @@ impl SerializedSearchIndex {
9597 ) -> Result < ( ) , Error > {
9698 let root_path = doc_root. join ( format ! ( "search.index/root{resource_suffix}.js" ) ) ;
9799 let column_path = doc_root. join ( format ! ( "search.index/{column_name}/" ) ) ;
100+
101+ struct Consumer < ' col > ( & ' col mut Vec < String > ) ;
102+
103+ impl < ' col > stringdex_internals:: Consumer for Consumer < ' col > {
104+ type Err = FromUtf8Error ;
105+
106+ fn consume ( & mut self , _id : u32 , cell : & [ u8 ] ) -> Result < ( ) , Self :: Err > {
107+ self . 0 . push ( String :: from_utf8 ( cell. to_vec ( ) ) ?) ;
108+ Ok ( ( ) )
109+ }
110+ }
111+
98112 stringdex_internals:: read_data_from_disk_column (
99113 root_path,
100114 column_name. as_bytes ( ) ,
101115 column_path. clone ( ) ,
102- & mut |_id, item| {
103- column. push ( String :: from_utf8 ( item. to_vec ( ) ) ?) ;
104- Ok ( ( ) )
105- } ,
106- )
107- . map_err (
108- |error : stringdex_internals:: ReadDataError < Box < dyn std:: error:: Error > > | Error {
109- file : column_path,
110- error : format ! ( "failed to read column from disk: {error}" ) ,
111- } ,
116+ & mut Consumer ( column) ,
112117 )
118+ . map_err ( |error| Error {
119+ file : column_path,
120+ error : format ! ( "failed to read column from disk: {error}" ) ,
121+ } )
113122 }
114123 fn perform_read_serde (
115124 resource_suffix : & str ,
@@ -119,25 +128,35 @@ impl SerializedSearchIndex {
119128 ) -> Result < ( ) , Error > {
120129 let root_path = doc_root. join ( format ! ( "search.index/root{resource_suffix}.js" ) ) ;
121130 let column_path = doc_root. join ( format ! ( "search.index/{column_name}/" ) ) ;
131+
132+ struct Consumer < ' col , T > ( & ' col mut Vec < Option < T > > ) ;
133+
134+ impl < ' col , T > stringdex_internals:: Consumer for Consumer < ' col , T >
135+ where
136+ T : for < ' de > Deserialize < ' de > + ' static ,
137+ {
138+ type Err = serde_json:: Error ;
139+
140+ fn consume ( & mut self , _id : u32 , cell : & [ u8 ] ) -> Result < ( ) , Self :: Err > {
141+ if cell. is_empty ( ) {
142+ self . 0 . push ( None ) ;
143+ } else {
144+ self . 0 . push ( Some ( serde_json:: from_slice ( cell) ?) ) ;
145+ }
146+ Ok ( ( ) )
147+ }
148+ }
149+
122150 stringdex_internals:: read_data_from_disk_column (
123151 root_path,
124152 column_name. as_bytes ( ) ,
125153 column_path. clone ( ) ,
126- & mut |_id, item| {
127- if item. is_empty ( ) {
128- column. push ( None ) ;
129- } else {
130- column. push ( Some ( serde_json:: from_slice ( item) ?) ) ;
131- }
132- Ok ( ( ) )
133- } ,
134- )
135- . map_err (
136- |error : stringdex_internals:: ReadDataError < Box < dyn std:: error:: Error > > | Error {
137- file : column_path,
138- error : format ! ( "failed to read column from disk: {error}" ) ,
139- } ,
154+ & mut Consumer ( column) ,
140155 )
156+ . map_err ( |error| Error {
157+ file : column_path,
158+ error : format ! ( "failed to read column from disk: {error}" ) ,
159+ } )
141160 }
142161 fn perform_read_postings (
143162 resource_suffix : & str ,
@@ -147,23 +166,30 @@ impl SerializedSearchIndex {
147166 ) -> Result < ( ) , Error > {
148167 let root_path = doc_root. join ( format ! ( "search.index/root{resource_suffix}.js" ) ) ;
149168 let column_path = doc_root. join ( format ! ( "search.index/{column_name}/" ) ) ;
169+
170+ struct Consumer < ' col > ( & ' col mut Vec < Vec < Vec < u32 > > > ) ;
171+
172+ impl < ' col > stringdex_internals:: Consumer for Consumer < ' col > {
173+ type Err = io:: Error ;
174+
175+ fn consume ( & mut self , _id : u32 , cell : & [ u8 ] ) -> Result < ( ) , Self :: Err > {
176+ let mut postings = Vec :: new ( ) ;
177+ encode:: read_postings_from_string ( & mut postings, cell) ;
178+ self . 0 . push ( postings) ;
179+ Ok ( ( ) )
180+ }
181+ }
182+
150183 stringdex_internals:: read_data_from_disk_column (
151184 root_path,
152185 column_name. as_bytes ( ) ,
153186 column_path. clone ( ) ,
154- & mut |_id, buf| {
155- let mut postings = Vec :: new ( ) ;
156- encode:: read_postings_from_string ( & mut postings, buf) ;
157- column. push ( postings) ;
158- Ok ( ( ) )
159- } ,
160- )
161- . map_err (
162- |error : stringdex_internals:: ReadDataError < Box < dyn std:: error:: Error > > | Error {
163- file : column_path,
164- error : format ! ( "failed to read column from disk: {error}" ) ,
165- } ,
187+ & mut Consumer ( column) ,
166188 )
189+ . map_err ( |error| Error {
190+ file : column_path,
191+ error : format ! ( "failed to read column from disk: {error}" ) ,
192+ } )
167193 }
168194
169195 assert_eq ! ( names. len( ) , path_data. len( ) ) ;
0 commit comments