-
-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove r2d2_sqlite dependency #4050
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::path::PathBuf; | ||
use std::time::Duration; | ||
|
||
use r2d2::ManageConnection; | ||
use rusqlite::{Connection, Error, OpenFlags}; | ||
|
||
#[derive(Debug)] | ||
pub struct ConnectionManager { | ||
/// Database file path. | ||
path: PathBuf, | ||
|
||
/// SQLite open flags. | ||
flags: rusqlite::OpenFlags, | ||
|
||
/// SQLCipher database passphrase. | ||
/// Empty string if database is not encrypted. | ||
passphrase: String, | ||
} | ||
|
||
impl ConnectionManager { | ||
/// Creates new connection manager. | ||
pub fn new(path: PathBuf, passphrase: String) -> Self { | ||
let mut flags = OpenFlags::SQLITE_OPEN_NO_MUTEX; | ||
flags.insert(OpenFlags::SQLITE_OPEN_READ_WRITE); | ||
flags.insert(OpenFlags::SQLITE_OPEN_CREATE); | ||
|
||
Self { | ||
path, | ||
flags, | ||
passphrase, | ||
} | ||
} | ||
} | ||
|
||
impl ManageConnection for ConnectionManager { | ||
type Connection = Connection; | ||
type Error = Error; | ||
|
||
fn connect(&self) -> Result<Connection, Error> { | ||
let conn = Connection::open_with_flags(&self.path, self.flags)?; | ||
conn.execute_batch(&format!( | ||
"PRAGMA cipher_memory_security = OFF; -- Too slow on Android | ||
PRAGMA secure_delete=on; | ||
PRAGMA busy_timeout = {}; | ||
PRAGMA temp_store=memory; -- Avoid SQLITE_IOERR_GETTEMPPATH errors on Android | ||
PRAGMA foreign_keys=on; | ||
", | ||
Duration::from_secs(60).as_millis() | ||
))?; | ||
conn.pragma_update(None, "key", &self.passphrase)?; | ||
// Try to enable auto_vacuum. This will only be | ||
// applied if the database is new or after successful | ||
// VACUUM, which usually happens before backup export. | ||
// When auto_vacuum is INCREMENTAL, it is possible to | ||
// use PRAGMA incremental_vacuum to return unused | ||
// database pages to the filesystem. | ||
conn.pragma_update(None, "auto_vacuum", "INCREMENTAL".to_string())?; | ||
|
||
conn.pragma_update(None, "journal_mode", "WAL".to_string())?; | ||
// Default synchronous=FULL is much slower. NORMAL is sufficient for WAL mode. | ||
conn.pragma_update(None, "synchronous", "NORMAL".to_string())?; | ||
|
||
Ok(conn) | ||
} | ||
|
||
fn is_valid(&self, _conn: &mut Connection) -> Result<(), Error> { | ||
Ok(()) | ||
} | ||
|
||
fn has_broken(&self, _conn: &mut Connection) -> bool { | ||
false | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunate that we need to keep this in memory the whole time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also because of r2d2 abstraction, it expects that connections may fail because underlying network connections fail, and have to be reestablished. Which is not the case with SQLite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in #4053.