Skip to content

Commit c35b342

Browse files
committed
fixes
1 parent 760bdef commit c35b342

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/storage/archive_index.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::error::Result;
22
use crate::storage::{FileRange, compression::CompressionAlgorithm};
33
use anyhow::{Context as _, bail};
44
use itertools::Itertools as _;
5-
use sqlx::{Acquire as _, QueryBuilder, Sqlite};
5+
use sqlx::{Acquire as _, QueryBuilder, Row as _, Sqlite};
66
use std::{fs, io, path::Path};
77
use tracing::instrument;
88

@@ -21,34 +21,40 @@ impl FileInfo {
2121
}
2222
}
2323

24-
/// create SQLite pool on freshly created database.
24+
/// crates a new empty SQLite database, and returns a configured connection
25+
/// pool to connect to the DB.
26+
/// Any existing DB at the given path will be deleted first.
2527
async fn sqlite_create<P: AsRef<Path>>(path: P) -> Result<sqlx::SqlitePool> {
2628
let path = path.as_ref();
2729
if path.exists() {
2830
fs::remove_file(path)?;
2931
}
3032

31-
Ok(sqlx::SqlitePool::connect_with(
33+
sqlx::SqlitePool::connect_with(
3234
sqlx::sqlite::SqliteConnectOptions::new()
3335
.filename(path)
3436
.read_only(false)
3537
.pragma("synchronous", "full")
3638
.create_if_missing(true),
3739
)
38-
.await?)
40+
.await
41+
.map_err(Into::into)
3942
}
4043

41-
/// open existing SQLite database.
44+
/// open existing SQLite database, return a configured connection poll
45+
/// to connect to the DB.
46+
/// Will error when the database doesn't exist at that path.
4247
async fn sqlite_open<P: AsRef<Path>>(path: P) -> Result<sqlx::SqlitePool> {
43-
Ok(sqlx::SqlitePool::connect_with(
48+
sqlx::SqlitePool::connect_with(
4449
sqlx::sqlite::SqliteConnectOptions::new()
4550
.filename(path)
4651
.read_only(true)
4752
.pragma("synchronous", "off") // not needed for readonly db
4853
.serialized(false) // same as OPEN_NOMUTEX
4954
.create_if_missing(false),
5055
)
51-
.await?)
56+
.await
57+
.map_err(Into::into)
5258
}
5359

5460
/// create an archive index based on a zipfile.
@@ -86,17 +92,17 @@ pub(crate) async fn create<R: io::Read + io::Seek, P: AsRef<Path> + std::fmt::De
8692
let mut insert_stmt =
8793
QueryBuilder::<Sqlite>::new("INSERT INTO files (path, start, end, compression) ");
8894

89-
let zf = archive.by_index(i)?;
95+
let entry = archive.by_index(i)?;
9096

91-
let start = zf.data_start() as i64;
92-
let end = (zf.data_start() + zf.compressed_size() - 1) as i64;
93-
let compression_raw = match zf.compression() {
97+
let start = entry.data_start() as i64;
98+
let end = (entry.data_start() + entry.compressed_size() - 1) as i64;
99+
let compression_raw = match entry.compression() {
94100
zip::CompressionMethod::Bzip2 => compression_bzip,
95101
c => bail!("unsupported compression algorithm {} in zip-file", c),
96102
};
97103

98104
insert_stmt.push_values([()], |mut b, _| {
99-
b.push_bind(zf.name())
105+
b.push_bind(entry.name())
100106
.push_bind(start)
101107
.push_bind(end)
102108
.push_bind(compression_raw);
@@ -126,8 +132,6 @@ async fn find_in_sqlite_index<'e, E>(executor: E, search_for: &str) -> Result<Op
126132
where
127133
E: sqlx::Executor<'e, Database = sqlx::Sqlite>,
128134
{
129-
use sqlx::Row;
130-
131135
let row = sqlx::query(
132136
"
133137
SELECT start, end, compression
@@ -163,20 +167,17 @@ pub(crate) async fn find_in_file<P: AsRef<Path> + std::fmt::Debug>(
163167
archive_index_path: P,
164168
search_for: &str,
165169
) -> Result<Option<FileInfo>> {
166-
// Converted to sqlx (async) from rusqlite
167170
let path = archive_index_path.as_ref();
168171

169172
let pool = sqlite_open(path).await?;
170173
let mut conn = pool.acquire().await?;
171174

172-
// Assuming find_in_sqlite_index now accepts &mut sqlx::SqliteConnection and is async
173175
find_in_sqlite_index(&mut *conn, search_for).await
174176
}
175177

176178
#[cfg(test)]
177179
mod tests {
178180
use super::*;
179-
use sqlx::Row as _;
180181
use std::io::Write;
181182
use zip::write::SimpleFileOptions;
182183

@@ -225,12 +226,11 @@ mod tests {
225226
let pool = sqlite_open(&tempfile).await?;
226227
let mut conn = pool.acquire().await?;
227228

228-
let result = sqlx::query("SELECT count(*) FROM files")
229-
.fetch_all(&mut *conn)
229+
let row = sqlx::query("SELECT count(*) FROM files")
230+
.fetch_one(&mut *conn)
230231
.await?;
231232

232-
assert_eq!(result.len(), 1);
233-
assert_eq!(result[0].get::<i64, _>(0), 100_000);
233+
assert_eq!(row.get::<i64, _>(0), 100_000);
234234

235235
Ok(())
236236
}

src/storage/compression.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ pub fn compress(content: impl Read, algorithm: CompressionAlgorithm) -> Result<V
9090
}
9191
}
9292

93+
/// Wrap an AsyncWrite sink for compression using the specified algorithm.
94+
///
95+
/// Will return an AsyncWrite you can just write data to, we will compress
96+
/// the data, and then write the compressed data into the provided output sink.
9397
pub fn wrap_writer_for_compression<'a>(
9498
output_sink: impl AsyncWrite + Unpin + Send + 'a,
9599
algorithm: CompressionAlgorithm,
@@ -110,6 +114,10 @@ pub fn wrap_writer_for_compression<'a>(
110114
}
111115
}
112116

117+
/// Wrap an AsyncRead for decompression.
118+
///
119+
/// You provide an AsyncRead that gives us the compressed data. With the
120+
/// wrapper we return you can then read decompressed data from the wrapper.
113121
pub fn wrap_reader_for_decompression<'a>(
114122
input: impl AsyncRead + Unpin + Send + 'a,
115123
algorithm: CompressionAlgorithm,

0 commit comments

Comments
 (0)