Trait ids_service::common::Uids

source ·
pub trait Uids {
    type Id;

    fn get_id(&mut self) -> Self::Id { ... }
    fn get_id_from_cache(&mut self) -> Option<Self::Id> { ... }
}
Expand description

Trait with functions to get an id

Required Associated Types§

Provided Methods§

Get an id from cache, if the cache is empty. The function try to create an id on fly.

Examples found in repository?
examples/quickstart.rs (line 35)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
fn main() {

    /*
     * Create an ids service with:
     * Cache size = 100'000
     * hash algo. = sha256
     * A pool of 20 threads
     */
    let mut ids = IdsService::default();
    ids.start();
    // Optional: Wait cache is filled at 10%
    let _ = ids.filled_at_percent_event(10).recv().is_ok();
    println!("Get an id: {}", ids.get_id().as_hex());
    println!("Get another id: {}", ids.get_id().as_base64());
    println!("Get an id from cache: {}", ids.get_id_from_cache().expect("Expect an id").as_hex());
    println!("Current numbers of items in cache: {}", ids.get_cache_len());
    // Graceful Shutdown and Cleanup
    ids.stop();
}
More examples
Hide additional examples
examples/quickstart_rh.rs (line 36)
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
fn main() {

    /*
     * Create an ids service with:
     * Cache size = 100'000
     * hash algo. = rust SipHasher
     * A pool of 20 threads
     */
    let mut ids = IdsService::default();
    ids.start();
    // Optional: Wait cache is filled at 10%
    let _ = ids.filled_at_percent_event(10).recv().is_ok();
    println!("Get an id: {}", ids.get_id());
    println!("Get an id: {}", ids.get_id().as_hex());
    println!("Get an id: {}", ids.get_id().as_base64());
    println!("Get an id: {}", ids.get_id().as_json());
    println!("Get an id from cache: {}", ids.get_id_from_cache().expect("Expect an id"));
    println!("Current numbers of items in cache: {}", ids.get_cache_len());
    // Graceful Shutdown and Cleanup
    ids.stop();
}
examples/ten_million_rh.rs (line 40)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
fn main() {

    const NUMBER_OF_ITEMS : usize = 10_000_000;

    info!("Number of CPU's={}",num_cpus::get());

    let _ = SimpleLogger::init(LevelFilter::Info, Config::default());
    let start = Instant::now();
    let mut ids = IdsService::new(NUMBER_OF_ITEMS/10,Some(num_cpus::get() * 4 as usize));
    ids.start();
    let _ = ids.filled_at_percent_event(10).recv().is_ok();

    for i in 0..NUMBER_OF_ITEMS {
        let id : String = ids.get_id().as_hex();
        if id.len() != 16 {
            warn!("Id not valid. {}", id);
        }
        if i%(NUMBER_OF_ITEMS/100) == 0 {
            info!("Number of items get {}, cache size {}, in {:?}", i,ids.get_cache_len(),start.elapsed());
        }
    }
    let duration = start.elapsed().as_secs();
    info!("Cache size {}. Operation duration: {}s, throughput {} ids/s ",ids.get_cache_len(),duration, NUMBER_OF_ITEMS as u64/duration);
    ids.stop();
}
examples/ten_million.rs (line 41)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
fn main() {

    const NUMBER_OF_ITEMS : usize = 10_000_000;

    info!("Number of CPU's={}",num_cpus::get());

    let _ = SimpleLogger::init(LevelFilter::Info, Config::default());
    let start = Instant::now();
    let mut ids = IdsService::new(NUMBER_OF_ITEMS/10, Sha3Mode::Sha3_512,Some(num_cpus::get() * 4 as usize));
    ids.start();
    let _ = ids.filled_at_percent_event(10).recv().is_ok();
    //info!("Cache filled. {} in {:?} ",ids.get_cache_len(), start.elapsed());

    for i in 0..NUMBER_OF_ITEMS {
        let id : String = ids.get_id().as_hex();
        if id.len() != 128 {
            warn!("Id not valid. {}", id);
        }
        if i%(NUMBER_OF_ITEMS/100) == 0 {
            info!("Number of items get {}, cache size {}, in {:?}", i,ids.get_cache_len(),start.elapsed());
        }
    }
    let duration = start.elapsed().as_secs();
    info!("Cache size {}. Operation duration: {}s, throughput {} ids/s ",ids.get_cache_len(),duration, NUMBER_OF_ITEMS as u64/duration);
    ids.stop();
}
examples/examples_rh.rs (line 40)
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
fn main() {

    let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

    /*
     * Create an ids service with:
     * Cache size = 100'000
     * hash algo. = rust SipHasher
     * A pool of 20 threads
     */
    let mut ids = IdsService::default();
    ids.start();
    // Optional: Wait cache is filled at 10%
    let _ = ids.filled_at_percent_event(10).recv().is_ok();
    println!("Get an id: {}", ids.get_id());
    println!("Get an id from cache: {}", ids.get_id_from_cache().expect("Expect an id"));
    println!("Current numbers of items in cache: {}", ids.get_cache_len());
    // Graceful Shutdown and Cleanup

    ids.set_cache_size(150_000);
    let _ = ids.filled_event().recv().is_ok();
    println!("Current numbers of items in cache: {}", ids.get_cache_len());
    println!("Get an id: {}", ids.get_id());
    ids.stop();


    let mut ids = IdsService::new(100,Some(10 as usize));
    ids.start();
    // Optional: Wait cache is filled
    let _ = ids.filled_event().recv().is_ok();
    for id in ids.take(200) {
        println!("Get an id:{}", id);
    }
}
examples/examples.rs (line 43)
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
fn main() {

    let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

    /*
     * Create an ids service with:
     * Cache size = 10'000
     * hash algo. = Sha3_256
     * A pool of 100 threads
     */

    let mut ids = IdsService::new(10_000, Sha3Mode::Sha3_256,Some(num_cpus::get() as usize));
    ids.start();

    //Wait the cache is filled at 10%
    let _r=ids.filled_at_percent_event(10).recv().is_ok();
    info!("Current numbers of items in cache: {}", ids.get_cache_len());
    info!("Get an id: {}", ids.get_id().as_hex());
    info!("Get another id: {}", ids.get_id().as_base64());
    info!("Current numbers of items in cache: {}", ids.get_cache_len());

    /*
     * Create a service instance without caching process.
     * Without call of start()
     * Note: You may see messages output log:
     * [INFO] The ids_cache is empty, create on fly an id
     * [INFO] Get an id: 5a1c6815de674c27d4a55f73181b61a88b062c3d1b55e828427e07d0be49557a8fce30dfa19f4f2352eb0044c341cde71ca3960cbf86c075de942677e3b91ed2
     */
    let mut ids2 = IdsService::default();
    info!("Get an id: {}", ids2.get_id().as_hex());

    let mut ids3 = IdsService::default();
    ids3.start();

    //Wait the cache is filled
    let _ = ids3.filled_event().recv().is_ok();
    info!("1) Current numbers of items in cache ids3: {}", ids3.get_cache_len());

    ids3.set_cache_size(150_000);

    //Wait the cache is filled
    let _ = ids3.filled_event().recv().is_ok();

    info!("2) Current numbers of items in cache ids3: {}", ids3.get_cache_len());

    ids.stop();
    ids2.stop();
    ids3.stop();

}

Get an id from cache. If cache is empty it return None.

Examples found in repository?
examples/quickstart.rs (line 37)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
fn main() {

    /*
     * Create an ids service with:
     * Cache size = 100'000
     * hash algo. = sha256
     * A pool of 20 threads
     */
    let mut ids = IdsService::default();
    ids.start();
    // Optional: Wait cache is filled at 10%
    let _ = ids.filled_at_percent_event(10).recv().is_ok();
    println!("Get an id: {}", ids.get_id().as_hex());
    println!("Get another id: {}", ids.get_id().as_base64());
    println!("Get an id from cache: {}", ids.get_id_from_cache().expect("Expect an id").as_hex());
    println!("Current numbers of items in cache: {}", ids.get_cache_len());
    // Graceful Shutdown and Cleanup
    ids.stop();
}
More examples
Hide additional examples
examples/quickstart_rh.rs (line 40)
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
fn main() {

    /*
     * Create an ids service with:
     * Cache size = 100'000
     * hash algo. = rust SipHasher
     * A pool of 20 threads
     */
    let mut ids = IdsService::default();
    ids.start();
    // Optional: Wait cache is filled at 10%
    let _ = ids.filled_at_percent_event(10).recv().is_ok();
    println!("Get an id: {}", ids.get_id());
    println!("Get an id: {}", ids.get_id().as_hex());
    println!("Get an id: {}", ids.get_id().as_base64());
    println!("Get an id: {}", ids.get_id().as_json());
    println!("Get an id from cache: {}", ids.get_id_from_cache().expect("Expect an id"));
    println!("Current numbers of items in cache: {}", ids.get_cache_len());
    // Graceful Shutdown and Cleanup
    ids.stop();
}
examples/examples_rh.rs (line 41)
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
fn main() {

    let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

    /*
     * Create an ids service with:
     * Cache size = 100'000
     * hash algo. = rust SipHasher
     * A pool of 20 threads
     */
    let mut ids = IdsService::default();
    ids.start();
    // Optional: Wait cache is filled at 10%
    let _ = ids.filled_at_percent_event(10).recv().is_ok();
    println!("Get an id: {}", ids.get_id());
    println!("Get an id from cache: {}", ids.get_id_from_cache().expect("Expect an id"));
    println!("Current numbers of items in cache: {}", ids.get_cache_len());
    // Graceful Shutdown and Cleanup

    ids.set_cache_size(150_000);
    let _ = ids.filled_event().recv().is_ok();
    println!("Current numbers of items in cache: {}", ids.get_cache_len());
    println!("Get an id: {}", ids.get_id());
    ids.stop();


    let mut ids = IdsService::new(100,Some(10 as usize));
    ids.start();
    // Optional: Wait cache is filled
    let _ = ids.filled_event().recv().is_ok();
    for id in ids.take(200) {
        println!("Get an id:{}", id);
    }
}

Implementors§

implement Uids

Implement trait Uids