bonsaidb-files 0.1.0

Efficient large file storage for BonsaiDb
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
use std::io::{Read, Seek, Write};
use std::mem::size_of;

use bonsaidb_core::key::time::TimestampAsNanoseconds;
use bonsaidb_core::test_util::TestDirectory;
use bonsaidb_local::config::{Builder, StorageConfiguration};
#[cfg(feature = "async")]
use bonsaidb_local::AsyncDatabase;
use bonsaidb_local::Database;
#[cfg(feature = "async")]
use futures::StreamExt;
#[cfg(feature = "async")]
use tokio::io::{AsyncReadExt, AsyncWriteExt};

use crate::{BonsaiFiles, Error, FileConfig, FilesSchema, Truncate};

#[test]
fn simple_file_test() {
    let directory = TestDirectory::new("simple-file");
    let database = Database::open::<FilesSchema>(StorageConfiguration::new(&directory)).unwrap();

    let data = "hello, world!";
    let mut file = BonsaiFiles::build("/hello/world.txt")
        .contents(data.as_bytes())
        .create(&database)
        .unwrap();
    assert_eq!(file.len().unwrap(), u64::try_from(data.len()).unwrap());

    println!("Created file: {file:?}, length: {}", file.len().unwrap());

    let contents = file.contents().unwrap();
    assert_eq!(contents.len(), u64::try_from(data.len()).unwrap());

    let bytes = contents.into_string().unwrap();
    assert_eq!(bytes, data);

    let file = BonsaiFiles::load("/hello/world.txt", &database)
        .unwrap()
        .unwrap();
    assert_eq!(file.name(), "world.txt");
    assert_eq!(file.containing_path(), "/hello/");
    assert_eq!(file.contents().unwrap().to_string().unwrap(), data);

    file.delete().unwrap();
    assert!(BonsaiFiles::load("/hello/world.txt", &database)
        .unwrap()
        .is_none());
}

#[cfg(feature = "async")]
#[tokio::test]
async fn async_simple_file_test() {
    let directory = TestDirectory::new("simple-file-async");
    let database = AsyncDatabase::open::<FilesSchema>(StorageConfiguration::new(&directory))
        .await
        .unwrap();

    let data = "hello, world!";
    let mut file = BonsaiFiles::build("/hello/world.txt")
        .contents(data.as_bytes())
        .create_async(&database)
        .await
        .unwrap();
    assert_eq!(
        file.len().await.unwrap(),
        u64::try_from(data.len()).unwrap()
    );

    println!(
        "Created file: {file:?}, length: {}",
        file.len().await.unwrap()
    );

    let contents = file.contents().await.unwrap();
    assert_eq!(contents.len(), u64::try_from(data.len()).unwrap());

    let bytes = contents.into_string().await.unwrap();
    assert_eq!(bytes, data);

    let file = BonsaiFiles::load_async("/hello/world.txt", &database)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(file.name(), "world.txt");
    assert_eq!(file.containing_path(), "/hello/");
    assert_eq!(
        file.contents().await.unwrap().to_string().await.unwrap(),
        data
    );

    file.delete().await.unwrap();
    assert!(BonsaiFiles::load_async("/hello/world.txt", &database)
        .await
        .unwrap()
        .is_none());
}

#[test]
fn load_or_create_test() {
    let directory = TestDirectory::new("load-or-create");
    let database = Database::open::<FilesSchema>(StorageConfiguration::new(&directory)).unwrap();

    let file = BonsaiFiles::load_or_create("/hello/world.txt", true, &database).unwrap();
    let reloaded = BonsaiFiles::load_or_create("/hello/world.txt", true, &database).unwrap();
    assert_eq!(file, reloaded);
    let reloaded_unoptimal =
        BonsaiFiles::load_or_create("/hello/world.txt", false, &database).unwrap();
    assert_eq!(file, reloaded_unoptimal);

    file.delete().unwrap();
}

#[cfg(feature = "async")]
#[tokio::test]
async fn async_load_or_create_test() {
    let directory = TestDirectory::new("load-or-create-async");
    let database = AsyncDatabase::open::<FilesSchema>(StorageConfiguration::new(&directory))
        .await
        .unwrap();

    let file = BonsaiFiles::load_or_create_async("/hello/world.txt", true, &database)
        .await
        .unwrap();
    let reloaded = BonsaiFiles::load_or_create_async("/hello/world.txt", true, &database)
        .await
        .unwrap();
    assert_eq!(file, reloaded);
    let reloaded_unoptimal =
        BonsaiFiles::load_or_create_async("/hello/world.txt", false, &database)
            .await
            .unwrap();
    assert_eq!(file, reloaded_unoptimal);

    file.delete().await.unwrap();
}

#[test]
fn invalid_name_test() {
    let directory = TestDirectory::new("invalid-name");
    let database = Database::open::<FilesSchema>(StorageConfiguration::new(&directory)).unwrap();

    let err = BonsaiFiles::build("hello/.txt")
        .contents(b"hello, world!")
        .create(&database)
        .unwrap_err();
    assert!(matches!(err, Error::InvalidName));
}

#[cfg(feature = "async")]
#[tokio::test]
async fn async_invalid_name_test() {
    let directory = TestDirectory::new("invalid-name-async");
    let database = AsyncDatabase::open::<FilesSchema>(StorageConfiguration::new(&directory))
        .await
        .unwrap();

    let err = BonsaiFiles::build("hello/.txt")
        .contents(b"hello, world!")
        .create_async(&database)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::InvalidName));
}

#[test]
fn simple_path_test() {
    let directory = TestDirectory::new("simple-path");
    let database = Database::open::<FilesSchema>(StorageConfiguration::new(&directory)).unwrap();

    let data = b"hello";
    let file = BonsaiFiles::build("hello.txt")
        .at_path("/some/containing/path")
        .contents(data)
        .create(&database)
        .unwrap();
    let contents = file.contents().unwrap();
    println!("Created file: {file:?}, length: {}", contents.len());
    let bytes = contents.into_vec().unwrap();
    assert_eq!(bytes, data);

    let file = BonsaiFiles::load("/some/containing/path/hello.txt", &database)
        .unwrap()
        .unwrap();
    assert_eq!(file.name(), "hello.txt");
    assert_eq!(file.containing_path(), "/some/containing/path/");

    let data2 = b"world!";
    let file2 = BonsaiFiles::build("world.txt")
        .at_path("/some/")
        .contents(data2)
        .create(&database)
        .unwrap();
    let contents = file2.contents().unwrap();
    let bytes = contents.into_vec().unwrap();
    assert_eq!(bytes, data2);

    let some_contents = BonsaiFiles::list("/", &database).unwrap();
    assert_eq!(some_contents.len(), 0);
    // First query intentionally has no trailing slash. The rest have trailing slashes.
    let path_contents = BonsaiFiles::list("/some/containing/path", &database).unwrap();
    assert_eq!(path_contents.len(), 1);
    assert_eq!(path_contents[0].name(), "hello.txt");
    let path_contents = BonsaiFiles::list("/some/", &database).unwrap();
    assert_eq!(path_contents.len(), 1);
    let path_contents = BonsaiFiles::list("/some/containing/", &database).unwrap();
    assert_eq!(path_contents.len(), 0);

    let all_contents = BonsaiFiles::list_recursive("/", &database).unwrap();
    assert_eq!(all_contents.len(), 2);
    let all_contents = BonsaiFiles::list_recursive("/some", &database).unwrap();
    assert_eq!(all_contents.len(), 2);
    let all_contents = BonsaiFiles::list_recursive("/some/containing/", &database).unwrap();
    assert_eq!(all_contents.len(), 1);
    let all_contents = BonsaiFiles::list_recursive("/some/containing/path/", &database).unwrap();
    assert_eq!(all_contents.len(), 1);

    let stats = BonsaiFiles::stats(&database).unwrap();
    assert_eq!(
        stats.total_bytes,
        u64::try_from(data.len() + data2.len()).unwrap()
    );
    assert_eq!(stats.file_count, 2);
    assert_eq!(
        BonsaiFiles::stats_for_path("/some", &database)
            .unwrap()
            .total_bytes,
        u64::try_from(data.len() + data2.len()).unwrap()
    );
    assert_eq!(
        BonsaiFiles::stats_for_path("/some/containing", &database)
            .unwrap()
            .total_bytes,
        u64::try_from(data.len()).unwrap()
    );
    assert_eq!(
        BonsaiFiles::stats_for_path("/nonexistant", &database)
            .unwrap()
            .total_bytes,
        0
    );

    // Test renaming and moving
    let mut file = file;
    file.rename(String::from("new-name.txt")).unwrap();
    assert!(
        BonsaiFiles::load("/some/containing/path/hello.txt", &database)
            .unwrap()
            .is_none()
    );
    let mut file = BonsaiFiles::load("/some/containing/path/new-name.txt", &database)
        .unwrap()
        .unwrap();
    file.move_to("/new/path/").unwrap();
    assert!(BonsaiFiles::load("/new/path/new-name.txt", &database)
        .unwrap()
        .is_some());
    file.move_to("/final/path/and_name.txt").unwrap();
    let file = BonsaiFiles::load("/final/path/and_name.txt", &database)
        .unwrap()
        .unwrap();
    let contents = file.contents().unwrap().into_vec().unwrap();
    assert_eq!(contents, data);

    assert_eq!(
        BonsaiFiles::stats_for_path("/some", &database)
            .unwrap()
            .total_bytes,
        u64::try_from(data2.len()).unwrap()
    );
}

#[cfg(feature = "async")]
#[tokio::test]
#[allow(clippy::too_many_lines)]
async fn async_simple_path_test() {
    let directory = TestDirectory::new("simple-path-async");
    let database = AsyncDatabase::open::<FilesSchema>(StorageConfiguration::new(&directory))
        .await
        .unwrap();

    let data = b"hello";
    let file = BonsaiFiles::build("hello.txt")
        .at_path("/some/containing/path")
        .contents(data)
        .create_async(&database)
        .await
        .unwrap();
    let contents = file.contents().await.unwrap();
    println!("Created file: {file:?}, length: {}", contents.len());
    let bytes = contents.into_vec().await.unwrap();
    assert_eq!(bytes, data);

    let file = BonsaiFiles::load_async("/some/containing/path/hello.txt", &database)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(file.name(), "hello.txt");
    assert_eq!(file.containing_path(), "/some/containing/path/");

    let data2 = b"world!";
    let file2 = BonsaiFiles::build("world.txt")
        .at_path("/some/")
        .contents(data2)
        .create_async(&database)
        .await
        .unwrap();
    let contents = file2.contents().await.unwrap();
    let bytes = contents.into_vec().await.unwrap();
    assert_eq!(bytes, data2);

    let some_contents = BonsaiFiles::list_async("/", &database).await.unwrap();
    assert_eq!(some_contents.len(), 0);
    // First query intentionally has no trailing slash. The rest have trailing slashes.
    let path_contents = BonsaiFiles::list_async("/some/containing/path", &database)
        .await
        .unwrap();
    assert_eq!(path_contents.len(), 1);
    assert_eq!(path_contents[0].name(), "hello.txt");
    let path_contents = BonsaiFiles::list_async("/some/", &database).await.unwrap();
    assert_eq!(path_contents.len(), 1);
    let path_contents = BonsaiFiles::list_async("/some/containing/", &database)
        .await
        .unwrap();
    assert_eq!(path_contents.len(), 0);

    let all_contents = BonsaiFiles::list_recursive_async("/", &database)
        .await
        .unwrap();
    assert_eq!(all_contents.len(), 2);
    let all_contents = BonsaiFiles::list_recursive_async("/some", &database)
        .await
        .unwrap();
    assert_eq!(all_contents.len(), 2);
    let all_contents = BonsaiFiles::list_recursive_async("/some/containing/", &database)
        .await
        .unwrap();
    assert_eq!(all_contents.len(), 1);
    let all_contents = BonsaiFiles::list_recursive_async("/some/containing/path/", &database)
        .await
        .unwrap();
    assert_eq!(all_contents.len(), 1);

    let stats = BonsaiFiles::stats_async(&database).await.unwrap();
    assert_eq!(
        stats.total_bytes,
        u64::try_from(data.len() + data2.len()).unwrap()
    );
    assert_eq!(stats.file_count, 2);
    assert_eq!(
        BonsaiFiles::stats_for_path_async("/some", &database)
            .await
            .unwrap()
            .total_bytes,
        u64::try_from(data.len() + data2.len()).unwrap()
    );
    assert_eq!(
        BonsaiFiles::stats_for_path_async("/some/containing", &database)
            .await
            .unwrap()
            .total_bytes,
        u64::try_from(data.len()).unwrap()
    );
    assert_eq!(
        BonsaiFiles::stats_for_path_async("/nonexistant", &database)
            .await
            .unwrap()
            .total_bytes,
        0
    );

    // Test renaming and moving
    let mut file = file;
    file.rename(String::from("new-name.txt")).await.unwrap();
    assert!(
        BonsaiFiles::load_async("/some/containing/path/hello.txt", &database)
            .await
            .unwrap()
            .is_none()
    );
    let mut file = BonsaiFiles::load_async("/some/containing/path/new-name.txt", &database)
        .await
        .unwrap()
        .unwrap();
    file.move_to("/new/path/").await.unwrap();
    assert!(BonsaiFiles::load_async("/new/path/new-name.txt", &database)
        .await
        .unwrap()
        .is_some());
    file.move_to("/final/path/and_name.txt").await.unwrap();
    let file = BonsaiFiles::load_async("/final/path/and_name.txt", &database)
        .await
        .unwrap()
        .unwrap();
    let contents = file.contents().await.unwrap().into_vec().await.unwrap();
    assert_eq!(contents, data);

    assert_eq!(
        BonsaiFiles::stats_for_path_async("/some", &database)
            .await
            .unwrap()
            .total_bytes,
        u64::try_from(data2.len()).unwrap()
    );
}

enum SmallBlocks {}
impl FileConfig for SmallBlocks {
    type Metadata = usize;

    const BLOCK_SIZE: usize = 8;

    fn files_name() -> bonsaidb_core::schema::CollectionName {
        BonsaiFiles::files_name()
    }

    fn blocks_name() -> bonsaidb_core::schema::CollectionName {
        BonsaiFiles::blocks_name()
    }
}

#[test]
fn blocked_file_test() {
    let mut big_file = Vec::with_capacity(SmallBlocks::BLOCK_SIZE * 31 / 2);
    let mut counter = 0_u8;
    while big_file.len() + 4 < big_file.capacity() {
        counter += 1;
        for _ in 0..4 {
            big_file.push(counter);
        }
    }
    let directory = TestDirectory::new("blocked-file");
    let database =
        Database::open::<FilesSchema<SmallBlocks>>(StorageConfiguration::new(&directory)).unwrap();

    let test_start = TimestampAsNanoseconds::now();
    let mut file = SmallBlocks::build("hello.txt")
        .contents(&big_file)
        .create(&database)
        .unwrap();
    let contents = file.contents().unwrap();
    println!("Created file: {file:?}, length: {}", contents.len());
    assert!(file.last_appended_at().unwrap().unwrap() > test_start);
    assert!(contents.last_appended_at().unwrap() > test_start);
    let bytes = contents.into_vec().unwrap();
    assert_eq!(bytes, big_file);

    // Truncate the beginning of the file
    let new_length = u64::try_from(SmallBlocks::BLOCK_SIZE * 3).unwrap();
    big_file.splice(..big_file.len() - SmallBlocks::BLOCK_SIZE * 3, []);
    file.truncate(new_length, Truncate::RemovingStart).unwrap();
    assert_eq!(file.len().unwrap(), new_length);

    let contents = file.contents().unwrap();
    assert_eq!(contents.len(), new_length);
    assert_eq!(contents.into_vec().unwrap(), big_file);

    // Truncate the end of the file.
    let new_length = u64::try_from(SmallBlocks::BLOCK_SIZE).unwrap();
    big_file.truncate(SmallBlocks::BLOCK_SIZE);
    file.truncate(new_length, Truncate::RemovingEnd).unwrap();

    let contents = file.contents().unwrap();
    assert_eq!(contents.len(), new_length);
    assert_eq!(contents.to_vec().unwrap(), big_file);

    // Clear the file.
    file.truncate(0, Truncate::RemovingEnd).unwrap();
    assert_eq!(file.len().unwrap(), 0);
    assert!(file.last_appended_at().unwrap().is_none());
    assert!(file.contents().unwrap().last_appended_at().is_none());

    let mut writer = file.append_buffered();
    let buffer_size = SmallBlocks::BLOCK_SIZE * 3 / 2;
    writer.set_buffer_size(buffer_size).unwrap();
    // Write more than the single buffer size.
    let data_written = &bytes[0..SmallBlocks::BLOCK_SIZE * 2];
    writer.write_all(data_written).unwrap();
    assert_eq!(writer.buffer.len(), SmallBlocks::BLOCK_SIZE / 2);
    drop(writer);

    let contents = file.contents().unwrap();
    assert_eq!(contents.to_vec().unwrap(), data_written);
}

#[cfg(feature = "async")]
#[tokio::test]
async fn async_blocked_file_test() {
    let mut big_file = Vec::with_capacity(SmallBlocks::BLOCK_SIZE * 31 / 2);
    let mut counter = 0_u8;
    while big_file.len() + 4 < big_file.capacity() {
        counter += 1;
        for _ in 0..4 {
            big_file.push(counter);
        }
    }
    let directory = TestDirectory::new("blocked-file-async");
    let database =
        AsyncDatabase::open::<FilesSchema<SmallBlocks>>(StorageConfiguration::new(&directory))
            .await
            .unwrap();

    let test_start = TimestampAsNanoseconds::now();
    let mut file = SmallBlocks::build("hello.txt")
        .contents(&big_file)
        .create_async(&database)
        .await
        .unwrap();
    let contents = file.contents().await.unwrap();
    println!("Created file: {file:?}, length: {}", contents.len());
    assert!(file.last_appended_at().await.unwrap().unwrap() > test_start);
    let initial_write_timestamp = contents.last_appended_at().unwrap();
    assert!(initial_write_timestamp > test_start);
    let bytes = contents.into_vec().await.unwrap();
    assert_eq!(bytes, big_file);
    // Truncate the beginning of the file
    let new_length = u64::try_from(SmallBlocks::BLOCK_SIZE * 3).unwrap();
    big_file.splice(..big_file.len() - SmallBlocks::BLOCK_SIZE * 3, []);
    file.truncate(new_length, Truncate::RemovingStart)
        .await
        .unwrap();
    assert_eq!(file.len().await.unwrap(), new_length);

    let contents = file.contents().await.unwrap();
    assert_eq!(contents.len(), new_length);
    assert_eq!(contents.into_vec().await.unwrap(), big_file);

    // Truncate the end of the file.
    let new_length = u64::try_from(SmallBlocks::BLOCK_SIZE).unwrap();
    big_file.truncate(SmallBlocks::BLOCK_SIZE);
    file.truncate(new_length, Truncate::RemovingEnd)
        .await
        .unwrap();

    let contents = file.contents().await.unwrap();
    assert_eq!(contents.len(), new_length);
    assert_eq!(contents.to_vec().await.unwrap(), big_file);

    // Clear the file.
    file.truncate(0, Truncate::RemovingEnd).await.unwrap();
    assert_eq!(file.len().await.unwrap(), 0);
    assert!(file.last_appended_at().await.unwrap().is_none());
    assert!(file.contents().await.unwrap().last_appended_at().is_none());

    let mut writer = file.append_buffered();
    let buffer_size = SmallBlocks::BLOCK_SIZE * 3 / 2;
    writer.set_buffer_size(buffer_size).await.unwrap();
    // Write more than the single buffer size.
    let data_written = &bytes[0..SmallBlocks::BLOCK_SIZE * 2];
    writer.write_all(data_written).await.unwrap();
    assert_eq!(writer.buffer.len(), SmallBlocks::BLOCK_SIZE / 2);
    drop(writer);

    // Dropping an unflushed writer will flush it in the background.
    tokio::time::sleep(std::time::Duration::from_millis(250)).await;

    let contents = file.contents().await.unwrap();
    assert!(contents.last_appended_at().unwrap() > initial_write_timestamp);
    assert_eq!(contents.to_vec().await.unwrap(), data_written);
}

#[test]
fn seek_read_test() {
    let mut file_contents = Vec::with_capacity(BonsaiFiles::BLOCK_SIZE * 3);
    let word_size = size_of::<usize>();
    while file_contents.len() + word_size < file_contents.capacity() {
        file_contents.extend(file_contents.len().to_be_bytes());
    }
    let directory = TestDirectory::new("seek-read");
    let database = Database::open::<FilesSchema>(StorageConfiguration::new(&directory)).unwrap();

    let file = BonsaiFiles::build("hello.bin")
        .contents(&file_contents)
        .create(&database)
        .unwrap();
    let mut contents = file
        .contents()
        .unwrap()
        .with_buffer_size(BonsaiFiles::BLOCK_SIZE);
    // Read the last 16 bytes
    contents.seek(std::io::SeekFrom::End(-16)).unwrap();
    let mut buffer = [0; 16];
    contents.read_exact(&mut buffer).unwrap();
    assert_eq!(&file_contents[file_contents.len() - 16..], &buffer);

    // Seek slightly ahead of the start, and read 16 bytes.
    contents.seek(std::io::SeekFrom::Start(16)).unwrap();
    contents.read_exact(&mut buffer).unwrap();
    assert_eq!(&file_contents[16..32], &buffer);

    // Move foward into the next block.
    contents
        .seek(std::io::SeekFrom::Current(
            i64::try_from(BonsaiFiles::BLOCK_SIZE).unwrap(),
        ))
        .unwrap();
    contents.read_exact(&mut buffer).unwrap();
    assert_eq!(
        &file_contents[BonsaiFiles::BLOCK_SIZE + 32..BonsaiFiles::BLOCK_SIZE + 48],
        &buffer
    );
    // Re-read the last 16 bytes
    contents.seek(std::io::SeekFrom::Current(-16)).unwrap();
    assert_eq!(
        &file_contents[BonsaiFiles::BLOCK_SIZE + 32..BonsaiFiles::BLOCK_SIZE + 48],
        &buffer
    );
    // Try seeking to the end of the universe. It should return the file's length.
    assert_eq!(
        contents.seek(std::io::SeekFrom::End(i64::MAX)).unwrap(),
        contents.len()
    );
    // Verify read returns 0 bytes.
    assert_eq!(contents.read(&mut buffer).unwrap(), 0);
}

#[cfg(feature = "async")]
#[tokio::test]
async fn async_seek_read_test() {
    let mut file_contents = Vec::with_capacity(BonsaiFiles::BLOCK_SIZE * 3);
    let word_size = size_of::<usize>();
    while file_contents.len() + word_size < file_contents.capacity() {
        file_contents.extend(file_contents.len().to_be_bytes());
    }
    let directory = TestDirectory::new("seek-read-async");
    let database = AsyncDatabase::open::<FilesSchema>(StorageConfiguration::new(&directory))
        .await
        .unwrap();

    let file = BonsaiFiles::build("hello.bin")
        .contents(&file_contents)
        .create_async(&database)
        .await
        .unwrap();
    let mut contents = file
        .contents()
        .await
        .unwrap()
        .with_buffer_size(BonsaiFiles::BLOCK_SIZE);
    // Read the last 16 bytes
    contents.seek(std::io::SeekFrom::End(-16)).unwrap();
    let mut buffer = [0; 16];
    contents.read_exact(&mut buffer).await.unwrap();
    assert_eq!(&file_contents[file_contents.len() - 16..], &buffer);

    // Seek slightly ahead of the start, and read 16 bytes.
    contents.seek(std::io::SeekFrom::Start(16)).unwrap();
    contents.read_exact(&mut buffer).await.unwrap();
    assert_eq!(&file_contents[16..32], &buffer);

    // Move foward into the next block.
    contents
        .seek(std::io::SeekFrom::Current(
            i64::try_from(BonsaiFiles::BLOCK_SIZE).unwrap(),
        ))
        .unwrap();
    contents.read_exact(&mut buffer).await.unwrap();
    assert_eq!(
        &file_contents[BonsaiFiles::BLOCK_SIZE + 32..BonsaiFiles::BLOCK_SIZE + 48],
        &buffer
    );
    // Re-read the last 16 bytes
    contents.seek(std::io::SeekFrom::Current(-16)).unwrap();
    assert_eq!(
        &file_contents[BonsaiFiles::BLOCK_SIZE + 32..BonsaiFiles::BLOCK_SIZE + 48],
        &buffer
    );
    // Try seeking to the end of the universe. It should return the file's length.
    assert_eq!(
        contents.seek(std::io::SeekFrom::End(i64::MAX)).unwrap(),
        contents.len()
    );
    // Verify read returns 0 bytes.
    assert_eq!(contents.read(&mut buffer).await.unwrap(), 0);
}

#[test]
fn block_iterator_test() {
    let mut file_contents = Vec::with_capacity(BonsaiFiles::BLOCK_SIZE * 3);
    let word_size = size_of::<usize>();
    while file_contents.len() + word_size < file_contents.capacity() {
        file_contents.extend(file_contents.len().to_be_bytes());
    }
    let directory = TestDirectory::new("block-iterator");
    let database = Database::open::<FilesSchema>(StorageConfiguration::new(&directory)).unwrap();

    let file = BonsaiFiles::build("hello.bin")
        .contents(&file_contents)
        .create(&database)
        .unwrap();
    let mut compare_against = &file_contents[..];
    let mut contents = file
        .contents()
        .unwrap()
        .with_buffer_size(BonsaiFiles::BLOCK_SIZE);
    // read a few bytes to test partial iteration.
    let mut buffer = [0; 4];
    contents.read_exact(&mut buffer).unwrap();
    compare_against = &compare_against[4..];
    assert_eq!(buffer, &compare_against[..4]);

    for block in contents {
        let bytes = block.unwrap();
        assert_eq!(bytes, compare_against[..bytes.len()]);
        compare_against = &compare_against[bytes.len()..];
    }
    assert!(compare_against.is_empty());
}

#[tokio::test]
#[cfg(feature = "async")]
async fn block_stream_test() {
    let mut file_contents = Vec::with_capacity(BonsaiFiles::BLOCK_SIZE * 3);
    let word_size = size_of::<usize>();
    while file_contents.len() + word_size < file_contents.capacity() {
        file_contents.extend(file_contents.len().to_be_bytes());
    }
    let directory = TestDirectory::new("block-stream");
    let database =
        AsyncDatabase::open::<FilesSchema<BonsaiFiles>>(StorageConfiguration::new(&directory))
            .await
            .unwrap();

    let file = BonsaiFiles::build("hello.bin")
        .contents(&file_contents)
        .create_async(&database)
        .await
        .unwrap();
    let mut compare_against = &file_contents[..];
    let mut contents = file
        .contents()
        .await
        .unwrap()
        .with_buffer_size(BonsaiFiles::BLOCK_SIZE);
    // read a few bytes to test partial iteration.
    let mut buffer = [0; 4];
    contents.read_exact(&mut buffer).await.unwrap();
    compare_against = &compare_against[4..];
    assert_eq!(buffer, &compare_against[..4]);

    while let Some(block) = contents.next().await {
        let bytes = block.unwrap();
        assert_eq!(bytes, compare_against[..bytes.len()]);
        compare_against = &compare_against[bytes.len()..];
    }
    assert!(compare_against.is_empty());
}

#[test]
fn simple_metadata_test() {
    let directory = TestDirectory::new("simple-metadata");
    let database =
        Database::open::<FilesSchema<SmallBlocks>>(StorageConfiguration::new(&directory)).unwrap();

    let file = SmallBlocks::build("/hello/world.txt")
        .contents(b"hello, world!")
        .metadata(42)
        .create(&database)
        .unwrap();
    assert_eq!(file.metadata(), &42);
    let mut file = SmallBlocks::get(file.id(), &database).unwrap().unwrap();
    assert_eq!(file.metadata(), &42);
    *file.metadata_mut() = 52;
    file.update_metadata().unwrap();

    let file = SmallBlocks::get(file.id(), &database).unwrap().unwrap();
    assert_eq!(file.metadata(), &52);
}

#[cfg(feature = "async")]
#[tokio::test]
async fn async_metadata_test() {
    let directory = TestDirectory::new("simple-metadata-async");
    let database =
        AsyncDatabase::open::<FilesSchema<SmallBlocks>>(StorageConfiguration::new(&directory))
            .await
            .unwrap();

    let file = SmallBlocks::build("/hello/world.txt")
        .contents(b"hello, world!")
        .metadata(42)
        .create_async(&database)
        .await
        .unwrap();
    assert_eq!(file.metadata(), &42);
    let mut file = SmallBlocks::get_async(file.id(), &database)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(file.metadata(), &42);
    *file.metadata_mut() = 52;
    file.update_metadata().await.unwrap();

    let file = SmallBlocks::get_async(file.id(), &database)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(file.metadata(), &52);
}