Skip to content

Commit

Permalink
fix: workspace export logic (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit authored Oct 20, 2023
1 parent 73285e5 commit 22f3e04
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ class MainActivity : AppCompatActivity() {
val database = File(filesDir, "jwst.db")
val storage = Storage(database.absolutePath, "ws://10.0.2.2:3000/collaboration", "debug")

storage.initWorkspace(getRandomId(), getStaticWorkspace().hexStringToByteArray())
val text = storage.getWorkspace("test1").get().get("test").get().get("test").get()
val randomId = getRandomId()
storage.initWorkspace(randomId, getStaticWorkspace().hexStringToByteArray())
val text = storage.getWorkspace(randomId).get().get("test").get().get("test").get()
Log.i("jwst", "text: $text")
storage.exportWorkspace(randomId).getOrNull()?.let { data ->
val isSame = data.contentEquals(getStaticWorkspace().hexStringToByteArray())
Log.i("jwst", "exported data: $isSame")
}

storage.getWorkspace("test").unwrap()?.let { workspace ->
setupWorkspace(workspace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<key>HeadersPath</key>
<string>Headers</string>
<key>LibraryIdentifier</key>
<string>ios-arm64</string>
<string>ios-arm64-simulator</string>
<key>LibraryPath</key>
<string>liboctobase.a</string>
<key>SupportedArchitectures</key>
Expand All @@ -35,14 +35,16 @@
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
<dict>
<key>BinaryPath</key>
<string>liboctobase.a</string>
<key>HeadersPath</key>
<string>Headers</string>
<key>LibraryIdentifier</key>
<string>ios-arm64-simulator</string>
<string>ios-arm64</string>
<key>LibraryPath</key>
<string>liboctobase.a</string>
<key>SupportedArchitectures</key>
Expand All @@ -51,8 +53,6 @@
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
</array>
<key>CFBundlePackageType</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ bool __swift_bridge__$Storage$is_connected(void* self);
bool __swift_bridge__$Storage$is_finished(void* self);
bool __swift_bridge__$Storage$is_error(void* self);
void* __swift_bridge__$Storage$get_sync_state(void* self);
bool __swift_bridge__$Storage$init(void* self, void* workspace_id, void* data);
void* __swift_bridge__$Storage$export(void* self, void* workspace_id);
bool __swift_bridge__$Storage$import_workspace(void* self, void* workspace_id, void* data);
void* __swift_bridge__$Storage$export_workspace(void* self, void* workspace_id);
void* __swift_bridge__$Storage$connect(void* self, void* workspace_id, void* remote);
void* __swift_bridge__$Storage$get_last_synced(void* self);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ bool __swift_bridge__$Storage$is_connected(void* self);
bool __swift_bridge__$Storage$is_finished(void* self);
bool __swift_bridge__$Storage$is_error(void* self);
void* __swift_bridge__$Storage$get_sync_state(void* self);
bool __swift_bridge__$Storage$init(void* self, void* workspace_id, void* data);
void* __swift_bridge__$Storage$export(void* self, void* workspace_id);
bool __swift_bridge__$Storage$import_workspace(void* self, void* workspace_id, void* data);
void* __swift_bridge__$Storage$export_workspace(void* self, void* workspace_id);
void* __swift_bridge__$Storage$connect(void* self, void* workspace_id, void* remote);
void* __swift_bridge__$Storage$get_last_synced(void* self);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ bool __swift_bridge__$Storage$is_connected(void* self);
bool __swift_bridge__$Storage$is_finished(void* self);
bool __swift_bridge__$Storage$is_error(void* self);
void* __swift_bridge__$Storage$get_sync_state(void* self);
bool __swift_bridge__$Storage$init(void* self, void* workspace_id, void* data);
void* __swift_bridge__$Storage$export(void* self, void* workspace_id);
bool __swift_bridge__$Storage$import_workspace(void* self, void* workspace_id, void* data);
void* __swift_bridge__$Storage$export_workspace(void* self, void* workspace_id);
void* __swift_bridge__$Storage$connect(void* self, void* workspace_id, void* remote);
void* __swift_bridge__$Storage$get_last_synced(void* self);

Expand Down
8 changes: 4 additions & 4 deletions apps/swift/OctoBaseSwift/Sources/OctoBase/jwst-swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -578,12 +578,12 @@ public class StorageRefMut: StorageRef {
}
}
extension StorageRefMut {
public func init<GenericIntoRustString: IntoRustString>(_ workspace_id: GenericIntoRustString, _ data: RustVec<UInt8>) -> Bool {
__swift_bridge__$Storage$init(ptr, { let rustString = workspace_id.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), { let val = data; val.isOwned = false; return val.ptr }())
public func import_workspace<GenericIntoRustString: IntoRustString>(_ workspace_id: GenericIntoRustString, _ data: RustVec<UInt8>) -> Bool {
__swift_bridge__$Storage$import_workspace(ptr, { let rustString = workspace_id.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), { let val = data; val.isOwned = false; return val.ptr }())
}

public func export<GenericIntoRustString: IntoRustString>(_ workspace_id: GenericIntoRustString) -> Optional<RustVec<UInt8>> {
{ let val = __swift_bridge__$Storage$export(ptr, { let rustString = workspace_id.intoRustString(); rustString.isOwned = false; return rustString.ptr }()); if val != nil { return RustVec(ptr: val!) } else { return nil } }()
public func export_workspace<GenericIntoRustString: IntoRustString>(_ workspace_id: GenericIntoRustString) -> Optional<RustVec<UInt8>> {
{ let val = __swift_bridge__$Storage$export_workspace(ptr, { let rustString = workspace_id.intoRustString(); rustString.isOwned = false; return rustString.ptr }()); if val != nil { return RustVec(ptr: val!) } else { return nil } }()
}

public func connect<GenericIntoRustString: IntoRustString>(_ workspace_id: GenericIntoRustString, _ remote: GenericIntoRustString) -> Optional<Workspace> {
Expand Down
2 changes: 1 addition & 1 deletion libs/jwst-binding/jwst-jni/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ publishing {
release(MavenPublication) {
groupId = 'com.toeverything'
artifactId = 'octobase'
version = '0.2.7'
version = '0.2.8'

afterEvaluate {
from components.release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class Storage constructor(path: String, private val remote: String = "", private
val error get() = this.storage.error()

fun initWorkspace(id: String, data: ByteArray): Result<Unit> {
val success = this.storage.init(id,data)
val success = this.storage.import_workspace(id,data)
return if (success) {
Result.success(Unit)
} else {
Expand All @@ -193,7 +193,7 @@ class Storage constructor(path: String, private val remote: String = "", private
}

fun exportWorkspace(id: String): Result<ByteArray> {
val data = this.storage.export(id)
val data = this.storage.export_workspace(id)
return if (data.isNotEmpty()) {
Result.success(data)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ public final boolean is_error() {
}
private static native @NonNull String do_get_sync_state(long self);

public final boolean import(@NonNull String workspace_id, @NonNull byte [] data) {
boolean ret = do_import(mNativeObj, workspace_id, data);
public final boolean import_workspace(@NonNull String workspace_id, @NonNull byte [] data) {
boolean ret = do_import_workspace(mNativeObj, workspace_id, data);

return ret;
}
private static native boolean do_import(long self, @NonNull String workspace_id, byte [] data);
private static native boolean do_import_workspace(long self, @NonNull String workspace_id, byte [] data);

public final byte [] export(@NonNull String workspace_id) {
byte [] ret = do_export(mNativeObj, workspace_id);
public final byte [] export_workspace(@NonNull String workspace_id) {
byte [] ret = do_export_workspace(mNativeObj, workspace_id);

return ret;
}
private static native byte [] do_export(long self, @NonNull String workspace_id);
private static native byte [] do_export_workspace(long self, @NonNull String workspace_id);

public final @NonNull java.util.Optional<Workspace> connect(@NonNull String workspace_id, @NonNull String remote) {
long ret = do_connect(mNativeObj, workspace_id, remote);
Expand Down
4 changes: 2 additions & 2 deletions libs/jwst-binding/jwst-jni/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ foreign_typemap!(
fn JwstStorage::is_finished(&self) -> bool;
fn JwstStorage::is_error(&self) -> bool;
fn JwstStorage::get_sync_state(&self) -> String;
fn JwstStorage::import(&mut self, workspace_id: String, data: &[u8]) -> bool; alias import;
fn JwstStorage::export(&mut self, workspace_id: String) -> Vec<u8>; alias export;
fn JwstStorage::import_workspace(&mut self, workspace_id: String, data: &[u8]) -> bool; alias import_workspace;
fn JwstStorage::export_workspace(&mut self, workspace_id: String) -> Vec<u8>; alias export_workspace;
fn JwstStorage::connect(&mut self, workspace_id: String, remote: String) -> Option<Workspace>; alias connect;
fn JwstStorage::get_last_synced(&self) ->Vec<i64>;
}
Expand Down
4 changes: 2 additions & 2 deletions libs/jwst-binding/jwst-jni/src/java_glue.rs.in
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ foreign_class!(
fn JwstStorage::is_finished(&self) -> bool;
fn JwstStorage::is_error(&self) -> bool;
fn JwstStorage::get_sync_state(&self) -> String;
fn JwstStorage::import(&mut self, workspace_id: String, data: &[u8]) -> bool; alias import;
fn JwstStorage::export(&mut self, workspace_id: String) -> Vec<u8>; alias export;
fn JwstStorage::import_workspace(&mut self, workspace_id: String, data: &[u8]) -> bool; alias import_workspace;
fn JwstStorage::export_workspace(&mut self, workspace_id: String) -> Vec<u8>; alias export_workspace;
fn JwstStorage::connect(&mut self, workspace_id: String, remote: String) -> Option<Workspace>; alias connect;
fn JwstStorage::get_last_synced(&self) ->Vec<i64>;
}
Expand Down
13 changes: 6 additions & 7 deletions libs/jwst-binding/jwst-jni/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ impl JwstStorage {
}
}

pub fn import(&mut self, workspace_id: String, data: &[u8]) -> bool {
match self.import_workspace(workspace_id, data) {
pub fn import_workspace(&mut self, workspace_id: String, data: &[u8]) -> bool {
match self.import_workspace_inner(workspace_id, data) {
Ok(_) => true,
Err(e) => {
let error = format!("Failed to init workspace: {:?}", e);
Expand All @@ -103,7 +103,7 @@ impl JwstStorage {
}
}

fn import_workspace(&self, workspace_id: String, data: &[u8]) -> JwstStorageResult {
fn import_workspace_inner(&self, workspace_id: String, data: &[u8]) -> JwstStorageResult {
let rt = Arc::new(
Builder::new_multi_thread()
.worker_threads(1)
Expand All @@ -115,8 +115,8 @@ impl JwstStorage {
rt.block_on(self.storage.init_workspace(workspace_id, data.to_vec()))
}

pub fn export(&mut self, workspace_id: String) -> Vec<u8> {
match self.export_workspace(workspace_id) {
pub fn export_workspace(&mut self, workspace_id: String) -> Vec<u8> {
match self.export_workspace_inner(workspace_id) {
Ok(data) => data,
Err(e) => {
let error = format!("Failed to export workspace: {:?}", e);
Expand All @@ -127,7 +127,7 @@ impl JwstStorage {
}
}

fn export_workspace(&self, workspace_id: String) -> JwstStorageResult<Vec<u8>> {
fn export_workspace_inner(&self, workspace_id: String) -> JwstStorageResult<Vec<u8>> {
let rt = Arc::new(
Builder::new_multi_thread()
.worker_threads(1)
Expand Down Expand Up @@ -165,7 +165,6 @@ impl JwstStorage {

match workspace {
Ok(mut workspace) => {
warn!("is_offline: {}, remote: {}", is_offline, remote);
if is_offline {
let identifier = nanoid!();
let (last_synced_tx, last_synced_rx) = channel::<i64>(128);
Expand Down
4 changes: 2 additions & 2 deletions libs/jwst-binding/jwst-swift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ mod ffi {

fn get_sync_state(self: &Storage) -> String;

fn import(self: &mut Storage, workspace_id: String, data: Vec<u8>) -> bool;
fn import_workspace(self: &mut Storage, workspace_id: String, data: Vec<u8>) -> bool;

fn export(self: &mut Storage, workspace_id: String) -> Option<Vec<u8>>;
fn export_workspace(self: &mut Storage, workspace_id: String) -> Option<Vec<u8>>;

fn connect(self: &mut Storage, workspace_id: String, remote: String) -> Option<Workspace>;

Expand Down
17 changes: 10 additions & 7 deletions libs/jwst-binding/jwst-swift/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ impl Storage {
}
}

pub fn import(&mut self, workspace_id: String, data: Vec<u8>) -> bool {
match self.import_workspace(workspace_id, data) {
pub fn import_workspace(&mut self, workspace_id: String, data: Vec<u8>) -> bool {
match self.import_workspace_inner(workspace_id, data) {
Ok(_) => true,
Err(e) => {
let error = format!("Failed to init workspace: {:?}", e);
Expand All @@ -100,7 +100,7 @@ impl Storage {
}
}

fn import_workspace(&self, workspace_id: String, data: Vec<u8>) -> JwstStorageResult {
fn import_workspace_inner(&self, workspace_id: String, data: Vec<u8>) -> JwstStorageResult {
let rt = Arc::new(
Builder::new_multi_thread()
.worker_threads(1)
Expand All @@ -112,8 +112,8 @@ impl Storage {
rt.block_on(self.storage.init_workspace(workspace_id, data))
}

pub fn export(&mut self, workspace_id: String) -> Option<Vec<u8>> {
match self.export_workspace(workspace_id) {
pub fn export_workspace(&mut self, workspace_id: String) -> Option<Vec<u8>> {
match self.export_workspace_inner(workspace_id) {
Ok(data) => Some(data),
Err(e) => {
let error = format!("Failed to export workspace: {:?}", e);
Expand All @@ -124,7 +124,7 @@ impl Storage {
}
}

fn export_workspace(&self, workspace_id: String) -> JwstStorageResult<Vec<u8>> {
fn export_workspace_inner(&self, workspace_id: String) -> JwstStorageResult<Vec<u8>> {
let rt = Arc::new(
Builder::new_multi_thread()
.worker_threads(1)
Expand All @@ -133,7 +133,10 @@ impl Storage {
.build()
.map_err(JwstStorageError::SyncThread)?,
);
rt.block_on(self.storage.export_workspace(workspace_id))
let workspace = rt.block_on(async { self.get_workspace(&workspace_id).await })?;
let binary = workspace.to_binary()?;

Ok(binary)
}

pub fn connect(&mut self, workspace_id: String, remote: String) -> Option<Workspace> {
Expand Down

1 comment on commit 22f3e04

@vercel
Copy link

@vercel vercel bot commented on 22f3e04 Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.