Skip to content

Commit

Permalink
replaced atomic bool with crossbeam atomic cell
Browse files Browse the repository at this point in the history
  • Loading branch information
Gal Ben David committed Apr 27, 2021
1 parent 1ad89d1 commit 94ad1e3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
22 changes: 9 additions & 13 deletions src/git_repository_scanner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::rules_manager;

use chrono::prelude::*;
use crossbeam_utils::atomic::AtomicCell;
use crossbeam_utils::thread as crossbeam_thread;
use crossbeam::queue::SegQueue;
use git2::{Oid, Repository, Delta};
Expand All @@ -9,12 +10,11 @@ use pyo3::prelude::*;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time;

fn scan_commit_oid(
should_stop: &AtomicBool,
should_stop: &AtomicCell<bool>,
git_repo: &Repository,
oid: &Oid,
rules_manager: &rules_manager::RulesManager,
Expand All @@ -39,7 +39,7 @@ fn scan_commit_oid(
};

for delta in commit_diff.deltas() {
if should_stop.load(Ordering::Relaxed) {
if should_stop.load() {
break;
}

Expand Down Expand Up @@ -80,7 +80,6 @@ fn scan_commit_oid(
if let Some(scan_matches) = scan_matches {
for scan_match in scan_matches.iter() {
let mut match_hashmap = HashMap::with_capacity(9);
let commit_date = Utc.timestamp(commit.time().seconds(), 0);
match_hashmap.insert(
"commit_id",
commit.id().to_string(),
Expand All @@ -91,7 +90,7 @@ fn scan_commit_oid(
);
match_hashmap.insert(
"commit_time",
commit_date.format("%Y-%m-%dT%H:%M:%S").to_string(),
Utc.timestamp(commit.time().seconds(), 0).format("%Y-%m-%dT%H:%M:%S").to_string(),
);
match_hashmap.insert(
"author_name",
Expand Down Expand Up @@ -185,22 +184,19 @@ pub fn scan_repository(
return Err(pyo3::exceptions::PyRuntimeError::new_err(error.to_string()))
},
}

py.check_signals()?;

let mut py_signal_error: PyResult<()> = Ok(());

let should_stop = AtomicCell::new(false);
crossbeam_thread::scope(
|scope| {
let should_stop = Arc::new(AtomicBool::new(false));

for _ in 0..num_cpus::get() {
let output_matches = output_matches.clone();
let oids_queue = oids_queue.clone();
let should_stop = should_stop.clone();
scope.spawn(
move |_| {
|_| {
if let Ok(git_repo) = Repository::open(repository_path) {
while !should_stop.load(Ordering::Relaxed) {
while !should_stop.load() {
if let Some(oid) = oids_queue.pop() {
scan_commit_oid(
&should_stop,
Expand All @@ -221,7 +217,7 @@ pub fn scan_repository(
while !oids_queue.is_empty() {
py_signal_error = py.check_signals();
if py_signal_error.is_err() {
should_stop.store(true, Ordering::Relaxed);
should_stop.store(true);

break;
}
Expand Down
22 changes: 9 additions & 13 deletions src/rules_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,21 @@ impl RulesManager {
&self,
file_path: &str,
) -> bool {
let skip_file = self.file_extensions_to_skip.iter().any(
if self.file_extensions_to_skip.iter().any(
|file_extension_to_skip| {
file_path.ends_with(file_extension_to_skip)
}
);
if skip_file {
return false;
}

let skip_file = self.file_paths_to_skip.iter().any(
) {
false
} else if self.file_paths_to_skip.iter().any(
|file_path_to_skip| {
file_path.contains(file_path_to_skip)
}
);
if skip_file {
return false;
) {
false
} else {
true
}

true
}

#[text_signature = "(file_path, content, /)"]
Expand All @@ -213,7 +209,7 @@ impl RulesManager {
let mut scan_matches = Vec::new();

for file_path_rule in self.file_path_rules.iter() {
for _match_text in file_path_rule.regex.find_iter(file_path) {
if file_path_rule.regex.is_match(file_path) {
let mut scan_match = HashMap::<&str, String>::new();
scan_match.insert("rule_name", file_path_rule.name.clone());
scan_match.insert("match_text", file_path.to_string());
Expand Down

0 comments on commit 94ad1e3

Please sign in to comment.