@@ -741,15 +741,18 @@ lazy_static! {
741741
742742/// An entity is represented as a map of attribute names to values.
743743#[ derive( Clone , CacheWeight , PartialEq , Eq , Serialize ) ]
744- pub struct Entity ( Object < Value > ) ;
744+ pub struct Entity {
745+ o : Object < Value > ,
746+ vid : Option < i64 > ,
747+ }
745748
746749impl < ' a > IntoIterator for & ' a Entity {
747750 type Item = ( Word , Value ) ;
748751
749752 type IntoIter = intern:: ObjectOwningIter < Value > ;
750753
751754 fn into_iter ( self ) -> Self :: IntoIter {
752- self . 0 . clone ( ) . into_iter ( )
755+ self . o . clone ( ) . into_iter ( )
753756 }
754757}
755758
@@ -853,7 +856,7 @@ impl Entity {
853856 obj. insert ( key, value)
854857 . map_err ( |e| EntityValidationError :: UnknownKey ( e. not_interned ( ) ) ) ?;
855858 }
856- let entity = Entity ( obj) ;
859+ let entity = Entity { o : obj, vid : None } ;
857860 entity. check_id ( ) ?;
858861 Ok ( entity)
859862 }
@@ -868,23 +871,23 @@ impl Entity {
868871 obj. insert ( key, value)
869872 . map_err ( |e| anyhow ! ( "unknown attribute {}" , e. not_interned( ) ) ) ?;
870873 }
871- let entity = Entity ( obj) ;
874+ let entity = Entity { o : obj, vid : None } ;
872875 entity. check_id ( ) ?;
873876 Ok ( entity)
874877 }
875878
876879 pub fn get ( & self , key : & str ) -> Option < & Value > {
877- self . 0 . get ( key)
880+ self . o . get ( key)
878881 }
879882
880883 pub fn contains_key ( & self , key : & str ) -> bool {
881- self . 0 . contains_key ( key)
884+ self . o . contains_key ( key)
882885 }
883886
884887 // This collects the entity into an ordered vector so that it can be iterated deterministically.
885888 pub fn sorted ( self ) -> Vec < ( Word , Value ) > {
886889 let mut v: Vec < _ > = self
887- . 0
890+ . o
888891 . into_iter ( )
889892 . filter ( |( k, _) | !k. eq ( VID_FIELD ) )
890893 . collect ( ) ;
@@ -893,15 +896,15 @@ impl Entity {
893896 }
894897
895898 pub fn sorted_ref ( & self ) -> Vec < ( & str , & Value ) > {
896- let mut v: Vec < _ > = self . 0 . iter ( ) . filter ( |( k, _) | !k. eq ( & VID_FIELD ) ) . collect ( ) ;
899+ let mut v: Vec < _ > = self . o . iter ( ) . filter ( |( k, _) | !k. eq ( & VID_FIELD ) ) . collect ( ) ;
897900 v. sort_by ( |( k1, _) , ( k2, _) | k1. cmp ( k2) ) ;
898901 v
899902 }
900903
901904 fn check_id ( & self ) -> Result < ( ) , EntityValidationError > {
902905 match self . get ( "id" ) {
903906 None => Err ( EntityValidationError :: MissingIDAttribute {
904- entity : format ! ( "{:?}" , self . 0 ) ,
907+ entity : format ! ( "{:?}" , self ) ,
905908 } ) ,
906909 Some ( Value :: String ( _) ) | Some ( Value :: Bytes ( _) ) | Some ( Value :: Int8 ( _) ) => Ok ( ( ) ) ,
907910 _ => Err ( EntityValidationError :: UnsupportedTypeForIDAttribute ) ,
@@ -918,31 +921,29 @@ impl Entity {
918921
919922 /// Return the VID of this entity and if its missing or of a type different than
920923 /// i64 it panics.
921- pub fn vid ( & self ) -> i64 {
922- self . get ( VID_FIELD )
923- . expect ( "the vid must be set" )
924- . as_int8 ( )
925- . expect ( "the vid must be set to a valid value" )
924+ pub fn vid ( & self ) -> Option < i64 > {
925+ self . vid
926926 }
927927
928928 /// Sets the VID of the entity. The previous one is returned.
929- pub fn set_vid ( & mut self , value : i64 ) -> Result < Option < Value > , InternError > {
930- self . 0 . insert ( VID_FIELD , value. into ( ) )
929+ pub fn set_vid ( & mut self , value : i64 ) -> Option < i64 > {
930+ let old_val = self . vid ;
931+ self . vid = Some ( value) ;
932+ old_val
931933 }
932934
933935 /// Clone entity and remove the VID.
934936 pub fn clone_no_vid ( & self ) -> Entity {
935937 let mut c = self . clone ( ) ;
936- c. 0 . remove ( VID_FIELD ) ;
938+ c. vid = None ;
937939 c
938940 }
939941
940942 /// Sets the VID if it's not already set. Should be used only for tests.
941943 #[ cfg( debug_assertions) ]
942944 pub fn set_vid_if_empty ( & mut self ) {
943- let vid = self . get ( VID_FIELD ) ;
944- if vid. is_none ( ) {
945- let _ = self . set_vid ( 100 ) . expect ( "the vid should be set" ) ;
945+ if self . vid . is_none ( ) {
946+ self . vid = Some ( 100 )
946947 }
947948 }
948949
@@ -952,7 +953,7 @@ impl Entity {
952953 /// If a key only exists on one entity, the value from that entity is chosen.
953954 /// If a key is set to `Value::Null` in `update`, the key/value pair is set to `Value::Null`.
954955 pub fn merge ( & mut self , update : Entity ) {
955- self . 0 . merge ( update. 0 ) ;
956+ self . o . merge ( update. o ) ;
956957 }
957958
958959 /// Merges an entity update `update` into this entity, removing `Value::Null` values.
@@ -961,18 +962,18 @@ impl Entity {
961962 /// If a key only exists on one entity, the value from that entity is chosen.
962963 /// If a key is set to `Value::Null` in `update`, the key/value pair is removed.
963964 pub fn merge_remove_null_fields ( & mut self , update : Entity ) -> Result < ( ) , InternError > {
964- for ( key, value) in update. 0 . into_iter ( ) {
965+ for ( key, value) in update. o . into_iter ( ) {
965966 match value {
966- Value :: Null => self . 0 . remove ( & key) ,
967- _ => self . 0 . insert ( & key, value) ?,
967+ Value :: Null => self . o . remove ( & key) ,
968+ _ => self . o . insert ( & key, value) ?,
968969 } ;
969970 }
970971 Ok ( ( ) )
971972 }
972973
973974 /// Remove all entries with value `Value::Null` from `self`
974975 pub fn remove_null_fields ( & mut self ) {
975- self . 0 . retain ( |_, value| !value. is_null ( ) )
976+ self . o . retain ( |_, value| !value. is_null ( ) )
976977 }
977978
978979 /// Add the key/value pairs from `iter` to this entity. This is the same
@@ -984,7 +985,7 @@ impl Entity {
984985 iter : impl IntoIterator < Item = ( impl AsRef < str > , Value ) > ,
985986 ) -> Result < ( ) , InternError > {
986987 for ( key, value) in iter {
987- self . 0 . insert ( key, value) ?;
988+ self . o . insert ( key, value) ?;
988989 }
989990 Ok ( ( ) )
990991 }
@@ -1074,19 +1075,19 @@ impl Entity {
10741075#[ cfg( debug_assertions) ]
10751076impl Entity {
10761077 pub fn insert ( & mut self , key : & str , value : Value ) -> Result < Option < Value > , InternError > {
1077- self . 0 . insert ( key, value)
1078+ self . o . insert ( key, value)
10781079 }
10791080
10801081 pub fn remove ( & mut self , key : & str ) -> Option < Value > {
1081- self . 0 . remove ( key)
1082+ self . o . remove ( key)
10821083 }
10831084
10841085 pub fn set (
10851086 & mut self ,
10861087 name : & str ,
10871088 value : impl Into < Value > ,
10881089 ) -> Result < Option < Value > , InternError > {
1089- self . 0 . insert ( name, value. into ( ) )
1090+ self . o . insert ( name, value. into ( ) )
10901091 }
10911092}
10921093
@@ -1098,16 +1099,17 @@ impl<'a> From<&'a Entity> for Cow<'a, Entity> {
10981099
10991100impl GasSizeOf for Entity {
11001101 fn gas_size_of ( & self ) -> Gas {
1101- self . 0 . gas_size_of ( )
1102+ self . o . gas_size_of ( )
11021103 }
11031104}
11041105
11051106impl std:: fmt:: Debug for Entity {
11061107 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
11071108 let mut ds = f. debug_struct ( "Entity" ) ;
1108- for ( k, v) in & self . 0 {
1109+ for ( k, v) in & self . o {
11091110 ds. field ( k, v) ;
11101111 }
1112+ ds. field ( "VID" , & self . vid ) ;
11111113 ds. finish ( )
11121114 }
11131115}
0 commit comments