Skip to content

Commit

Permalink
Saving vertex serde progress
Browse files Browse the repository at this point in the history
  • Loading branch information
criminosis committed Nov 5, 2024
1 parent 35d22ed commit 490e925
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 14 deletions.
53 changes: 52 additions & 1 deletion gremlin-client/src/io/graph_binary_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
message::{ReponseStatus, Response, ResponseResult},
process::traversal::Instruction,
structure::{Traverser, T},
GKey, GValue, GremlinError, GremlinResult, Vertex, GID,
GKey, GValue, GremlinError, GremlinResult, ToGValue, Vertex, VertexProperty, GID,
};

use super::IoProtocol;
Expand Down Expand Up @@ -284,6 +284,10 @@ impl GraphBinaryV1Ser for &GValue {
buf.push(VALUE_FLAG);
value.to_be_bytes(buf)?;
}
GValue::Vertex(value) => {
buf.push(VERTEX);
buf.push(VALUE_FLAG);
}
GValue::Bytecode(code) => {
//Type code of 0x15: Bytecode
buf.push(BYTECODE);
Expand Down Expand Up @@ -688,6 +692,53 @@ impl GraphBinaryV1Ser for &Uuid {
}
}

impl GraphBinaryV1Ser for &Vertex {
fn to_be_bytes(self, buf: &mut Vec<u8>) -> GremlinResult<()> {
//Format: {id}{label}{properties}

//{id} is a fully qualified typed value composed of {type_code}{type_info}{value_flag}{value}.
self.id().to_be_bytes(buf)?;

//{label} is a String value
self.label().to_be_bytes(buf)?;

//{properties} is a fully qualified typed value composed of {type_code}{type_info}{value_flag}{value} which contains properties.
self.properties.len();
todo!()
}
}

impl GraphBinaryV1Ser for &VertexProperty {
fn to_be_bytes(self, buf: &mut Vec<u8>) -> GremlinResult<()> {
//Format: {id}{label}{value}{parent}{properties}

//{id} is a fully qualified typed value composed of {type_code}{type_info}{value_flag}{value}.
self.id().to_be_bytes(buf)?;

//{label} is a String value.
self.label().to_be_bytes(buf)?;

//{value} is a fully qualified typed value composed of {type_code}{type_info}{value_flag}{value}.
//???????

//{parent} is a fully qualified typed value composed of {type_code}{type_info}{value_flag}{value} which contains the parent Vertex. Note that as TinkerPop currently send "references" only, this value will always be null.}

//{properties} is a fully qualified typed value composed of {type_code}{type_info}{value_flag}{value} which contains properties.

todo!()
}
}

impl GraphBinaryV1Ser for &GID {
fn to_be_bytes(self, buf: &mut Vec<u8>) -> GremlinResult<()> {
match self {
GID::String(s) => s.to_gvalue().to_be_bytes(buf),
GID::Int32(i) => i.to_gvalue().to_be_bytes(buf),
GID::Int64(i) => i.to_gvalue().to_be_bytes(buf),
}
}
}

impl GraphBinaryV1Deser for Uuid {
fn from_be_bytes<'a, S: Iterator<Item = &'a u8>>(bytes: &mut S) -> GremlinResult<Self> {
bytes
Expand Down
2 changes: 1 addition & 1 deletion gremlin-client/src/structure/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::hash::Hasher;
pub struct Vertex {
id: GID,
label: String,
properties: HashMap<String, Vec<VertexProperty>>,
pub(crate) properties: HashMap<String, Vec<VertexProperty>>,
}

impl Vertex {
Expand Down
48 changes: 36 additions & 12 deletions gremlin-client/tests/integration_traversal_omni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,15 +1168,21 @@ fn test_value_map(client: GremlinClient) {

let value = &results[0];

assert_eq!("test", get_map::<String, _>(value, "name").unwrap().unwrap());
assert_eq!(
"test",
get_map::<String, _>(value, "name").unwrap().unwrap()
);

let results = g.v(vertex.id()).value_map("name").to_list().unwrap();

assert_eq!(1, results.len());

let value = &results[0];

assert_eq!("test", get_map::<String, _>(value, "name").unwrap().unwrap());
assert_eq!(
"test",
get_map::<String, _>(value, "name").unwrap().unwrap()
);

let results = g.v(vertex.id()).value_map("fake").to_list().unwrap();

Expand All @@ -1186,9 +1192,15 @@ fn test_value_map(client: GremlinClient) {
assert_eq!(1, results.len());
let value = &results[0];

assert_eq!(Some(vertex.id().get().unwrap()), get_map_id(&value).unwrap());
assert_eq!(
Some(vertex.id().get().unwrap()),
get_map_id(&value).unwrap()
);
assert_eq!(Some(vertex.label()), get_map_label(&value).unwrap());
assert_eq!(Some("test".to_owned()).as_ref(), get_map(&value, "name").unwrap());
assert_eq!(
Some("test".to_owned()).as_ref(),
get_map(&value, "name").unwrap()
);
}

#[apply(serializers)]
Expand Down Expand Up @@ -1248,15 +1260,24 @@ fn test_element_map(client: GremlinClient) {
let value = &results[0];

assert_eq!(2, value.len());
assert_eq!(Some(vertex.id().get().unwrap()), get_map_id(&value).unwrap());
assert_eq!(
Some(vertex.id().get().unwrap()),
get_map_id(&value).unwrap()
);
assert_eq!(Some(vertex.label()), get_map_label(&value).unwrap());

let results = g.v(vertex.id()).element_map(()).to_list().unwrap();
let value = &results[0];

assert_eq!(Some(vertex.id().get().unwrap()), get_map_id(&value).unwrap());
assert_eq!(
Some(vertex.id().get().unwrap()),
get_map_id(&value).unwrap()
);
assert_eq!(Some(vertex.label()), get_map_label(&value).unwrap());
assert_eq!(Some("test".to_owned()).as_ref(), get_map(&value, "name").unwrap());
assert_eq!(
Some("test".to_owned()).as_ref(),
get_map(&value, "name").unwrap()
);
}

#[apply(serializers)]
Expand Down Expand Up @@ -2316,7 +2337,10 @@ fn test_anonymous_traversal_properties_drop(client: GremlinClient) {
//Make sure the property was assigned
assert_map_property(&element_map, pre_drop_prop_key, expected_prop_value);

let created_vertex_id = element_map.get("id").or(element_map.get(T::Id)).expect("Should have id property");
let created_vertex_id = element_map
.get("id")
.or(element_map.get(T::Id))
.expect("Should have id property");
let GValue::Int64(id) = created_vertex_id else {
panic!("Not expected id type");
};
Expand Down Expand Up @@ -2619,21 +2643,21 @@ fn test_none_step(client: GremlinClient) {
assert_eq!(1, vertex_count);
}

fn get_map_id<'a>(map: &'a Map) -> Result<Option<&i64>, GremlinError>{
fn get_map_id<'a>(map: &'a Map) -> Result<Option<&i64>, GremlinError> {
let string_keyed = get_map(map, "id")?;
if string_keyed.is_some() {
Ok(string_keyed)
} else {
get_map(map, T::Id)
get_map(map, T::Id)
}
}

fn get_map_label<'a>(map: &'a Map) -> Result<Option<&String>, GremlinError>{
fn get_map_label<'a>(map: &'a Map) -> Result<Option<&String>, GremlinError> {
let string_keyed = get_map(map, "label")?;
if string_keyed.is_some() {
Ok(string_keyed)
} else {
get_map(map, T::Label)
get_map(map, T::Label)
}
}

Expand Down

0 comments on commit 490e925

Please sign in to comment.