consistent_hasher 0.1.5

An implementation of consistent hashing, a technique commonly used in distributed systems to map keys (such as data items or requests) to nodes (e.g., servers or storage units) in a way that minimizes disruptions when nodes are added or removed.
Documentation
// use std::io;
// use consistent_hasher::CircularBTreeMap;

// fn main() {
//     let mut x: CircularBTreeMap<usize> = CircularBTreeMap::new();

//     loop {

//         let mut input = String::new();
//         io::stdin().read_line(&mut input).expect("Failed to read line");

//         // Trim whitespace and check if the user wants to quit
//         let input = input.trim();
//         if input == "q" {
//             break;
//         }

//         // Parse input and perform insertions
//         if let Some(stripped) = input.strip_prefix("n ") {
//             if let Ok(num) = stripped.trim().parse::<usize>() {
                
//                 println!("Inserted node: {:?}", x.insert_node(num));
//             } else {
//                 println!("Invalid number for insert_node.");
//             }
//         } else if let Some(stripped) = input.strip_prefix("d ") {
//             if let Ok(num) = stripped.trim().parse::<usize>() {
                
//                 println!("Deleted node: {:?}", x.delete_node(&num));
//             } else {
//                 println!("Invalid number for insert_node.");
//             }
//         }

//         else {
//             println!("Invalid input. Please enter a number or 'n + number'.");
//         }

//         // Print the tree after each insertion
//         x.print();
//         println!();
//     }

//     // Display final tree details
//     println!("Final tree nodes count: {} ", x.nodes_count());
// }

use std::io;
use consistent_hasher::{Identifier, LDB}; // Ensure `Transaction` is in scope
use std::time::Instant;
struct X {
    pub x: usize,
}

impl X {
    pub fn new(x: usize) -> Self {
        Self { x }
    }
}

impl Identifier for X {
    fn identify(&self) -> usize {
        self.x
    }
}

fn main() {
    // let time = Instant::now();
     let mut db = LDB::new(7,4); // Adjust parameters as needed
    // for i in 0..10000
    // {
    //     let _=db.add_node(X::new(i));
    // }
    // println!("time-elapsed: {:?}",time.elapsed());
    loop {
        let mut input = String::new();
        io::stdin().read_line(&mut input).expect("Failed to read line");

        let input = input.trim();
        if input == "q" {
            break;
        }

        if let Some(stripped) = input.strip_prefix("n ") {
            if let Ok(num) = stripped.trim().parse::<usize>() {
                let result = db.add_node(X::new(num)); // Adding node
                match result {
                    Ok(Some(transactions)) => {
                        println!("Added node {}. Transactions: ", num);
                        for (i, transaction) in transactions.iter().enumerate() {
                            println!("Transaction {}: {}", i + 1, transaction);
                        }
                    }
                    Ok(None) => println!("Node {} already exists.", num),
                    Err(e) => println!("Error: {:?}", e),
                }
            } else {
                println!("Invalid node number.");
            }
        } else if let Some(stripped) = input.strip_prefix("d ") {
            if let Ok(num) = stripped.trim().parse::<usize>() {
                let result = db.delete_node(X::new(num)); // Deleting node
                match result {
                    Ok(Some(transactions)) => {
                        println!("Deleted node {}. Transactions: ", num, );
                        for (i, transaction) in transactions.iter().enumerate() {
                            println!("Transaction {}: {}", i + 1, transaction);
                        }
                    }
                    Ok(None) => (),
                    Err(e) => println!("Error: {:?}", e),
                }
            } else {
                println!("Invalid node number.");
            }
        }  else if let Some(stripped) = input.strip_prefix("vi ") {
            if let Ok(num) = stripped.trim().parse::<usize>() {
                let result = db.set_virtual_instances(num); // Setting virtual instances
                match result {
                    Some(transactions) => {
                        for (i, transaction) in transactions.iter().enumerate() {
                            println!("Transaction {}: {}", i + 1, transaction);
                        }
                    }
                   None => println!("No change in virtual instances or invalid count."),

                }
            } else {
                println!("Invalid number of virtual instances.");
            }}
            else if let Some(stripped) = input.strip_prefix("s ") {
                if let Ok(num) = stripped.trim().parse::<String>(){
                    let result = db.key(&num); // Setting virtual instances
                    match result {
                        Ok((x,y)) => {
                           
                                println!("go to :{x} with hash:{y}");

                        }
                       Err(e) => println!("Error:{}",e.to_string()),
    
                    }
                } else {
                    println!("Invalid number of virtual instances.");
                }}
                
         else {
            println!("Invalid command. Commands are:");
            println!(" - n <node number>   : Add node");
            println!(" - d <node number>   : Delete node");
            println!(" - vi <count>        : Set virtual instances");
        }

        db.print();
    }

    println!("Final state:");
    db.print();
}