summaryrefslogtreecommitdiff
path: root/src/db.rs
blob: 7f0ccefc661a13daa728e6598003075e9fad2ca6 (plain)
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
use anyhow::{Result, anyhow};
use directories::BaseDirs;
use rusqlite::{Connection, params};
use std::{
    path::{Path, PathBuf},
    time::UNIX_EPOCH,
};

use crate::flac::{CURRENT_VENDOR, get_vendor};

const TABLE_CREATE: &str = "CREATE TABLE IF NOT EXISTS flacs (path TEXT PRIMARY KEY UNIQUE, toencode BOOLEAN NOT NULL, modtime INTEGER)";
const ADD_ITEM: &str = "INSERT INTO flacs (path, toencode, modtime) VALUES (?1, ?2, ?3)";
const UPDATE_ITEM: &str = "UPDATE flacs SET toencode = ?2, modtime = ?3 WHERE path = ?1";
const TOENCODE_PATHS: &str = "SELECT path FROM flacs WHERE toencode";
const TOENCODE_NUMBER: &str = "SELECT COUNT(*) from flacs WHERE toencode";
const CHECK_FILE: &str = "SELECT exists(SELECT 1 FROM flacs WHERE path = ?1)";
const FETCH_FILES: &str = "SELECT path FROM flacs";
const REMOVE_FILE: &str = "DELETE FROM flacs WHERE path = ?1";
const DEDUPE_DB: &str =
    "DELETE FROM flacs WHERE rowid NOT IN (SELECT MAX(rowid) FROM flacs GROUP BY path)";
const GET_MODTIME: &str = "SELECT modtime FROM flacs WHERE path = ?1";

pub trait Database {
    type Conn;
    fn new(path: Option<impl AsRef<Path>>) -> Result<Self::Conn>;
    fn insert_file(&self, filename: impl AsRef<Path>) -> Result<()>;
    fn update_file(&self, filename: impl AsRef<Path>) -> Result<()>;
    fn check_file(&self, filename: impl AsRef<Path>) -> Result<bool>;
    fn init_clean_files(&self) -> Result<Vec<PathBuf>, rusqlite::Error>;
    fn remove_file(&self, filename: impl AsRef<Path>) -> Result<()>;
    fn get_toencode_files(&self) -> Result<Vec<PathBuf>, rusqlite::Error>;
    fn get_toencode_number(&self) -> Result<u64, rusqlite::Error>;
    fn get_modtime(&self, file: impl AsRef<Path>) -> Result<u64>;
    fn vacuum(&self) -> Result<()>;
}

impl Database for Connection {
    type Conn = Connection;
    fn new(path: Option<impl AsRef<Path>>) -> Result<Self> {
        let conn = if let Some(file) = path {
            Connection::open(file)?
        } else if let Some(base_dir) = BaseDirs::new() {
            let file = Path::new(base_dir.data_dir()).join("reencoder.db");
            Connection::open(file)?
        } else {
            return Err(anyhow!("Failed to locate data directory"));
        };
        conn.execute(TABLE_CREATE, ())?;
        Ok(conn)
    }

    fn insert_file(&self, filename: impl AsRef<Path>) -> Result<()> {
        let toencode = !matches!(get_vendor(&filename)?.as_str(), CURRENT_VENDOR);

        let modtime = filename
            .as_ref()
            .metadata()?
            .modified()?
            .duration_since(UNIX_EPOCH)?
            .as_secs();

        self.execute(
            ADD_ITEM,
            params![filename.as_ref().to_str().unwrap(), toencode, modtime],
        )?;

        Ok(())
    }

    fn update_file(&self, filename: impl AsRef<Path>) -> Result<()> {
        let modtime = filename
            .as_ref()
            .metadata()?
            .modified()?
            .duration_since(UNIX_EPOCH)?
            .as_secs();

        self.execute(
            UPDATE_ITEM,
            params![filename.as_ref().to_str().unwrap(), false, modtime],
        )?;

        Ok(())
    }

    fn check_file(&self, filename: impl AsRef<Path>) -> Result<bool> {
        if self.query_one(
            CHECK_FILE,
            params!(filename.as_ref().to_str().unwrap()),
            |row| {
                let num: bool = row.get(0)?;
                Ok(num)
            },
        )? {
            Ok(true)
        } else {
            Ok(false)
        }
    }

    fn init_clean_files(&self) -> Result<Vec<PathBuf>, rusqlite::Error> {
        self.execute(DEDUPE_DB, ())?;
        let mut stmt = self.prepare(FETCH_FILES)?;
        let mut rows = stmt.query(())?;
        let mut files = Vec::new();
        while let Ok(Some(row)) = rows.next() {
            let path: String = row.get(0)?;
            files.push(PathBuf::from(path));
        }
        Ok(files)
    }

    fn remove_file(&self, filename: impl AsRef<Path>) -> Result<()> {
        self.execute(REMOVE_FILE, params!(filename.as_ref().to_str().unwrap()))?;
        Ok(())
    }

    fn get_toencode_files(&self) -> Result<Vec<PathBuf>, rusqlite::Error> {
        let mut stmt = self.prepare(TOENCODE_PATHS)?;
        let mut rows = stmt.query(())?;
        let mut files: Vec<PathBuf> = Vec::new();
        while let Ok(Some(row)) = rows.next() {
            let path: String = row.get(0)?;
            files.push(PathBuf::from(path));
        }
        Ok(files)
    }

    fn get_toencode_number(&self) -> Result<u64, rusqlite::Error> {
        self.query_one(TOENCODE_NUMBER, (), |row| {
            let num: u64 = row.get(0)?;
            Ok(num)
        })
    }

    fn get_modtime(&self, file: impl AsRef<Path>) -> Result<u64> {
        Ok(self.query_one(
            GET_MODTIME,
            params![file.as_ref().to_str().unwrap()],
            |row| {
                let modtime: u64 = row.get(0)?;
                Ok(modtime)
            },
        )?)
    }

    fn vacuum(&self) -> Result<()> {
        self.execute("VACUUM", ())?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn check_localfiles() {
        let dbname = String::from("temp1.db");
        let filenames = [
            "./samples/16bit.flac",
            "./samples/24bit.flac",
            "./samples/32bit.flac",
        ];
        let mut counter = 0;
        let conn = Connection::new(Some(&dbname)).unwrap();
        for file in filenames {
            conn.insert_file(&file.to_string()).unwrap();
        }
        let mut stmt = conn.prepare(TOENCODE_PATHS).unwrap();
        let mut returned = stmt.query(()).unwrap();

        while let Ok(Some(_)) = returned.next() {
            counter += 1
        }
        std::fs::remove_file(dbname).unwrap();
        assert!(counter == 0)
    }

    #[test]
    fn check_update() {
        let dbname = String::from("temp2.db");
        let filenames = [
            "./samples/16bit.flac",
            "./samples/24bit.flac",
            "./samples/32bit.flac",
        ];
        let conn = Connection::new(Some(&dbname)).unwrap();
        for file in filenames {
            conn.insert_file(Path::new(file).canonicalize().unwrap())
                .unwrap();
        }

        conn.execute(
            UPDATE_ITEM,
            params![
                Path::new("./samples/16bit.flac")
                    .canonicalize()
                    .unwrap()
                    .to_str()
                    .unwrap(),
                true,
                ""
            ],
        )
        .unwrap();

        conn.update_file(
            Path::new("./samples/16bit.flac")
                .canonicalize()
                .unwrap()
                .to_str()
                .unwrap(),
        )
        .unwrap();

        let mut stmt = conn.prepare(TOENCODE_PATHS).unwrap();
        let mut returned = stmt.query(()).unwrap();
        let mut counter = 0;
        while let Ok(Some(_)) = returned.next() {
            counter += 1
        }
        std::fs::remove_file(dbname).unwrap();
        assert!(counter == 0)
    }
}