-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1408 from fermyon/sqlite-host-component
[SIP] sqlite host component
- Loading branch information
Showing
26 changed files
with
843 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "spin-sqlite" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
spin-core = { path = "../core" } | ||
spin-app = { path = "../app" } | ||
spin-key-value = { path = "../key-value" } | ||
spin-world = { path = "../world" } | ||
anyhow = "1.0" | ||
wit-bindgen-wasmtime = { workspace = true } | ||
rusqlite = { version = "0.29.0", features = [ "bundled" ] } | ||
rand = "0.8" | ||
once_cell = "1" | ||
tokio = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use std::{ | ||
collections::HashMap, | ||
path::PathBuf, | ||
sync::{Arc, Mutex}, | ||
}; | ||
|
||
use once_cell::sync::OnceCell; | ||
use rusqlite::Connection; | ||
use spin_app::{AppComponent, DynamicHostComponent}; | ||
use spin_core::HostComponent; | ||
use spin_world::sqlite; | ||
|
||
use crate::SqliteImpl; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum DatabaseLocation { | ||
InMemory, | ||
Path(PathBuf), | ||
} | ||
|
||
/// A connection to a sqlite database | ||
pub struct SqliteConnection { | ||
location: DatabaseLocation, | ||
connection: OnceCell<Arc<Mutex<Connection>>>, | ||
} | ||
|
||
impl SqliteConnection { | ||
pub fn new(location: DatabaseLocation) -> Self { | ||
Self { | ||
location, | ||
connection: OnceCell::new(), | ||
} | ||
} | ||
} | ||
|
||
impl ConnectionManager for SqliteConnection { | ||
fn get_connection(&self) -> Result<Arc<Mutex<Connection>>, sqlite::Error> { | ||
let connection = self | ||
.connection | ||
.get_or_try_init(|| -> Result<_, sqlite::Error> { | ||
let c = match &self.location { | ||
DatabaseLocation::InMemory => Connection::open_in_memory(), | ||
DatabaseLocation::Path(path) => Connection::open(path), | ||
} | ||
.map_err(|e| sqlite::Error::Io(e.to_string()))?; | ||
Ok(Arc::new(Mutex::new(c))) | ||
})? | ||
.clone(); | ||
Ok(connection) | ||
} | ||
} | ||
|
||
pub trait ConnectionManager: Send + Sync { | ||
fn get_connection(&self) -> Result<Arc<Mutex<Connection>>, sqlite::Error>; | ||
} | ||
|
||
pub struct SqliteComponent { | ||
connection_managers: HashMap<String, Arc<dyn ConnectionManager>>, | ||
} | ||
|
||
impl SqliteComponent { | ||
pub fn new(connection_managers: HashMap<String, Arc<dyn ConnectionManager>>) -> Self { | ||
Self { | ||
connection_managers, | ||
} | ||
} | ||
} | ||
|
||
impl HostComponent for SqliteComponent { | ||
type Data = super::SqliteImpl; | ||
|
||
fn add_to_linker<T: Send>( | ||
linker: &mut spin_core::Linker<T>, | ||
get: impl Fn(&mut spin_core::Data<T>) -> &mut Self::Data + Send + Sync + Copy + 'static, | ||
) -> anyhow::Result<()> { | ||
sqlite::add_to_linker(linker, get) | ||
} | ||
|
||
fn build_data(&self) -> Self::Data { | ||
SqliteImpl::new(self.connection_managers.clone()) | ||
} | ||
} | ||
|
||
impl DynamicHostComponent for SqliteComponent { | ||
fn update_data(&self, data: &mut Self::Data, component: &AppComponent) -> anyhow::Result<()> { | ||
let allowed_databases = component | ||
.get_metadata(crate::DATABASES_KEY)? | ||
.unwrap_or_default(); | ||
data.component_init(allowed_databases); | ||
// TODO: allow dynamically updating connection manager | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.