@@ -5,6 +5,7 @@ use std::{fmt, str};
55use columnar:: MonotonicallyMappableToU128 ;
66use common:: json_path_writer:: { JSON_END_OF_PATH , JSON_PATH_SEGMENT_SEP_STR } ;
77use common:: JsonPathWriter ;
8+ use serde:: { Deserialize , Serialize } ;
89
910use super :: date_time_options:: DATE_TIME_PRECISION_INDEXED ;
1011use super :: { Field , Schema } ;
@@ -18,7 +19,7 @@ use crate::DateTime;
1819///
1920/// A term is composed of Field and the serialized value bytes.
2021/// The serialized value bytes themselves start with a one byte type tag followed by the payload.
21- #[ derive( Clone , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
22+ #[ derive( Clone , Eq , PartialEq , Ord , PartialOrd , Hash , Serialize , Deserialize ) ]
2223pub struct Term {
2324 field : Field ,
2425 serialized_value_bytes : Vec < u8 > ,
@@ -28,6 +29,34 @@ pub struct Term {
2829const TERM_TYPE_TAG_LEN : usize = 1 ;
2930
3031impl Term {
32+ /// Takes a serialized term and wraps it as a Term.
33+ /// First 4 bytes are the field id
34+ #[ deprecated(
35+ note = "we want to avoid working on the serialized representation directly, replace with \
36+ typed API calls (add more if needed)"
37+ ) ]
38+ pub fn wrap ( serialized : & [ u8 ] ) -> Term {
39+ let field_id_bytes: [ u8 ; 4 ] = serialized[ 0 ..4 ] . try_into ( ) . unwrap ( ) ;
40+ let field_id = u32:: from_be_bytes ( field_id_bytes) ;
41+ Term {
42+ field : Field :: from_field_id ( field_id) ,
43+ serialized_value_bytes : serialized[ 4 ..] . to_vec ( ) ,
44+ }
45+ }
46+
47+ /// Returns the serialized representation of the term.
48+ /// First 4 bytes are the field id
49+ #[ deprecated(
50+ note = "we want to avoid working on the serialized representation directly, replace with \
51+ typed API calls (add more if needed)"
52+ ) ]
53+ pub fn serialized_term ( & self ) -> Vec < u8 > {
54+ let mut serialized = Vec :: with_capacity ( 4 + self . serialized_value_bytes . len ( ) ) ;
55+ serialized. extend ( self . field . field_id ( ) . to_be_bytes ( ) . as_ref ( ) ) ;
56+ serialized. extend_from_slice ( & self . serialized_value_bytes ) ;
57+ serialized
58+ }
59+
3160 /// Create a new Term with a buffer with a given capacity.
3261 pub fn with_capacity ( capacity : usize ) -> Term {
3362 let mut data = Vec :: with_capacity ( TERM_TYPE_TAG_LEN + capacity) ;
0 commit comments