Skip to content

Commit

Permalink
Small tweaks to the storage API
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikvanantwerpen committed Jul 4, 2023
1 parent dd67086 commit bee8826
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 6 deletions.
2 changes: 2 additions & 0 deletions stack-graphs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- New `SQLiteReader::clear` and `SQLiteReader::clear_paths` methods that make it easier to reuse instances.
- The method `SQLiteReader::load_graph_for_file` now returns the file handle for the loaded file.

### Changed

- The `Appendable` trait has been simplified. Its `Ctx` type parameter is gone, in favor of a separate trait `ToAppendable` that is used to find appendables for a handle. The type itself moved from the `cycles` to the `stitching` module.
- The `ForwardPartialPathStitcher` has been generalized so that it can be used to build paths from a database or from graph edges. It now takes a type parameter indicating the type of candidates it uses. Instead of a `Database` instance, it expects a value that implements the `Candidates` and `ToAppendable` traits. The `ForwardPartialPathStitcher::process_next_phase` expects an additional `extend_until` closure that controls whether the extended paths are considered for further extension or not (using `|_,_,_| true` retains old behavior).
- The SQLite database implementation is using a new schema which stores binary instead of JSON values, resulting in faster write times and smaller databases.
- Renamed method `SQLiteReader::load_graph_for_file_or_directory` to `SQLiteReader::load_graphs_for_file_or_directory`.

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions stack-graphs/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl<T> Arena<T> {

/// Clear the arena, keeping underlying allocated capacity. After this, all previous handles into
/// the arena are invalid.
#[cfg_attr(not(feature = "storage"), allow(dead_code))]
#[inline(always)]
pub(crate) fn clear(&mut self) {
self.items.clear();
Expand Down Expand Up @@ -289,6 +290,7 @@ impl<H, T> SupplementalArena<H, T> {

/// Clear the supplemantal arena, keeping underlying allocated capacity. After this,
/// all previous handles into the arena are invalid.
#[cfg_attr(not(feature = "storage"), allow(dead_code))]
#[inline(always)]
pub(crate) fn clear(&mut self) {
self.items.clear();
Expand Down
1 change: 1 addition & 0 deletions stack-graphs/src/partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,7 @@ impl PartialPaths {
}
}

#[cfg_attr(not(feature = "storage"), allow(dead_code))]
pub(crate) fn clear(&mut self) {
self.partial_symbol_stacks.clear();
self.partial_scope_stacks.clear();
Expand Down
1 change: 1 addition & 0 deletions stack-graphs/src/stitching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ impl Database {

/// Clear the database. After this, all previous handles into the database are
/// invalid.
#[cfg_attr(not(feature = "storage"), allow(dead_code))]
pub(crate) fn clear(&mut self) {
self.partial_paths.clear();
self.local_nodes.clear();
Expand Down
10 changes: 5 additions & 5 deletions stack-graphs/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ impl SQLiteReader {
}

/// Ensure the graph for the given file is loaded.
pub fn load_graph_for_file(&mut self, file: &str) -> Result<()> {
pub fn load_graph_for_file(&mut self, file: &str) -> Result<Handle<File>> {
Self::load_graph_for_file_inner(file, &mut self.graph, &mut self.loaded_graphs, &self.conn)
}

Expand All @@ -516,21 +516,21 @@ impl SQLiteReader {
graph: &mut StackGraph,
loaded_graphs: &mut HashSet<String>,
conn: &Connection,
) -> Result<()> {
) -> Result<Handle<File>> {
copious_debugging!("--> Load graph for {}", file);
if !loaded_graphs.insert(file.to_string()) {
copious_debugging!(" * Already loaded");
return Ok(());
return Ok(graph.get_file(file).expect("loaded file to exist"));
}
copious_debugging!(" * Load from database");
let mut stmt = conn.prepare_cached("SELECT value FROM graphs WHERE file = ?")?;
let value = stmt.query_row([file], |row| row.get::<_, Vec<u8>>(0))?;
let file_graph = rmp_serde::from_slice::<serde::StackGraph>(&value)?;
file_graph.load_into(graph)?;
Ok(())
Ok(graph.get_file(file).expect("loaded file to exist"))
}

pub fn load_graph_for_file_or_directory(
pub fn load_graphs_for_file_or_directory(
&mut self,
file_or_directory: &Path,
cancellation_flag: &dyn CancellationFlag,
Expand Down
2 changes: 1 addition & 1 deletion tree-sitter-stack-graphs/src/cli/visualize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl VisualizeArgs {
let mut db = SQLiteReader::open(&db_path)?;
for source_path in &self.source_paths {
let source_path = source_path.canonicalize()?;
db.load_graph_for_file_or_directory(&source_path, cancellation_flag)?;
db.load_graphs_for_file_or_directory(&source_path, cancellation_flag)?;
}
let (graph, _, _) = db.get();
let starting_nodes = graph
Expand Down

0 comments on commit bee8826

Please sign in to comment.