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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
// Copyright 2018 Zach Miller
//
// Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT License (https://opensource.org/licenses/MIT), at your option.
//
// This file may not be copied, modified, or distributed except according to those terms.

use bincode;
use serde::de::DeserializeOwned;
use serde::ser::Serialize;

use std::fs::{self, File};
use std::io::{BufReader, BufWriter};
use std::path::{Path, PathBuf};

use error::{Error, Result};
use storage::{CommittedCheckpoint, Storage, UncommittedCheckpoint};
use wrappers::GuardWrapper;

/// Provides checkpoint storage via the file system.
#[derive(Debug)]
pub struct FileStorage {
    committed_path: PathBuf,
    uncommitted_path: PathBuf,
}

impl FileStorage {
    /// Creates a `FileStorage` object around a new or existing file path and wraps the object in a
    /// [`GuardWrapper`](
    ///     https://docs.rs/checkpoint/0.1.5/checkpoint/wrappers/struct.GuardWrapper.html).
    ///
    /// If `path` already exists it must point to a directory which contains two sub-folders,
    /// "committed" and "uncommitted".
    pub fn open(path: &Path) -> Result<GuardWrapper<Self>> {
        let committed_path = path.join("committed");
        let uncommitted_path = path.join("uncommitted");

        if path.exists() {
            if !path.is_dir() {
                Err(Error::initialization(format!(
                    "{:?} is not a directory",
                    path
                )))?;
            }

            if !committed_path.exists() || !uncommitted_path.exists() {
                Err(Error::initialization(format!(
                    "{:?} does not contain checkpoint data",
                    path
                )))?;
            }
        } else {
            fs::create_dir_all(path).map_err(|_| {
                Error::io(format!("Could not create {:?} to store checkpoints", path))
            })?;

            fs::create_dir(&committed_path).map_err(|_| {
                Error::io(format!(
                    "Could not create {:?} to store committed checkpoints",
                    committed_path
                ))
            })?;

            fs::create_dir(&uncommitted_path).map_err(|_| {
                Error::io(format!(
                    "Could not create {:?} to store uncommitted checkpoints",
                    uncommitted_path
                ))
            })?;
        }

        Ok(GuardWrapper::wrap(Self {
            committed_path,
            uncommitted_path,
        })?)
    }
}

impl Storage for FileStorage {
    type Committed = Committed;
    type Uncommitted = Uncommitted;

    /// Creates a new checkpoint with the specified identifier.
    ///
    /// The checkpoint will not be saved permanently until it has been committed via the
    /// `commit_checkpoint` method.
    ///
    /// Note: The provided identifier must be a valid filename.
    fn create_checkpoint(&mut self, identifier: &str) -> Result<Self::Uncommitted> {
        let uncommitted_checkpoint_path = self.uncommitted_path.join(identifier);
        if uncommitted_checkpoint_path.exists() {
            fs::remove_dir_all(&uncommitted_checkpoint_path).map_err(|_| {
                Error::io(format!(
                    "Could not remove {:?} which contains uncommitted checkpoint data",
                    uncommitted_checkpoint_path
                ))
            })?;
        }

        fs::create_dir(&uncommitted_checkpoint_path).map_err(|_| {
            Error::io(format!(
                "Could not create {:?} to store uncommitted checkpoint data",
                uncommitted_checkpoint_path
            ))
        })?;

        Ok(Self::Uncommitted::new(
            identifier,
            &uncommitted_checkpoint_path,
        ))
    }

    fn commit_checkpoint(&mut self, mut uncommitted: Self::Uncommitted) -> Result<Self::Committed> {
        let identifier = uncommitted.identifier()?.to_string();
        let committed_checkpoint_path = self.committed_path.join(&identifier);

        fs::rename(&uncommitted.inner.path, &committed_checkpoint_path).map_err(|_| {
            Error::io(format!(
                "Could not create {:?} to store committed checkpoint data",
                committed_checkpoint_path
            ))
        })?;

        Ok(Self::Committed::new(
            &identifier,
            &committed_checkpoint_path,
        ))
    }

    fn load_checkpoint(&mut self, identifier: &str) -> Result<Self::Committed> {
        Ok(Self::Committed::new(
            identifier,
            &self.committed_path.join(identifier),
        ))
    }

    fn remove_checkpoint(&mut self, identifier: &str) -> Result<()> {
        let committed_checkpoint_path = self.committed_path.join(identifier);
        fs::remove_dir_all(committed_checkpoint_path)
            .map_err(|_| Error::io(format!("Could not remove checkpoint: {}", identifier)))?;

        Ok(())
    }

    fn checkpoint_identifiers(&mut self) -> Result<Vec<String>> {
        let mut identifiers = Vec::new();
        let entries = fs::read_dir(&self.committed_path)
            .map_err(|_| Error::io("Could not retrieve checkpoint identifiers".to_string()))?;

        for entry in entries {
            let entry = entry
                .map_err(|_| Error::io("Could not retrieve checkpoint identifiers".to_string()))?;

            let path = entry.path();
            if path.is_dir() {
                if let Some(file_name) = path.file_name() {
                    if let Some(file_name) = file_name.to_str() {
                        identifiers.push(file_name.to_string());
                    } else {
                        Err(Error::invalid_identifier(format!(
                            "Directory {:?} contains invalid checkpoint identifier",
                            self.committed_path
                        )))?;
                    }
                }
            }
        }

        Ok(identifiers)
    }
}

// Provides access to checkpoint data stored in the file system.
#[derive(Debug)]
struct CheckpointStorage {
    identifier: String,
    path: PathBuf,
}

impl CheckpointStorage {
    fn new(identifier: &str, path: &Path) -> Self {
        Self {
            identifier: identifier.to_string(),
            path: path.to_path_buf(),
        }
    }
}

impl CheckpointStorage {
    fn get<T: DeserializeOwned + Serialize>(&mut self, key: &str) -> Result<T> {
        let filepath = self.path.join(key);
        let identifier = self.identifier()?;
        if !filepath.exists() {
            Err(Error::cp_key_doesnt_exist(format!(
                "No value stored for checkpoint: {}, key: {}",
                identifier, key
            )))?;
        }

        let file = File::open(filepath).map_err(|_| {
            Error::io(format!(
                "Could not retrieve value for checkpoint: {}, key: {}",
                identifier, key
            ))
        })?;

        let reader = BufReader::new(file);
        let value = bincode::deserialize_from(reader).map_err(|_| {
            Error::deserialize(format!(
                "Failed to deserialize value from checkpoint: {}, key: {}",
                identifier, key
            ))
        })?;

        Ok(value)
    }

    fn keys(&mut self) -> Result<Vec<String>> {
        let identifier = self.identifier()?.to_string();
        let entries = fs::read_dir(&self.path).map_err(|_| {
            Error::io(format!(
                "Could not retrieve keys for checkpoint: {}",
                &identifier
            ))
        })?;

        let mut keys = Vec::new();
        for entry in entries {
            let entry = entry.map_err(|_| {
                Error::io(format!(
                    "Could not retrieve keys for checkpoint: {}",
                    &identifier
                ))
            })?;

            let path = entry.path();
            if path.is_file() {
                if let Some(file_name) = path.file_name() {
                    if let Some(file_name) = file_name.to_str() {
                        keys.push(file_name.to_string());
                    } else {
                        Err(Error::invalid_identifier(format!(
                            "Directory {:?} contains invalid checkpoint identifier",
                            self.path
                        )))?;
                    }
                }
            }
        }

        Ok(keys)
    }

    fn identifier(&mut self) -> Result<&str> {
        Ok(self.identifier.as_str())
    }

    fn put<T: DeserializeOwned + Serialize>(&mut self, key: &str, value: &T) -> Result<()> {
        let identifier = self.identifier()?.to_string();
        let filepath = self.path.join(key);
        let file = File::create(filepath).map_err(|_| {
            Error::io(format!(
                "Could not insert value into checkpoint: {}, key: {}",
                &identifier, key
            ))
        })?;

        let writer = BufWriter::new(file);
        bincode::serialize_into(writer, value).map_err(|_| {
            Error::serialize(format!(
                "Failed to serialize value into checkpoint: {}, key: {}",
                identifier, key
            ))
        })?;
        Ok(())
    }

    fn remove(&mut self, key: &str) -> Result<()> {
        let identifier = self.identifier()?.to_string();
        let filepath = self.path.join(key);
        if !filepath.exists() {
            Err(Error::cp_key_doesnt_exist(format!(
                "No value to remove for checkpoint: {}, key: {}",
                &identifier, key
            )))?;
        }

        fs::remove_file(filepath).map_err(|_| {
            Error::io(format!(
                "Could not remove value from checkpoint: {}, key: {}",
                &identifier, key
            ))
        })?;
        Ok(())
    }
}

/// Provides access to committed checkpoint data stored in the file system.
#[derive(Debug)]
pub struct Committed {
    inner: CheckpointStorage,
}

impl Committed {
    fn new(identifier: &str, path: &Path) -> Self {
        Self {
            inner: CheckpointStorage::new(identifier, path),
        }
    }
}

impl CommittedCheckpoint for Committed {
    fn get<T: DeserializeOwned + Serialize>(&mut self, key: &str) -> Result<T> {
        self.inner.get(key)
    }

    fn keys(&mut self) -> Result<Vec<String>> {
        self.inner.keys()
    }

    fn identifier(&mut self) -> Result<&str> {
        self.inner.identifier()
    }
}

/// Provides access to uncommitted checkpoint data stored in the file system.
#[derive(Debug)]
pub struct Uncommitted {
    inner: CheckpointStorage,
}

impl Uncommitted {
    fn new(identifier: &str, path: &Path) -> Self {
        Self {
            inner: CheckpointStorage::new(identifier, path),
        }
    }
}

impl UncommittedCheckpoint for Uncommitted {
    fn get<T: DeserializeOwned + Serialize>(&mut self, key: &str) -> Result<T> {
        self.inner.get(key)
    }

    fn keys(&mut self) -> Result<Vec<String>> {
        self.inner.keys()
    }

    fn identifier(&mut self) -> Result<&str> {
        self.inner.identifier()
    }

    fn put<T: DeserializeOwned + Serialize>(&mut self, key: &str, value: &T) -> Result<()> {
        self.inner.put(key, value)
    }

    fn remove(&mut self, key: &str) -> Result<()> {
        self.inner.remove(key)
    }
}

impl Drop for Uncommitted {
    fn drop(&mut self) {
        if self.inner.path.exists() {
            let _ = fs::remove_dir_all(&self.inner.path);
        }
    }
}

#[cfg(all(test, feature = "filestorage-tests"))]
mod test {
    use std::fs;
    use std::mem;

    use error::ErrorKind;
    use storage::{CommittedCheckpoint, FileStorage, Storage, UncommittedCheckpoint};
    use test_utils::{create_named_test_file, create_test_directory,
                     create_test_directory_with_checkpoint_folders};

    // Verify that opening a FileStorage object with an existing path works as expected.
    #[test]
    fn file_storage_open_existing_path() {
        let dir = create_test_directory();
        let path = dir.path();
        let committed_path = path.join("committed");
        let uncommitted_path = path.join("uncommitted");

        // Verify that FileStorage::open() returns an error when a filepath is provided.
        let temp_file = create_named_test_file(&path);
        assert_eq!(
            FileStorage::open(&temp_file.path()).unwrap_err().kind(),
            ErrorKind::Initialization
        );

        // Verify that FileStorage::open() returns an error when the specified directory does not
        // contain a "committed" sub-directory.
        fs::create_dir(&uncommitted_path).unwrap();
        assert_eq!(
            FileStorage::open(path).unwrap_err().kind(),
            ErrorKind::Initialization
        );
        fs::remove_dir(&uncommitted_path).unwrap();

        // Verify that FileStorage::open() returns an error when the specified directory does not
        // contain an "uncommitted" sub-directory.
        fs::create_dir(&committed_path).unwrap();
        assert_eq!(
            FileStorage::open(path).unwrap_err().kind(),
            ErrorKind::Initialization
        );
        fs::remove_dir(&committed_path).unwrap();

        // Verify that FileStorage::open() is successful when the specified directory contains both
        // a "committed" sub-directory and an "uncommitted" sub-directory.
        fs::create_dir(&committed_path).unwrap();
        fs::create_dir(&uncommitted_path).unwrap();
        assert!(FileStorage::open(path).is_ok());
    }

    // Verify that opening a FileStorage object with a non-existing path works as expected.
    #[test]
    fn file_storage_open_non_existing_path() {
        let dir = create_test_directory();

        // Add "Checkpoints" to the path so that an empty directory can be provided to
        // FileStorage::open() below.
        let path = dir.path().join("Checkpoints");

        // Verify that FileStorage::open() is successful when the specified directory does
        // not exist.
        assert!(FileStorage::open(&path).is_ok());
    }

    // Verify that checkpoint creation and commit behave as expected.
    #[test]
    fn file_storage_create_and_commit_checkpoint() {
        let dir = create_test_directory_with_checkpoint_folders();
        let path = dir.path();

        let mut storage = FileStorage::open(&path).unwrap();

        // Verify that create_checkpoint() can be called twice with the same checkpoint identifier
        // as long as the uncommitted checkpoint returned from the first call is dropped before
        // the second call.
        assert!(storage.create_checkpoint("CP").is_ok());
        assert!(storage.create_checkpoint("CP").is_ok());

        // Verify that create_checkpoint() returns an error when a checkpoint creation is attempted
        // with the same identifier as an uncommitted checkpoint.
        let uncommitted_cp = storage.create_checkpoint("CP").unwrap();
        assert_eq!(
            storage.create_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpAlreadyExists
        );

        // Verify that create_checkpoint() returns an error when a checkpoint creation is attempted
        // with the same identifier as a committed checkpoint that is in use (i.e. has a variable
        // binding).
        let committed_cp = storage.commit_checkpoint(uncommitted_cp).unwrap();
        assert_eq!(
            storage.create_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpAlreadyExists
        );

        // Verify that create_checkpoint() returns an error when a checkpoint creation is attempted
        // with the same identifier as a committed checkpoint that is not in use (i.e. has no
        // variable binding).
        mem::drop(committed_cp);
        assert_eq!(
            storage.create_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpAlreadyExists
        );
    }

    // Verify that checkpoint loading and removal behave as expected.
    #[test]
    fn file_storage_load_and_remove_checkpoint() {
        let dir = create_test_directory_with_checkpoint_folders();
        let path = dir.path();

        let mut storage = FileStorage::open(&path).unwrap();

        // Verify that load_checkpoint() returns an error when a checkpoint load is attempted but
        // a committed checkpoint with the given identifier does not exist.
        assert_eq!(
            storage.load_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpDoesntExist
        );

        // Verify that load_checkpoint() returns an error when a checkpoint load is attempted, even
        // if an uncommitted checkpoint with the given identifier exists.
        let uncommitted_cp = storage.create_checkpoint("CP").unwrap();
        assert_eq!(
            storage.load_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpDoesntExist
        );

        // Verify that load_checkpoint() returns a committed checkpoint binding when a committed
        // checkpoint with the given identifier exists and is in use (i.e. has a variable binding).
        let committed_cp = storage.commit_checkpoint(uncommitted_cp).unwrap();
        assert!(storage.load_checkpoint("CP").is_ok());

        // Verify that load_checkpoint() returns a committed checkpoint binding when a committed
        // checkpoint with the given identifier exists and is not in use (i.e. has no
        // variable binding).
        mem::drop(committed_cp);
        let result = storage.load_checkpoint("CP");
        assert!(result.is_ok());

        // Verify that remove_checkpoint() returns an error when an attempt to remove a checkpoint
        // that is in use (i.e. has a variable binding) is made.
        let committed_cp = result.unwrap();
        assert_eq!(
            storage.remove_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpInUse
        );

        // Verify that remove_checkpoint() is successful when an attempt to remove a checkpoint
        // that is not in use (i.e. does not have a variable binding) is made.
        mem::drop(committed_cp);
        assert!(storage.remove_checkpoint("CP").is_ok());

        // Verify that remove_checkpoint() returns an error when an attempt to remove a checkpoint
        // that doesn't exist is made.
        assert_eq!(
            storage.remove_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpDoesntExist
        );

        // Verify that the mechanism which keeps track of the number of times a committed checkpoint
        // has been loaded works as expected.
        let uncommitted_cp = storage.create_checkpoint("CP").unwrap();
        let cp1 = storage.commit_checkpoint(uncommitted_cp).unwrap();
        assert_eq!(
            storage.remove_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpInUse
        );

        let cp2 = storage.load_checkpoint("CP").unwrap();
        assert_eq!(
            storage.remove_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpInUse
        );

        mem::drop(cp1);
        assert_eq!(
            storage.remove_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpInUse
        );

        let cp3 = storage.load_checkpoint("CP").unwrap();
        assert_eq!(
            storage.remove_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpInUse
        );

        let cp4 = storage.load_checkpoint("CP").unwrap();
        assert_eq!(
            storage.remove_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpInUse
        );

        let cp5 = storage.load_checkpoint("CP").unwrap();
        assert_eq!(
            storage.remove_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpInUse
        );

        // Start dropping loaded checkpoints in an arbitrary order.
        mem::drop(cp4);
        assert_eq!(
            storage.remove_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpInUse
        );

        mem::drop(cp2);
        assert_eq!(
            storage.remove_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpInUse
        );

        mem::drop(cp5);
        assert_eq!(
            storage.remove_checkpoint("CP").unwrap_err().kind(),
            ErrorKind::CpInUse
        );

        mem::drop(cp3);
        assert!(storage.remove_checkpoint("CP").is_ok());
    }

    // Verify that checkpoint identifiers are returned properly.
    #[test]
    fn file_storage_checkpoint_identifiers() {
        let dir = create_test_directory_with_checkpoint_folders();
        let path = dir.path();

        let mut storage = FileStorage::open(&path).unwrap();

        // Verify that checkpoint_identifiers() returns an empty list when there are no
        // committed checkpoints.
        assert_eq!(
            storage.checkpoint_identifiers().unwrap(),
            Vec::<String>::new()
        );

        // Verify that checkpoint_identifiers() returns an empty list even when there are
        // uncommitted checkpoints.
        let uncommitted_cp_1 = storage.create_checkpoint("CP1").unwrap();
        assert_eq!(
            storage.checkpoint_identifiers().unwrap(),
            Vec::<String>::new()
        );

        // Verify that checkpoint_identifiers() returns a list of identifiers when committed
        // checkpoints exist and are loaded (i.e. when they have variable bindings).
        let committed_cp_1 = storage.commit_checkpoint(uncommitted_cp_1).unwrap();
        assert_eq!(
            storage.checkpoint_identifiers().unwrap(),
            vec!["CP1".to_string()]
        );

        let uncommitted_cp_2 = storage.create_checkpoint("CP2").unwrap();
        let committed_cp_2 = storage.commit_checkpoint(uncommitted_cp_2).unwrap();
        let mut identifiers = storage.checkpoint_identifiers().unwrap();
        identifiers.sort();
        assert_eq!(identifiers, vec!["CP1".to_string(), "CP2".to_string()]);

        let uncommitted_cp_3 = storage.create_checkpoint("CP3").unwrap();
        let committed_cp_3 = storage.commit_checkpoint(uncommitted_cp_3).unwrap();
        let mut identifiers = storage.checkpoint_identifiers().unwrap();
        identifiers.sort();
        assert_eq!(
            identifiers,
            vec!["CP1".to_string(), "CP2".to_string(), "CP3".to_string()]
        );

        // Verify that checkpoint_identifiers() returns a list of identifiers when committed
        // checkpoints exist and are not loaded (i.e. when they do not have variable bindings).
        mem::drop(committed_cp_1);
        mem::drop(committed_cp_2);
        mem::drop(committed_cp_3);

        let mut identifiers = storage.checkpoint_identifiers().unwrap();
        identifiers.sort();
        assert_eq!(
            identifiers,
            vec!["CP1".to_string(), "CP2".to_string(), "CP3".to_string()]
        );

        // Verify that when storage is opened with an existing path that checkpoint identifiers are
        // returned properly.
        drop(storage);

        let mut storage = FileStorage::open(&path).unwrap();
        let mut identifiers = storage.checkpoint_identifiers().unwrap();
        identifiers.sort();
        assert_eq!(
            identifiers,
            vec!["CP1".to_string(), "CP2".to_string(), "CP3".to_string()]
        );

        // Verify that checkpoint identifiers are removed from the list of identifiers returned by
        // checkpoint_identifiers() when the associated checkpoint is removed.
        storage.remove_checkpoint("CP2").unwrap();
        let mut identifiers = storage.checkpoint_identifiers().unwrap();
        identifiers.sort();
        assert_eq!(identifiers, vec!["CP1".to_string(), "CP3".to_string()]);

        storage.remove_checkpoint("CP1").unwrap();
        let mut identifiers = storage.checkpoint_identifiers().unwrap();
        identifiers.sort();
        assert_eq!(identifiers, vec!["CP3".to_string()]);

        storage.remove_checkpoint("CP3").unwrap();
        let mut identifiers = storage.checkpoint_identifiers().unwrap();
        identifiers.sort();
        assert_eq!(identifiers, Vec::<String>::new());
    }

    // Verify that uncommitted and committed checkpoints behave as expected.
    #[test]
    fn uncommitted_and_committed() {
        let dir = create_test_directory_with_checkpoint_folders();
        let path = dir.path();

        let mut storage = FileStorage::open(&path).unwrap();

        // Verify that the methods of an empty uncommitted checkpoint behave as expected.
        let mut uncommitted_cp_1 = storage.create_checkpoint("CP1").unwrap();
        assert_eq!(uncommitted_cp_1.identifier().unwrap(), "CP1");
        assert_eq!(uncommitted_cp_1.keys().unwrap(), Vec::<String>::new());
        assert_eq!(
            uncommitted_cp_1.get::<u8>("Key1").unwrap_err().kind(),
            ErrorKind::CpKeyDoesntExist
        );
        assert_eq!(
            uncommitted_cp_1.remove("Key1").unwrap_err().kind(),
            ErrorKind::CpKeyDoesntExist
        );

        // Add data to the uncommitted checkpoint.
        let data_1 = 0u8;
        let data_2 = String::from("Test String.");
        let data_3 = Some(true);
        assert!(uncommitted_cp_1.put("Key1", &data_1).is_ok());
        assert!(uncommitted_cp_1.put("Key2", &data_2).is_ok());
        assert!(uncommitted_cp_1.put("Key3", &data_3).is_ok());

        // Verify that adding and removing values to existing checkpoints works as expected and
        // that creating a new uncommitted checkpoint does not affect the data stored in another
        // uncommitted checkpoint.
        let mut uncommitted_cp_2 = storage.create_checkpoint("CP2").unwrap();
        assert_eq!(uncommitted_cp_2.identifier().unwrap(), "CP2");
        assert_eq!(uncommitted_cp_2.keys().unwrap(), Vec::<String>::new());
        assert_eq!(
            uncommitted_cp_2.get::<u8>("Key1").unwrap_err().kind(),
            ErrorKind::CpKeyDoesntExist
        );
        assert_eq!(
            uncommitted_cp_2.remove("Key1").unwrap_err().kind(),
            ErrorKind::CpKeyDoesntExist
        );

        assert_eq!(uncommitted_cp_1.identifier().unwrap(), "CP1");
        let mut keys = uncommitted_cp_1.keys().unwrap();
        keys.sort();
        assert_eq!(keys, vec!["Key1", "Key2", "Key3"]);
        assert_eq!(uncommitted_cp_1.get::<u8>("Key1").unwrap(), data_1);
        assert_eq!(uncommitted_cp_1.get::<String>("Key2").unwrap(), data_2);
        assert_eq!(
            uncommitted_cp_1.get::<Option<bool>>("Key3").unwrap(),
            data_3
        );
        assert!(uncommitted_cp_1.remove("Key2").is_ok());
        assert_eq!(
            uncommitted_cp_1.remove("Key2").unwrap_err().kind(),
            ErrorKind::CpKeyDoesntExist
        );
        let mut keys = uncommitted_cp_1.keys().unwrap();
        keys.sort();
        assert_eq!(keys, vec!["Key1", "Key3"]);
        assert!(uncommitted_cp_1.remove("Key1").is_ok());
        assert_eq!(
            uncommitted_cp_1.remove("Key1").unwrap_err().kind(),
            ErrorKind::CpKeyDoesntExist
        );
        let mut keys = uncommitted_cp_1.keys().unwrap();
        keys.sort();
        assert_eq!(keys, vec!["Key3"]);
        assert!(uncommitted_cp_1.remove("Key3").is_ok());
        assert_eq!(
            uncommitted_cp_1.remove("Key3").unwrap_err().kind(),
            ErrorKind::CpKeyDoesntExist
        );
        assert_eq!(uncommitted_cp_1.keys().unwrap(), Vec::<String>::new());

        // Verify that the value for a given key can be added even if the key was previously
        // removed.
        assert!(uncommitted_cp_1.put("Key1", &data_1).is_ok());
        assert!(uncommitted_cp_1.put("Key2", &data_2).is_ok());
        assert!(uncommitted_cp_1.put("Key3", &data_3).is_ok());
        assert_eq!(uncommitted_cp_1.identifier().unwrap(), "CP1");
        let mut keys = uncommitted_cp_1.keys().unwrap();
        keys.sort();
        assert_eq!(keys, vec!["Key1", "Key2", "Key3"]);
        assert_eq!(uncommitted_cp_1.get::<u8>("Key1").unwrap(), data_1);
        assert_eq!(uncommitted_cp_1.get::<String>("Key2").unwrap(), data_2);
        assert_eq!(
            uncommitted_cp_1.get::<Option<bool>>("Key3").unwrap(),
            data_3
        );

        // Verify that the value for a given key can be changed.
        let data_1 = String::from("Test String.");
        let data_2 = Some(true);
        let data_3 = 0u8;
        assert!(uncommitted_cp_1.put("Key1", &data_1).is_ok());
        assert!(uncommitted_cp_1.put("Key2", &data_2).is_ok());
        assert!(uncommitted_cp_1.put("Key3", &data_3).is_ok());

        let mut keys = uncommitted_cp_1.keys().unwrap();
        keys.sort();
        assert_eq!(keys, vec!["Key1", "Key2", "Key3"]);
        assert_eq!(uncommitted_cp_1.get::<String>("Key1").unwrap(), data_1);
        assert_eq!(
            uncommitted_cp_1.get::<Option<bool>>("Key2").unwrap(),
            data_2
        );
        assert_eq!(uncommitted_cp_1.get::<u8>("Key3").unwrap(), data_3);

        assert!(uncommitted_cp_1.put("Key1", &data_2).is_ok());
        assert!(uncommitted_cp_1.put("Key2", &data_3).is_ok());
        assert!(uncommitted_cp_1.put("Key3", &data_1).is_ok());
        assert_eq!(uncommitted_cp_1.identifier().unwrap(), "CP1");
        let mut keys = uncommitted_cp_1.keys().unwrap();
        keys.sort();
        assert_eq!(keys, vec!["Key1", "Key2", "Key3"]);
        assert_eq!(
            uncommitted_cp_1.get::<Option<bool>>("Key1").unwrap(),
            data_2
        );
        assert_eq!(uncommitted_cp_1.get::<u8>("Key2").unwrap(), data_3);
        assert_eq!(uncommitted_cp_1.get::<String>("Key3").unwrap(), data_1);

        // Verify that uncommitted checkpoints contain the same values after being committed.
        let mut committed_cp_1 = storage.commit_checkpoint(uncommitted_cp_1).unwrap();
        let mut committed_cp_2 = storage.commit_checkpoint(uncommitted_cp_2).unwrap();

        assert_eq!(committed_cp_1.identifier().unwrap(), "CP1");
        let mut keys = committed_cp_1.keys().unwrap();
        keys.sort();
        assert_eq!(keys, vec!["Key1", "Key2", "Key3"]);
        assert_eq!(committed_cp_1.get::<Option<bool>>("Key1").unwrap(), data_2);
        assert_eq!(committed_cp_1.get::<u8>("Key2").unwrap(), data_3);
        assert_eq!(committed_cp_1.get::<String>("Key3").unwrap(), data_1);
        assert_eq!(
            committed_cp_1.get::<u8>("Key4").unwrap_err().kind(),
            ErrorKind::CpKeyDoesntExist
        );

        assert_eq!(committed_cp_2.identifier().unwrap(), "CP2");
        assert_eq!(committed_cp_2.keys().unwrap(), Vec::<String>::new());
        assert_eq!(
            committed_cp_2.get::<u8>("Key1").unwrap_err().kind(),
            ErrorKind::CpKeyDoesntExist
        );
    }
}