@@ -107,6 +107,14 @@ async fn seg_cell_by_id(txn: &Transaction, id: Option<Id>) -> Result<Option<Owne
107107 None => Ok ( None ) ,
108108 }
109109}
110+
111+ async fn seg_list_by_id ( txn : & Transaction , id : Option < Id > ) -> Result < Option < OwnedCell > , TxnError > {
112+ match id {
113+ Some ( id) => txn. read_selected ( id, vec ! [ * LIST_KEY_ID ] ) . await ,
114+ None => Ok ( None ) ,
115+ }
116+ }
117+
110118pub struct IdList < ' a > {
111119 pub txn : & ' a Transaction ,
112120 container_id : Id ,
@@ -186,11 +194,9 @@ impl<'a> IdList<'a> {
186194
187195 // Find or create the specific list for this schema
188196 if ensure_container {
189- self . get_ensured_schema_list_id ( type_list_id)
190- . await
197+ self . get_ensured_schema_list_id ( type_list_id) . await
191198 } else {
192- self . get_schema_list_id ( type_list_id)
193- . await
199+ self . get_schema_list_id ( type_list_id) . await
194200 }
195201 }
196202
@@ -266,7 +272,11 @@ impl<'a> IdList<'a> {
266272 type_list_id : Id ,
267273 ) -> Result < Result < Id , IdListError > , TxnError > {
268274 // Read the type list cell
269- let type_list_cell = match self . txn . read_selected ( type_list_id, vec ! [ * ID_TYPES_MAP_ID ] ) . await ? {
275+ let type_list_cell = match self
276+ . txn
277+ . read_selected ( type_list_id, vec ! [ * ID_TYPES_MAP_ID ] )
278+ . await ?
279+ {
270280 Some ( cell) => cell,
271281 None => {
272282 error ! ( "Failed to get type list cell, id {:?}" , type_list_id) ;
@@ -289,37 +299,38 @@ impl<'a> IdList<'a> {
289299 Ok ( Ok ( Id :: unit_id ( ) ) )
290300 }
291301
292- /// Gets or creates a list for the specific schema within the type list
293- async fn get_ensured_schema_list_id (
294- & mut self ,
295- type_list_id : Id ,
296- ) -> Result < Result < Id , IdListError > , TxnError > {
297- // Read the type list cell
298- let type_list_cell = match self . txn . read ( type_list_id) . await ? {
299- Some ( cell) => cell,
300- None => {
301- error ! ( "Failed to get type list cell, id {:?}" , type_list_id) ;
302- return Ok ( Err ( IdListError :: FormatError ) ) ;
303- }
304- } ;
305-
306- // Find existing list for this schema
307- if let OwnedValue :: Array ( ref type_list) = type_list_cell[ * ID_TYPES_MAP_ID ] {
308- if let Some ( list_id) = self . find_schema_list_id ( type_list) {
309- return Ok ( Ok ( list_id) ) ;
310- }
311- } else {
312- error ! (
313- "Failed to get type list, index {:?} got {:?}" ,
314- * ID_TYPES_MAP_ID , type_list_cell
315- ) ;
302+ /// Gets or creates a list for the specific schema within the type list
303+ /// This function ensures that the type list exists, also slightly heavier than get_schema_list_id
304+ async fn get_ensured_schema_list_id (
305+ & mut self ,
306+ type_list_id : Id ,
307+ ) -> Result < Result < Id , IdListError > , TxnError > {
308+ // Read the type list cell
309+ let type_list_cell = match self . txn . read ( type_list_id) . await ? {
310+ Some ( cell) => cell,
311+ None => {
312+ error ! ( "Failed to get type list cell, id {:?}" , type_list_id) ;
316313 return Ok ( Err ( IdListError :: FormatError ) ) ;
317314 }
318-
319- // Create new list if needed
320- self . create_schema_list ( type_list_cell) . await
315+ } ;
316+
317+ // Find existing list for this schema
318+ if let OwnedValue :: Array ( ref type_list) = type_list_cell[ * ID_TYPES_MAP_ID ] {
319+ if let Some ( list_id) = self . find_schema_list_id ( type_list) {
320+ return Ok ( Ok ( list_id) ) ;
321+ }
322+ } else {
323+ error ! (
324+ "Failed to get type list, index {:?} got {:?}" ,
325+ * ID_TYPES_MAP_ID , type_list_cell
326+ ) ;
327+ return Ok ( Err ( IdListError :: FormatError ) ) ;
321328 }
322329
330+ // Create new list if needed
331+ self . create_schema_list ( type_list_cell) . await
332+ }
333+
323334 /// Finds an existing list ID for this schema in the type list
324335 fn find_schema_list_id ( & self , type_list : & Vec < OwnedValue > ) -> Option < Id > {
325336 type_list
@@ -678,7 +689,7 @@ impl<'a> IdListSegmentIterator<'a> {
678689 /// * `None` - If there are no more segments or an error occurred
679690 pub async fn next ( & mut self ) -> Option < OwnedCell > {
680691 let next_id = self . id_iter . next ( ) . await ;
681- if let Ok ( Some ( cell) ) = seg_cell_by_id ( self . id_iter . txn , next_id) . await {
692+ if let Ok ( Some ( cell) ) = seg_list_by_id ( self . id_iter . txn , next_id) . await {
682693 Some ( cell)
683694 } else {
684695 None
@@ -728,13 +739,11 @@ impl<'a> IdListIterator<'a> {
728739 /// - List value is not a PrimArray of Ids
729740 pub fn get_curr_seg_list ( & self ) -> Option < & Vec < Id > > {
730741 if let Some ( ref cell) = self . current_seg {
731- if let & OwnedValue :: Map ( ref map) = & cell. data {
732- let list_val = map. get_by_key_id ( * LIST_KEY_ID ) ;
733- if let & OwnedValue :: PrimArray ( OwnedPrimArray :: Id ( ref list) ) = list_val {
734- return Some ( & list) ;
735- } else {
736- error ! ( "Expecting primitive array got: {:?}" , list_val) ;
737- }
742+ let list_val = & cell[ 0u64 ] ;
743+ if let & OwnedValue :: PrimArray ( OwnedPrimArray :: Id ( ref list) ) = list_val {
744+ return Some ( & list) ;
745+ } else {
746+ error ! ( "Expecting primitive array got: {:?}" , list_val) ;
738747 }
739748 }
740749 None
@@ -791,15 +800,13 @@ impl<'a> IdListIterator<'a> {
791800 . unwrap_or ( None )
792801 . cloned ( ) ;
793802 if let Some ( last_seg) = self . segments . last ( ) . await {
794- if let & OwnedValue :: Map ( ref map) = & last_seg. data {
795- let list_val = map. get_by_key_id ( * LIST_KEY_ID ) ;
796- if let & OwnedValue :: PrimArray ( OwnedPrimArray :: Id ( ref list) ) = list_val {
797- if let Some ( id) = list. last ( ) {
798- return Some ( * id) ;
799- }
800- } else {
801- error ! ( "Expecting primitive array but got {:?}" , list_val) ;
803+ let list_val = & last_seg[ 0u64 ] ;
804+ if let & OwnedValue :: PrimArray ( OwnedPrimArray :: Id ( ref list) ) = list_val {
805+ if let Some ( id) = list. last ( ) {
806+ return Some ( * id) ;
802807 }
808+ } else {
809+ error ! ( "Expecting primitive array but got {:?}" , list_val) ;
803810 }
804811 }
805812 return last_value;
@@ -818,9 +825,9 @@ impl<'a> IdListIterator<'a> {
818825 {
819826 let mut count = self . get_curr_seg_list ( ) . map ( |l| l. len ( ) ) . unwrap_or ( 0 ) ;
820827 while let Some ( seg) = self . segments . next ( ) . await {
821- if let & OwnedValue :: Map ( ref map ) = & seg. data {
822- if let & OwnedValue :: PrimArray ( OwnedPrimArray :: Id ( ref list) ) =
823- map . get_by_key_id ( * LIST_KEY_ID )
828+ let list_val = & seg[ 0u64 ] ;
829+ if let & OwnedValue :: PrimArray ( OwnedPrimArray :: Id ( ref list) ) = list_val {
830+ count += list . len ( ) ;
824831 {
825832 count += list. len ( ) ;
826833 }
0 commit comments