Move prepared statements out of transaction

There's no need to create these on each loop iteration.
This commit is contained in:
cloudhead 2024-05-14 16:36:04 +02:00
parent 21cced05c1
commit cadd996a76
No known key found for this signature in database
1 changed files with 20 additions and 19 deletions

View File

@ -109,28 +109,29 @@ impl Store for Database {
time: Timestamp, time: Timestamp,
) -> Result<Vec<(RepoId, InsertResult)>, Error> { ) -> Result<Vec<(RepoId, InsertResult)>, Error> {
let mut results = Vec::new(); let mut results = Vec::new();
let mut select_stmt = self
transaction(&self.db, |db| { .db
.prepare("SELECT (timestamp) FROM routing WHERE repo = ? AND node = ?")?;
let mut insert_stmt = self.db.prepare(
"INSERT INTO routing (repo, node, timestamp)
VALUES (?, ?, ?)
ON CONFLICT DO UPDATE
SET timestamp = ?3
WHERE timestamp < ?3",
)?;
transaction(&self.db, |_| {
for id in ids.into_iter() { for id in ids.into_iter() {
let mut stmt = select_stmt.bind((1, id))?;
db.prepare("SELECT (timestamp) FROM routing WHERE repo = ? AND node = ?")?; select_stmt.bind((2, &node))?;
stmt.bind((1, id))?; let existed = select_stmt.iter().next().is_some();
stmt.bind((2, &node))?; select_stmt.reset()?;
let existed = stmt.into_iter().next().is_some(); insert_stmt.bind((1, id))?;
let mut stmt = db.prepare( insert_stmt.bind((2, &node))?;
"INSERT INTO routing (repo, node, timestamp) insert_stmt.bind((3, &time))?;
VALUES (?, ?, ?) insert_stmt.next()?;
ON CONFLICT DO UPDATE insert_stmt.reset()?;
SET timestamp = ?3
WHERE timestamp < ?3",
)?;
stmt.bind((1, id))?;
stmt.bind((2, &node))?;
stmt.bind((3, &time))?;
stmt.next()?;
let result = match (self.db.change_count() > 0, existed) { let result = match (self.db.change_count() > 0, existed) {
(true, true) => InsertResult::TimeUpdated, (true, true) => InsertResult::TimeUpdated,