about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2022-11-04 15:44:59 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2022-11-04 15:44:59 +0100
commit09b038e8da139fe306abdf6592a8fa83c0892eaa (patch)
tree582c1c6d6535965713e2343090fcda443f062b12
parentIndexer: Add missing /search/ route (diff)
downloadOSSE-09b038e8da139fe306abdf6592a8fa83c0892eaa.tar.gz
OSSE-09b038e8da139fe306abdf6592a8fa83c0892eaa.tar.bz2
OSSE-09b038e8da139fe306abdf6592a8fa83c0892eaa.zip
Indexer: Make & implement the trait insert() taking a [word] for insert
This has the advantage of taking less calls to the insert() and being
able to add all the logic previous to inertion to the actual Indexer
implementation.
-rw-r--r--indexer/src/indexer_implementation.rs31
-rw-r--r--indexer/src/main.rs20
2 files changed, 24 insertions, 27 deletions
diff --git a/indexer/src/indexer_implementation.rs b/indexer/src/indexer_implementation.rs
index d5cfead..e3f0495 100644
--- a/indexer/src/indexer_implementation.rs
+++ b/indexer/src/indexer_implementation.rs
@@ -28,27 +28,28 @@ impl IndexerImplementation {
 impl crate::Indexer for IndexerImplementation {
     fn insert(
         &mut self,
-        word: &str,
+        words: &[String],
         url: &str,
         title: Option<String>,
         description: Option<String>,
         content: &str,
-        fixed_words: &[String],
     ) -> Result<(), String> {
-        let resource_to_add = IndexedResource {
-            url: url.to_string(),
-            priority: Self::calculate_word_priority(word, content, fixed_words),
-            word: Arc::new(word.to_string()),
-            title: title.map(String::from),
-            description: description.map(String::from),
-        };
+        for word in words {
+            let resource_to_add = IndexedResource {
+                url: url.to_string(),
+                priority: Self::calculate_word_priority(word, content, words),
+                word: Arc::new(word.to_string()),
+                title: title.as_ref().map(String::from),
+                description: description.as_ref().map(String::from),
+            };
 
-        match self.database.get_mut(word) {
-            Some(resources) => _ = resources.insert(resource_to_add),
-            None => {
-                _ = self
-                    .database
-                    .insert(word.to_string(), HashSet::from([resource_to_add]))
+            match self.database.get_mut(word) {
+                Some(resources) => _ = resources.insert(resource_to_add),
+                None => {
+                    _ = self
+                        .database
+                        .insert(word.to_string(), HashSet::from([resource_to_add]))
+                }
             }
         }
 
diff --git a/indexer/src/main.rs b/indexer/src/main.rs
index 51a0e20..dcb4b9a 100644
--- a/indexer/src/main.rs
+++ b/indexer/src/main.rs
@@ -12,12 +12,11 @@ pub trait Indexer {
     //too many args?
     fn insert(
         &mut self,
-        word: &str,
+        words: &[String],
         url: &str,
         title: Option<String>,
         description: Option<String>,
         content: &str,
-        fixed_words: &[String],
     ) -> Result<(), String>;
     fn search(&self, term: &str) -> Result<HashSet<IndexedResource>, String>;
     fn num_of_words(&self) -> usize;
@@ -112,16 +111,13 @@ async fn add_resource(
 
     //and for each changed content word we add it to the db (word -> list.append(url))
     let mut indexer = data.indexer.lock().unwrap();
-    for word in &fixed_words {
-        let _ = indexer.insert(
-            word,
-            &resource.url,
-            page_title.clone(),
-            page_description.clone(),
-            &resource.content,
-            &fixed_words,
-        );
-    }
+    let _ = indexer.insert(
+        &fixed_words,
+        &resource.url,
+        page_title.clone(),
+        page_description.clone(),
+        &resource.content,
+    );
 
     //TODO: ADD LANG? EN in meta tag (frontend)