refactor: adapt latest octobase

This commit is contained in:
linonetwo
2023-01-31 22:29:21 +08:00
parent 7b6addbe28
commit 44d9fbf264
9 changed files with 594 additions and 150 deletions
+9 -26
View File
@@ -51,33 +51,16 @@ pub async fn get_doc<'s>(
) -> Result<GetDocumentResponse, String> {
// TODO: check user permission
let doc_file_path = &state
.0
.lock()
.await
.doc_storage
.get_path(parameters.id.clone());
let mut file = File::open(doc_file_path).await.unwrap();
let mut updates_vector: Vec<Vec<u8>> = Vec::new();
loop {
let len = file.read_u64_le().await;
let len = match len {
Ok(len) => len,
Err(e) if e.kind() == ErrorKind::UnexpectedEof => break,
Err(e) => return Err(format!("Failed to get yDoc from {}", parameters.id)),
};
let mut update = vec![0; len as usize];
file.read_exact(&mut update).await.unwrap();
updates_vector.push(update);
file.read_u64_le().await.unwrap();
if let Ok(all_updates_of_workspace) = &state.0.lock().await.doc_storage.all(&parameters.id).await {
Ok(GetDocumentResponse {
updates: all_updates_of_workspace.iter().map(|model| model.blob.clone()).collect::<Vec<Vec<u8>>>(),
})
} else {
Err(format!(
"Failed to get yDoc from workspace {}",
parameters.id
))
}
Ok(GetDocumentResponse {
updates: updates_vector,
})
}
#[tauri::command]
+1 -1
View File
@@ -1,5 +1,5 @@
use cloud_database::{CreateUser, User};
use ipc_types::document::CreateDocumentParameter;
use jwst_storage::{CreateUser, User};
use crate::state::AppState;
+26 -15
View File
@@ -1,34 +1,45 @@
use jwst_storage::{BlobFsStorage, DocFsStorage, SqliteDBContext};
use cloud_database::SqliteDBContext;
use jwst_storage::{BlobAutoStorage, DocAutoStorage};
use std::{fs, path::Path};
use tauri::api::path::document_dir;
use tokio::sync::Mutex;
pub struct AppStateRaw {
pub blob_storage: BlobFsStorage,
pub doc_storage: DocFsStorage,
pub doc_storage: DocAutoStorage,
pub blob_storage: BlobAutoStorage,
pub metadata_db: SqliteDBContext,
}
impl AppStateRaw {
pub async fn new() -> Option<AppStateRaw> {
let affine_document_path = Path::new(&document_dir()?.into_os_string()).join("affine");
let doc_path = affine_document_path.join("doc");
let blob_env = affine_document_path.join("blob");
let db_env = format!(
let metadata_db_env = format!(
"sqlite://{}?mode=rwc",
affine_document_path
.join("metadata.db")
.into_os_string()
.into_string()
.unwrap()
.join("metadata")
.with_extension("db")
.display()
);
fs::create_dir_all(doc_path.clone()).unwrap();
fs::create_dir_all(blob_env.clone()).unwrap();
let blob_db_env = format!(
"sqlite://{}?mode=rwc",
affine_document_path
.join("blob")
.with_extension("db")
.display()
);
let doc_db_env = format!(
"sqlite://{}?mode=rwc",
affine_document_path
.join("doc")
.with_extension("db")
.display()
);
fs::create_dir_all(affine_document_path.clone()).unwrap();
Some(Self {
doc_storage: DocFsStorage::new(Some(16), 500, Path::new(&doc_path).into()).await,
blob_storage: BlobFsStorage::new(Some(16), Path::new(&blob_env).into()).await,
metadata_db: SqliteDBContext::new(db_env).await,
doc_storage: DocAutoStorage::init_pool(&doc_db_env).await.unwrap(),
blob_storage: BlobAutoStorage::init_pool(&blob_db_env).await.unwrap(),
metadata_db: SqliteDBContext::new(metadata_db_env).await,
})
}
}