use std::any::Any;
use arrow_array::Float32Array;
use byteorder::{ByteOrder, LE};
use super::{Vertex, VertexSerDe};
use crate::Result;
#[derive(Clone, Debug)]
pub struct RowVertex {
pub row_id: u64,
pub vector: Option<Float32Array>,
}
impl RowVertex {
pub fn new(row_id: u64, vector: Option<Float32Array>) -> Self {
Self { row_id, vector }
}
}
impl Vertex for RowVertex {
fn vector(&self) -> &[f32] {
self.vector.as_ref().unwrap().values()
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
pub struct RowVertexSerDe {}
impl RowVertexSerDe {
pub fn new() -> Self {
Self {}
}
}
impl VertexSerDe<RowVertex> for RowVertexSerDe {
fn size(&self) -> usize {
8
}
fn serialize(&self, vertex: &RowVertex) -> Vec<u8> {
let mut buf = vec![0u8; 8];
buf.copy_from_slice(&vertex.row_id.to_le_bytes());
buf
}
fn deserialize(&self, data: &[u8]) -> Result<RowVertex> {
let row_id = LE::read_u64(data);
Ok(RowVertex {
row_id,
vector: None,
})
}
}