Skip to content

Commit 6fd3fda

Browse files
committed
Define read_cache_data_map_file in Rust
This is not much more performant in itself, but we might as well move this down before trying to optimize.
1 parent 800a8a3 commit 6fd3fda

3 files changed

Lines changed: 61 additions & 2 deletions

File tree

rust/src/caching.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use crate::errors::{GrimpError, GrimpResult};
2+
use crate::filesystem::get_file_system_boxed;
3+
use crate::import_scanning::{DirectImport, imports_by_module_to_py};
4+
use crate::module_finding::Module;
5+
use pyo3::types::PyDict;
6+
use pyo3::{Bound, PyAny, PyResult, Python, pyfunction};
7+
use std::collections::{HashMap, HashSet};
8+
9+
/// Reads the cache file containing all the imports for a given package.
10+
/// Args:
11+
/// - filename: str
12+
/// - file_system: The file system interface to use. (A BasicFileSystem.)
13+
/// Returns Dict[Module, Set[DirectImport]]
14+
#[pyfunction]
15+
pub fn read_cache_data_map_file<'py>(
16+
py: Python<'py>,
17+
filename: &str,
18+
file_system: Bound<'py, PyAny>,
19+
) -> PyResult<Bound<'py, PyDict>> {
20+
let file_system_boxed = get_file_system_boxed(&file_system)?;
21+
22+
let file_contents = file_system_boxed.read(filename)?;
23+
24+
let imports_by_module = parse_json_to_map(&file_contents, filename)?;
25+
26+
Ok(imports_by_module_to_py(py, imports_by_module))
27+
}
28+
29+
pub fn parse_json_to_map(
30+
json_str: &str,
31+
filename: &str,
32+
) -> GrimpResult<HashMap<Module, HashSet<DirectImport>>> {
33+
let raw_map: HashMap<String, Vec<(String, usize, String)>> = serde_json::from_str(json_str)
34+
.map_err(|_| GrimpError::CorruptCache(filename.to_string()))?;
35+
36+
let mut parsed_map: HashMap<Module, HashSet<DirectImport>> = HashMap::new();
37+
38+
for (module_name, imports) in raw_map {
39+
let module = Module {
40+
name: module_name.clone(),
41+
};
42+
let import_set: HashSet<DirectImport> = imports
43+
.into_iter()
44+
.map(|(imported, line_number, line_contents)| DirectImport {
45+
importer: module_name.clone(),
46+
imported,
47+
line_number,
48+
line_contents,
49+
})
50+
.collect();
51+
parsed_map.insert(module, import_set);
52+
}
53+
54+
Ok(parsed_map)
55+
}

rust/src/import_scanning.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use serde::Deserialize;
12
use crate::errors::GrimpResult;
23
use crate::filesystem::FileSystem;
34
use crate::{import_parsing, module_finding};
@@ -10,7 +11,7 @@ use rayon::prelude::*;
1011
use std::collections::{HashMap, HashSet};
1112
use std::io::{self, ErrorKind};
1213

13-
#[derive(Debug, Hash, Eq, PartialEq)]
14+
#[derive(Debug, Hash, Eq, PartialEq, Deserialize)]
1415
pub struct DirectImport {
1516
pub importer: String,
1617
pub imported: String,

rust/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod caching;
12
pub mod errors;
23
pub mod exceptions;
34
mod filesystem;
@@ -7,13 +8,14 @@ mod import_scanning;
78
pub mod module_expressions;
89
mod module_finding;
910

11+
use crate::caching::read_cache_data_map_file;
1012
use crate::errors::{GrimpError, GrimpResult};
1113
use crate::exceptions::{CorruptCache, InvalidModuleExpression, ModuleNotPresent, NoSuchContainer, ParseError};
1214
use crate::filesystem::{PyFakeBasicFileSystem, PyRealBasicFileSystem};
1315
use crate::graph::higher_order_queries::Level;
1416
use crate::graph::{Graph, Module, ModuleIterator, ModuleTokenIterator};
1517
use crate::import_scanning::{
16-
py_found_packages_to_rust, scan_for_imports_no_py, to_py_direct_imports,
18+
py_found_packages_to_rust, scan_for_imports_no_py,
1719
};
1820
use crate::module_expressions::ModuleExpression;
1921
use derive_new::new;
@@ -30,6 +32,7 @@ use std::collections::HashSet;
3032
#[pymodule]
3133
fn _rustgrimp(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
3234
m.add_wrapped(wrap_pyfunction!(scan_for_imports))?;
35+
m.add_wrapped(wrap_pyfunction!(read_cache_data_map_file))?;
3336
m.add_class::<GraphWrapper>()?;
3437
m.add_class::<PyRealBasicFileSystem>()?;
3538
m.add_class::<PyFakeBasicFileSystem>()?;

0 commit comments

Comments
 (0)