use std::marker::PhantomData;
use crate::{field::FieldData, raw::RawVertexCursor, Result};
use super::{
iter::{IntoVertexFields, IntoVertexIds, IntoVertexIter, IntoVertexLabelIds, IntoVertexLabels},
InEdgeCur, InEdgeCurMut, OutEdgeCur, OutEdgeCurMut,
};
trait AsRawVertexCursor {
fn as_raw(&self) -> &RawVertexCursor;
}
pub trait VertexCursor {
fn id(&self) -> Result<i64>;
fn label(&self) -> Result<String>;
fn lid(&self) -> Result<u16>;
fn is_valid(&self) -> bool;
fn seek_to_next(&mut self) -> Result<Option<&mut Self>>;
fn seek(&mut self, vid: i64, nearest: bool) -> Result<&mut Self>;
fn into_vertices(self) -> IntoVertexIter<Self>
where
Self: Sized;
fn into_vertex_ids(self) -> IntoVertexIds<Self>
where
Self: Sized;
fn into_vertex_labels(self) -> IntoVertexLabels<Self>
where
Self: Sized;
fn into_vertex_lids(self) -> IntoVertexLabelIds<Self>
where
Self: Sized;
fn into_vertex_fields(self) -> IntoVertexFields<Self>
where
Self: Sized;
fn into_vertices_from(self, vid: i64, nearest: bool) -> Result<IntoVertexIter<Self>>
where
Self: Sized;
fn out_edge_cursor(&mut self) -> Result<OutEdgeCur<'_>>;
fn in_edge_cursor(&mut self) -> Result<InEdgeCur<'_>>;
fn field(&self, name: &str) -> Result<FieldData>;
fn fields(&self, names: &[&str]) -> Result<Vec<FieldData>>;
fn field_by_id(&self, id: usize) -> Result<FieldData>;
fn fields_by_ids(&self, ids: &[usize]) -> Result<Vec<FieldData>>;
fn all_fields(&self) -> Result<Vec<(String, FieldData)>>;
fn associated_edges_src_vids(&self, limit: usize) -> Result<(bool, Vec<i64>)>;
fn associated_edges_dst_vids(&self, limit: usize) -> Result<(bool, Vec<i64>)>;
fn num_in_edges(&self, limit: usize) -> Result<(bool, usize)>;
fn num_out_edges(&self, limit: usize) -> Result<(bool, usize)>;
}
impl<T> VertexCursor for T
where
T: AsRawVertexCursor,
{
fn id(&self) -> Result<i64> {
self.as_raw().get_id()
}
fn label(&self) -> Result<String> {
self.as_raw().get_label()
}
fn lid(&self) -> Result<u16> {
self.as_raw().get_label_id()
}
fn is_valid(&self) -> bool {
self.as_raw().is_valid()
}
fn seek_to_next(&mut self) -> Result<Option<&mut Self>> {
if self.as_raw().next()? {
Ok(Some(self))
} else {
Ok(None)
}
}
fn seek(&mut self, vid: i64, nearest: bool) -> Result<&mut Self> {
let ret = self.as_raw().goto(vid, nearest)?;
debug_assert!(ret);
Ok(self)
}
fn into_vertices(self) -> IntoVertexIter<Self>
where
Self: Sized,
{
IntoVertexIter::new(self)
}
fn into_vertex_ids(self) -> IntoVertexIds<Self>
where
Self: Sized,
{
IntoVertexIds::new(self)
}
fn into_vertex_labels(self) -> IntoVertexLabels<Self>
where
Self: Sized,
{
IntoVertexLabels::new(self)
}
fn into_vertex_lids(self) -> IntoVertexLabelIds<Self>
where
Self: Sized,
{
IntoVertexLabelIds::new(self)
}
fn into_vertex_fields(self) -> IntoVertexFields<Self>
where
Self: Sized,
{
IntoVertexFields::new(self)
}
fn into_vertices_from(mut self, vid: i64, nearest: bool) -> Result<IntoVertexIter<Self>>
where
Self: Sized,
{
self.seek(vid, nearest)?;
Ok(IntoVertexIter::new(self))
}
fn out_edge_cursor(&mut self) -> Result<OutEdgeCur<'_>> {
self.as_raw().get_out_edge_cursor().map(OutEdgeCur::new)
}
fn in_edge_cursor(&mut self) -> Result<InEdgeCur<'_>> {
self.as_raw().get_in_edge_cursor().map(InEdgeCur::new)
}
fn field(&self, name: &str) -> Result<FieldData> {
self.as_raw()
.get_field_by_name(name)
.map(|fd| FieldData::from_raw_field_data(&fd))
}
fn fields(&self, names: &[&str]) -> Result<Vec<FieldData>> {
self.as_raw()
.get_fields_by_names(names)
.map(|v| v.iter().map(FieldData::from_raw_field_data).collect())
}
fn field_by_id(&self, id: usize) -> Result<FieldData> {
self.as_raw()
.get_field_by_id(id)
.map(|fd| FieldData::from_raw_field_data(&fd))
}
fn fields_by_ids(&self, ids: &[usize]) -> Result<Vec<FieldData>> {
self.as_raw()
.get_fields_by_ids(ids)
.map(|v| v.iter().map(FieldData::from_raw_field_data).collect())
}
fn all_fields(&self) -> Result<Vec<(String, FieldData)>> {
self.as_raw().get_all_fields().map(|(names, datas)| {
names
.into_iter()
.zip(datas.into_iter())
.map(|(name, data)| (name, FieldData::from_raw_field_data(&data)))
.collect()
})
}
fn associated_edges_src_vids(&self, limit: usize) -> Result<(bool, Vec<i64>)> {
self.as_raw().list_src_vids(limit)
}
fn associated_edges_dst_vids(&self, limit: usize) -> Result<(bool, Vec<i64>)> {
self.as_raw().list_dst_vids(limit)
}
fn num_in_edges(&self, limit: usize) -> Result<(bool, usize)> {
self.as_raw().get_num_in_edges(limit)
}
fn num_out_edges(&self, limit: usize) -> Result<(bool, usize)> {
self.as_raw().get_num_out_edges(limit)
}
}
pub struct VertexCur<'txn> {
inner: RawVertexCursor,
_marker: PhantomData<&'txn ()>,
}
impl<'txn> VertexCur<'txn> {
pub(crate) fn new(raw_cursor: RawVertexCursor) -> VertexCur<'txn> {
VertexCur {
inner: raw_cursor,
_marker: PhantomData,
}
}
}
impl<'txn> AsRawVertexCursor for VertexCur<'txn> {
fn as_raw(&self) -> &RawVertexCursor {
&self.inner
}
}
pub trait VertexCursorMut {
fn out_edge_cursor_mut(&mut self) -> Result<OutEdgeCurMut<'_>>;
fn int_edge_cursor_mut(&mut self) -> Result<InEdgeCurMut<'_>>;
fn set_field(&self, name: &str, value: &FieldData) -> Result<()>;
fn set_field_by_id(&self, id: usize, value: &FieldData) -> Result<()>;
fn set_fields(&self, names: &[&str], values: &[FieldData]) -> Result<()>;
fn set_fields_by_ids(&self, ids: &[usize], values: &[FieldData]) -> Result<()>;
fn delete(&self) -> Result<(usize, usize)>;
}
impl<'txn> VertexCursorMut for VertexCurMut<'txn> {
fn out_edge_cursor_mut(&mut self) -> Result<OutEdgeCurMut<'_>> {
self.as_raw().get_out_edge_cursor().map(OutEdgeCurMut::new)
}
fn int_edge_cursor_mut(&mut self) -> Result<InEdgeCurMut<'_>> {
self.as_raw().get_in_edge_cursor().map(InEdgeCurMut::new)
}
fn set_field(&self, name: &str, value: &FieldData) -> Result<()> {
self.as_raw()
.set_field_by_name(name, &value.as_raw_field_data())
}
fn set_field_by_id(&self, id: usize, value: &FieldData) -> Result<()> {
self.as_raw()
.set_field_by_id(id, &value.as_raw_field_data())
}
fn set_fields(&self, names: &[&str], values: &[FieldData]) -> Result<()> {
let values: Vec<_> = values.iter().map(|fd| fd.as_raw_field_data()).collect();
self.as_raw()
.set_fields_by_data(names.iter().copied(), &values)
}
fn set_fields_by_ids(&self, ids: &[usize], values: &[FieldData]) -> Result<()> {
let values: Vec<_> = values.iter().map(|fd| fd.as_raw_field_data()).collect();
self.as_raw().set_fields_by_ids(ids, &values)
}
fn delete(&self) -> Result<(usize, usize)> {
self.as_raw().delete()
}
}
pub struct VertexCurMut<'txn> {
inner: RawVertexCursor,
_marker: PhantomData<&'txn mut ()>,
}
impl<'txn> VertexCurMut<'txn> {
pub(crate) fn new(raw_cursor: RawVertexCursor) -> VertexCurMut<'txn> {
VertexCurMut {
inner: raw_cursor,
_marker: PhantomData,
}
}
}
impl<'txn> AsRawVertexCursor for VertexCurMut<'txn> {
fn as_raw(&self) -> &RawVertexCursor {
&self.inner
}
}