6262 * permissions under this License.
6363 */
6464
65+ use std:: mem:: size_of;
6566use std:: ops:: Range ;
6667
6768use crate :: engine_prelude:: * ;
@@ -316,6 +317,22 @@ impl DbCodec<NodeId> for NodeIdDbCodec {
316317#[ derive( Default ) ]
317318pub struct TypeAndCreationIndexKeyDbCodec { }
318319
320+ impl TypeAndCreationIndexKeyDbCodec {
321+ /// An extracted "how are parts encoded together" knowledge, to be shared with the
322+ /// [`BoundedDbCodec`] implementation.
323+ fn encode_parts (
324+ entity_byte : u8 ,
325+ state_version_bytes : & [ u8 ; StateVersion :: BYTE_LEN ] ,
326+ index_within_txn_bytes : & [ u8 ; size_of :: < u32 > ( ) ] ,
327+ ) -> Vec < u8 > {
328+ let mut bytes = Vec :: new ( ) ;
329+ bytes. push ( entity_byte) ;
330+ bytes. extend_from_slice ( state_version_bytes) ;
331+ bytes. extend_from_slice ( index_within_txn_bytes) ;
332+ bytes
333+ }
334+ }
335+
319336impl DbCodec < ( EntityType , CreationId ) > for TypeAndCreationIndexKeyDbCodec {
320337 fn encode ( & self , value : & ( EntityType , CreationId ) ) -> Vec < u8 > {
321338 let (
@@ -325,11 +342,11 @@ impl DbCodec<(EntityType, CreationId)> for TypeAndCreationIndexKeyDbCodec {
325342 index_within_txn,
326343 } ,
327344 ) = value;
328- let mut bytes = Vec :: new ( ) ;
329- bytes . push ( * entity_type as u8 ) ;
330- bytes . extend_from_slice ( & state_version. to_be_bytes ( ) ) ;
331- bytes . extend_from_slice ( & index_within_txn. to_be_bytes ( ) ) ;
332- bytes
345+ Self :: encode_parts (
346+ * entity_type as u8 ,
347+ & state_version. to_be_bytes ( ) ,
348+ & index_within_txn. to_be_bytes ( ) ,
349+ )
333350 }
334351
335352 fn decode ( & self , bytes : & [ u8 ] ) -> ( EntityType , CreationId ) {
@@ -350,6 +367,16 @@ impl DbCodec<(EntityType, CreationId)> for TypeAndCreationIndexKeyDbCodec {
350367 }
351368}
352369
370+ impl BoundedDbCodec for TypeAndCreationIndexKeyDbCodec {
371+ fn upper_bound_encoding ( & self ) -> Vec < u8 > {
372+ Self :: encode_parts (
373+ u8:: MAX ,
374+ & [ u8:: MAX ; StateVersion :: BYTE_LEN ] ,
375+ & [ u8:: MAX ; size_of :: < u32 > ( ) ] ,
376+ )
377+ }
378+ }
379+
353380impl GroupPreservingDbCodec for TypeAndCreationIndexKeyDbCodec {
354381 type Group = EntityType ;
355382
@@ -372,6 +399,24 @@ impl IntraGroupOrderPreservingDbCodec<(EntityType, CreationId)> for TypeAndCreat
372399#[ derive( Default ) ]
373400pub struct BlueprintAndCreationIndexKeyDbCodec { }
374401
402+ impl BlueprintAndCreationIndexKeyDbCodec {
403+ /// An extracted "how are parts encoded together" knowledge, to be shared with the
404+ /// [`BoundedDbCodec`] implementation.
405+ fn encode_parts (
406+ package_address_bytes : & [ u8 ; NodeId :: LENGTH ] ,
407+ blueprint_name_hash_bytes : & [ u8 ; Hash :: LENGTH ] ,
408+ state_version_bytes : & [ u8 ; StateVersion :: BYTE_LEN ] ,
409+ index_within_txn_bytes : & [ u8 ; size_of :: < u32 > ( ) ] ,
410+ ) -> Vec < u8 > {
411+ let mut bytes = Vec :: new ( ) ;
412+ bytes. extend_from_slice ( package_address_bytes) ;
413+ bytes. extend_from_slice ( blueprint_name_hash_bytes) ;
414+ bytes. extend_from_slice ( state_version_bytes) ;
415+ bytes. extend_from_slice ( index_within_txn_bytes) ;
416+ bytes
417+ }
418+ }
419+
375420impl DbCodec < ( PackageAddress , Hash , CreationId ) > for BlueprintAndCreationIndexKeyDbCodec {
376421 fn encode ( & self , value : & ( PackageAddress , Hash , CreationId ) ) -> Vec < u8 > {
377422 let (
@@ -382,12 +427,12 @@ impl DbCodec<(PackageAddress, Hash, CreationId)> for BlueprintAndCreationIndexKe
382427 index_within_txn,
383428 } ,
384429 ) = value;
385- let mut bytes = Vec :: new ( ) ;
386- bytes . extend_from_slice ( & package_address. as_node_id ( ) . 0 ) ;
387- bytes . extend_from_slice ( & blueprint_name_hash. 0 ) ;
388- bytes . extend_from_slice ( & state_version. to_be_bytes ( ) ) ;
389- bytes . extend_from_slice ( & index_within_txn. to_be_bytes ( ) ) ;
390- bytes
430+ Self :: encode_parts (
431+ & package_address. as_node_id ( ) . 0 ,
432+ & blueprint_name_hash. 0 ,
433+ & state_version. to_be_bytes ( ) ,
434+ & index_within_txn. to_be_bytes ( ) ,
435+ )
391436 }
392437
393438 fn decode ( & self , bytes : & [ u8 ] ) -> ( PackageAddress , Hash , CreationId ) {
@@ -413,6 +458,17 @@ impl DbCodec<(PackageAddress, Hash, CreationId)> for BlueprintAndCreationIndexKe
413458 }
414459}
415460
461+ impl BoundedDbCodec for BlueprintAndCreationIndexKeyDbCodec {
462+ fn upper_bound_encoding ( & self ) -> Vec < u8 > {
463+ Self :: encode_parts (
464+ & [ u8:: MAX ; NodeId :: LENGTH ] ,
465+ & [ u8:: MAX ; Hash :: LENGTH ] ,
466+ & [ u8:: MAX ; StateVersion :: BYTE_LEN ] ,
467+ & [ u8:: MAX ; size_of :: < u32 > ( ) ] ,
468+ )
469+ }
470+ }
471+
416472impl GroupPreservingDbCodec for BlueprintAndCreationIndexKeyDbCodec {
417473 type Group = ( PackageAddress , Hash ) ;
418474
0 commit comments