@@ -2,7 +2,7 @@ use crate::error::Result;
22use crate :: storage:: { FileRange , compression:: CompressionAlgorithm } ;
33use anyhow:: { Context as _, bail} ;
44use itertools:: Itertools as _;
5- use sqlx:: { Acquire as _, QueryBuilder , Sqlite } ;
5+ use sqlx:: { Acquire as _, QueryBuilder , Row as _ , Sqlite } ;
66use std:: { fs, io, path:: Path } ;
77use 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.
2527async 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.
4247async 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
126132where
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) ]
177179mod 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 }
0 commit comments