Skip to content

Commit

Permalink
fix: ensure oasysdb works well with postgres (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinkys authored Aug 2, 2024
2 parents ae6324c + 27d0c4f commit 36b32c5
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 10 deletions.
93 changes: 93 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ categories = ["database", "algorithms", "embedded"]
[dependencies]
uuid = { version = "1.9.1", features = ["v4", "fast-rng", "serde"] }
half = { version = "2.4.1", features = ["serde"] }
tokio = { version = "1.39.2", features = ["rt-multi-thread"] }
url = "2.5.2"
futures = "0.3.30"
rand = "0.8.5"
Expand All @@ -34,7 +35,7 @@ serde_json = "1.0.120"
[dependencies.sqlx]
version = "0.7.4"
default-features = false
features = ["all-databases"]
features = ["all-databases", "runtime-tokio"]

[dev-dependencies]
byteorder = "1.5.0"
Expand Down
5 changes: 3 additions & 2 deletions examples/measure_recall.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use common::Dataset;
use futures::executor;
use oasysdb::prelude::*;
use std::error::Error;
use tokio::runtime::Runtime;

mod common;

Expand All @@ -10,7 +10,8 @@ fn main() -> Result<(), Box<dyn Error>> {
let db_url = dataset.database_url();
let config = SourceConfig::new(dataset.name(), "id", "vector");

executor::block_on(dataset.populate_database())?;
let rt = Runtime::new()?;
rt.block_on(dataset.populate_database())?;

let db = Database::open("odb_example", Some(db_url))?;
create_index_flat(&db, &config)?;
Expand Down
22 changes: 15 additions & 7 deletions src/db/database.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::*;
use futures::executor;
use futures::stream::StreamExt;
use sqlx::any::install_default_drivers;
use sqlx::Acquire;
use std::sync::{Arc, Mutex};
use tokio::runtime::Runtime;
use url::Url;
use uuid::Uuid;

Expand Down Expand Up @@ -159,7 +159,8 @@ impl Database {
algorithm: IndexAlgorithm,
config: SourceConfig,
) -> Result<(), Error> {
executor::block_on(self.async_create_index(name, algorithm, config))
let rt = Runtime::new()?;
rt.block_on(self.async_create_index(name, algorithm, config))
}

/// Returns an index reference.
Expand Down Expand Up @@ -263,7 +264,8 @@ impl Database {
/// Updates the index with new records from the source synchronously.
/// - `name`: Index name.
pub fn refresh_index(&self, name: impl AsRef<str>) -> Result<(), Error> {
executor::block_on(self.async_refresh_index(name))
let rt = Runtime::new()?;
rt.block_on(self.async_refresh_index(name))
}

/// Searches the index for nearest neighbors.
Expand Down Expand Up @@ -417,7 +419,8 @@ impl DatabaseState {

/// Connects to the source SQL database.
pub fn connect(&self) -> Result<SourceConnection, Error> {
executor::block_on(self.async_connect())
let rt = Runtime::new()?;
rt.block_on(self.async_connect())
}

/// Disconnects from the source SQL database asynchronously.
Expand All @@ -429,7 +432,8 @@ impl DatabaseState {
/// Disconnects from the source SQL database.
/// - `conn`: Database connection.
pub fn disconnect(conn: SourceConnection) -> Result<(), Error> {
executor::block_on(Self::async_disconnect(conn))
let rt = Runtime::new()?;
rt.block_on(Self::async_disconnect(conn))
}

/// Validates the connection to the source database.
Expand Down Expand Up @@ -539,7 +543,9 @@ mod tests {
fn test_database_refresh_index() -> Result<(), Error> {
let db = create_test_database()?;
let query = generate_insert_query(100, 10);
executor::block_on(db.async_execute_sql(query))?;

let rt = Runtime::new()?;
rt.block_on(db.async_execute_sql(query))?;

db.refresh_index(TEST_INDEX).unwrap();

Expand Down Expand Up @@ -616,7 +622,9 @@ mod tests {
let state = db.state()?;
assert_eq!(state.source_type(), SourceType::SQLITE);

executor::block_on(setup_test_source(&db_url))?;
let rt = Runtime::new()?;
rt.block_on(setup_test_source(&db_url))?;

create_test_index(&mut db)?;
Ok(db)
}
Expand Down

0 comments on commit 36b32c5

Please sign in to comment.