1
1
use crate :: packet:: { Blob , BlobError , BLOB_HEADER_SIZE } ;
2
2
use crate :: result:: Error as SErr ;
3
3
4
- use byteorder:: { BigEndian , ByteOrder } ;
4
+ use byteorder:: { BigEndian , ByteOrder , WriteBytesExt } ;
5
5
6
6
use std:: borrow:: Borrow ;
7
7
use std:: collections:: HashMap ;
@@ -17,7 +17,8 @@ const INDEX_FILE_NAME: &str = "index";
17
17
pub ( super ) const ERASURE_FILE_NAME : & str = "erasure" ;
18
18
pub ( super ) const ERASURE_INDEX_FILE_NAME : & str = "erasure_index" ;
19
19
20
- const DATA_FILE_BUF_SIZE : usize = 64 * 1024 * 32 ;
20
+ //const DATA_FILE_BUF_SIZE: usize = 2 * 1024 * 1024;
21
+ const DATA_FILE_BUF_SIZE : usize = 64 * 1024 ;
21
22
22
23
pub ( super ) const INDEX_RECORD_SIZE : u64 = 3 * 8 ;
23
24
@@ -133,7 +134,6 @@ impl Store {
133
134
{
134
135
let mut blobs: Vec < _ > = iter. into_iter ( ) . collect ( ) ;
135
136
assert ! ( !blobs. is_empty( ) ) ;
136
- println ! ( "insert_blobs: inserting {} blobs" , blobs. len( ) ) ;
137
137
138
138
// sort on lexi order (slot_idx, blob_idx)
139
139
// TODO: this sort may cause panics while malformed blobs result in `Result`s elsewhere
@@ -160,14 +160,7 @@ impl Store {
160
160
. or_insert ( index..( index + 1 ) ) ;
161
161
}
162
162
163
- println ! ( "insert_blobs: arranged slots and stuff" ) ;
164
-
165
163
for ( slot, range) in slot_ranges {
166
- println ! ( "insert_blobs: slot = {}" , slot) ;
167
- println ! (
168
- "insert_blobs: range.start = {}, range.end = {}" ,
169
- range. start, range. end
170
- ) ;
171
164
let slot_path = self . mk_slot_path ( slot) ;
172
165
ensure_slot ( & slot_path) ?;
173
166
@@ -177,18 +170,6 @@ impl Store {
177
170
slot_path. join ( store_impl:: INDEX_FILE_NAME ) ,
178
171
) ;
179
172
180
- println ! (
181
- "insert_blobs: paths: data = {}" ,
182
- data_path. to_string_lossy( )
183
- ) ;
184
- println ! (
185
- "insert_blobs: paths: meta = {}" ,
186
- meta_path. to_string_lossy( )
187
- ) ;
188
- println ! (
189
- "insert_blobs: paths: index = {}" ,
190
- index_path. to_string_lossy( )
191
- ) ;
192
173
// load meta_data
193
174
let ( mut meta_file, mut meta) = if meta_path. exists ( ) {
194
175
let mut f = OpenOptions :: new ( ) . read ( true ) . write ( true ) . open ( & meta_path) ?;
@@ -207,46 +188,40 @@ impl Store {
207
188
)
208
189
} ;
209
190
210
- println ! ( "insert_blobs: loaded meta data: {:?}" , meta) ;
191
+ let slot_blobs = & blobs[ range] ;
192
+ let mut idx_buf = Vec :: with_capacity ( slot_blobs. len ( ) * INDEX_RECORD_SIZE as usize ) ;
211
193
212
194
let mut data_wtr =
213
195
BufWriter :: with_capacity ( DATA_FILE_BUF_SIZE , open_append ( & data_path) ?) ;
214
- let mut index_wtr = BufWriter :: new ( open_append ( & index_path) ?) ;
215
- let slot_blobs = & blobs[ range] ;
216
- let mut blob_indices = Vec :: with_capacity ( slot_blobs. len ( ) ) ;
217
-
218
- let mut idx_buf = [ 0u8 ; INDEX_RECORD_SIZE as usize ] ;
196
+ let mut offset = data_wtr. seek ( SeekFrom :: Current ( 0 ) ) ?;
197
+ let mut blob_slices_to_write = Vec :: with_capacity ( slot_blobs. len ( ) ) ;
219
198
220
199
for blob in slot_blobs {
221
200
let blob = blob. borrow ( ) ;
222
201
let blob_index = blob. index ( ) . map_err ( bad_blob) ?;
223
202
let blob_size = blob. size ( ) . map_err ( bad_blob) ?;
224
203
225
- let offset = data_wtr. seek ( SeekFrom :: Current ( 0 ) ) ?;
226
- // TODO: blob.size() is wrong here, but should be right
227
- let serialized_blob_datas = & blob. data [ ..BLOB_HEADER_SIZE + blob_size] ;
204
+ let serialized_blob_data = & blob. data [ ..BLOB_HEADER_SIZE + blob_size] ;
228
205
let serialized_entry_data = & blob. data [ BLOB_HEADER_SIZE ..] ;
229
206
let entry: Entry = bincode:: deserialize ( serialized_entry_data)
230
207
. expect ( "Blobs must be well formed by the time they reach the ledger" ) ;
231
208
232
- data_wtr . write_all ( & serialized_blob_datas ) ? ;
233
- let data_len = serialized_blob_datas . len ( ) as u64 ;
209
+ blob_slices_to_write . push ( serialized_blob_data ) ;
210
+ let data_len = serialized_blob_data . len ( ) as u64 ;
234
211
235
212
let blob_idx = BlobIndex {
236
213
index : blob_index,
237
214
size : data_len,
238
215
offset,
239
216
} ;
240
217
241
- BigEndian :: write_u64 ( & mut idx_buf[ 0 ..8 ] , blob_idx. index ) ;
242
- BigEndian :: write_u64 ( & mut idx_buf[ 8 ..16 ] , blob_idx. offset ) ;
243
- BigEndian :: write_u64 ( & mut idx_buf[ 16 ..24 ] , blob_idx. size ) ;
218
+ offset += data_len;
244
219
245
- // update index file
246
- // TODO: batch writes for outer loop
247
- blob_indices . push ( blob_idx) ;
248
- println ! ( "insert_blobs: blob_idx = {:?}" , blob_idx) ;
249
- index_wtr . write_all ( & idx_buf ) ?;
220
+ // Write indices to buffer, which will be written to index file
221
+ // in the outer (per-slot) loop
222
+ idx_buf . write_u64 :: < BigEndian > ( blob_idx. index ) ? ;
223
+ idx_buf . write_u64 :: < BigEndian > ( blob_idx. offset ) ? ;
224
+ idx_buf . write_u64 :: < BigEndian > ( blob_idx . size ) ?;
250
225
251
226
// update meta. write to file once in outer loop
252
227
if blob_index > meta. received {
@@ -263,9 +238,15 @@ impl Store {
263
238
}
264
239
}
265
240
266
- println ! ( "insert_blobs: saving meta data: {:?}" , meta) ;
241
+ let mut index_wtr = BufWriter :: new ( open_append ( & index_path) ?) ;
242
+
243
+ // write blob slices
244
+ for slice in blob_slices_to_write {
245
+ data_wtr. write_all ( slice) ?;
246
+ }
267
247
268
248
bincode:: serialize_into ( & mut meta_file, & meta) ?;
249
+ index_wtr. write_all ( & idx_buf) ?;
269
250
270
251
data_wtr. flush ( ) ?;
271
252
let data_f = data_wtr. into_inner ( ) ?;
@@ -298,7 +279,7 @@ impl Store {
298
279
let index_rdr = File :: open ( & index_path) ?;
299
280
300
281
let index_size = index_rdr. metadata ( ) ?. len ( ) ;
301
- println ! ( "slot_data: index_size = {}" , index_size ) ;
282
+
302
283
let mut index_rdr = BufReader :: new ( index_rdr) ;
303
284
let mut buf = [ 0u8 ; INDEX_RECORD_SIZE as usize ] ;
304
285
let mut blob_indices: Vec < BlobIndex > =
@@ -321,7 +302,6 @@ impl Store {
321
302
}
322
303
323
304
blob_indices. sort_unstable_by_key ( |bix| bix. index ) ;
324
- println ! ( "slot_data: blob_indices: {:#?}" , blob_indices) ;
325
305
326
306
let data_rdr = BufReader :: new ( File :: open ( & data_path) ?) ;
327
307
0 commit comments