Skip to content

Commit 75abbb2

Browse files
jyn514Joshua Nelson
authored and
Joshua Nelson
committed
Don't read all files into memory at once
1 parent 5e96468 commit 75abbb2

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/storage/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl<'a> Storage<'a> {
101101
let trans = conn.transaction()?;
102102
let mut file_paths_and_mimes = HashMap::new();
103103

104-
get_file_list(root_dir)?
104+
let mut blobs = get_file_list(root_dir)?
105105
.into_iter()
106106
.filter_map(|file_path| {
107107
// Some files have insufficient permissions
@@ -111,7 +111,7 @@ impl<'a> Storage<'a> {
111111
.ok()
112112
.map(|file| (file_path, file))
113113
})
114-
.map(|(file_path, mut file)| {
114+
.map(|(file_path, mut file)| -> Result<_, Error> {
115115
let mut content: Vec<u8> = Vec::new();
116116
file.read_to_end(&mut content)?;
117117

@@ -132,10 +132,17 @@ impl<'a> Storage<'a> {
132132
// this field is ignored by the backend
133133
date_updated: Timespec::new(0, 0),
134134
})
135-
})
136-
.collect::<Result<Vec<_>, Error>>()?
137-
.chunks(MAX_CONCURRENT_UPLOADS)
138-
.try_for_each(|batch| self.store_batch(batch, &trans))?;
135+
});
136+
loop {
137+
let batch: Vec<_> = blobs
138+
.by_ref()
139+
.take(MAX_CONCURRENT_UPLOADS)
140+
.collect::<Result<_, Error>>()?;
141+
if batch.is_empty() {
142+
break;
143+
}
144+
self.store_batch(&batch, &trans)?;
145+
}
139146

140147
trans.commit()?;
141148
Ok(file_paths_and_mimes)

0 commit comments

Comments
 (0)