Skip to content

Commit

Permalink
feature: API to get C4DocumentInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy authored and Dushistov committed Jan 17, 2024
1 parent 60fd9ba commit dcffa2e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
35 changes: 32 additions & 3 deletions couchbase-lite/src/doc_enumerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use crate::{
document::{C4DocumentOwner, Document},
error::{c4error_init, Error, Result},
ffi::{
c4db_enumerateAllDocs, c4enum_free, c4enum_getDocument, c4enum_next, C4DocEnumerator,
C4EnumeratorFlags, C4EnumeratorOptions,
c4db_enumerateAllDocs, c4enum_free, c4enum_getDocument, c4enum_getDocumentInfo,
c4enum_next, C4DocEnumerator, C4DocumentInfo, C4EnumeratorFlags, C4EnumeratorOptions,
},
Database,
};
use bitflags::bitflags;
use fallible_streaming_iterator::FallibleStreamingIterator;
use std::ptr::NonNull;
use std::{marker::PhantomData, mem::MaybeUninit, ptr::NonNull, str};

pub struct DocEnumerator<'a> {
_db: &'a Database,
Expand All @@ -24,6 +24,24 @@ impl Drop for DocEnumerator<'_> {
}
}

pub struct DocumentInfo<'a, 'b> {
inner: C4DocumentInfo,
phantom: PhantomData<&'a DocEnumerator<'b>>,
}

impl<'a, 'b> DocumentInfo<'_, '_> {
pub(crate) fn new(inner: C4DocumentInfo) -> Self {
Self {
inner,
phantom: PhantomData,
}
}
#[inline]
pub fn doc_id(&self) -> &str {
unsafe { str::from_utf8_unchecked(self.inner.docID.into()) }
}
}

impl<'a> DocEnumerator<'a> {
pub(crate) fn enumerate_all_docs(
db: &'a Database,
Expand All @@ -43,6 +61,17 @@ impl<'a> DocEnumerator<'a> {
.ok_or_else(|| c4err.into())
}

#[inline]
pub fn get_doc_info(&self) -> Result<Option<DocumentInfo>> {
let mut di = MaybeUninit::<C4DocumentInfo>::uninit();
if !unsafe { c4enum_getDocumentInfo(self.inner.as_ptr(), di.as_mut_ptr()) } {
return Ok(None);
}
let di = unsafe { di.assume_init() };

Ok(Some(DocumentInfo::new(di)))
}

pub fn get_doc(&self) -> Result<Document> {
let mut c4err = c4error_init();
let doc_ptr = unsafe { c4enum_getDocument(self.inner.as_ptr(), &mut c4err) };
Expand Down
2 changes: 1 addition & 1 deletion couchbase-lite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mod value;
pub use crate::{
conflict_resolver::resolve_conflict,
database::{Database, DatabaseConfig, DatabaseFlags},
doc_enumerator::DocEnumeratorFlags,
doc_enumerator::{DocEnumeratorFlags, DocumentInfo},
document::{Document, DocumentFlags},
error::Error,
fallible_streaming_iterator::FallibleStreamingIterator,
Expand Down
2 changes: 2 additions & 0 deletions couchbase-lite/tests/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ fn test_write_read() {
ids_and_data.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
let mut ids_and_data_iter = ids_and_data.iter();
while let Some(item) = iter.next().unwrap() {
let doc_info = item.get_doc_info().unwrap().unwrap();
let doc = item.get_doc().unwrap();
let (doc_id, foo) = ids_and_data_iter.next().unwrap();
assert_eq!(doc_id, doc.id());
assert_eq!(doc_id, doc_info.doc_id());
let loaded_foo: Foo = doc.decode_body().unwrap();
assert_eq!(
Foo {
Expand Down

0 comments on commit dcffa2e

Please sign in to comment.