mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-20 01:44:19 +01:00
I/O trait
This commit is contained in:
@@ -8,8 +8,16 @@ pub struct Database {
|
|||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
impl Database {
|
impl Database {
|
||||||
pub fn open(path: &str) -> Database {
|
pub fn open(path: &str) -> Database {
|
||||||
let io = lig_core::IO::new().unwrap();
|
let io = IO {};
|
||||||
let inner = lig_core::Database::open(io, path).unwrap();
|
let inner = lig_core::Database::open(&io, path).unwrap();
|
||||||
Database { _inner: inner }
|
Database { _inner: inner }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct IO {}
|
||||||
|
|
||||||
|
impl lig_core::IO for IO {
|
||||||
|
fn open(&self, _path: &str) -> anyhow::Result<lig_core::PageSource> {
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use clap::{Parser, ValueEnum};
|
use clap::{Parser, ValueEnum};
|
||||||
use cli_table::{Cell, Table};
|
use cli_table::{Cell, Table};
|
||||||
use lig_core::{Database, Value, IO};
|
use lig_core::{Database, Value};
|
||||||
use rustyline::{error::ReadlineError, DefaultEditor};
|
use rustyline::{error::ReadlineError, DefaultEditor};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
@@ -29,8 +29,8 @@ struct Opts {
|
|||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let opts = Opts::parse();
|
let opts = Opts::parse();
|
||||||
let io = IO::new()?;
|
let io = lig_core::default_io()?;
|
||||||
let db = Database::open(io, opts.database.to_str().unwrap())?;
|
let db = Database::open(&io, opts.database.to_str().unwrap())?;
|
||||||
let conn = db.connect();
|
let conn = db.connect();
|
||||||
let mut rl = DefaultEditor::new()?;
|
let mut rl = DefaultEditor::new()?;
|
||||||
let home = dirs::home_dir().unwrap();
|
let home = dirs::home_dir().unwrap();
|
||||||
|
|||||||
@@ -1,12 +1,21 @@
|
|||||||
|
use crate::{PageSource, IO};
|
||||||
use anyhow::{Ok, Result};
|
use anyhow::{Ok, Result};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::io::{Read, Seek};
|
use std::io::{Read, Seek};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub(crate) struct Loop {}
|
pub(crate) struct DarwinIO {}
|
||||||
|
|
||||||
impl Loop {
|
impl IO for DarwinIO {
|
||||||
|
fn open(&self, path: &str) -> Result<PageSource> {
|
||||||
|
let file = self.open_file(path)?;
|
||||||
|
Ok(PageSource { io: Arc::new(file) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DarwinIO {
|
||||||
pub(crate) fn new() -> Result<Self> {
|
pub(crate) fn new() -> Result<Self> {
|
||||||
Ok(Loop {})
|
Ok(DarwinIO {})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn open_file(&self, path: &str) -> Result<File> {
|
pub(crate) fn open_file(&self, path: &str) -> Result<File> {
|
||||||
|
|||||||
@@ -1,16 +1,25 @@
|
|||||||
|
use crate::io::{PageSource, IO};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub(crate) struct Loop {
|
pub(crate) struct LinuxIO {
|
||||||
ring: Rc<RefCell<io_uring::IoUring>>,
|
ring: Rc<RefCell<io_uring::IoUring>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Loop {
|
impl IO for LinuxIO {
|
||||||
|
fn open(&self, path: &str) -> Result<PageSource> {
|
||||||
|
let file = self.open_file(path)?;
|
||||||
|
Ok(PageSource { io: Arc::new(file) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LinuxIO {
|
||||||
pub(crate) fn new() -> Result<Self> {
|
pub(crate) fn new() -> Result<Self> {
|
||||||
let ring = io_uring::IoUring::new(8)?;
|
let ring = io_uring::IoUring::new(8)?;
|
||||||
Ok(Loop {
|
Ok(LinuxIO {
|
||||||
ring: Rc::new(RefCell::new(ring)),
|
ring: Rc::new(RefCell::new(ring)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,75 +1,24 @@
|
|||||||
use anyhow::{Ok, Result};
|
use anyhow::Result;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[cfg(all(feature = "fs", target_os = "linux"))]
|
#[cfg(target_os = "linux")]
|
||||||
mod linux;
|
mod linux;
|
||||||
|
|
||||||
#[cfg(feature = "fs")]
|
#[cfg(target_os = "macos")]
|
||||||
mod darwin;
|
mod darwin;
|
||||||
|
|
||||||
/// I/O access method
|
pub trait IO {
|
||||||
enum IOMethod {
|
fn open(&self, path: &str) -> Result<PageSource>;
|
||||||
#[cfg(not(feature = "fs"))]
|
|
||||||
Memory,
|
|
||||||
|
|
||||||
#[cfg(feature = "fs")]
|
|
||||||
Sync { io: darwin::Loop },
|
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
|
||||||
IoUring { io: linux::Loop },
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// I/O access interface.
|
#[cfg(target_os = "linux")]
|
||||||
pub struct IO {
|
pub fn default_io() -> Result<impl IO> {
|
||||||
io_method: IOMethod,
|
Ok(linux::LinuxIO::new()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IO {
|
#[cfg(target_os = "macos")]
|
||||||
#[cfg(all(feature = "fs", target_os = "linux"))]
|
pub fn default_io() -> Result<impl IO> {
|
||||||
pub fn new() -> Result<Self> {
|
Ok(darwin::DarwinIO::new()?)
|
||||||
Ok(IO {
|
|
||||||
io_method: IOMethod::IoUring {
|
|
||||||
io: linux::Loop::new()?,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(all(feature = "fs", target_os = "macos"))]
|
|
||||||
pub fn new() -> Result<Self> {
|
|
||||||
Ok(IO {
|
|
||||||
io_method: IOMethod::Sync {
|
|
||||||
io: darwin::Loop::new()?,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "fs"))]
|
|
||||||
pub fn new() -> Result<Self> {
|
|
||||||
Ok(IO {
|
|
||||||
io_method: IOMethod::Memory,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IO {
|
|
||||||
pub fn open(&self, path: &str) -> Result<PageSource> {
|
|
||||||
match &self.io_method {
|
|
||||||
#[cfg(feature = "fs")]
|
|
||||||
IOMethod::Sync { io } => {
|
|
||||||
let io = Arc::new(io.open_file(path)?);
|
|
||||||
Ok(PageSource { io })
|
|
||||||
}
|
|
||||||
#[cfg(all(feature = "fs", target_os = "linux"))]
|
|
||||||
IOMethod::IoUring { io } => {
|
|
||||||
let io = Arc::new(io.open_file(path)?);
|
|
||||||
Ok(PageSource { io })
|
|
||||||
}
|
|
||||||
#[cfg(not(feature = "fs"))]
|
|
||||||
IOMethod::Memory => {
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PageSource {
|
pub struct PageSource {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ use schema::Schema;
|
|||||||
use sqlite3_parser::{ast::Cmd, lexer::sql::Parser};
|
use sqlite3_parser::{ast::Cmd, lexer::sql::Parser};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub use io::default_io;
|
||||||
pub use io::{PageSource, IO};
|
pub use io::{PageSource, IO};
|
||||||
pub use types::Value;
|
pub use types::Value;
|
||||||
|
|
||||||
@@ -29,8 +30,8 @@ pub struct Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Database {
|
impl Database {
|
||||||
pub fn open(io: IO, path: &str) -> Result<Database> {
|
pub fn open(io: &impl IO, path: &str) -> Result<Database> {
|
||||||
let pager = Arc::new(Pager::open(&io, path)?);
|
let pager = Arc::new(Pager::open(io, path)?);
|
||||||
let bootstrap_schema = Arc::new(Schema::new());
|
let bootstrap_schema = Arc::new(Schema::new());
|
||||||
let conn = Connection {
|
let conn = Connection {
|
||||||
pager: pager.clone(),
|
pager: pager.clone(),
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ pub struct Pager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Pager {
|
impl Pager {
|
||||||
pub fn open(io: &IO, path: &str) -> anyhow::Result<Self> {
|
pub fn open(io: &impl IO, path: &str) -> anyhow::Result<Self> {
|
||||||
let database = io.open(path)?;
|
let database = io.open(path)?;
|
||||||
let db_header = sqlite3_ondisk::read_database_header(&database)?;
|
let db_header = sqlite3_ondisk::read_database_header(&database)?;
|
||||||
let page_size = db_header.page_size as usize;
|
let page_size = db_header.page_size as usize;
|
||||||
|
|||||||
Reference in New Issue
Block a user