From 654f1db3627c68a82e3395a6098f0477d99c6231 Mon Sep 17 00:00:00 2001 From: WangJiangtong Date: Tue, 11 Jul 2023 16:41:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20service=5Fimpl=20=E5=B1=82?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cloud_extension/src/c_adapter/mod.rs | 1 + src/cloud_extension/src/lib.rs | 21 ++-- .../src/service_impl/asset_loader.rs | 74 +++++++++++++ .../src/service_impl/cloud_db.rs | 85 +++++++++++++++ .../src/service_impl/cloud_service.rs | 102 ++++++++++++++++++ .../src/service_impl/db_types.rs | 76 +++++++++++++ src/cloud_extension/src/service_impl/error.rs | 32 ++++++ src/cloud_extension/src/service_impl/mod.rs | 42 +++++++- 8 files changed, 423 insertions(+), 10 deletions(-) create mode 100644 src/cloud_extension/src/service_impl/db_types.rs diff --git a/src/cloud_extension/src/c_adapter/mod.rs b/src/cloud_extension/src/c_adapter/mod.rs index e69de29..8b13789 100644 --- a/src/cloud_extension/src/c_adapter/mod.rs +++ b/src/cloud_extension/src/c_adapter/mod.rs @@ -0,0 +1 @@ + diff --git a/src/cloud_extension/src/lib.rs b/src/cloud_extension/src/lib.rs index d7159e1..fa8d934 100644 --- a/src/cloud_extension/src/lib.rs +++ b/src/cloud_extension/src/lib.rs @@ -1,9 +1,18 @@ +// Copyright (c) 2023 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #[cfg(feature = "c_adapter")] pub mod c_adapter; -mod ipc_conn; -mod service_impl; - -pub use service_impl::*; -// TODO: 可能改名 -pub use ipc_conn::IpcConfig; +pub mod ipc_conn; +pub mod service_impl; diff --git a/src/cloud_extension/src/service_impl/asset_loader.rs b/src/cloud_extension/src/service_impl/asset_loader.rs index e69de29..2d31f7d 100644 --- a/src/cloud_extension/src/service_impl/asset_loader.rs +++ b/src/cloud_extension/src/service_impl/asset_loader.rs @@ -0,0 +1,74 @@ +// Copyright (c) 2023 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::collections::HashMap; + +use crate::ipc_conn::Asset as AssetInner; +use crate::service_impl::db_types::{Database, Table}; +use crate::service_impl::error::SyncError; +use crate::service_impl::{VBucket, Value}; + +pub struct AssetLoader<'a> { + tables: &'a HashMap, + user_id: u64, + bundle_name: String, +} + +impl<'a> AssetLoader<'a> { + pub fn new_from_ipc_conn(token_id: u32, db: &Database) -> AssetLoader { + todo!(); + + // AssetLoader::new() + } + + pub fn new(user_id: u64, bundle_name: &str, db: &Database) -> Self { + AssetLoader { + tables: &db.tables, + user_id, + bundle_name: bundle_name.to_string(), + } + } + + // 发消息给 JS 对端,不管文件,结束 + pub fn upload( + &self, + src: &Asset, + table_name: &str, + gid: &str, + prefix: &Value, + ) -> Result<(), SyncError> { + todo!(); + } + + // 发消息给 JS 对端,不管文件,结束 + pub fn download( + &self, + table_name: &str, + gid: &str, + prefix: &Value, + ) -> Result { + todo!(); + } + + // 删除本地文件,用本地文件系统 + pub fn remove_local_assets(&self, assets: &VBucket) -> Result<(), SyncError> { + todo!(); + } +} + +pub struct Asset { + inner: AssetInner, + createTime: String, + modifyTime: String, + size: String, +} diff --git a/src/cloud_extension/src/service_impl/cloud_db.rs b/src/cloud_extension/src/service_impl/cloud_db.rs index e69de29..72a616f 100644 --- a/src/cloud_extension/src/service_impl/cloud_db.rs +++ b/src/cloud_extension/src/service_impl/cloud_db.rs @@ -0,0 +1,85 @@ +// Copyright (c) 2023 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::service_impl::db_types::{Database, Record}; +use crate::service_impl::error::SyncError; +use crate::service_impl::VBucket; +use std::sync::MutexGuard; + +pub struct CloudDatabase { + database: Database, +} + +impl CloudDatabase { + // cloud server::connect cloud db + pub fn new_from_ipc_conn(token_id: u32, db: &Database) -> CloudDatabase { + todo!(); + + // CloudDatabase::new() + } + + pub fn new(db: &Database) -> Self { + todo!(); + } + + pub fn execute(&mut self, table: &str, sql: &str, extend: &VBucket) -> Result<(), SyncError> { + todo!(); + } + + pub fn batch_insert( + &mut self, + table: &str, + values: &[VBucket], + extends: &[VBucket], + ) -> Result<(), SyncError> { + todo!(); + } + + pub fn batch_update( + &mut self, + table: &str, + values: &[VBucket], + extends: &[VBucket], + ) -> Result<(), SyncError> { + todo!(); + } + + pub fn batch_delete(&mut self, extends: &[VBucket]) -> Result<(), SyncError> { + todo!(); + } + + pub fn batch_query(&mut self, table: &str, extends: &[VBucket]) -> Result { + todo!(); + } + + // Unlock when MutexGuard is dropped in Rust + pub fn lock(&mut self) -> Result, SyncError> { + todo!(); + } + + pub fn flush(&self) -> bool { + todo!(); + } + + pub fn close(&self) -> bool { + todo!(); + } + + pub fn heartbeat(&self) -> i32 { + todo!(); + } + + pub fn alive_time(&self) -> i64 { + todo!(); + } +} diff --git a/src/cloud_extension/src/service_impl/cloud_service.rs b/src/cloud_extension/src/service_impl/cloud_service.rs index e69de29..3db0a2e 100644 --- a/src/cloud_extension/src/service_impl/cloud_service.rs +++ b/src/cloud_extension/src/service_impl/cloud_service.rs @@ -0,0 +1,102 @@ +// Copyright (c) 2023 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::ipc_conn::{AppInfo as AppInfoInner, Infos}; +use crate::service_impl::db_types::Database; +use crate::service_impl::db_types::SchemaMeta; +use crate::service_impl::error::SyncError; +use std::collections::HashMap; + +pub struct CloudServer { + user_id: u64, + drive_kit: &'static Infos, +} + +impl CloudServer { + // Function as RegisterCloudInstance to get instance through IPC + pub fn new(user_id: u64) -> Self { + // Establish ipc conn + + // Initialize DRIVE_KIT + + // Get DriveKitNative + + todo!(); + } + + pub fn get_service_info(&self) -> CloudInfo { + todo!(); + } + + pub fn get_app_schema(&self, bundle_name: &str) -> SchemaMeta { + todo!(); + } + + pub fn subscribe(&mut self, dbs: &HashMap>) -> Result<(), SyncError> { + todo!(); + } + + pub fn unsubscribe(&mut self, dbs: &HashMap>) -> Result<(), SyncError> { + todo!(); + } +} + +pub struct CloudInfo { + user: i32, + id: String, + total_space: u64, + remain_space: u64, + enable_cloud: bool, + apps: HashMap, +} + +impl CloudInfo { + pub fn get_key(&self) -> String { + todo!() + } + + pub fn get_schema_keys(&self) -> HashMap { + todo!(); + } + + pub fn get_schema_key(&self, bundle_name: &str, instance_id: i32) -> String { + todo!(); + } + + pub fn get_schema_prefix(&self, bundle_name: &str) -> String { + todo!(); + } + + pub fn is_valid(&self) -> bool { + !self.id.is_empty() + } + + pub fn exist(&self, bundle_name: &str, instance_id: i32) -> bool { + todo!(); + } + + pub fn is_on(&self, bundle_name: &str, instance_id: i32) -> bool { + todo!(); + } + + pub fn get_prefix(&self, fields: &[String]) -> String { + todo!(); + } +} + +pub(crate) struct AppInfo { + inner: AppInfoInner, + version: u64, + instance_id: i32, + cloud_switch: bool, +} diff --git a/src/cloud_extension/src/service_impl/db_types.rs b/src/cloud_extension/src/service_impl/db_types.rs new file mode 100644 index 0000000..d60cbfe --- /dev/null +++ b/src/cloud_extension/src/service_impl/db_types.rs @@ -0,0 +1,76 @@ +// Copyright (c) 2023 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::ipc_conn::{Database as DatabaseInner, Lock as LockInner, Record as RecordInner}; +use std::collections::HashMap; + +pub(crate) struct SchemaMeta { + version: i32, + bundle_name: String, + databases: Vec, +} + +impl SchemaMeta { + pub fn get_database(&mut self, store_id: &str) -> &Database { + todo!(); + } + + pub fn is_valid(&self) -> bool { + todo!(); + } +} + +pub(crate) struct Database { + inner: DatabaseInner, + cloud_lock: LockInner, + name: String, + alias: String, + pub(crate) tables: HashMap, +} + +pub(crate) struct Table { + pub(crate) name: String, + alias: String, + fields: Vec, +} + +pub(crate) struct Record { + cursor: String, + data: Vec, + current_cursor: todo!(), + consumed: bool, + finished: bool, + names: Vec, +} + +pub(crate) struct Field { + colName: String, + alias: String, + typ: i32, + // By default is false + primary: bool, + // By default is true + nullable: bool, +} + +impl Default for Field { + fn default() -> Self { + Field { + colName: String::default(), + alias: String::default(), + typ: i32::default(), + primary: false, + nullable: true, + } + } +} diff --git a/src/cloud_extension/src/service_impl/error.rs b/src/cloud_extension/src/service_impl/error.rs index e69de29..deb8b37 100644 --- a/src/cloud_extension/src/service_impl/error.rs +++ b/src/cloud_extension/src/service_impl/error.rs @@ -0,0 +1,32 @@ +// Copyright (c) 2023 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::fmt::{Debug, Display, Formatter}; + +pub enum SyncError { + Unknown, +} + +impl Debug for SyncError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + todo!() + } +} + +impl Display for SyncError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + todo!() + } +} + +impl std::error::Error for SyncError {} diff --git a/src/cloud_extension/src/service_impl/mod.rs b/src/cloud_extension/src/service_impl/mod.rs index 2c01c57..d4f04ee 100644 --- a/src/cloud_extension/src/service_impl/mod.rs +++ b/src/cloud_extension/src/service_impl/mod.rs @@ -1,4 +1,38 @@ -mod error; -mod asset_loader; -mod cloud_db; -mod cloud_service; \ No newline at end of file +// Copyright (c) 2023 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::ipc_conn::IpcConn; +use crate::service_impl::asset_loader::Asset; +use std::collections::HashMap; +use std::sync::Mutex; + +pub mod asset_loader; +pub mod cloud_db; +pub mod cloud_service; +pub mod db_types; +pub mod error; + +pub(crate) static IPC_CONNS: Mutex = Mutex::new(IpcConn::new()); + +pub(crate) enum Value { + Empty, + Int(i64), + Double(f64), + String(String), + Bool(bool), + Bytes(Vec), + Asset(Asset), + Assets(Vec), +} + +pub(crate) type VBucket = HashMap; -- Gitee