simd-r-drive 0.4.0-alpha

SIMD-optimized append-only schema-less storage engine. Key-based binary storage in a single-file storage container.
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
use serial_test::serial;
use std::fs;
use std::io::Write;
use std::process::Command;

const TEST_STORAGE: &str = "test_storage.bin";
const TARGET_STORAGE: &str = "target_storage.bin";

#[test]
#[serial]
fn test_write_and_read() {
    fs::remove_file(TEST_STORAGE).ok(); // Cleanup before test

    // Write a value to the storage
    let output = Command::new("cargo")
        .args(&[
            "run",
            "--quiet",
            "--",
            TEST_STORAGE,
            "write",
            "test_key",
            "hello",
        ])
        .output()
        .expect("Failed to execute process");

    assert!(
        output.status.success(),
        "Write command failed: {:?}",
        output
    );

    // Read the value back
    let output = Command::new("cargo")
        .args(&["run", "--quiet", "--", TEST_STORAGE, "read", "test_key"])
        .output()
        .expect("Failed to execute process");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert_eq!(
        stdout.trim(),
        "hello",
        "Unexpected read output: {:?}",
        stdout
    );

    // Cleanup
    fs::remove_file(TEST_STORAGE).ok();
}

#[test]
#[serial]
fn test_write_without_value() {
    fs::remove_file(TEST_STORAGE).ok(); // Cleanup before test

    // Try writing without a value (should fail)
    let output = Command::new("cargo")
        .args(&["run", "--quiet", "--", TEST_STORAGE, "write", "test_key"])
        .env("FORCE_NO_TTY", "1") // Set env variable to override is_terminal()
        .stdin(std::process::Stdio::null()) // Explicitly set no stdin
        .output()
        .expect("Failed to execute process");

    assert!(
        !output.status.success(),
        "Expected failure on missing value"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("No value provided"),
        "Unexpected error message: {:?}",
        stderr
    );

    fs::remove_file(TEST_STORAGE).ok(); // Cleanup
}

#[test]
#[serial]
fn test_read_nonexistent_key() {
    fs::remove_file(TEST_STORAGE).ok(); // Cleanup before test

    // Ensure the storage file exists
    let mut file = fs::File::create(TEST_STORAGE).expect("Failed to create storage file");
    file.write_all(b"")
        .expect("Failed to initialize storage file");

    // Attempt to read a nonexistent key (should fail)
    let output = Command::new("cargo")
        .args(&[
            "run",
            "--quiet",
            "--",
            TEST_STORAGE,
            "read",
            "nonexistent_key",
        ])
        .stderr(std::process::Stdio::piped()) // Capture stderr
        .output()
        .expect("Failed to execute process");

    assert!(
        !output.status.success(),
        "Expected failure for nonexistent key, but command succeeded."
    );

    let stderr = String::from_utf8_lossy(&output.stderr);

    assert!(
        stderr.trim().contains("Key 'nonexistent_key' not found")
            || stderr.trim().contains("Failed to open storage"),
        "Unexpected error output: {:?}",
        stderr
    );

    fs::remove_file(TEST_STORAGE).ok();
}

#[test]
#[serial]
fn test_read_with_buffer_size() {
    fs::remove_file(TEST_STORAGE).ok(); // Cleanup before test

    let large_value = "A".repeat(128 * 1024); // 128KB of data

    // Write the large value to the storage using stdin
    let mut child = Command::new("cargo")
        .args(&["run", "--quiet", "--", TEST_STORAGE, "write", "large_key"])
        .stdin(std::process::Stdio::piped()) // Open a pipe to send data
        .spawn()
        .expect("Failed to execute process");

    // Send data through stdin
    if let Some(mut stdin) = child.stdin.take() {
        stdin
            .write_all(large_value.as_bytes())
            .expect("Failed to write to stdin");
    }

    let output = child
        .wait_with_output()
        .expect("Failed to wait on child process");

    assert!(
        output.status.success(),
        "Write command failed: {:?}",
        output
    );

    // Read the value back with a 64KB buffer size
    let output = Command::new("cargo")
        .args(&[
            "run",
            "--quiet",
            "--",
            TEST_STORAGE,
            "read",
            "large_key",
            "--buffer-size",
            "64K",
        ])
        .output()
        .expect("Failed to execute process");

    assert!(output.status.success(), "Read command failed: {:?}", output);

    let stdout = output.stdout;
    assert_eq!(
        stdout.len(),
        large_value.len(),
        "Output length does not match expected value length"
    );

    // Ensure output is chunked correctly
    assert!(
        stdout.chunks(65536).all(|chunk| chunk.len() <= 65536),
        "Read output was not properly chunked according to buffer size"
    );

    fs::remove_file(TEST_STORAGE).ok(); // Cleanup
}

#[test]
#[serial]
fn test_copy_key() {
    fs::remove_file(TEST_STORAGE).ok();
    fs::remove_file(TARGET_STORAGE).ok();

    // Write a value to the storage
    let output = Command::new("cargo")
        .args(&[
            "run",
            "--quiet",
            "--",
            TEST_STORAGE,
            "write",
            "copy_key",
            "copy_test",
        ])
        .output()
        .expect("Failed to execute process");
    assert!(
        output.status.success(),
        "Write command failed: {:?}",
        output
    );

    // Copy the key to target storage
    let output = Command::new("cargo")
        .args(&[
            "run",
            "--quiet",
            "--",
            TEST_STORAGE,
            "copy",
            "copy_key",
            TARGET_STORAGE,
        ])
        .output()
        .expect("Failed to execute process");
    assert!(output.status.success(), "Copy command failed: {:?}", output);

    // Read from target storage
    let output = Command::new("cargo")
        .args(&["run", "--quiet", "--", TARGET_STORAGE, "read", "copy_key"])
        .output()
        .expect("Failed to execute process");
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert_eq!(
        stdout.trim(),
        "copy_test",
        "Unexpected read output: {:?}",
        stdout
    );

    fs::remove_file(TEST_STORAGE).ok();
    fs::remove_file(TARGET_STORAGE).ok();
}

#[test]
#[serial]
fn test_rename_key() {
    fs::remove_file(TEST_STORAGE).ok();

    // Write a value
    let output = Command::new("cargo")
        .args(&[
            "run",
            "--quiet",
            "--",
            TEST_STORAGE,
            "write",
            "old_key",
            "rename_test",
        ])
        .output()
        .expect("Failed to execute process");
    assert!(
        output.status.success(),
        "Write command failed: {:?}",
        output
    );

    // Rename the key
    let output = Command::new("cargo")
        .args(&[
            "run",
            "--quiet",
            "--",
            TEST_STORAGE,
            "rename",
            "old_key",
            "new_key",
        ])
        .output()
        .expect("Failed to execute process");
    assert!(
        output.status.success(),
        "Rename command failed: {:?}",
        output
    );

    // Ensure old key doesn't exist
    let output = Command::new("cargo")
        .args(&["run", "--quiet", "--", TEST_STORAGE, "read", "old_key"])
        .output()
        .expect("Failed to execute process");
    assert!(!output.status.success(), "Old key should not exist");

    // Ensure new key exists
    let output = Command::new("cargo")
        .args(&["run", "--quiet", "--", TEST_STORAGE, "read", "new_key"])
        .output()
        .expect("Failed to execute process");
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert_eq!(
        stdout.trim(),
        "rename_test",
        "Unexpected read output: {:?}",
        stdout
    );

    fs::remove_file(TEST_STORAGE).ok();
}

#[test]
#[serial]
fn test_delete_key() {
    fs::remove_file(TEST_STORAGE).ok();

    // Write a value
    let output = Command::new("cargo")
        .args(&[
            "run",
            "--quiet",
            "--",
            TEST_STORAGE,
            "write",
            "delete_key",
            "delete_test",
        ])
        .output()
        .expect("Failed to execute process");
    assert!(
        output.status.success(),
        "Write command failed: {:?}",
        output
    );

    // Delete the key
    let output = Command::new("cargo")
        .args(&["run", "--quiet", "--", TEST_STORAGE, "delete", "delete_key"])
        .output()
        .expect("Failed to execute process");
    assert!(
        output.status.success(),
        "Delete command failed: {:?}",
        output
    );

    // Ensure key doesn't exist
    let output = Command::new("cargo")
        .args(&["run", "--quiet", "--", TEST_STORAGE, "read", "delete_key"])
        .output()
        .expect("Failed to execute process");
    assert!(!output.status.success(), "Deleted key should not exist");

    fs::remove_file(TEST_STORAGE).ok();
}

#[test]
#[serial]
fn test_metadata() {
    fs::remove_file(TEST_STORAGE).ok(); // Cleanup before test

    // Write a test value
    let output = Command::new("cargo")
        .args(&[
            "run",
            "--quiet",
            "--",
            TEST_STORAGE,
            "write",
            "test_key",
            "hello",
        ])
        .output()
        .expect("Failed to execute process");

    assert!(
        output.status.success(),
        "Write command failed: {:?}",
        output
    );

    // Retrieve metadata for the key
    let output = Command::new("cargo")
        .args(&["run", "--quiet", "--", TEST_STORAGE, "metadata", "test_key"])
        .output()
        .expect("Failed to execute process");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("METADATA SUMMARY"),
        "Metadata output invalid: {:?}",
        stdout
    );
    assert!(
        stdout.contains("ENTRY FOR:"),
        "Metadata missing ENTRY FOR: {:?}",
        stdout
    );
    assert!(
        stdout.contains("test_key"),
        "Metadata does not contain key: {:?}",
        stdout
    );
    assert!(
        stdout.contains("PAYLOAD SIZE:"),
        "Metadata missing payload size: {:?}",
        stdout
    );
    assert!(
        stdout.contains("TOTAL SIZE (W/ METADATA):"),
        "Metadata missing total size: {:?}",
        stdout
    );
    assert!(
        stdout.contains("OFFSET RANGE:"),
        "Metadata missing offset range: {:?}",
        stdout
    );
    assert!(
        stdout.contains("MEMORY ADDRESS:"),
        "Metadata missing memory address: {:?}",
        stdout
    );
    assert!(
        stdout.contains("KEY HASH:"),
        "Metadata missing key hash: {:?}",
        stdout
    );
    assert!(
        stdout.contains("CHECKSUM:"),
        "Metadata missing checksum: {:?}",
        stdout
    );
    assert!(
        stdout.contains("CHECKSUM VALIDITY:"),
        "Metadata missing checksum validity: {:?}",
        stdout
    );
    assert!(
        stdout.contains("STORED METADATA:"),
        "Metadata missing stored metadata: {:?}",
        stdout
    );

    fs::remove_file(TEST_STORAGE).ok(); // Cleanup
}

#[test]
#[serial]
fn test_info() {
    fs::remove_file(TEST_STORAGE).ok(); // Cleanup before test

    // Initialize an empty storage file
    let _ = fs::File::create(TEST_STORAGE).expect("Failed to create storage file");

    // Retrieve storage info
    let output = Command::new("cargo")
        .args(&["run", "--quiet", "--", TEST_STORAGE, "info"])
        .output()
        .expect("Failed to execute process");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("STORAGE INFO"),
        "Info output invalid: {:?}",
        stdout
    );
    assert!(
        stdout.contains("STORAGE FILE:"),
        "Info missing storage file: {:?}",
        stdout
    );
    assert!(
        stdout.contains("TOTAL SIZE:"),
        "Info missing total size: {:?}",
        stdout
    );
    assert!(
        stdout.contains("ACTIVE ENTRIES:"),
        "Info missing active entries: {:?}",
        stdout
    );
    assert!(
        stdout.contains("COMPACTION SAVINGS:"),
        "Info missing compaction savings: {:?}",
        stdout
    );

    fs::remove_file(TEST_STORAGE).ok(); // Cleanup
}