1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
use std::{io, path::{PathBuf, Path}, collections::VecDeque, fs, error::Error, fmt::{Display, Formatter}};
use raphtory::core::tgraph_shard::errors::GraphError;
use regex::Regex;
use serde::de::DeserializeOwned;
use rayon::prelude::*;
#[derive(Debug)]
pub enum JsonErr {
/// An IO error that occurred during file read.
IoError(io::Error),
/// A CSV parsing error that occurred while parsing the CSV data.
JsonError(serde_json::Error),
/// A GraphError that occurred while loading the CSV data into the graph.
GraphError(GraphError)
}
impl From<io::Error> for JsonErr {
fn from(value: io::Error) -> Self {
Self::IoError(value)
}
}
impl From<serde_json::Error> for JsonErr {
fn from(value: serde_json::Error) -> Self {
Self::JsonError(value)
}
}
impl From<GraphError> for JsonErr {
fn from(value: GraphError) -> Self {
Self::GraphError(value)
}
}
impl Display for JsonErr {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self.source() {
Some(error) => write!(f, "CSV loader failed with error: {}", error),
None => write!(f, "CSV loader failed with unknown error"),
}
}
}
impl Error for JsonErr {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
JsonErr::IoError(error) => Some(error),
JsonErr::JsonError(error) => Some(error),
JsonErr::GraphError(error) => Some(error),
}
}
}
/// A struct that defines the CSV loader with configurable options.
#[derive(Debug)]
pub struct JsonLinesLoader<REC: DeserializeOwned> {
/// Path of the CSV file or directory containing CSV files.
path: PathBuf,
/// Optional regex filter to select specific CSV files by name.
regex_filter: Option<Regex>,
_a: std::marker::PhantomData<REC>,
}
impl<REC: DeserializeOwned + std::fmt::Debug + std::marker::Sync> JsonLinesLoader<REC> {
/// Creates a new CSV loader with the given path.
pub fn new(path: PathBuf, regex_filter: Option<Regex>) -> Self {
Self {
path,
regex_filter,
_a: std::marker::PhantomData,
}
}
/// Check if the provided path is a directory or not.
///
/// # Arguments
///
/// * `p` - A reference to the path to be checked.
///
/// # Returns
///
/// A Result containing a boolean value indicating whether the path is a directory or not.
///
/// # Errors
///
/// An error of type CsvErr is returned if an I/O error occurs while checking the path.
///
fn is_dir<P: AsRef<Path>>(p: &P) -> Result<bool, JsonErr> {
Ok(fs::metadata(p)?.is_dir())
}
/// Check if a file matches the specified regex pattern and add it to the provided vector of paths.
///
/// # Arguments
///
/// * `path` - The path to the file to be checked.
/// * `paths` - A mutable reference to the vector of paths where the file should be added.
///
/// # Returns
///
/// Nothing is returned, the function only modifies the provided vector of paths.
///
fn accept_file<P: Into<PathBuf>>(&self, path: P, paths: &mut Vec<PathBuf>) {
let p: PathBuf = path.into();
// this is an actual file so push it into the paths vec if it matches the pattern
if let Some(pattern) = &self.regex_filter {
let is_match = &p
.to_str()
.filter(|file_name| pattern.is_match(file_name))
.is_some();
if *is_match {
paths.push(p);
}
} else {
paths.push(p)
}
}
/// Traverse the directory recursively and return a vector of paths to all files in the directory.
///
/// # Arguments
///
/// * No arguments are required.
///
/// # Returns
///
/// A Result containing a vector of PathBuf objects representing the paths to all files in the directory.
///
/// # Errors
///
/// An error of type CsvErr is returned if an I/O error occurs while reading the directory.
///
fn files_vec(&self) -> Result<Vec<PathBuf>, JsonErr> {
let mut paths = vec![];
let mut queue = VecDeque::from([self.path.to_path_buf()]);
while let Some(ref path) = queue.pop_back() {
match fs::read_dir(path) {
Ok(entries) => {
for entry in entries {
if let Ok(f_path) = entry {
let p = f_path.path();
if Self::is_dir(&p)? {
queue.push_back(p.clone())
} else {
self.accept_file(f_path.path(), &mut paths);
}
}
}
}
Err(err) => {
if !Self::is_dir(path)? {
self.accept_file(path.to_path_buf(), &mut paths);
} else {
return Err(err.into());
}
}
}
}
Ok(paths)
}
/// Load data from all JSON files in the directory into a graph.
///
/// # Arguments
///
/// * `g` - A reference to the graph object where the data should be loaded.
/// * `loader` - A closure that takes a deserialized record and the graph object as arguments and adds the record to the graph.
///
/// # Returns
///
/// A Result containing an empty Ok value if the data is loaded successfully.
///
/// # Errors
///
/// An error of type CsvErr is returned if an I/O error occurs while reading the files or parsing the CSV data.
///
pub fn load_into_graph<F, G>(&self, g: &G, loader: F) -> Result<(), JsonErr>
where
REC: DeserializeOwned + std::fmt::Debug,
F: Fn(REC, &G)->Result<(), GraphError> + Send + Sync,
G: Sync,
{
//FIXME: loader function should return a result for reporting parsing errors
let paths = self.files_vec()?;
paths
.par_iter()
.try_for_each(move |path| self.load_file_into_graph(path, g, &loader))?;
Ok(())
}
/// Loads a JSON file into a graph using the specified loader function.
///
/// # Arguments
///
/// * `path` - The path to the JSON file to load.
/// * `g` - A reference to the graph to load the data into.
/// * `loader` - The function to use for loading the CSV records into the graph.
///
/// # Returns
///
/// Returns `Ok(())` if the operation was successful, or a `CsvErr` if there was an error.
///
fn load_file_into_graph<F, P: Into<PathBuf> + std::fmt::Debug, G>(
&self,
path: P,
g: &G,
loader: &F,
) -> Result<(), JsonErr>
where
F: Fn(REC, &G) -> Result<(), GraphError>,
{
let file_path: PathBuf = path.into();
let json_reader = serde_json::Deserializer::from_reader(std::io::BufReader::new(
std::fs::File::open(file_path)?,
));
let records_iter = json_reader.into_iter::<REC>();
//TODO this needs better error handling for files without perfect data
for rec in records_iter {
if let Ok(record) = rec {
loader(record, g)?
} else{
println!("Error parsing record: {:?}", rec);
}
}
Ok(())
}
}