Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,23 @@ thiserror = "1.0.50"
tokio = { version = "1.34.0", features = ["macros", "rt", "sync"] }
tokio-stream = "0.1.14"
tracing = "0.1.40"
uniffi = { version = "0.26.1", features = ["cli"], optional = true }
tracing-subscriber = {version = "0.3.18", features = ["env-filter"] }
uniffi = { version = "0.28.1", features = ["cli", "tokio"], optional = true }

[dependencies.hypercore]
version = "0.13.0"
default-features = false
features = ["sparse", "tokio"]
#version = "0.13.0"
path = "../core"

[build-dependencies]
prost-build = "0.12.1"
uniffi = { version = "0.26.1", features = ["build"], optional = true}
uniffi = { version = "0.28.1", features = ["build"], optional = true}

[dev-dependencies]
tracing-subscriber = "0.3.18"
tokio = { version = "1.34.0", features = ["rt-multi-thread"] }
async-recursion = "1.0.5"
random-access-memory = "3.0.0"
once_cell = "1.19.0"
tempfile = "3.10.0"
serde_json = "1.0.114"
14 changes: 14 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,25 @@ async fn hyperbee_from_storage_dir(path_to_storage_dir: &str) -> Result<Hyperbee
})
}

/// log current rt
#[uniffi::export]
fn try_current_rt() {
dbg!(tokio::runtime::Handle::try_current());
}

#[derive(Debug, uniffi::Object)]
struct Prefixed {
rust_prefixed: Shared<RustPrefixed>,
}

/// initialize logging
#[uniffi::export]
pub fn init_env_logs() {
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) // Reads `RUST_LOG` environment variable
.init();
}

#[uniffi::export]
impl Prefixed {
/// Get the value associated with the provided key plus this [`Prefixed`]'s instance's `prefix`
Expand Down
2 changes: 2 additions & 0 deletions src/hb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,13 @@ impl Hyperbee {
pub async fn from_storage_dir<T: AsRef<Path>>(
path_to_storage_dir: T,
) -> Result<Hyperbee, HyperbeeError> {
dbg!(tokio::runtime::Handle::try_current());
Self::from_tree(Tree::from_storage_dir(path_to_storage_dir).await?)
}

/// Create a [`Hyperbee`] in RAM
pub async fn from_ram() -> Result<Hyperbee, HyperbeeError> {
dbg!(tokio::runtime::Handle::try_current());
Self::from_tree(Tree::from_ram().await?)
}

Expand Down
11 changes: 11 additions & 0 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,29 @@ impl Tree {
pub async fn from_storage_dir<T: AsRef<Path>>(
path_to_storage_dir: T,
) -> Result<Tree, HyperbeeError> {
dbg!(tokio::runtime::Handle::try_current());
dbg!();
dbg!();
dbg!();
let p: PathBuf = path_to_storage_dir.as_ref().to_owned();
dbg!();
let storage = Storage::new_disk(&p, false).await?;
dbg!();
dbg!(tokio::runtime::Handle::current());
let hc = Arc::new(Mutex::new(HypercoreBuilder::new(storage).build().await?));
dbg!();
let blocks = BlocksBuilder::default().core(hc).build()?;
dbg!();
Self::from_blocks(blocks)
}
pub async fn from_ram() -> Result<Tree, HyperbeeError> {
dbg!(tokio::runtime::Handle::try_current());
let hc = Arc::new(Mutex::new(
HypercoreBuilder::new(Storage::new_memory().await?)
.build()
.await?,
));
dbg!(tokio::runtime::Handle::try_current());
let blocks = BlocksBuilder::default().core(hc).build()?;
Self::from_blocks(blocks)
}
Expand Down
6 changes: 5 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub fn run_code(
);
let script_path = working_dir.path().join(script_file_name);
let script_file = File::create(&script_path)?;
println!("{code}");
write!(&script_file, "{}", &code)?;

let working_dir_path = working_dir.path().display().to_string();
Expand All @@ -107,7 +108,10 @@ pub fn run_code(
}
let script_path_str = script_path.display().to_string();
let cmd = build_command(&working_dir_path, &script_path_str);
check_cmd_output(Command::new("sh").arg("-c").arg(cmd).output()?)
let o = Command::new("sh").arg("-c").arg(cmd).output()?;
println!("OUT\n{}", String::from_utf8_lossy(&o.stdout));
println!("ERR\n{}", String::from_utf8_lossy(&o.stderr));
check_cmd_output(o)
}

pub fn run_make_from_with(dir: &str, arg: &str) -> Result<Output> {
Expand Down
29 changes: 29 additions & 0 deletions tests/common/python/foo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import asyncio
from target.hyperbee import *
from target.hyperbee import uniffi_set_event_loop


async def main():
import json
init_env_logs()
try_current_rt()
print(asyncio.get_running_loop())
uniffi_set_event_loop(asyncio.get_running_loop())
#hb = await hyperbee_from_storage_dir('./hbdir')
hb = await hyperbee_from_ram();
try_current_rt()
res = await hb.get(b'hello')
assert(res is None)

try_current_rt()
hb = await hyperbee_from_storage_dir('./hbdir')
hb = await hyperbee_from_ram();
res = await hb.get(b'hello')

res = await hb.put(b'skipval', None)
res = await hb.get(b'skipval')
assert(res.value is None)


if __name__ == '__main__':
asyncio.run(main())
2 changes: 2 additions & 0 deletions tests/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ async fn optionals() -> Result<()> {
"
async def main():
import json
init_env_logs()
try_current_rt()
hb = await hyperbee_from_storage_dir('{}')
res = await hb.get(b'hello')
assert(res.value is None)
Expand Down