Skip to content

Commit

Permalink
Merge branch 'develop' into fix/database-optims
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludo Galabru authored Nov 27, 2023
2 parents ea5cba6 + a84c951 commit 8464d0d
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions components/ordhook-core/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,107 @@ where
results
}

fn perform_query_exists(
query: &str,
args: &[&dyn ToSql],
db_conn: &Connection,
ctx: &Context,
) -> bool {
let res = perform_query(query, args, db_conn, ctx, |_| true, true);
!res.is_empty()
}

fn perform_query_one<F, T>(
query: &str,
args: &[&dyn ToSql],
db_conn: &Connection,
ctx: &Context,
mapping_func: F,
) -> Option<T>
where
F: Fn(&rusqlite::Row<'_>) -> T,
{
let mut res = perform_query(query, args, db_conn, ctx, mapping_func, true);
match res.is_empty() {
true => None,
false => Some(res.remove(0)),
}
}

fn perform_query_set<F, T>(
query: &str,
args: &[&dyn ToSql],
db_conn: &Connection,
ctx: &Context,
mapping_func: F,
) -> Vec<T>
where
F: Fn(&rusqlite::Row<'_>) -> T,
{
perform_query(query, args, db_conn, ctx, mapping_func, false)
}

fn perform_query<F, T>(
query: &str,
args: &[&dyn ToSql],
db_conn: &Connection,
ctx: &Context,
mapping_func: F,
stop_at_first: bool,
) -> Vec<T>
where
F: Fn(&rusqlite::Row<'_>) -> T,
{
let mut results = vec![];
loop {
let mut stmt = match db_conn.prepare(query) {
Ok(stmt) => stmt,
Err(e) => {
ctx.try_log(|logger| {
warn!(logger, "unable to prepare query {query}: {}", e.to_string())
});
std::thread::sleep(std::time::Duration::from_secs(5));
continue;
}
};

match stmt.query(args) {
Ok(mut rows) => loop {
match rows.next() {
Ok(Some(row)) => {
let r = mapping_func(row);
results.push(r);
if stop_at_first {
break;
}
}
Ok(None) => break,
Err(e) => {
ctx.try_log(|logger| {
warn!(
logger,
"unable to iterate over results from {query}: {}",
e.to_string()
)
});
std::thread::sleep(std::time::Duration::from_secs(5));
continue;
}
}
},
Err(e) => {
ctx.try_log(|logger| {
warn!(logger, "unable to execute query {query}: {}", e.to_string())
});
std::thread::sleep(std::time::Duration::from_secs(5));
continue;
}
};
break;
}
results
}

pub fn get_any_entry_in_ordinal_activities(
block_height: &u64,
db_conn: &Connection,
Expand Down

0 comments on commit 8464d0d

Please sign in to comment.