Skip to content

Commit

Permalink
Begin implementing db for RepositoryMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Apr 1, 2024
1 parent 955089b commit a79cbed
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ members = [
"crates/tabby-scheduler",
"crates/tabby-download",
"crates/tabby-inference",
"crates/tabby-repositories",
"crates/llama-cpp-bindings",
"crates/http-api-bindings",
"crates/aim-downloader",
"crates/juniper-axum",
"ee/tabby-webserver",
"ee/tabby-db", "ee/tabby-db-macros",
"ee/tabby-db", "ee/tabby-db-macros", "crates/tabby-repositories",
]

[workspace.package]
Expand All @@ -21,6 +22,7 @@ authors = ["Meng Zhang"]
homepage = "https://github.com/TabbyML/tabby"

[workspace.dependencies]
sqlx = { version = "0.7.3", features = ["sqlite", "chrono", "runtime-tokio", "macros"] }
lazy_static = "1.4.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
Expand Down
4 changes: 4 additions & 0 deletions crates/tabby-common/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub fn config_file() -> PathBuf {
tabby_root().join("config.toml")
}

pub fn repository_meta_db() -> PathBuf {
tabby_root().join("repositories.sqlite")
}

pub fn usage_id_file() -> PathBuf {
tabby_root().join("usage_anonymous_id")
}
Expand Down
1 change: 1 addition & 0 deletions crates/tabby-repositories/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL=sqlite://crates/tabby-repositories/schema.sqlite
14 changes: 14 additions & 0 deletions crates/tabby-repositories/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "tabby-repositories"
version.workspace = true
edition.workspace = true
authors.workspace = true
homepage.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sqlx = { workspace = true }
anyhow = { workspace = true }
tabby-common = { path = "../tabby-common" }
serde_json = { workspace = true }
10 changes: 10 additions & 0 deletions crates/tabby-repositories/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DROP TABLE IF EXISTS repository_meta;
CREATE TABLE repository_meta(
git_url TEXT NOT NULL,
filepath TEXT NOT NULL,
language TEXT NOT NULL,
max_line_length INTEGER NOT NULL,
avg_line_length REAL NOT NULL,
alphanum_fraction REAL NOT NULL,
tags TEXT NOT NULL
);
Binary file added crates/tabby-repositories/schema.sqlite
Binary file not shown.
57 changes: 57 additions & 0 deletions crates/tabby-repositories/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use anyhow::Result;
use sqlx::sqlite::SqlitePoolOptions;
use sqlx::{query, SqlitePool};
use sqlx::{sqlite::SqliteConnectOptions, Pool, Sqlite};
use std::str::FromStr;
use tabby_common::Tag;

pub struct RepositoryCache {
pool: Pool<Sqlite>,
}

struct RepositoryMetaDAO {
git_url: String,
filepath: String,
language: String,
max_line_length: usize,
avg_line_length: f32,
alphanum_fraction: f32,
tags: String,
}

impl RepositoryCache {
pub async fn new() -> Result<Self> {
let init_query = include_str!("../schema.sql");
let options = SqliteConnectOptions::new()
.filename(tabby_common::path::repository_meta_db())
.create_if_missing(true);
let pool = SqlitePool::connect_with(options).await?;
sqlx::query(init_query).execute(&pool).await?;
Ok(RepositoryCache { pool })
}

pub async fn clear(&self) -> Result<()> {
query!("DELETE FROM repository_meta")
.execute(&self.pool)
.await?;
Ok(())
}

pub async fn add_repository_meta(
&self,
git_url: String,
filepath: String,
language: String,
max_line_length: i64,
avg_line_length: f32,
alphanum_fraction: f32,
tags: Vec<Tag>,
) -> Result<()> {
let tags = serde_json::to_string(&tags)?;
query!("INSERT INTO repository_meta (git_url, filepath, language, max_line_length, avg_line_length, alphanum_fraction, tags)
VALUES ($1, $2, $3, $4, $5, $6, $7)",
git_url, filepath, language, max_line_length, avg_line_length, alphanum_fraction, tags
).execute(&self.pool).await?;
Ok(())
}
}
2 changes: 1 addition & 1 deletion ee/tabby-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ anyhow.workspace = true
chrono = { workspace = true, features = ["serde"] }
hash-ids.workspace = true
lazy_static.workspace = true
sqlx = { workspace = true }
sql_query_builder = { version = "2.1.0", features = ["sqlite"] }
sqlx = { version = "0.7.3", features = ["sqlite", "chrono", "runtime-tokio", "macros"] }
tabby-common = { path = "../../crates/tabby-common" }
tokio = { workspace = true, features = ["fs"] }
uuid.workspace = true
Expand Down

0 comments on commit a79cbed

Please sign in to comment.