From e6d9ec2fe034c9d36fd730c969555c459606d42f Mon Sep 17 00:00:00 2001 From: Timon Vonk Date: Wed, 2 Oct 2024 17:07:03 +0200 Subject: [PATCH] fix(lancedb): Should not error if table exists (#349) --- .github/workflows/test.yml | 2 +- Cargo.lock | 12 ++--- examples/index_codebase_reduced_context.rs | 2 +- swiftide-integrations/src/lancedb/persist.rs | 51 +++++++++++++++++-- .../src/treesitter/code_tree.rs | 4 +- .../src/treesitter/outliner.rs | 2 +- swiftide/tests/query_pipeline.rs | 4 +- 7 files changed, 59 insertions(+), 18 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 59db6f8a..c143d2c9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -74,4 +74,4 @@ jobs: repo-token: ${{ secrets.GITHUB_TOKEN }} - uses: r7kamura/rust-problem-matchers@v1 - name: clippy - run: cargo clippy --workspace --all-features + run: cargo clippy --all-targets --all-features diff --git a/Cargo.lock b/Cargo.lock index 1fcc9375..a940524e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3750,7 +3750,7 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite", - "socket2 0.5.7", + "socket2 0.4.10", "tokio", "tower-service", "tracing", @@ -6090,7 +6090,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e9552f850d5f0964a4e4d0bf306459ac29323ddfbae05e35a7c0d35cb0803cc5" dependencies = [ "anyhow", - "itertools 0.13.0", + "itertools 0.10.5", "proc-macro2", "quote", "syn 2.0.77", @@ -8312,9 +8312,9 @@ dependencies = [ [[package]] name = "tonic" -version = "0.12.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6f6ba989e4b2c58ae83d862d3a3e27690b6e3ae630d0deb59f3697f32aa88ad" +checksum = "877c5b330756d856ffcc4553ab34a5684481ade925ecc54bcd1bf02b1d0d4d52" dependencies = [ "async-stream", "async-trait", @@ -8332,7 +8332,7 @@ dependencies = [ "percent-encoding", "pin-project", "prost 0.13.3", - "rustls-native-certs 0.7.3", + "rustls-native-certs 0.8.0", "rustls-pemfile 2.1.3", "socket2 0.5.7", "tokio", @@ -8975,7 +8975,7 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.48.0", ] [[package]] diff --git a/examples/index_codebase_reduced_context.rs b/examples/index_codebase_reduced_context.rs index 5125b883..837744a2 100644 --- a/examples/index_codebase_reduced_context.rs +++ b/examples/index_codebase_reduced_context.rs @@ -7,7 +7,7 @@ //! The pipeline will: //! - Load all `.rs` files from the current directory //! - Skip any nodes previously processed; hashes are based on the path and chunk (not the -//! metadata!) +//! metadata!) //! - Generate an outline of the symbols defined in each file to be used as context in a later step and store it in the metadata //! - Chunk the code into pieces of 10 to 2048 bytes //! - For each chunk, generate a condensed subset of the symbols outline tailored for that specific chunk and store that in the metadata diff --git a/swiftide-integrations/src/lancedb/persist.rs b/swiftide-integrations/src/lancedb/persist.rs index c578f473..0a64ca10 100644 --- a/swiftide-integrations/src/lancedb/persist.rs +++ b/swiftide-integrations/src/lancedb/persist.rs @@ -25,11 +25,19 @@ impl Persist for LanceDB { let conn = self.get_connection().await?; let schema = self.schema.clone(); - conn.create_empty_table(&self.table_name, schema) - .execute() - .await - .map(|_| ()) - .map_err(Into::into) + if let Err(err) = conn.open_table(&self.table_name).execute().await { + if matches!(err, lancedb::Error::TableNotFound { .. }) { + conn.create_empty_table(&self.table_name, schema) + .execute() + .await + .map(|_| ()) + .map_err(anyhow::Error::from)?; + } else { + return Err(err.into()); + } + } + + Ok(()) } #[tracing::instrument(skip_all)] @@ -151,3 +159,36 @@ impl LanceDB { Ok(batches) } } + +#[cfg(test)] +mod test { + use swiftide_core::{indexing::EmbeddedField, Persist as _}; + use temp_dir::TempDir; + + use super::*; + + async fn setup() -> (TempDir, LanceDB) { + let tempdir = TempDir::new().unwrap(); + let lancedb = LanceDB::builder() + .uri(tempdir.child("lancedb").to_str().unwrap()) + .vector_size(384) + .with_metadata("filter") + .with_vector(EmbeddedField::Combined) + .table_name("swiftide_test") + .build() + .unwrap(); + lancedb.setup().await.unwrap(); + + (tempdir, lancedb) + } + + #[tokio::test] + async fn test_no_error_when_table_exists() { + let (_guard, lancedb) = setup().await; + + lancedb + .setup() + .await + .expect("Should not error if table exists"); + } +} diff --git a/swiftide-integrations/src/treesitter/code_tree.rs b/swiftide-integrations/src/treesitter/code_tree.rs index 5b04990a..724b5e53 100644 --- a/swiftide-integrations/src/treesitter/code_tree.rs +++ b/swiftide-integrations/src/treesitter/code_tree.rs @@ -252,7 +252,7 @@ mod tests { #[test] fn test_parsing_on_java_enum() { let parser = CodeParser::from_language(SupportedLanguages::Java); - let code = r#" + let code = r" enum Material { DENIM, CANVAS, @@ -274,7 +274,7 @@ mod tests { c = Material.DENIM; } } - "#; + "; let tree = parser.parse(code).unwrap(); let result = tree.references_and_definitions().unwrap(); assert_eq!(result.definitions, vec!["Material", "Person", "getName"]); diff --git a/swiftide-integrations/src/treesitter/outliner.rs b/swiftide-integrations/src/treesitter/outliner.rs index 37f2839a..8d413fb3 100644 --- a/swiftide-integrations/src/treesitter/outliner.rs +++ b/swiftide-integrations/src/treesitter/outliner.rs @@ -316,7 +316,7 @@ public class HelloWorld { "#; let outliner = CodeOutliner::new(SupportedLanguages::Java); let summary = outliner.outline(code).unwrap(); - println!("{}", summary); + println!("{summary}"); assert_eq!( summary, "\nimport java.io.PrintStream;\nimport java.util.Scanner;\n\npublic class HelloWorld {\n // This is a comment\n public static void main(String[] args) \n}" diff --git a/swiftide/tests/query_pipeline.rs b/swiftide/tests/query_pipeline.rs index 46be3d2d..5e8865a0 100644 --- a/swiftide/tests/query_pipeline.rs +++ b/swiftide/tests/query_pipeline.rs @@ -89,8 +89,8 @@ async fn test_hybrid_search_qdrant() { .build() .unwrap(); - let fastembed_sparse = FastEmbed::try_default_sparse().unwrap().to_owned(); - let fastembed = FastEmbed::try_default().unwrap().to_owned(); + let fastembed_sparse = FastEmbed::try_default_sparse().unwrap().clone(); + let fastembed = FastEmbed::try_default().unwrap().clone(); println!("Qdrant URL: {qdrant_url}");