Skip to content

Commit

Permalink
Update main.rs to use hscan...
Browse files Browse the repository at this point in the history
  • Loading branch information
ckcr4lyf committed Nov 30, 2024
1 parent 684e524 commit 632b2eb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
11 changes: 7 additions & 4 deletions examples/redis_hscan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main(){
// insert 10000 peers
let mut base = Instant::now();

for i in 0..1000 {
for i in 0..10 {
let rando: [u8; 6] = rand::thread_rng().gen();
let () = redis_connection.hset(&key, &rando, 1u8).await.expect("failed to hset");
}
Expand All @@ -30,14 +30,17 @@ async fn main(){

base = Instant::now();
let (dudes, count) = kiryuu::db::get_hash_keys_scan_stack(&mut redis_connection, &key, 50).await;
println!("got u8u8 in {}us", base.elapsed().as_micros());
println!("got scan-u8u8 in {}us", base.elapsed().as_micros());
base = Instant::now();
let dudes = kiryuu::db::get_hash_keys_scan(&mut redis_connection, &key, 50).await;
println!("got scan-vecu8 in {}us", base.elapsed().as_micros());

base = Instant::now();
let k: Vec<Vec<u8>> = redis_connection.hkeys(&key).await.expect("failed to hkeys");
println!("got vecvec in {}us", base.elapsed().as_micros());
println!("got hkeys-vecvec in {}us", base.elapsed().as_micros());

base = Instant::now();
let k: Vec<[u8; 6]> = redis_connection.hkeys(&key).await.expect("failed to hkeys");
println!("got vecu8 in {}us", base.elapsed().as_micros());
println!("got hkeys-vecu8 in {}us", base.elapsed().as_micros());

}
7 changes: 4 additions & 3 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use redis::AsyncCommands;
use crate::byte_functions::types;

#[inline(always)]
pub async fn get_hash_keys_scan(rc: &mut redis::aio::MultiplexedConnection, key: &types::RawVal<22>, count: usize) -> Vec<[u8; 6]> {
// TBD: what if its Vec<Vec<u8>> in the context of memory allocation
let mut keys: Vec<[u8; 6]> = Vec::with_capacity(count);
let mut iter = rc.hscan::<&types::RawVal<22>, [u8; 6]>(&key).await.expect("fail to scan");
let mut iter = rc.hscan::<&types::RawVal<22>, ([u8; 6], u8)>(&key).await.expect("fail to scan");

while let Some(element) = iter.next_item().await {
keys.push(element);
while let Some((peer, _)) = iter.next_item().await {
keys.push(peer);

if keys.len() == count {
return keys;
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod req_log;
mod db;

use actix_web::{get, App, HttpServer, web, HttpRequest, HttpResponse, http::header, http::StatusCode, dev::Service};
use db::get_hash_keys_scan;
use std::time::{SystemTime, UNIX_EPOCH};
use clap::Parser;
use std::collections::HashMap;
Expand Down Expand Up @@ -180,8 +181,10 @@ async fn announce(req: HttpRequest, data: web::Data<AppState>) -> HttpResponse {
let mut p = redis::pipe();
// TODO: this will become 2x HKEYS w/o limit (O(log(N) + M) -> O(N)) => This could potentially be a bit of a regression; e.g. we would now get all 1000 IPs from redis (previously limit 50).
// Alternatively: We can HSCAN till 50, which should be very efficient (Basically HSCAN is O(1) per call... , but multiple calls so latency is more? - Need to check).
let pp = p.hkeys(&seeders_key).hkeys(&leechers_key);
let (seeders, leechers) : (Vec<Vec<u8>>, Vec<Vec<u8>>) = trace_wrap_v2!(pp.query_async(&mut rc).await, "redis", "seeders_leechers").unwrap();
// let pp = p.hkeys(&seeders_key).hkeys(&leechers_key);
// let (seeders, leechers) : (Vec<Vec<u8>>, Vec<Vec<u8>>) = trace_wrap_v2!(pp.query_async(&mut rc).await, "redis", "seeders_leechers").unwrap();
let seeders = get_hash_keys_scan(&mut rc, &seeders_key, 50).await;
let leechers = get_hash_keys_scan(&mut rc, &leechers_key, 50).await;

// endex = end index XD. seems in rust cannot select first 50 elements, or limit to less if vector doesnt have 50
// e.g. &seeders[0..50] is panicking when seeders len is < 50. Oh well.
Expand Down
8 changes: 4 additions & 4 deletions src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn parse_announce(ip_addr: &std::net::Ipv4Addr, query: &[u8]) -> Result<Peer
});
}

pub fn announce_reply(seeders_count: i64, leechers_count: i64, seeders: &[Vec<u8>], leechers: &[Vec<u8>]) -> Vec<u8> {
pub fn announce_reply(seeders_count: i64, leechers_count: i64, seeders: &[[u8; 6]], leechers: &[[u8; 6]]) -> Vec<u8> {
// This is the number of peers in the response, not total peer count
let peers_length = seeders.len() + leechers.len();

Expand All @@ -93,13 +93,13 @@ mod tests {

#[test]
fn is_legit(){
let bytes: Vec<u8> = Vec::from([1, 2, 3, 4]);
let bytes = [0u8; 6];
let _no_bytes: Vec<u8> = Vec::from([]);

let mut p1: Vec<Vec<u8>> = Vec::new();
let mut p1: Vec<[u8; 6]> = Vec::new();
p1.push(bytes);

let p2: Vec<Vec<u8>> = Vec::new();
let p2: Vec<[u8; 6]> = Vec::new();
// p2.push(no_bytes);

// TODO: Actually implement a test here...
Expand Down

0 comments on commit 632b2eb

Please sign in to comment.