Skip to content

Commit

Permalink
feat: return number of occurrences (#37)
Browse files Browse the repository at this point in the history
* return count

* return occurence

* support occurence

* Update main.rs

* why Result?

* Update squid.proto
  • Loading branch information
RealHinome authored Mar 10, 2024
1 parent c1ea97c commit 3cbb51a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 33 deletions.
4 changes: 2 additions & 2 deletions squid-algorithm/src/hashtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ impl MapAlgorithm {
}

/// Classify the most frequently used words.
pub fn rank(&self, length: usize) -> Vec<String> {
pub fn rank(&self, length: usize) -> Vec<(String, usize)> {
let mut sorted_word_counts: Vec<_> =
self.data.clone().into_iter().collect();
sorted_word_counts.sort_by(|a, b| b.1.cmp(&a.1));

let most_used_words: Vec<_> = sorted_word_counts
.iter()
.take(length)
.map(|(word, _count)| word.clone())
.map(|(word, count)| (word.clone(), *count))
.collect();

most_used_words
Expand Down
4 changes: 2 additions & 2 deletions squid/proto/squid.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ message LeaderboardRequest {
// The sentence added to the entrie and its lifetime.
message AddRequest {
string sentence = 1;
uint64 lifetime = 2;
uint64 lifetime = 2;
}

// Representation of a word.
Expand All @@ -38,5 +38,5 @@ message Word {

// List of ranked most used words.
message Ranking {
Word word = 1;
repeated Word word = 1;
}
7 changes: 3 additions & 4 deletions squid/src/helpers/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use squid_algorithm::hashtable::MapAlgorithm;
use squid_db::Instance;

/// The algorithms managed by Squid.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum Algorithm {
Map(MapAlgorithm),
}
Expand Down Expand Up @@ -37,11 +37,10 @@ pub fn set<A: Into<Algorithm>>(

/// Rank the most used words.
pub fn rank<A: Into<Algorithm>>(
instance: &mut Instance<Entity>,
algorithm: A,
length: usize,
) -> Result<Vec<String>> {
) -> Vec<(String, usize)> {
match algorithm.into() {
Algorithm::Map(implementation) => Ok(implementation.rank(length)),
Algorithm::Map(implementation) => implementation.rank(length),
}
}
41 changes: 16 additions & 25 deletions squid/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use squid::{
squid_server::{Squid, SquidServer},
{AddRequest, LeaderboardRequest, Ranking, Void, Word},
};
use squid_tokenizer::tokenize;
use std::time::{SystemTime, UNIX_EPOCH};
use tonic::{transport::Server, Request, Response, Status};

Expand All @@ -16,6 +17,7 @@ pub mod squid {

struct SuperSquid {
algorithm: helpers::database::Algorithm,
instance: squid_db::Instance<models::database::Entity>,
}

#[tonic::async_trait]
Expand All @@ -25,16 +27,22 @@ impl Squid for SuperSquid {
request: Request<LeaderboardRequest>,
) -> Result<Response<Ranking>, Status> {
Ok(Response::new(Ranking {
word: Some(Word {
word: "test".to_string(),
occurence: 0,
}),
word: helpers::database::rank(
self.algorithm.clone(),
request.into_inner().length as usize,
)
.iter()
.map(|(word, occurence)| Word {
word: word.to_string(),
occurence: (*occurence).try_into().unwrap_or_default(),
})
.collect::<Vec<_>>(),
}))
}

async fn add(
&self,
request: Request<AddRequest>,
_request: Request<AddRequest>,
) -> Result<Response<Void>, Status> {
Ok(Response::new(Void {}))
}
Expand Down Expand Up @@ -69,7 +77,7 @@ async fn main() {
let config = helpers::config::read();

// Start database.
let mut db_instance: squid_db::Instance<models::database::Entity> =
let db_instance: squid_db::Instance<models::database::Entity> =
squid_db::Instance::new().unwrap();
log::info!(
"Loaded instance with {} entities.",
Expand All @@ -85,24 +93,6 @@ async fn main() {
}
}

let start = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_micros();

let rank = algo.rank(3);

println!("Rank of the 3 most used words: {:?}", rank);

println!(
"Took {}μs",
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_micros()
- start
);

let addr = format!("0.0.0.0:{}", config.port.unwrap_or(50051))
.parse()
.unwrap();
Expand All @@ -112,6 +102,7 @@ async fn main() {
Server::builder()
.add_service(SquidServer::new(SuperSquid {
algorithm: helpers::database::Algorithm::Map(algo),
instance: db_instance,
}))
.serve(addr)
.await
Expand Down

0 comments on commit 3cbb51a

Please sign in to comment.