[offline] Use buffering for JSON file reading / writing

This commit is contained in:
Jonas Platte
2020-12-29 19:48:42 +01:00
committed by Ryan Leckey
parent 1966bd5aeb
commit 1e71237c04
2 changed files with 11 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ use remove_dir_all::remove_dir_all;
use sqlx::any::{AnyConnectOptions, AnyKind}; use sqlx::any::{AnyConnectOptions, AnyKind};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::fs::File; use std::fs::File;
use std::io::BufWriter;
use std::process::Command; use std::process::Command;
use std::str::FromStr; use std::str::FromStr;
use std::time::SystemTime; use std::time::SystemTime;
@@ -32,7 +33,9 @@ pub fn run(url: &str, merge: bool, cargo_args: Vec<String>) -> anyhow::Result<()
} }
serde_json::to_writer_pretty( serde_json::to_writer_pretty(
File::create("sqlx-data.json").context("failed to create/open `sqlx-data.json`")?, BufWriter::new(
File::create("sqlx-data.json").context("failed to create/open `sqlx-data.json`")?,
),
&DataFile { db: db_kind, data }, &DataFile { db: db_kind, data },
) )
.context("failed to write to `sqlx-data.json`")?; .context("failed to write to `sqlx-data.json`")?;

View File

@@ -40,6 +40,7 @@ pub mod offline {
use std::fmt::{self, Formatter}; use std::fmt::{self, Formatter};
use std::fs::File; use std::fs::File;
use std::io::{BufReader, BufWriter};
use std::path::Path; use std::path::Path;
use proc_macro2::Span; use proc_macro2::Span;
@@ -60,11 +61,11 @@ pub mod offline {
/// Find and deserialize the data table for this query from a shared `sqlx-data.json` /// Find and deserialize the data table for this query from a shared `sqlx-data.json`
/// file. The expected structure is a JSON map keyed by the SHA-256 hash of queries in hex. /// file. The expected structure is a JSON map keyed by the SHA-256 hash of queries in hex.
pub fn from_data_file(path: impl AsRef<Path>, query: &str) -> crate::Result<Self> { pub fn from_data_file(path: impl AsRef<Path>, query: &str) -> crate::Result<Self> {
serde_json::Deserializer::from_reader( serde_json::Deserializer::from_reader(BufReader::new(
File::open(path.as_ref()).map_err(|e| { File::open(path.as_ref()).map_err(|e| {
format!("failed to open path {}: {}", path.as_ref().display(), e) format!("failed to open path {}: {}", path.as_ref().display(), e)
})?, })?,
) ))
.deserialize_map(DataFileVisitor { .deserialize_map(DataFileVisitor {
query, query,
hash: hash_string(query), hash: hash_string(query),
@@ -107,8 +108,10 @@ pub mod offline {
)); ));
serde_json::to_writer_pretty( serde_json::to_writer_pretty(
File::create(&path) BufWriter::new(
.map_err(|e| format!("failed to open path {}: {}", path.display(), e))?, File::create(&path)
.map_err(|e| format!("failed to open path {}: {}", path.display(), e))?,
),
self, self,
) )
.map_err(Into::into) .map_err(Into::into)