Skip to main content

diskann_benchmark_runner/
files.rs

1/*
2 * Copyright (c) Microsoft Corporation.
3 * Licensed under the MIT license.
4 */
5
6use std::path::{Path, PathBuf};
7
8use serde::{Deserialize, Serialize};
9
10use super::Checker;
11
12/// A file that is used as an input to for a benchmark.
13///
14/// When used during input deserialization, with a [`Checker`], the following actions will be
15/// taken:
16///
17/// 1. If the path is absolute or points to an existing file relative to the current working
18///    directory, no additional measure will be taken.
19///
20/// 2. If the path is not absolute, then every directory in [`Checker::search_directories()`]
21///    will be explored in-order. The first directory where `path` exists will be selected.
22///
23/// 3. If all these steps fail, then post-deserialization checking will fail with an error.
24#[derive(Debug, Serialize, Deserialize, Clone)]
25#[serde(transparent)]
26pub struct InputFile {
27    path: PathBuf,
28}
29
30impl InputFile {
31    /// Create a new new input file from the path-like `path``
32    pub fn new<P>(path: P) -> Self
33    where
34        PathBuf: From<P>,
35    {
36        Self {
37            path: PathBuf::from(path),
38        }
39    }
40
41    pub fn resolve(&mut self, checker: &mut Checker) -> anyhow::Result<()> {
42        let checked_path = checker.check_path(self);
43        match checked_path {
44            Ok(p) => {
45                self.path = p;
46                Ok(())
47            }
48            Err(e) => Err(e),
49        }
50    }
51}
52
53impl std::ops::Deref for InputFile {
54    type Target = Path;
55    fn deref(&self) -> &Self::Target {
56        &self.path
57    }
58}
59
60///////////
61// Tests //
62///////////
63
64#[cfg(test)]
65mod tests {
66    use std::fs::{create_dir, File};
67
68    use super::*;
69
70    #[test]
71    fn test_input_file() {
72        let file = InputFile::new("hello/world");
73        let file_deref: &Path = &file;
74        assert_eq!(file_deref.to_str().unwrap(), "hello/world");
75    }
76
77    #[test]
78    fn test_serialization() {
79        let st: &str = "\"path/to/directory\"";
80        let file: InputFile = serde_json::from_str(st).unwrap();
81        assert_eq!(file.to_str().unwrap(), st.trim_matches('\"'));
82
83        assert_eq!(&*serde_json::to_string(&file).unwrap(), st);
84    }
85
86    #[test]
87    fn test_resolve() {
88        // We create a directory that looks like this:
89        //
90        // dir/
91        //     file_a.txt
92        //     dir0/
93        //        file_b.txt
94        //     dir1/
95        //        file_c.txt
96        //        dir0/
97        //           file_c.txt
98        let dir = tempfile::tempdir().unwrap();
99        let path = dir.path();
100
101        File::create(path.join("file_a.txt")).unwrap();
102        create_dir(path.join("dir0")).unwrap();
103        create_dir(path.join("dir1")).unwrap();
104        create_dir(path.join("dir1/dir0")).unwrap();
105        File::create(path.join("dir0/file_b.txt")).unwrap();
106        File::create(path.join("dir1/file_c.txt")).unwrap();
107        File::create(path.join("dir1/dir0/file_c.txt")).unwrap();
108
109        // Test absolute path success.
110        {
111            let absolute = path.join("file_a.txt");
112            let mut file = InputFile::new(absolute.clone());
113            let mut checker = Checker::new(Vec::new(), None);
114            file.resolve(&mut checker).unwrap();
115            assert_eq!(file.path, absolute);
116
117            let absolute = path.join("dir0/file_b.txt");
118            let mut file = InputFile::new(absolute.clone());
119            let mut checker = Checker::new(Vec::new(), None);
120            file.resolve(&mut checker).unwrap();
121            assert_eq!(file.path, absolute);
122        }
123
124        // Absolute path fail.
125        {
126            let absolute = path.join("dir0/file_c.txt");
127            let mut file = InputFile::new(absolute.clone());
128            let mut checker = Checker::new(Vec::new(), None);
129            let err = file.resolve(&mut checker).unwrap_err();
130            let message = err.to_string();
131            assert!(message.contains("input file with absolute path"));
132            assert!(message.contains("either does not exist or is not a file"));
133        }
134
135        // Directory search
136        {
137            let mut checker = Checker::new(
138                vec![path.join("dir1/dir0"), path.join("dir1"), path.join("dir0")],
139                None,
140            );
141
142            // Directories are searched in order.
143            let mut file = InputFile::new("file_c.txt");
144            file.resolve(&mut checker).unwrap();
145            assert_eq!(file.path, path.join("dir1/dir0/file_c.txt"));
146
147            let mut file = InputFile::new("file_b.txt");
148            file.resolve(&mut checker).unwrap();
149            assert_eq!(file.path, path.join("dir0/file_b.txt"));
150
151            // Directory search can fail.
152            let mut file = InputFile::new("file_a.txt");
153            let err = file.resolve(&mut checker).unwrap_err();
154            let message = err.to_string();
155            assert!(message.contains("could not find input file"));
156            assert!(message.contains("in the search directories"));
157
158            // If we give an absolute path, no directory search is performed.
159            let mut file = InputFile::new(path.join("file_c.txt"));
160            let err = file.resolve(&mut checker).unwrap_err();
161            let message = err.to_string();
162            assert!(message.starts_with("input file with absolute path"));
163        }
164    }
165}