Skip to content

Commit

Permalink
More debugging and query func
Browse files Browse the repository at this point in the history
  • Loading branch information
krshrimali committed Nov 8, 2024
2 parents 64d2de4 + 8a935d1 commit 59aa5b2
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 47 deletions.
56 changes: 55 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use std::{collections::HashMap, fs::File, path::Path};

// use simple_home_dir::home_dir;
Expand Down Expand Up @@ -51,20 +52,22 @@ impl DB {
self.index = *valid_index;

self.db_file_path = format!("{}/{}.json", self.folder_path, self.index);
println!("Reading: {:?}", self.db_file_path);
let db_obj = Path::new(&self.db_file_path);
if !db_obj.exists() {
File::create(db_obj).expect("Couldn't find the DB file");
return HashMap::new();
}
let current_data = self.read();
let values = current_data.get(&self.curr_file_path);
println!("Values: {:?}", values);
if let Some(valid_values) = values {
let mut default: HashMap<u32, Vec<AuthorDetails>> = HashMap::new();
let insert_data_here = init_data
.get_mut(&self.curr_file_path)
.unwrap_or(&mut default);
insert_data_here.extend(valid_values.clone());
let copy_data = insert_data_here.clone();
let copy_data = insert_data_here.clone();
init_data.insert(self.curr_file_path.clone(), copy_data);
}
}
Expand Down Expand Up @@ -105,7 +108,9 @@ impl DB {
let db_file_index = self.find_index(curr_file_path);
// Filename will be: <db_file_index>.json
let valid_indices = db_file_index.unwrap_or(vec![self.index]);
println!("Valid indices: {:?}", valid_indices);
self.current_data = self.read_all(valid_indices.clone());
println!("{:?}", self.current_data);
}

fn find_index(&mut self, curr_file_path: &str) -> Option<Vec<u32>> {
Expand Down Expand Up @@ -273,6 +278,8 @@ impl DB {
) -> (Option<Vec<&AuthorDetails>>, Vec<u32>) {
let mut already_computed_data: Vec<&AuthorDetails> = vec![];
let mut uncovered_indices: Vec<u32> = vec![];
println!("{:?}", search_field_first);
println!("{:?}", self.current_data);
if self.current_data.contains_key(search_field_first) {
let output = self.current_data.get_mut(search_field_first);
if let Some(all_line_data) = output {
Expand All @@ -297,6 +304,53 @@ impl DB {
(None, uncovered_indices)
}
}

pub fn query(
&mut self,
file_path: String,
start_number: usize,
end_number: usize,
// current_data: HashMap<String, Vec<AuthorDetails>>,
) {
self.init_db(file_path.as_str());
println!("File path: {:?}", file_path);
// self.init_db(file_path.as_str());
let mut res = String::new();
let mut visited_count_map: HashMap<String, usize> = HashMap::new();
let (existing_result_optional, _) =
self.exists_and_return(&file_path, &start_number, &end_number);
println!("{:?}", existing_result_optional);
match existing_result_optional {
Some(existing_result) => {
for author_detail in existing_result {
for each_file in author_detail.contextual_file_paths.clone() {
if each_file == file_path {
continue;
}
if visited_count_map.contains_key(&each_file) {
visited_count_map.insert(
each_file.clone(),
visited_count_map.get(&each_file).unwrap() + 1
);
continue;
}
visited_count_map.insert(each_file.clone(), 0);
res.push_str(&each_file);
res.push(',');
}
}
if res.ends_with(',') {
res.pop();
}
println!("Result: {:?}", res);
},
None => {
println!("Hi");
todo!()
}
}
return;
}
}

mod test {
Expand Down
138 changes: 92 additions & 46 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl Server {
final_authordetails
}

pub async fn start_file(&mut self, _: &mut DBMetadata, _: Option<PathBuf>) {
pub async fn start_file(&mut self, _: &mut DBMetadata, _: Option<String>) {
return;
}

Expand Down Expand Up @@ -254,16 +254,14 @@ impl Server {
.await;
}

fn init_db(&mut self, metadata: &mut DBMetadata, file_path: Option<PathBuf>) {

}

async fn query_db(&mut self, metadata: &mut DBMetadata, file_path: Option<PathBuf>) {
// Query the DB that was initiated in init_db.
self.db.query(file_path);
}

pub async fn handle_server(&mut self, workspace_path: &str, file_path: Option<PathBuf>, mode: RequestTypeOptions) {
pub async fn handle_server(
&mut self,
workspace_path: &str,
file_path: Option<String>,
start_number: Option<usize>,
end_number: Option<usize>,
request_type: RequestTypeOptions,
) {
// this will initialise any required states
self.state_db_handler.init(workspace_path);

Expand All @@ -280,7 +278,7 @@ impl Server {
// the server is not in running state -> for state of it's attempting to start -> let it finish and then see if it was successful
// TODO: @krshrimali
self.state_db_handler.start(&metadata);
tasks.push(self.start_file(&mut metadata, file_path));
tasks.push(self.start_file(&mut metadata, file_path.clone()));
} else if metadata.state == State::Failed {
// in case of failure though, ideally it would have been alr handled by other process -> but in any case, starting from here as well to just see how it works out
// I'm in the favor of not restarting in case of failure from another process though
Expand All @@ -289,16 +287,39 @@ impl Server {
} else if metadata.state == State::Dead {
// should be blocking rn
self.state_db_handler.start(&metadata);
if mode == RequestTypeOptions::Index {
if request_type == RequestTypeOptions::Index {
self.start_indexing(&mut metadata).await;
} else if request_type == RequestTypeOptions::Query {
// Then you query
assert!(file_path.is_some());
assert!(start_number.is_some());
assert!(end_number.is_some());
let f_path_string = file_path.clone().unwrap();
// start the server for the given workspace
// TODO: see if you just want to pass the workspace path and avoiding passing the whole metadata here
let workspace_path = &metadata.workspace_path;

// the server will start going through all the "valid" files in the workspace and will index them
// defn of valid: files that satisfy .gitignore check (if exists and not disabled in the config)
// and files that are not in the ignore list if provided in the config

// let workspace_path_buf = PathBuf::from(workspace_path);

let db = DB {
folder_path: workspace_path.clone(),
..Default::default()
};
let curr_db: Arc<Mutex<DB>> = Arc::new(db.into());
// curr_db.lock().unwrap().init_db(workspace_path.as_str());
// let mut server = Server::new(State::Dead, DBHandler::new(metadata.clone()));
// server.init_server(curr_db.clone());
curr_db.lock().unwrap().query(
f_path_string,
start_number.unwrap(),
end_number.unwrap(),
);
} else {
if mode == RequestTypeOptions::Query {
// just call what we used to call for `File` and `Author`.
self.init_db(&mut metadata, file_path);
self.query_db(&mut metadata, file_path).await;
} else {
self.start_file(&mut metadata, file_path).await;
}
self.start_file(&mut metadata, file_path).await;
}
}
}
Expand All @@ -320,34 +341,59 @@ async fn main() -> CliResult {

// TODO: Add support for config file.
// let config_obj: config_impl::Config = config_impl::read_config(config::CONFIG_FILE_NAME);
let mut file_path: Option<PathBuf> = None;
let mut file_path: Option<String> = None;
if args.file.is_some() {
file_path = PathBuf::from_str(args.file.unwrap().as_str())
.unwrap()
.into();
file_path = Some(args.file.unwrap());
// file_path = PathBuf::from_str(args.file.unwrap().as_str())
// .unwrap()
// .into();
}

server.handle_server(args.folder_path.as_str(), file_path, args.request_type).await;

// match args.request_type {
// RequestTypeOptions::File => {
// server
// .handle_server(args.folder_path.as_str(), file_path)
// .await;
// }
// RequestTypeOptions::Author => {
// server
// .handle_server(args.folder_path.as_str(), file_path)
// .await;
// }
// RequestTypeOptions::Query => {
// server
// .handle_server(args.folder_path.as_str(), file_path, args.request_type)
// .await;
// }
// RequestTypeOptions::Index => {
// server.handle_server(args.folder_path.as_str(), None).await;
// }
// };
match args.request_type {
RequestTypeOptions::File => {
server
.handle_server(
args.folder_path.as_str(),
file_path,
None,
None,
args.request_type,
)
.await;
}
RequestTypeOptions::Author => {
server
.handle_server(
args.folder_path.as_str(),
file_path,
None,
None,
args.request_type,
)
.await;
}
RequestTypeOptions::Index => {
server
.handle_server(
args.folder_path.as_str(),
file_path,
None,
None,
args.request_type,
)
.await;
}
RequestTypeOptions::Query => {
server
.handle_server(
args.folder_path.as_str(),
file_path,
args.start_number,
args.end_number,
args.request_type,
)
.await;
}
};
Ok(())
}

0 comments on commit 59aa5b2

Please sign in to comment.