quick_stream 0.1.3

Quick Stream is a Rust-based solution designed to efficiently handle data upsert operations with a focus on performance and scalability. Utilizing asynchronous programming and a dynamic sender-receiver model, Quick Stream aims to streamline the process of synchronizing large datasets with minimal overhead.
Documentation
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
use std::process::{ExitCode, Termination};

use log::trace;
use native_tls::Certificate;
use random_word::Lang;
use support::{MultiTableUpsertQueryHolder, QueryHolder};
use tokio_util::sync::CancellationToken;

use crate::{delete::DeleteQuickStream, upsert::{multi_table_upsert::MultiTableUpsertQuickStream, UpsertQuickStream}};

pub mod support;

#[derive(Clone)]
pub struct QuickStreamBuilder {
    cancellation_token: Option<CancellationToken>,
    max_con_count: Option<usize>,
    buffer_size: Option<usize>,
    single_digits: Option<usize>,
    tens: Option<usize>,
    hundreds: Option<usize>,
    init_delete_con_count: Option<usize>,
    db_config: Option<tokio_postgres::Config>,
    tls: Option<Certificate>,
    queries: Option<QueryHolder>,
    multi_table_upsert_queries: Option<MultiTableUpsertQueryHolder>,
    delete_query: Option<String>,
    max_records_per_cycle_batch: Option<usize>, //a batch = introduced_lag_cycles
    introduced_lag_cycles: Option<usize>,
    introduced_lag_in_millies: Option<u64>,
    connection_creation_threshold: Option<f64>,
    name: Option<String>,
    print_connection_configuration: bool
}

impl Default for QuickStreamBuilder {
    fn default() -> Self {
        Self {
            cancellation_token: None,
            max_con_count: None,
            buffer_size: None,
            single_digits: None,
            tens: None,
            hundreds: None,
            db_config: None,
            tls: None,
            queries: None,
            max_records_per_cycle_batch: None,
            introduced_lag_cycles: None,
            introduced_lag_in_millies: None,
            connection_creation_threshold: None,
            name: Some(format!("{}_{}", random_word::gen(Lang::En), random_word::gen(Lang::En))),
            print_connection_configuration: false,
            init_delete_con_count: None,
            delete_query: None,
            multi_table_upsert_queries: None,
        }
    }
}

impl Termination for QuickStreamBuilder {
    fn report(self) -> std::process::ExitCode {
        ExitCode::SUCCESS
    }
}

impl QuickStreamBuilder {
    pub fn cancellation_tocken(&mut self, cancellation_token: CancellationToken) -> &mut Self {
        self.cancellation_token = Some(cancellation_token);
        self
    }

    pub fn max_connection_count(&mut self, max_con_count: usize) -> &mut Self {
        self.max_con_count = Some(max_con_count);
        self
    }

    pub fn buffer_size(&mut self, buffer_size: usize) -> &mut Self {
        self.buffer_size = Some(buffer_size);
        self
    }

    pub fn single_digits(&mut self, single_digits: usize) -> &mut Self {
        self.single_digits = Some(single_digits);
        self
    }

    pub fn tens(&mut self, tens: usize) -> &mut Self {
        self.tens = Some(tens);
        self
    }

    pub fn hundreds(&mut self, hundreds: usize) -> &mut Self {
        self.hundreds = Some(hundreds);
        self
    }

    pub fn init_delete_con_count(&mut self, init_delete_con_count: usize) -> &mut Self {
        self.init_delete_con_count = Some(init_delete_con_count);
        self
    }

    pub fn db_config(&mut self, db_config: tokio_postgres::Config) -> &mut Self {
        self.db_config = Some(db_config);
        self
    }

    pub fn tls(&mut self, tls: Certificate) -> &mut Self {
        self.tls = Some(tls);
        self
    }

    pub fn queries(&mut self, queries: QueryHolder) -> &mut Self {
        self.queries = Some(queries);
        self
    }

    pub fn multi_table_queries(&mut self, multi_table_queries: MultiTableUpsertQueryHolder) -> &mut Self {
        self.multi_table_upsert_queries = Some(multi_table_queries);
        self
    }

    pub fn delete_query(&mut self, delete_query: String) -> &mut Self {
        self.delete_query = Some(delete_query);
        self
    }

    pub fn max_records_per_cycle_batch(&mut self, max_records_per_cycle_batch: usize) -> &mut Self {
        self.max_records_per_cycle_batch = Some(max_records_per_cycle_batch);
        self
    }

    pub fn introduced_lag_cycles(&mut self, introduced_lag_cycles: usize) -> &mut Self {
        self.introduced_lag_cycles = Some(introduced_lag_cycles);
        self
    }

    pub fn introduced_lag_in_millies(&mut self, introduced_lag_in_millies: u64) -> &mut Self {
        self.introduced_lag_in_millies = Some(introduced_lag_in_millies);
        self
    }

    pub fn connection_creation_threshold(&mut self, connection_creation_threshold: f64) -> &mut Self {
        self.connection_creation_threshold = Some(connection_creation_threshold);
        self
    }

    pub fn name(&mut self, name: String) -> &mut Self {
        self.name = Some(name);
        self
    }

    /**
     This will print the current connections configuration end of every lag cycle.
     * ***Default behaviour is to print the current connections confguration iff it changed***
     */
    pub fn print_connection_configuration(&mut self) -> &mut Self {
        self.print_connection_configuration = true;
        self
    }

    pub fn build_update(&self) -> UpsertQuickStream {
        trace!("building UpsertQuickStream from builder");
        UpsertQuickStream {
            cancellation_token: self.clone().cancellation_token.expect("cancellation_token is None"),
            max_con_count: self.max_con_count.expect("max_con_count is None"),
            buffer_size: self.buffer_size.expect("buffer_size is None"),
            single_digits: self.single_digits.expect("single_digits is None"),
            tens: self.tens.expect("tens is None"),
            hundreds: self.hundreds.expect("hundreds is None"),
            db_config: self.clone().db_config.expect("db_config is None"),
            tls: self.clone().tls,
            queries: self.clone().queries.expect("queries is None"),
            max_records_per_cycle_batch: self.max_records_per_cycle_batch.expect("max_records_per_cycle_batch is None"),
            introduced_lag_cycles: self.introduced_lag_cycles.expect("introduced_lag_cycles is None"),
            introduced_lag_in_millies: self.introduced_lag_in_millies.expect("introduced_lag_in_millies is None"),
            connection_creation_threshold: self.connection_creation_threshold.expect("connection_creation_threshold is None"),
            name: self.clone().name.expect("not a possible scenario"),
            print_con_config: self.print_connection_configuration
        }
    }

    pub fn build_delete(&self) -> DeleteQuickStream {
        trace!("building DeleteQuickStream from builder");
        DeleteQuickStream {
            cancellation_token: self.clone().cancellation_token.expect("cancellation_token is None"),
            max_con_count: self.max_con_count.expect("max_con_count is None"),
            buffer_size: self.buffer_size.expect("buffer_size is None"),
            init_con_count: self.init_delete_con_count.expect("init_delete_con_count is None"),
            db_config: self.clone().db_config.expect("db_config is None"),
            tls: self.clone().tls,
            query: self.clone().delete_query.expect("delete_query is None"),
            max_records_per_cycle_batch: self.max_records_per_cycle_batch.expect("max_records_per_cycle_batch is None"),
            introduced_lag_cycles: self.introduced_lag_cycles.expect("introduced_lag_cycles is None"),
            introduced_lag_in_millies: self.introduced_lag_in_millies.expect("introduced_lag_in_millies is None"),
            connection_creation_threshold: self.connection_creation_threshold.expect("connection_creation_threshold is None"),
            name: self.clone().name.expect("not a possible scenario"),
            print_con_config: self.print_connection_configuration
        }
    }

    pub fn build_multi_part_upsert(&self) -> MultiTableUpsertQuickStream {
        trace!("building MultiTableUpsertQuickStream from builder");
        MultiTableUpsertQuickStream {
            cancellation_token: self.clone().cancellation_token.expect("cancellation_token is None"),
            max_con_count: self.max_con_count.expect("max_con_count is None"),
            buffer_size: self.buffer_size.expect("buffer_size is None"),
            single_digits: self.single_digits.expect("single_digits is None"),
            tens: self.tens.expect("tens is None"),
            hundreds: self.hundreds.expect("hundreds is None"),
            db_config: self.clone().db_config.expect("db_config is None"),
            tls: self.clone().tls,
            queries: self.clone().multi_table_upsert_queries.expect("multi table queries is None"),
            max_records_per_cycle_batch: self.max_records_per_cycle_batch.expect("max_records_per_cycle_batch is None"),
            introduced_lag_cycles: self.introduced_lag_cycles.expect("introduced_lag_cycles is None"),
            introduced_lag_in_millies: self.introduced_lag_in_millies.expect("introduced_lag_in_millies is None"),
            connection_creation_threshold: self.connection_creation_threshold.expect("connection_creation_threshold is None"),
            name: self.clone().name.expect("not a possible scenario"),
            print_con_config: self.print_connection_configuration
        
        }
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use tokio_postgres::Config;
    use tokio_util::sync::CancellationToken;

    use super::{support::QueryHolder, QuickStreamBuilder};

#[test]
    pub fn test_builder() -> QuickStreamBuilder {
        let cancellation_token = CancellationToken::new();

        let db_config = Config::new();
        let queries = QueryHolder::default();

        let mut builder = QuickStreamBuilder::default();
        builder
            .cancellation_tocken(cancellation_token.clone())
            .max_connection_count(2)
            .buffer_size(10)
            .single_digits(2)
            .tens(12)
            .hundreds(1)
            .db_config(db_config.clone())
            .queries(queries.clone())
            .max_records_per_cycle_batch(10)
            .introduced_lag_cycles(2)
            .introduced_lag_in_millies(10)
            .connection_creation_threshold(15.0)
            .print_connection_configuration();

        let upsert_processor = builder.clone().build_update();
        
        assert_eq!(upsert_processor.max_con_count, 2);
        assert_eq!(upsert_processor.buffer_size, 10);
        assert_eq!(upsert_processor.single_digits, 2);
        assert_eq!(upsert_processor.tens, 12);
        assert_eq!(upsert_processor.hundreds, 1);
        // Compare the string representation of db_config as Config doesn't implement PartialEq
        assert_eq!(format!("{:?}", upsert_processor.db_config), format!("{:?}", db_config));
        // Compare the string representation of queries as it may not implement PartialEq
        assert_eq!(format!("{:?}", upsert_processor.queries), format!("{:?}", queries));
        assert_eq!(upsert_processor.max_records_per_cycle_batch, 10);
        assert_eq!(upsert_processor.introduced_lag_cycles, 2);
        assert_eq!(upsert_processor.introduced_lag_in_millies, 10);
        assert_eq!(upsert_processor.connection_creation_threshold, 15.0);
        assert_eq!(upsert_processor.print_con_config, true);
        assert_eq!(upsert_processor.cancellation_token.is_cancelled(), cancellation_token.is_cancelled());

        cancellation_token.cancel();
        assert_eq!(upsert_processor.cancellation_token.is_cancelled(), cancellation_token.is_cancelled());

        builder
    }

    #[test]
    #[should_panic(expected = "cancellation_token is None")]
    fn test_missing_cancellation_token() {
        let mut builder = QuickStreamBuilder::default();
        builder
            .max_connection_count(2)
            .buffer_size(10)
            .single_digits(1)
            .tens(2)
            .hundreds(1)
            .db_config(Config::new())
            .queries(QueryHolder::default())
            .max_records_per_cycle_batch(10)
            .introduced_lag_cycles(2)
            .introduced_lag_in_millies(10)
            .connection_creation_threshold(15.0)
            .name("test".to_string())
            .print_connection_configuration();

        let _ = builder.build_update();
    }

    #[test]
    #[should_panic(expected = "max_con_count is None")]
    fn test_missing_max_con_count() {
        let cancellation_token = CancellationToken::new();
        let mut builder = QuickStreamBuilder::default();
        builder
            .cancellation_tocken(cancellation_token)
            .buffer_size(10)
            .single_digits(1)
            .tens(2)
            .hundreds(1)
            .db_config(Config::new())
            .queries(QueryHolder::default())
            .max_records_per_cycle_batch(10)
            .introduced_lag_cycles(2)
            .introduced_lag_in_millies(10)
            .connection_creation_threshold(15.0)
            .name("test".to_string())
            .print_connection_configuration();

        let _ = builder.build_update();
    }

    #[test]
    #[should_panic(expected = "buffer_size is None")]
    fn test_missing_buffer_size() {
        let cancellation_token = CancellationToken::new();
        let mut builder = QuickStreamBuilder::default();
        builder
            .cancellation_tocken(cancellation_token)
            .max_connection_count(2)
            .single_digits(1)
            .tens(2)
            .hundreds(1)
            .db_config(Config::new())
            .queries(QueryHolder::default())
            .max_records_per_cycle_batch(10)
            .introduced_lag_cycles(2)
            .introduced_lag_in_millies(10)
            .connection_creation_threshold(15.0)
            .name("test".to_string())
            .print_connection_configuration();

        let _ = builder.build_update();
    }

    #[test]
    #[should_panic(expected = "single_digits is None")]
    fn test_missing_single_digits() {
        let cancellation_token = CancellationToken::new();
        let mut builder = QuickStreamBuilder::default();
        builder
            .cancellation_tocken(cancellation_token)
            .max_connection_count(2)
            .buffer_size(10)
            .tens(2)
            .hundreds(1)
            .db_config(Config::new())
            .queries(QueryHolder::default())
            .max_records_per_cycle_batch(10)
            .introduced_lag_cycles(2)
            .introduced_lag_in_millies(10)
            .connection_creation_threshold(15.0)
            .name("test".to_string())
            .print_connection_configuration();

        let _ = builder.build_update();
    }

    #[test]
    #[should_panic(expected = "tens is None")]
    fn test_missing_tens() {
        let cancellation_token = CancellationToken::new();
        let mut builder = QuickStreamBuilder::default();
        builder
            .cancellation_tocken(cancellation_token)
            .max_connection_count(2)
            .buffer_size(10)
            .single_digits(1)
            .hundreds(1)
            .db_config(Config::new())
            .queries(QueryHolder::default())
            .max_records_per_cycle_batch(10)
            .introduced_lag_cycles(2)
            .introduced_lag_in_millies(10)
            .connection_creation_threshold(15.0)
            .name("test".to_string())
            .print_connection_configuration();

        let _ = builder.build_update();
    }

    #[test]
    #[should_panic(expected = "hundreds is None")]
    fn test_missing_hundreds() {
        let cancellation_token = CancellationToken::new();
        let mut builder = QuickStreamBuilder::default();
        builder
            .cancellation_tocken(cancellation_token)
            .max_connection_count(2)
            .buffer_size(10)
            .single_digits(1)
            .tens(2)
            .db_config(Config::new())
            .queries(QueryHolder::default())
            .max_records_per_cycle_batch(10)
            .introduced_lag_cycles(2)
            .introduced_lag_in_millies(10)
            .connection_creation_threshold(15.0)
            .name("test".to_string())
            .print_connection_configuration();

        let _ = builder.build_update();
    }

    #[test]
    #[should_panic(expected = "db_config is None")]
    fn test_missing_db_config() {
        let cancellation_token = CancellationToken::new();
        let mut builder = QuickStreamBuilder::default();
        builder
            .cancellation_tocken(cancellation_token)
            .max_connection_count(2)
            .buffer_size(10)
            .single_digits(1)
            .tens(2)
            .hundreds(1)
            .queries(QueryHolder::default())
            .max_records_per_cycle_batch(10)
            .introduced_lag_cycles(2)
            .introduced_lag_in_millies(10)
            .connection_creation_threshold(15.0)
            .name("test".to_string())
            .print_connection_configuration();

        let _ = builder.build_update();
    }

    #[test]
    #[should_panic(expected = "queries is None")]
    fn test_missing_queries() {
        let cancellation_token = CancellationToken::new();
        let mut builder = QuickStreamBuilder::default();
        builder
            .cancellation_tocken(cancellation_token)
            .max_connection_count(2)
            .buffer_size(10)
            .single_digits(1)
            .tens(2)
            .hundreds(1)
            .db_config(Config::new())
            .max_records_per_cycle_batch(10)
            .introduced_lag_cycles(2)
            .introduced_lag_in_millies(10)
            .connection_creation_threshold(15.0)
            .name("test".to_string())
            .print_connection_configuration();

        let _ = builder.build_update();
    }

    #[test]
    #[should_panic(expected = "max_records_per_cycle_batch is None")]
    fn test_missing_max_records_per_cycle_batch() {
        let cancellation_token = CancellationToken::new();
        let mut builder = QuickStreamBuilder::default();
        builder
            .cancellation_tocken(cancellation_token)
            .max_connection_count(2)
            .buffer_size(10)
            .single_digits(1)
            .tens(2)
            .hundreds(1)
            .db_config(Config::new())
            .queries(QueryHolder::default())
            .introduced_lag_cycles(2)
            .introduced_lag_in_millies(10)
            .connection_creation_threshold(15.0)
            .name("test".to_string())
            .print_connection_configuration();

        let _ = builder.build_update();
    }

    #[test]
    #[should_panic(expected = "introduced_lag_cycles is None")]
    fn test_missing_introduced_lag_cycles() {
        let cancellation_token = CancellationToken::new();
        let mut builder = QuickStreamBuilder::default();
        builder
            .cancellation_tocken(cancellation_token)
            .max_connection_count(2)
            .buffer_size(10)
            .single_digits(1)
            .tens(2)
            .hundreds(1)
            .db_config(Config::new())
            .queries(QueryHolder::default())
            .max_records_per_cycle_batch(10)
            .introduced_lag_in_millies(10)
            .connection_creation_threshold(15.0)
            .name("test".to_string())
            .print_connection_configuration();

        let _ = builder.build_update();
    }

    #[test]
    #[should_panic(expected = "introduced_lag_in_millies is None")]
    fn test_missing_introduced_lag_in_millies() {
        let cancellation_token = CancellationToken::new();
        let mut builder = QuickStreamBuilder::default();
        builder
            .cancellation_tocken(cancellation_token)
            .max_connection_count(2)
            .buffer_size(10)
            .single_digits(1)
            .tens(2)
            .hundreds(1)
            .db_config(Config::new())
            .queries(QueryHolder::default())
            .max_records_per_cycle_batch(10)
            .introduced_lag_cycles(2)
            .connection_creation_threshold(15.0)
            .name("test".to_string())
            .print_connection_configuration();

        let _ = builder.build_update();
    }

    #[test]
    #[should_panic(expected = "connection_creation_threshold is None")]
    fn test_missing_connection_creation_threshold() {
        let cancellation_token = CancellationToken::new();
        let mut builder = QuickStreamBuilder::default();
        builder
            .cancellation_tocken(cancellation_token)
            .max_connection_count(2)
            .buffer_size(10)
            .single_digits(1)
            .tens(2)
            .hundreds(1)
            .db_config(Config::new())
            .queries(QueryHolder::default())
            .max_records_per_cycle_batch(10)
            .introduced_lag_cycles(2)
            .introduced_lag_in_millies(10)
            .name("test".to_string())
            .print_connection_configuration();

        let _ = builder.build_update();
    }
}