Skip to content
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

Storage: Add Indexes for common queries #308

Merged
merged 4 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Douglas Creager <[email protected]>
Hendrik van Antwerpen <[email protected]>
Akshay <[email protected]>
blusk <[email protected]>
William Manley <[email protected]>
18 changes: 17 additions & 1 deletion stack-graphs/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ const SCHEMA: &str = r#"
) STRICT;
"#;

const INDEXES: &str = r#"
CREATE INDEX IF NOT EXISTS idx_graphs_file ON graphs(file);
CREATE INDEX IF NOT EXISTS idx_file_paths_local_id ON file_paths(file, local_id);
CREATE INDEX IF NOT EXISTS idx_root_paths_symbol_stack ON root_paths(symbol_stack);
"#;

const PRAGMAS: &str = r#"
PRAGMA journal_mode = WAL;
PRAGMA foreign_keys = false;
Expand Down Expand Up @@ -147,6 +153,7 @@ impl SQLiteWriter {
pub fn open_in_memory() -> Result<Self> {
let mut conn = Connection::open_in_memory()?;
Self::init(&mut conn)?;
init_indexes(&mut conn)?;
Ok(Self { conn })
}

Expand All @@ -161,6 +168,7 @@ impl SQLiteWriter {
} else {
check_version(&conn)?;
}
init_indexes(&mut conn)?;
Ok(Self { conn })
}

Expand Down Expand Up @@ -432,9 +440,10 @@ impl SQLiteReader {
path.as_ref().to_string_lossy().to_string(),
));
}
let conn = Connection::open(path)?;
let mut conn = Connection::open(path)?;
set_pragmas_and_functions(&conn)?;
check_version(&conn)?;
init_indexes(&mut conn)?;
Ok(Self {
conn,
loaded_graphs: HashSet::new(),
Expand Down Expand Up @@ -781,6 +790,13 @@ fn set_pragmas_and_functions(conn: &Connection) -> Result<()> {
Ok(())
}

fn init_indexes(conn: &mut Connection) -> Result<()> {
let tx = conn.transaction()?;
tx.execute_batch(INDEXES)?;
tx.commit()?;
Ok(())
}

fn status_for_file<T: AsRef<str>>(
conn: &Connection,
file: &str,
Expand Down