use std::path::Path;
use antfly_embedded_sys::{self as sys, antfly_buffer, antfly_db, antfly_error_code, antfly_slice};
use crate::error::{Error, Result};
use crate::ffi::{
HandleGate, borrow_slice, check, check_outcome, path_has_suffix, path_to_bytes,
path_to_cstring, take_buffer,
};
use crate::options::{GraphDirection, OpenOptions, WriteIntent};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutcomeReceiptStatus {
Applied,
NotApplied,
}
pub const SUPPORTED_ABI_VERSION: u32 = 3;
pub const THREADING_SERIALIZED: u32 = sys::ANTFLY_THREADING_SERIALIZED;
pub const MIN_THREAD_STACK_SIZE: usize = sys::ANTFLY_MIN_THREAD_STACK_SIZE;
pub fn threading_mode() -> u32 {
unsafe { sys::antfly_threading_mode() }
}
pub fn abi_version() -> u32 {
unsafe { sys::antfly_abi_version() }
}
pub fn open_options_size() -> u32 {
unsafe { sys::antfly_open_options_size() }
}
fn compiled_open_options_size() -> u32 {
std::mem::size_of::<sys::antfly_open_options>() as u32
}
pub fn validate_abi() -> Result<()> {
if abi_version() != SUPPORTED_ABI_VERSION {
return Err(Error::Internal);
}
if open_options_size() != compiled_open_options_size() {
return Err(Error::Internal);
}
Ok(())
}
pub struct Database {
gate: HandleGate<antfly_db>,
}
unsafe impl Send for Database {}
unsafe impl Sync for Database {}
pub struct Table<'db> {
database: Database,
_owner: &'db Database,
}
impl std::ops::Deref for Table<'_> {
type Target = Database;
fn deref(&self) -> &Database {
&self.database
}
}
impl Database {
fn from_handle(handle: *mut antfly_db) -> Database {
Database {
gate: HandleGate::new(handle),
}
}
pub fn open_table(&self, name: &str) -> Result<Table<'_>> {
self.with_handle(|handle| {
let mut table = std::ptr::null_mut();
check(unsafe {
sys::antfly_db_open_table(handle, borrow_slice(name.as_bytes()), &mut table)
})?;
Ok(Table {
database: Self::from_handle(table),
_owner: self,
})
})
}
pub(crate) fn with_handle<T>(&self, f: impl FnOnce(*mut antfly_db) -> Result<T>) -> Result<T> {
self.gate.with_handle(f)
}
pub(crate) fn read_buffer(
&self,
f: impl FnOnce(*mut antfly_db, *mut antfly_buffer) -> antfly_error_code,
) -> Result<Vec<u8>> {
self.with_handle(|handle| {
let mut out = antfly_buffer::default();
check(f(handle, &mut out))?;
Ok(unsafe { take_buffer(out) })
})
}
pub(crate) fn with_input(
&self,
input: &[u8],
f: impl FnOnce(*mut antfly_db, antfly_slice) -> antfly_error_code,
) -> Result<()> {
self.with_handle(|handle| check(f(handle, borrow_slice(input))))
}
pub(crate) fn with_input_output(
&self,
input: &[u8],
f: impl FnOnce(*mut antfly_db, antfly_slice, *mut antfly_buffer) -> antfly_error_code,
) -> Result<Vec<u8>> {
self.with_handle(|handle| {
let mut out = antfly_buffer::default();
check(f(handle, borrow_slice(input), &mut out))?;
Ok(unsafe { take_buffer(out) })
})
}
fn graph_lookup(
&self,
index_name: &str,
key: &str,
edge_type: &str,
direction: u8,
f: impl FnOnce(
*mut antfly_db,
antfly_slice,
antfly_slice,
antfly_slice,
u8,
*mut antfly_buffer,
) -> antfly_error_code,
) -> Result<Vec<u8>> {
self.with_handle(|handle| {
let mut out = antfly_buffer::default();
check(f(
handle,
borrow_slice(index_name.as_bytes()),
borrow_slice(key.as_bytes()),
borrow_slice(edge_type.as_bytes()),
direction,
&mut out,
))?;
Ok(unsafe { take_buffer(out) })
})
}
pub fn open(path: impl AsRef<Path>, options: &OpenOptions) -> Result<Database> {
Self::open_with(path.as_ref(), options, false)
}
pub fn create(path: impl AsRef<Path>, options: &OpenOptions) -> Result<Database> {
Self::open_with(path.as_ref(), options, true)
}
pub fn open_default(path: impl AsRef<Path>) -> Result<Database> {
Self::open(path, &OpenOptions::default())
}
pub fn create_default(path: impl AsRef<Path>) -> Result<Database> {
Self::create(path, &OpenOptions::default())
}
pub fn open_readonly(path: impl AsRef<Path>) -> Result<Database> {
Self::open(
path,
&OpenOptions {
mode: crate::options::OpenMode::Readonly,
..Default::default()
},
)
}
pub fn open_status_only(path: impl AsRef<Path>) -> Result<Database> {
Self::open(
path,
&OpenOptions {
mode: crate::options::OpenMode::StatusOnly,
..Default::default()
},
)
}
pub fn open_hosted(path: impl AsRef<Path>) -> Result<Database> {
validate_abi()?;
let c_path = path_to_cstring(path.as_ref())?;
let mut handle: *mut antfly_db = std::ptr::null_mut();
check(unsafe { sys::antfly_lite_open_hosted(c_path.as_ptr(), &mut handle) })?;
Ok(Database::from_handle(handle))
}
pub fn create_hosted(path: impl AsRef<Path>) -> Result<Database> {
validate_abi()?;
let c_path = path_to_cstring(path.as_ref())?;
let mut handle: *mut antfly_db = std::ptr::null_mut();
check(unsafe { sys::antfly_lite_create_hosted(c_path.as_ptr(), &mut handle) })?;
Ok(Database::from_handle(handle))
}
fn open_with(path: &Path, options: &OpenOptions, create: bool) -> Result<Database> {
validate_abi()?;
let c_path = path_to_cstring(path)?;
let mut c_opts: sys::antfly_open_options = unsafe { std::mem::zeroed() };
check(unsafe { sys::antfly_open_options_init(&mut c_opts) })?;
c_opts.storage_kind = options.storage.as_u32();
c_opts.open_mode = options.mode.as_u32();
c_opts.profile = options.profile.as_u32();
c_opts.map_size = options.map_size;
if options.no_sync {
c_opts.flags |= sys::ANTFLY_OPEN_FLAG_NO_SYNC;
}
if options.remote_provider_configured {
c_opts.flags |= sys::ANTFLY_OPEN_FLAG_REMOTE_PROVIDER_CONFIGURED;
}
if options.local_runtime_configured {
c_opts.flags |= sys::ANTFLY_OPEN_FLAG_LOCAL_RUNTIME_CONFIGURED;
}
if options.generated_enrichment_replay {
c_opts.flags |= sys::ANTFLY_OPEN_FLAG_GENERATED_ENRICHMENT_REPLAY;
}
c_opts.inference_host_budget_mb = options.host_budget_mb;
c_opts.inference_backend_budget_mb = options.backend_budget_mb;
c_opts.inference_combined_budget_mb = options.combined_budget_mb;
c_opts.inference_kv_budget_mb = options.kv_budget_mb;
c_opts.inference_scratch_budget_mb = options.scratch_budget_mb;
c_opts.inference_process_memory_budget_mb = options.process_memory_budget_mb;
c_opts.busy_timeout_ms = options.busy_timeout_ms();
let models_dir_bytes = options.models_dir.as_deref().map(path_to_bytes);
if let Some(bytes) = &models_dir_bytes {
c_opts.models_dir = borrow_slice(bytes);
}
if let Some(ttl) = &options.ttl_cleanup {
c_opts.flags |= sys::ANTFLY_OPEN_FLAG_TTL_CLEANUP;
c_opts.ttl_cleanup_enabled = ttl.enabled;
c_opts.ttl_cleanup_lease_owned = ttl.lease_owned;
c_opts.ttl_cleanup_lease_ttl_ms = ttl.lease_ttl_ms;
c_opts.ttl_cleanup_interval_ms = ttl.interval_ms;
c_opts.ttl_cleanup_batch_size = ttl.batch_size;
c_opts.ttl_cleanup_grace_period_ns = ttl.grace_period_ns;
c_opts.ttl_cleanup_owner_id = borrow_slice(ttl.owner_id.as_bytes());
}
let mut handle: *mut antfly_db = std::ptr::null_mut();
let code = if create {
unsafe { sys::antfly_db_create_with_options(c_path.as_ptr(), &c_opts, &mut handle) }
} else {
unsafe { sys::antfly_db_open_with_options(c_path.as_ptr(), &c_opts, &mut handle) }
};
check(code)?;
Ok(Database::from_handle(handle))
}
pub fn close(&self) -> Result<()> {
self.close_inner();
Ok(())
}
fn close_inner(&self) {
self.gate
.close(|handle| unsafe { sys::antfly_db_close(handle) });
}
pub fn status_json(&self) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe { sys::antfly_db_status_json(h, out) })
}
pub fn capabilities_json(&self) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe { sys::antfly_db_capabilities_json(h, out) })
}
pub fn replay_generated_enrichments_json(&self) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe {
sys::antfly_db_replay_generated_enrichments_json(h, out)
})
}
pub fn backup(&self) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe { sys::antfly_db_backup(h, out) })
}
pub fn backup_json(&self, options: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(options.as_ref(), |h, input, out| unsafe {
sys::antfly_db_backup_json(h, input, out)
})
}
pub fn import_backup(&self, backup: impl AsRef<[u8]>) -> Result<()> {
self.with_input(backup.as_ref(), |h, input| unsafe {
sys::antfly_db_import_backup(h, input)
})
}
pub fn check_json(&self) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe { sys::antfly_lite_check_json(h, out) })
}
pub fn vacuum_json(&self) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe { sys::antfly_lite_vacuum_json(h, out) })
}
pub fn compact_json(&self) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe { sys::antfly_lite_compact_json(h, out) })
}
pub fn copy_stable_snapshot_json(
&self,
dest_path: impl AsRef<Path>,
replace: bool,
) -> Result<Vec<u8>> {
let dest_path = dest_path.as_ref();
if !path_has_suffix(dest_path, ".aflite") {
return Err(Error::InvalidArgument);
}
let c_dest = path_to_cstring(dest_path)?;
self.with_handle(|handle| {
let mut out = antfly_buffer::default();
check(unsafe {
sys::antfly_lite_copy_stable_snapshot_json(
handle,
c_dest.as_ptr(),
replace,
&mut out,
)
})?;
Ok(unsafe { take_buffer(out) })
})
}
pub fn batch(&self, writes: &[WriteIntent], timestamp_ns: u64) -> Result<()> {
self.with_handle(|handle| {
let c_writes: Vec<sys::antfly_write_intent> = writes
.iter()
.map(|w| sys::antfly_write_intent {
key: borrow_slice(&w.key),
value: borrow_slice(&w.value),
is_delete: w.delete,
})
.collect();
let ptr = if c_writes.is_empty() {
std::ptr::null()
} else {
c_writes.as_ptr()
};
check_outcome(unsafe {
sys::antfly_db_batch(
handle,
ptr,
c_writes.len(),
std::ptr::null(),
0,
timestamp_ns,
0,
)
})
})
}
pub fn batch_json(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
let request = request.as_ref();
self.with_handle(|handle| {
let mut out = antfly_buffer::default();
let code =
unsafe { sys::antfly_db_batch_json(handle, borrow_slice(request), &mut out) };
if let Err(error) = check_outcome(code) {
unsafe { take_buffer(out) };
return Err(error);
}
Ok(unsafe { take_buffer(out) })
})
}
pub fn outcome_receipt_status(&self, receipt: &str) -> Result<OutcomeReceiptStatus> {
self.with_handle(|handle| {
let mut status: u8 = 0;
check(unsafe {
sys::antfly_db_outcome_receipt_status(
handle,
borrow_slice(receipt.as_bytes()),
&mut status,
)
})?;
match status {
sys::ANTFLY_OUTCOME_RECEIPT_APPLIED => Ok(OutcomeReceiptStatus::Applied),
sys::ANTFLY_OUTCOME_RECEIPT_NOT_APPLIED => Ok(OutcomeReceiptStatus::NotApplied),
_ => Err(Error::Internal),
}
})
}
pub fn forget_outcome_receipt(&self, receipt: &str) -> Result<()> {
self.with_input(receipt.as_bytes(), |h, input| unsafe {
sys::antfly_db_outcome_receipt_forget(h, input)
})
}
pub fn lookup_json(&self, key: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(key.as_ref(), |h, input, out| unsafe {
sys::antfly_db_lookup_json(h, input, out)
})
}
pub fn get_raw(&self, key: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(key.as_ref(), |h, input, out| unsafe {
sys::antfly_db_get_raw(h, input, out)
})
}
pub fn schema_json(&self) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe { sys::antfly_db_get_schema_json(h, out) })
}
pub fn set_schema_json(&self, schema: impl AsRef<[u8]>) -> Result<()> {
self.with_input(schema.as_ref(), |h, input| unsafe {
sys::antfly_db_set_schema_json(h, input)
})
}
pub fn run_until_idle(&self) -> Result<()> {
self.with_handle(|handle| check(unsafe { sys::antfly_db_run_until_idle(handle) }))
}
pub fn run_until_idle_json(&self) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe { sys::antfly_db_run_until_idle_json(h, out) })
}
pub fn pending_work_stats_json(&self) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe { sys::antfly_db_pending_work_stats_json(h, out) })
}
pub fn indexes_json(&self) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe { sys::antfly_db_list_indexes_json(h, out) })
}
pub fn render_resolver_key_json(
&self,
resolver: &str,
mention: impl AsRef<[u8]>,
) -> Result<Vec<u8>> {
self.with_input_output(mention.as_ref(), |h, input, out| unsafe {
sys::antfly_db_render_resolver_key_json(
h,
borrow_slice(resolver.as_bytes()),
input,
out,
)
})
}
pub fn memory_json(&self, space: &str, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_memory_json(h, borrow_slice(space.as_bytes()), input, out)
})
}
pub fn resolve_query_json(&self, resolver: &str, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_resolve_query_json(h, borrow_slice(resolver.as_bytes()), input, out)
})
}
pub fn merge_resolver_keys_json(
&self,
resolver: &str,
request: impl AsRef<[u8]>,
) -> Result<()> {
self.with_input(request.as_ref(), |h, input| unsafe {
sys::antfly_db_merge_resolver_keys_json(h, borrow_slice(resolver.as_bytes()), input)
})
}
pub fn unmerge_resolver_key_json(
&self,
resolver: &str,
request: impl AsRef<[u8]>,
) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_unmerge_resolver_key_json(
h,
borrow_slice(resolver.as_bytes()),
input,
out,
)
})
}
pub fn suppress_resolver_keys_json(
&self,
resolver: &str,
request: impl AsRef<[u8]>,
) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_suppress_resolver_keys_json(
h,
borrow_slice(resolver.as_bytes()),
input,
out,
)
})
}
pub fn unsuppress_resolver_key_json(
&self,
resolver: &str,
request: impl AsRef<[u8]>,
) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_unsuppress_resolver_key_json(
h,
borrow_slice(resolver.as_bytes()),
input,
out,
)
})
}
pub fn resolver_curation_json(&self, resolver: &str) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe {
sys::antfly_db_resolver_curation_json(h, borrow_slice(resolver.as_bytes()), out)
})
}
pub fn resolver_reviews_json(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_resolver_reviews_json(h, input, out)
})
}
pub fn propose_curation_json(&self, table: &str, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_propose_curation_json(h, borrow_slice(table.as_bytes()), input, out)
})
}
pub fn list_curation_proposals_json(
&self,
table: &str,
request: impl AsRef<[u8]>,
) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_list_curation_proposals_json(
h,
borrow_slice(table.as_bytes()),
input,
out,
)
})
}
pub fn get_curation_proposal_json(&self, table: &str, id: &str) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe {
sys::antfly_db_get_curation_proposal_json(
h,
borrow_slice(table.as_bytes()),
borrow_slice(id.as_bytes()),
out,
)
})
}
pub fn decide_curation_proposal_json(
&self,
table: &str,
id: &str,
request: impl AsRef<[u8]>,
) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_decide_curation_proposal_json(
h,
borrow_slice(table.as_bytes()),
borrow_slice(id.as_bytes()),
input,
out,
)
})
}
pub fn curate_entity_json(
&self,
table: &str,
key: &str,
request: impl AsRef<[u8]>,
) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_curate_entity_json(
h,
borrow_slice(table.as_bytes()),
borrow_slice(key.as_bytes()),
input,
out,
)
})
}
pub fn resolver_identities_json(&self) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe { sys::antfly_db_list_resolver_identities_json(h, out) })
}
pub fn drop_resolver_identity(&self, name: &str) -> Result<bool> {
self.with_handle(|handle| {
let mut deleted = false;
check(unsafe {
sys::antfly_db_drop_resolver_identity(
handle,
borrow_slice(name.as_bytes()),
&mut deleted,
)
})?;
Ok(deleted)
})
}
pub fn set_resolver_namespace_json(
&self,
resolver: &str,
request: impl AsRef<[u8]>,
) -> Result<()> {
self.with_input(request.as_ref(), |h, input| unsafe {
sys::antfly_db_set_resolver_namespace_json(h, borrow_slice(resolver.as_bytes()), input)
})
}
pub fn add_index_json(&self, config: impl AsRef<[u8]>) -> Result<()> {
self.with_input(config.as_ref(), |h, input| unsafe {
sys::antfly_db_add_index_json(h, input)
})
}
pub fn delete_index(&self, name: &str) -> Result<bool> {
self.with_handle(|handle| {
let mut deleted = false;
check(unsafe {
sys::antfly_db_delete_index(handle, borrow_slice(name.as_bytes()), &mut deleted)
})?;
Ok(deleted)
})
}
pub fn cancel_replacement(&self, name: &str) -> Result<bool> {
self.with_handle(|handle| {
let mut deleted = false;
check(unsafe {
sys::antfly_db_cancel_replacement(
handle,
borrow_slice(name.as_bytes()),
&mut deleted,
)
})?;
Ok(deleted)
})
}
pub fn enrichments_json(&self) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe { sys::antfly_db_list_enrichments_json(h, out) })
}
pub fn add_enrichment_json(&self, config: impl AsRef<[u8]>) -> Result<()> {
self.with_input(config.as_ref(), |h, input| unsafe {
sys::antfly_db_add_enrichment_json(h, input)
})
}
pub fn delete_enrichment(&self, kind: &str, name: &str) -> Result<bool> {
self.with_handle(|handle| {
let mut deleted = false;
check(unsafe {
sys::antfly_db_delete_enrichment(
handle,
borrow_slice(kind.as_bytes()),
borrow_slice(name.as_bytes()),
&mut deleted,
)
})?;
Ok(deleted)
})
}
pub fn scan_json(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_scan_json(h, input, out)
})
}
pub fn stats_json(&self) -> Result<Vec<u8>> {
self.read_buffer(|h, out| unsafe { sys::antfly_db_stats_json(h, out) })
}
pub fn search_json(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_search_json(h, input, out)
})
}
pub fn dense_search_wire(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_search_dense_wire(h, input, out)
})
}
pub fn text_match_wire(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_search_text_match_wire(h, input, out)
})
}
pub fn text_term_wire(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_search_text_term_wire(h, input, out)
})
}
pub fn text_match_phrase_wire(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_search_text_match_phrase_wire(h, input, out)
})
}
pub fn aggregate_hits_json(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_aggregate_hits_json(h, input, out)
})
}
pub fn lookup_artifact_json(&self, artifact_id_base64: &str) -> Result<Vec<u8>> {
self.with_input_output(artifact_id_base64.as_bytes(), |h, input, out| unsafe {
sys::antfly_db_lookup_artifact_json(h, input, out)
})
}
pub fn extract_enrichments_json(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_extract_enrichments_json(h, input, out)
})
}
pub fn compute_enrichments_json(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_compute_enrichments_json(h, input, out)
})
}
pub fn edges_json(
&self,
index_name: &str,
key: &str,
edge_type: &str,
direction: GraphDirection,
) -> Result<Vec<u8>> {
self.graph_lookup(
index_name,
key,
edge_type,
direction.as_u8(),
|h, i, k, e, d, out| unsafe { sys::antfly_db_get_edges_json(h, i, k, e, d, out) },
)
}
pub fn traverse_edges_json(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_traverse_edges_json(h, input, out)
})
}
pub fn execute_graph_queries_json(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_execute_graph_queries_json(h, input, out)
})
}
pub fn neighbors_json(
&self,
index_name: &str,
key: &str,
edge_type: &str,
direction: GraphDirection,
) -> Result<Vec<u8>> {
self.graph_lookup(
index_name,
key,
edge_type,
direction.as_u8(),
|h, i, k, e, d, out| unsafe { sys::antfly_db_get_neighbors_json(h, i, k, e, d, out) },
)
}
pub fn find_shortest_path_json(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_find_shortest_path_json(h, input, out)
})
}
pub fn find_k_shortest_paths_json(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_find_k_shortest_paths_json(h, input, out)
})
}
pub fn match_pattern_json(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>> {
self.with_input_output(request.as_ref(), |h, input, out| unsafe {
sys::antfly_db_match_pattern_json(h, input, out)
})
}
}
impl Drop for Database {
fn drop(&mut self) {
self.close_inner();
}
}
impl std::fmt::Debug for Database {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Database")
.field("open", &self.gate.is_open())
.finish()
}
}
pub fn decode_artifact_id_json(artifact_id_base64: &str) -> Result<Vec<u8>> {
let mut out = antfly_buffer::default();
check(unsafe {
sys::antfly_decode_artifact_id_json(borrow_slice(artifact_id_base64.as_bytes()), &mut out)
})?;
Ok(unsafe { take_buffer(out) })
}