1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
 * This file is part of ids_service
 *
 * ids_service is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * ids_service is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ids_service.  If not, see <http://www.gnu.org/licenses/>
 */

extern crate rand;

/// Module implementing ids service using Rust [std::collections::hash_map::DefaultHasher]
use super::common::Encode;
use super::common::Service;
use super::common::Uids;
use super::common::NUMBER_OF_FILL_THREAD;
use data_encoding::{BASE32, BASE64, BASE64URL, HEXLOWER};
use log::*;
use rand::prelude::*;
use std::collections::hash_map::DefaultHasher;
use std::collections::VecDeque;
use std::fmt;
use std::hash::Hasher;
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
use std::thread::JoinHandle;
use std::time::{Duration, SystemTime};

const RAND_LENGTH: usize = 16; // 64/8*2


/// Struct for internal use of IdsService
pub struct IdsService {
    ids_cache: Arc<Mutex<VecDeque<u64>>>,
    cache_size: usize,
    number_of_threads: usize,
    threads_pool: Vec<Option<JoinHandle<()>>>,
    stop_state: Arc<AtomicBool>,
}

///
/// Allow to create an instance using default parameters:
/// * cache size: 100'000 items
/// * number of threads: 20
impl Default for IdsService {
    fn default() -> Self {
        let cache_size = 100_000_usize;
        IdsService {
            ids_cache: Arc::new(Mutex::new(VecDeque::with_capacity(cache_size))),
            cache_size,
            number_of_threads: *NUMBER_OF_FILL_THREAD,
            threads_pool: Vec::with_capacity(*NUMBER_OF_FILL_THREAD),
            stop_state: Arc::new(AtomicBool::new(false)),
        }
    }
}

/// Debug: internal state of IdsService
impl fmt::Debug for IdsService {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "IdsService {{ ids_cache nb. ids: {}, cache_size: {}, number_of_threads : {} }}",
            self.ids_cache.lock().unwrap().len(),
            self.cache_size,
            self.number_of_threads
        )
    }
}

/// Implement trait Service
impl Service for IdsService {
    /// Start the background threads to keep cache filled.
    fn start(&mut self) {
        info!("Start ids_service!");
        let mut rng = thread_rng();

        self.stop_state.store(false, Ordering::Relaxed);

        // Create all workers
        for i in 0..self.number_of_threads {
            // Create builder
            let builder = thread::Builder::new().name(format!("{}", i));

            // Clone cache ids
            let ids_cache_clone = self.ids_cache.clone();

            let cache_size = self.cache_size;

            // Waiting delay
            let delay_ms: u64 = rng.gen_range(50..200);

            let stop_state_clone = self.stop_state.clone();

            // create the thread
            let handler = builder
                .spawn(move || {
                    let thread_id: usize =
                        usize::from_str(thread::current().name().unwrap()).unwrap();
                    trace!("Thread id {}: is up", thread_id);
                    loop {
                        if ids_cache_clone.lock().unwrap().len() < cache_size {
                            let id_result = build_id();
                            if let Ok(id) = id_result {
                                ids_cache_clone.lock().unwrap().push_back(id);
                            } else {
                                warn!(
                                    "Thread id {}: Error on random data generator. {}",
                                    thread_id,
                                    id_result.err().unwrap().to_string()
                                );
                            }
                        } else {
                            //trace!("Thread id {}: wait for {} ms", thread_id, delay_ms);
                            thread::sleep(Duration::from_millis(delay_ms));
                        }
                        if stop_state_clone.load(Ordering::Relaxed) {
                            trace!("Thread id {}: stopped", thread_id);
                            break;
                        }
                    }
                })
                .unwrap_or_else(|_| panic!("Expect no error from thread {}", i));
            self.threads_pool.push(Some(handler));
        }
    }

    /// Stop the background threads to Graceful Shutdown and Cleanup
    fn stop(&mut self) {
        info!("Stop ids_service!");
        self.stop_state.store(true, Ordering::Relaxed);
        for handle in &mut self.threads_pool {
            if let Some(handle) = handle.take() {
                handle.join().unwrap();
            }
        }
        self.ids_cache.lock().unwrap().clear();
    }
}

/// Implementation for u64
impl Encode for u64 {
    fn as_hex(&self) -> String {
        HEXLOWER.encode(&self.to_ne_bytes())
    }
    fn as_base64(&self) -> String {
        BASE64.encode(&self.to_ne_bytes())
    }

    fn as_base64_url(&self) -> String {
        BASE64URL.encode(&self.to_ne_bytes())
    }

    fn as_base32(&self) -> String {
        BASE32.encode(&self.to_ne_bytes())
    }

    fn as_json(&self) -> String {
        let mut message: String = String::new();
        message.push_str(format!("{{\n\"u64\" : \"{}\",", self).as_str());
        message.push_str(format!("\n\"base64\" : \"{}\",", self.as_base64()).as_str());
        message.push_str(format!("\n\"base32\" : \"{}\",", self.as_base32()).as_str());
        message.push_str(format!("\n\"hex\" : \"0x{}\"\n}}", self.as_hex()).as_str());
        message
    }
}

/// Implementation Iterator.
/// # Example
/// ```
/// extern crate ids_service;
///
/// use crate::ids_service::rust_hash::*;
/// use crate::ids_service::common::*;
///
/// 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
///     ids.filled_event().recv().is_ok();
///     for id in ids.take(10) {
///         println!("Get an id:\n{}", id.as_json());
///     }
///
/// }
/// ```
impl Iterator for IdsService {
    type Item = u64;

    fn next(&mut self) -> Option<Self::Item> {
        Some(self.get_id())
    }
}


/// Implement trait Uids
impl Uids for IdsService {
    type Id = u64;

    ///
    /// Get an id. It may never failed even if cache is empty.
    /// If cache is empty it create an id on fly.
    /// # Examples
    /// ```
    /// extern crate ids_service;
    ///
    /// use crate::ids_service::rust_hash::*;
    /// use crate::ids_service::common::*;
    ///
    /// fn main() {
    ///
    ///     let mut ids = IdsService::default();
    ///     ids.start();
    ///     println!("Get an id: {}", ids.get_id().as_hex());
    /// }
    /// ```
    fn get_id(&mut self) -> Self::Id {
        let id = self.ids_cache.lock().unwrap().pop_front();
        match id {
            Some(i) => i,
            None => {
                info!("The ids_cache is empty, create on fly an id");
                self.create_id().unwrap()
            }
        }
    }

    ///
    /// Get an id from the cache. If the cache is empty
    /// It return None
    /// # Examples
    /// ```
    /// extern crate ids_service;
    ///
    /// use crate::ids_service::rust_hash::*;
    /// use crate::ids_service::common::*;
    ///
    /// fn main() {
    ///
    ///     let mut ids = IdsService::default();
    ///     ids.start();
    ///     ids.filled_event().recv().is_ok();
    ///     println!("Get an id from cache: {}", ids.get_id_from_cache().expect("Expect an id").as_hex());
    /// }
    /// ```
    fn get_id_from_cache(&mut self) -> Option<Self::Id> {
        self.ids_cache.lock().unwrap().pop_front()
    }
}



/// Implementation to create a new IdsService
/// # Examples
///
/// ```
/// extern crate ids_service;
///
/// use crate::ids_service::rust_hash::*;
/// use crate::ids_service::common::*;
///
/// fn main() {
///
///     /*
///      * Create an ids service with:
///      * Cache size = 10'000
///      * A pool of 30 threads
///      */
///     let mut ids = IdsService::new(10_000,Some(30 as usize));
///     ids.start();
///     println!("Get an id: {}", ids.get_id());
///
/// }
/// ```
impl IdsService {
    pub fn new(cache_size: usize, number_of_threads: Option<usize>) -> IdsService {
        let threads = match number_of_threads {
            Some(n) => n,
            None => *NUMBER_OF_FILL_THREAD,
        };
        IdsService {
            ids_cache: Arc::new(Mutex::new(VecDeque::with_capacity(cache_size))),
            cache_size,
            number_of_threads: threads,
            threads_pool: Vec::with_capacity(*NUMBER_OF_FILL_THREAD),
            stop_state: Arc::new(AtomicBool::new(false)),
        }
    }

    // Create an id based on function create_id
    fn create_id(&mut self)  -> Result<u64, rand::Error> {
        build_id()
    }

    /// Set a new cache size. The function restart the service to make new value active.
    /// The cache isn't cleared
    pub fn set_cache_size(&mut self, new_size: usize) {
        self.cache_size = new_size;
        info!("Restart ids_service!");
        self.stop();
        self.start();
    }

    ///
    /// True if the cash length >= cash size needed
    pub fn is_filled(&self) -> bool {
        let len = self.ids_cache.lock().unwrap().len();
        len >= self.cache_size
    }

    ///
    /// Send an event when cache is filled at 100%
    ///
    /// # Examples
    ///
    /// ```
    /// extern crate ids_service;
    ///
    /// use crate::ids_service::crypto_hash::*;
    /// use crate::ids_service::common::*;
    ///
    /// fn main() {
    ///
    ///     let mut ids = IdsService::default();
    ///     ids.start();
    ///     ids.filled_event().recv().is_ok();
    /// }
    /// ```
    ///
    pub fn filled_event(&mut self) -> mpsc::Receiver<bool> {
        self.filled_at_percent_event(100)
    }

    ///
    /// Sends an event when the cache is filled to a percentage
    ///
    /// # Examples
    ///
    /// ```
    /// extern crate ids_service;
    ///
    /// use crate::ids_service::crypto_hash::*;
    /// use crate::ids_service::common::*;
    ///
    /// fn main() {
    ///
    ///     let mut ids = IdsService::default();
    ///     ids.start();
    ///     ids.filled_at_percent_event(20).recv().is_ok();
    /// }
    /// ```
    ///
    pub fn filled_at_percent_event(&mut self, percentage : u8) -> mpsc::Receiver<bool> {
        let percent = {if percentage>100 {100} else {percentage}};

        let filled_chn: (mpsc::Sender<bool>, mpsc::Receiver<bool>) = mpsc::channel();
        let sender = filled_chn.0.clone();

        let name = format!("filled_event {}", self.threads_pool.len() + 1);
        // Create builder
        let builder = thread::Builder::new().name(name.clone());

        let stop_state_clone = self.stop_state.clone();

        // Clone cache ids
        let ids_cache_clone = self.ids_cache.clone();

        let cache_limit = self.cache_size * percent as usize / 100;

        // create the thread
        let handler = builder
            .spawn(move || {
                trace!("Thread id {}: is up", thread::current().name().unwrap());
                loop {
                    if ids_cache_clone.lock().unwrap().len() >= cache_limit {
                        sender.send(true).unwrap();
                        break;
                    }
                    if stop_state_clone.load(Ordering::Relaxed) {
                        sender.send(false).unwrap();
                        break;
                    }
                    thread::yield_now();
                    //thread::sleep(Duration::from_millis(13));
                }
                trace!("Thread id {}: stopped", thread::current().name().unwrap());
            })
            .unwrap_or_else(|_| panic!("Expect no error from thread {}", name));
        self.threads_pool.push(Some(handler));
        filled_chn.1
    }

    /// Get the internal cache size.
    pub fn get_cache_len(&mut self) -> usize {
        self.ids_cache.lock().unwrap().len()
    }
}

/// Function to create an id without the service and caching
pub fn create_id() -> Result<u64, rand::Error> {
    build_id()
}

// Internal function that create an ID
fn build_id() -> Result<u64, rand::Error> {
    let mut rng = thread_rng();
    // Take a timestamp
    let ts = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_nanos();
    let mut hasher = DefaultHasher::new();
    let mut arr = [0u8; RAND_LENGTH];
    rng.try_fill(&mut arr[..])?;
    hasher.write(&arr);
    hasher.write(&ts.to_ne_bytes());
    Ok(hasher.finish())
}

///
/// Tests
///
#[cfg(test)]
mod tests {

    use crate::rust_hash::*;
    use simplelog::*;
    use crate::common::{Service, Uids, Encode};
    use std::collections::HashSet;

    #[test]
    fn aaaa_init() {
        println!("Call test init");
        let _ = SimpleLogger::init(LevelFilter::Info, Config::default());
        assert!(true);
    }

    #[test]
    fn test_create_id() {
        let result = build_id();
        assert!(result.is_ok());
        assert!(result.unwrap() > 0);
    }

    #[test]
    fn test_new() {
        let mut service = IdsService::new(1000,  None);
        service.start();
        let _ = service.filled_event().recv().is_ok();
        assert!(service.get_cache_len() >= 1000);
    }

    #[test]
    fn test_default() {
        let mut service = IdsService::default();
        service.start();
        let _ = service.filled_event().recv().is_ok();
        assert!(service.get_cache_len() >= 100_000);
    }

    #[test]
    fn test_get_id() {
        let mut service = IdsService::default();
        service.start();
        let _ = service.filled_at_percent_event(5).recv().is_ok();

        assert_ne!(service.get_id(),0);
    }

    #[test]
    fn test_get_id_from_cache() {
        let mut service = IdsService::default();
        service.start();
        let _ = service.filled_at_percent_event(5).recv().is_ok();
        let r1 = service.get_id_from_cache();
        assert!(r1.is_some());
        assert_ne!(r1.unwrap(),0);
        service.stop();
        let r2 = service.get_id_from_cache();
        assert!(r2.is_none());
    }

    #[test]
    fn test_filled() {
        let mut service = IdsService::default();
        service.start();
        let _ = service.filled_event().recv().is_ok();

        assert!(service.is_filled());
    }

    #[test]
    fn test_filled_at_percent_event() {
        let mut ids01 = IdsService::new(10000,  None);
        ids01.start();
        let _ = ids01.filled_at_percent_event(5).recv().is_ok();
        let cache_len = ids01.get_cache_len();
        debug!("len: {}", cache_len);
        assert!(cache_len > 200);
    }

    #[test]
    fn test_iterator() {
        let mut ids01 = IdsService::new(10_000, None);
        ids01.start();
        let _ = ids01.filled_event().recv().is_ok();
        let number = 10_000;
        let mut ids = HashSet::with_capacity(number);
        for _ in 0..number {
            ids.insert(ids01.get_id().as_hex());
        }
        assert_eq!(ids.len(), number);
        ids.clear();
        for x in ids01.take(number) {
            ids.insert(x.as_hex());
        }
        assert_eq!(ids.len(), number);
    }

}