voltdb-client-rust 0.2.1

A socket client library for VoltDB server supporting both sync and async operations.
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
extern crate lazy_static;

use std::sync::atomic::AtomicPtr;
use std::sync::atomic::Ordering::Acquire;
use std::sync::*;
use std::thread::JoinHandle;
use std::time::SystemTime;
use std::{fs, thread};

use testcontainers::core::WaitFor;
use testcontainers::runners::SyncRunner;
use testcontainers::{Container, GenericImage, ImageExt};

use voltdb_client_rust::*;

static POPULATE: Once = Once::new();
static mut VOLTDB_PORT: u16 = 0;
static TEST_MUTEX: Mutex<()> = Mutex::new(());
static VOLTDB_CONTAINER: OnceLock<Container<GenericImage>> = OnceLock::new();
static CLEANUP_REGISTERED: OnceLock<()> = OnceLock::new();

fn setup_voltdb_once() {
    VOLTDB_CONTAINER.get_or_init(|| {
        // Register cleanup handler once
        CLEANUP_REGISTERED.get_or_init(|| {
            extern "C" fn cleanup() {
                // Force stop the container
                if let Some(container) = VOLTDB_CONTAINER.get() {
                    container.stop().unwrap();
                }
            }
            // SAFETY: registering atexit callback is safe
            unsafe {
                libc::atexit(cleanup);
            }
        });

        let voltdb = GenericImage::new("basvanbeek/voltdb-community", "9.2.1")
            .with_wait_for(WaitFor::message_on_stdout(
                "Server completed initialization.",
            ))
            .with_env_var("HOST_COUNT", "1");
        let docker = voltdb.start().unwrap();
        let host_port = docker.get_host_port_ipv4(21211).unwrap();
        unsafe {
            VOLTDB_PORT = host_port;
        }
        docker
    });
}

fn populate(node: &mut Node) {
    POPULATE.call_once(|| {
        let jars = fs::read("tests/procedures.jar").unwrap();
        let x = node.upload_jar(jars).unwrap();
        let mut table = x.recv().unwrap();
        assert!(table.has_error().is_none());

        let create = "CREATE TABLE test_types
                    (
                    t1 TINYINT,
                    t2 SMALLINT,
                    t3 INTEGER,
                    t4 BIGINT,
                    t5 FLOAT,
                    t6 DECIMAL,
                    t7 VARCHAR,
                    t8 VARBINARY,
                    t9 TIMESTAMP,
                    );";
        execute_success(node, create);
        let script = "CREATE PROCEDURE  FROM CLASS com.johnny.ApplicationCreate;";
        let x = node.query(script).unwrap();
        let mut table = x.recv().unwrap();
        assert!(table.has_error().is_none());
    });
    let clean = "delete from test_types;";
    execute_success(node, clean);
}

fn execute_success(node: &mut Node, sql: &str) {
    let x = node.query(sql).unwrap();
    let mut table = x.recv().unwrap();
    if let Some(err) = table.has_error() {
        panic!("err {:?} ", err)
    }
}

fn get_test_node() -> Node {
    setup_voltdb_once();
    let url = "localhost";
    let port = unsafe { VOLTDB_PORT };
    get_node(&format!("{}:{}", url, port)).unwrap()
}

#[derive(Debug, PartialEq)]
struct Test {
    t1: Option<bool>,
    t2: Option<i16>,
    t3: Option<i32>,
    t4: Option<i64>,
    t5: Option<f64>,
    t6: Option<BigDecimal>,
    t7: Option<String>,
    t8: Option<Vec<u8>>,
    t9: Option<DateTime<Utc>>,
}

impl From<&mut VoltTable> for Test {
    fn from(table: &mut VoltTable) -> Self {
        let t1 = table.get_bool_by_column("T1").unwrap();
        let t2 = table.get_i16_by_column("t2").unwrap();
        let t3 = table.get_i32_by_column("t3").unwrap();
        let t4 = table.get_i64_by_column("t4").unwrap();
        let t5 = table.get_f64_by_column("t5").unwrap();
        let t6 = table.get_decimal_by_column("t6").unwrap();
        let t7 = table.get_string_by_column("t7").unwrap();
        let t8 = table.get_bytes_op_by_column("t8").unwrap();
        let t9 = table.get_time_by_column("t9").unwrap();
        Test {
            t1,
            t2,
            t3,
            t4,
            t5,
            t6,
            t7,
            t8,
            t9,
        }
    }
}

#[test]
fn test_connection_establishment() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);
    // Connection is implicitly tested by getting the node
    Ok(())
}

#[test]
fn test_invalid_connection() {
    let _guard = TEST_MUTEX.lock().unwrap();
    let result = get_node("localhost:99999");
    assert!(result.is_err());
}

#[test]
fn test_table_creation() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let node = get_test_node();

    let create = "CREATE TABLE simple_test (id INTEGER, name VARCHAR);";
    let result = node.query(create);
    // May fail if table already exists from another test, that's ok
    assert!(result.is_ok());

    Ok(())
}

#[test]
fn test_insert_null_values() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    // Clear any existing null rows
    execute_success(&mut node, "delete from test_types where t1 IS NULL;");

    let insert = "insert into test_types (T1) values (NULL);";
    execute_success(&mut node, insert);

    let mut table = block_for_result(&node.query("select * from test_types where t1 IS NULL;")?)?;
    assert!(table.get_row_count() > 0);
    table.advance_row();
    let test: Test = table.map_row();
    assert_eq!(test.t1, None);

    Ok(())
}

#[test]
fn test_insert_and_retrieve_all_types() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    // Clear existing data
    execute_success(&mut node, "delete from test_types where t2 = 2;");

    let insert = "insert into test_types (T1,T2,T3,T4,T5,T6,T7,T8,T9) values (1,2,3,4,5.5,6.66,'test','DEADBEEF',NOW());";
    execute_success(&mut node, insert);

    let mut table = block_for_result(&node.query("select * from test_types where t2 = 2;")?)?;
    assert!(table.get_row_count() > 0);
    table.advance_row();
    let test: Test = table.map_row();

    assert_eq!(test.t1, Some(true));
    assert_eq!(test.t2, Some(2i16));
    assert_eq!(test.t3, Some(3i32));
    assert_eq!(test.t4, Some(4i64));
    assert_eq!(test.t7, Some("test".to_owned()));

    Ok(())
}

#[test]
fn test_query_with_no_results() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    let table = block_for_result(&node.query("select * from test_types where t1 = 99;")?)?;
    assert_eq!(table.get_row_count(), 0);

    Ok(())
}

#[test]
fn test_multiple_inserts() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    // Clear existing data
    execute_success(
        &mut node,
        "delete from test_types where t2 >= 1000 and t2 < 1010;",
    );

    for i in 0..10 {
        let insert = format!(
            "insert into test_types (T1,T2,T3) values (1,{},{});",
            1000 + i,
            i * 10
        );
        execute_success(&mut node, &insert);
    }

    let mut table = block_for_result(
        &node.query("select count(*) from test_types where t2 >= 1000 and t2 < 1010;")?,
    )?;
    table.advance_row();
    let count = table.get_i64_by_idx(0).unwrap();
    assert_eq!(count, Some(10i64));

    Ok(())
}

#[test]
fn test_update_operation() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    // Clear and insert fresh data
    execute_success(&mut node, "delete from test_types where t2 = 5000;");
    let insert = "insert into test_types (T1,T2,T3) values (1,5000,200);";
    execute_success(&mut node, insert);

    let update = "update test_types set T3 = 999 where T2 = 5000;";
    execute_success(&mut node, update);

    let mut table = block_for_result(&node.query("select T3 from test_types where T2 = 5000;")?)?;
    assert!(table.get_row_count() > 0);
    table.advance_row();
    let t3 = table.get_i32_by_column("T3").unwrap();
    assert_eq!(t3, Some(999i32));

    Ok(())
}

#[test]
fn test_delete_operation() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    let insert = "insert into test_types (T1,T2) values (1,6000);";
    execute_success(&mut node, insert);

    let delete = "delete from test_types where T2 = 6000;";
    execute_success(&mut node, delete);

    let table = block_for_result(&node.query("select * from test_types where T2 = 6000;")?)?;
    assert_eq!(table.get_row_count(), 0);

    Ok(())
}

#[test]
fn test_transaction_rollback() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    // Attempt invalid insert (should fail/rollback)
    let invalid_insert =
        "insert into test_types (T1,T2,T3,T4,T5,T6,T7,T8,T9) values (1,2,3,4,5,6,'7','8',NOW());";
    let result = node.query(invalid_insert).unwrap();
    let mut table = result.recv().unwrap();
    assert!(table.has_error().is_some());

    Ok(())
}

#[test]
fn test_boundary_values() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    // Clear and insert fresh data
    execute_success(&mut node, "delete from test_types where t2 = 7000;");

    let insert = format!(
        "insert into test_types (T1,T2,T3,T4) values (1,{},{},{});",
        7000,
        i32::MAX,
        i64::MAX
    );
    execute_success(&mut node, &insert);

    let mut table =
        block_for_result(&node.query("select T2,T3,T4 from test_types where T2 = 7000;")?)?;
    assert!(table.get_row_count() > 0);
    table.advance_row();
    assert_eq!(table.get_i16_by_column("T2").unwrap(), Some(7000));
    assert_eq!(table.get_i32_by_column("T3").unwrap(), Some(i32::MAX));
    assert_eq!(table.get_i64_by_column("T4").unwrap(), Some(i64::MAX));

    Ok(())
}

#[test]
fn test_empty_string_and_binary() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    // Clear and insert fresh data
    execute_success(&mut node, "delete from test_types where t2 = 8000;");

    let insert = "insert into test_types (T1,T2,T7,T8) values (1,8000,'','');";
    execute_success(&mut node, insert);

    let mut table =
        block_for_result(&node.query("select T7,T8 from test_types where T2 = 8000;")?)?;
    assert!(table.get_row_count() > 0);
    table.advance_row();
    let t7 = table.get_string_by_column("T7").unwrap();
    assert_eq!(t7, Some("".to_owned()));

    Ok(())
}

#[test]
fn test_concurrent_reads() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock()?;
    let mut node = get_test_node();
    populate(&mut node);

    // Clear and insert fresh data
    execute_success(&mut node, "delete from test_types where t2 = 9000;");
    let insert = "insert into test_types (T1,T2) values (1,9000);";
    execute_success(&mut node, insert);

    let rc = Arc::new(AtomicPtr::new(&mut node));
    let mut handles = vec![];

    for _ in 0..10 {
        let local = Arc::clone(&rc);
        let handle = thread::spawn(move || unsafe {
            let load = local.load(Acquire);
            let res = (*load)
                .query("select T2 from test_types where T2 = 9000;")
                .unwrap();
            let mut table = block_for_result(&res).unwrap();
            if table.get_row_count() > 0 {
                table.advance_row();
                let t2 = table.get_i16_by_column("T2").unwrap();
                assert_eq!(t2, Some(9000i16));
            }
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }

    Ok(())
}

#[test]
fn test_multiples_thread() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    let insert = "insert into test_types (T1) values (NULL);";
    execute_success(&mut node, insert);

    let insert_value = "insert into test_types (T1,T2,T3,T4,T5,T6,T7,T8,T9) values (1,2,3,4,5,6,'7','089CD7B35220FFB686012A0B08B49ECD8C06109893971F422F4D4F4E49544F52494E475F33393766643034662D656161642D346230372D613638302D62663562633736666132363148D535A8019CD7B352B001DDEE8501B801BAEE8501C001AAE98601CA01054341534831D0010AE00102E80102F20103555344FA010A0A0355534410809BEE028202050A035553448A020B08B49ECD8C06109893971F9202046E756C6CA2020A0A0355534410C0BD9A2FBA0219312C323139313936382C323139333231302C32313933323435C802C91E8A0400920400D80401880505B20500',NOW());";

    block_for_result(&node.query(insert_value)?)?;
    let mut table =
        block_for_result(&node.query("select * from test_types where t1 = 1;")?).unwrap();
    if table.get_row_count() > 0 {
        table.advance_row();
        let test: Test = table.map_row();
        assert_eq!(test.t1, Some(true));
        assert_eq!(test.t2, Some(2i16));
        assert_eq!(test.t3, Some(3i32));
        assert_eq!(test.t4, Some(4i64));
        assert_eq!(test.t5, Some(5f64));
        assert_eq!(test.t6, Some(BigDecimal::from(6)));
        assert_eq!(test.t7, Some("7".to_owned()));
    }

    let rc = Arc::new(AtomicPtr::new(&mut node));
    let mut vec: Vec<JoinHandle<_>> = vec![];
    let start = SystemTime::now();

    for _ in 0..512 {
        let local = Arc::clone(&rc);
        let handle = thread::spawn(move || unsafe {
            let load = local.load(Acquire);
            let res = (*load)
                .query("select * from test_types where t1 = 1;")
                .unwrap();
            let mut table = block_for_result(&res).unwrap();
            if table.get_row_count() > 0 {
                table.advance_row();
                let _: Test = table.map_row();
            }
        });
        vec.push(handle);
    }

    for handle in vec {
        handle.join().unwrap();
    }

    let since_the_epoch = SystemTime::now()
        .duration_since(start)
        .expect("Time went backwards");
    println!("Total time: {:?}", since_the_epoch);
    Ok(())
}

#[test]
fn test_stored_procedure_execution() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    // Execute the stored procedure created during populate
    let result = node.query("EXEC ApplicationCreate;");
    assert!(result.is_ok());

    Ok(())
}

#[test]
fn test_jar_upload() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let node = get_test_node();

    let jars = fs::read("tests/procedures.jar").unwrap();
    let x = node.upload_jar(jars).unwrap();
    let mut table = x.recv().unwrap();
    assert!(table.has_error().is_none());

    Ok(())
}

#[test]
fn test_special_characters_in_varchar() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    // Clear and insert fresh data
    execute_success(&mut node, "delete from test_types where t2 = 10000;");

    let special_chars = "Test with 'quotes' and \"double\" and \\ backslash";
    let insert = format!(
        "insert into test_types (T1,T2,T7) values (1,10000,'{}');",
        special_chars.replace("'", "''")
    );
    execute_success(&mut node, &insert);

    Ok(())
}

#[test]
fn test_decimal_precision() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    // Clear and insert fresh data
    execute_success(&mut node, "delete from test_types where t2 = 11000;");

    let insert = "insert into test_types (T1,T2,T6) values (1,11000,123.456789);";
    execute_success(&mut node, insert);

    let mut table = block_for_result(&node.query("select T6 from test_types where T2 = 11000;")?)?;
    assert!(table.get_row_count() > 0);
    table.advance_row();
    let decimal = table.get_decimal_by_column("T6").unwrap();
    assert!(decimal.is_some());

    Ok(())
}

#[test]
fn test_timestamp_operations() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    // Clear and insert fresh data
    execute_success(&mut node, "delete from test_types where t2 = 12000;");

    let insert = "insert into test_types (T1,T2,T9) values (1,12000,NOW());";
    execute_success(&mut node, insert);

    let mut table = block_for_result(&node.query("select T9 from test_types where T2 = 12000;")?)?;
    assert!(table.get_row_count() > 0);
    table.advance_row();
    let timestamp = table.get_time_by_column("T9").unwrap();
    assert!(timestamp.is_some());

    Ok(())
}

#[test]
fn test_query_error_handling() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let node = get_test_node();

    let result = node.query("SELECT * FROM non_existent_table;");
    assert!(result.is_ok()); // Query sends successfully

    let mut table = result.unwrap().recv().unwrap();
    assert!(table.has_error().is_some()); // But response contains error

    Ok(())
}

#[test]
fn test_sequential_queries() -> Result<(), VoltError> {
    let _guard = TEST_MUTEX.lock().unwrap();
    let mut node = get_test_node();
    populate(&mut node);

    // Clear existing data in range
    execute_success(
        &mut node,
        "delete from test_types where t2 >= 13000 and t2 < 13100;",
    );

    for i in 0..100 {
        let query = format!(
            "insert into test_types (T1,T2,T3) values (1,{},{});",
            13000 + i,
            i
        );
        execute_success(&mut node, &query);
    }

    let mut table = block_for_result(
        &node.query("select count(*) from test_types where t2 >= 13000 and t2 < 13100;")?,
    )?;
    table.advance_row();
    let count = table.get_i64_by_idx(0).unwrap();
    assert_eq!(count, Some(100i64));

    Ok(())
}