@@ -114,7 +114,7 @@ static TDEMapEntry *pg_tde_find_map_entry(const RelFileLocator *rlocator, uint32
114
114
static InternalKey * tde_decrypt_rel_key (TDEPrincipalKey * principal_key , TDEMapEntry * map_entry );
115
115
static int pg_tde_open_file_basic (const char * tde_filename , int fileFlags , bool ignore_missing );
116
116
static void pg_tde_file_header_read (const char * tde_filename , int fd , TDEFileHeader * fheader , off_t * bytes_read );
117
- static bool pg_tde_read_one_map_entry (int fd , const RelFileLocator * rlocator , int flags , TDEMapEntry * map_entry , off_t * offset );
117
+ static bool pg_tde_read_one_map_entry (int fd , TDEMapEntry * map_entry , off_t * offset );
118
118
static void pg_tde_read_one_map_entry2 (int keydata_fd , int32 key_index , TDEMapEntry * map_entry , Oid databaseId );
119
119
static int pg_tde_open_file_read (const char * tde_filename , off_t * curr_pos );
120
120
static InternalKey * pg_tde_get_key_from_cache (const RelFileLocator * rlocator , uint32 key_type );
@@ -449,16 +449,12 @@ pg_tde_write_key_map_entry(const RelFileLocator *rlocator, InternalKey *rel_key_
449
449
while (1 )
450
450
{
451
451
TDEMapEntry read_map_entry ;
452
- bool found ;
453
452
454
453
prev_pos = curr_pos ;
455
- found = pg_tde_read_one_map_entry (map_fd , NULL , MAP_ENTRY_EMPTY , & read_map_entry , & curr_pos );
454
+ if (!pg_tde_read_one_map_entry (map_fd , & read_map_entry , & curr_pos ))
455
+ break ;
456
456
457
- /*
458
- * We either reach EOF or found an empty slot in the middle of the
459
- * file
460
- */
461
- if (prev_pos == curr_pos || found )
457
+ if (read_map_entry .flags == MAP_ENTRY_EMPTY )
462
458
break ;
463
459
}
464
460
@@ -522,16 +518,12 @@ pg_tde_write_key_map_entry_redo(const TDEMapEntry *write_map_entry, TDESignedPri
522
518
while (1 )
523
519
{
524
520
TDEMapEntry read_map_entry ;
525
- bool found ;
526
521
527
522
prev_pos = curr_pos ;
528
- found = pg_tde_read_one_map_entry (map_fd , NULL , MAP_ENTRY_EMPTY , & read_map_entry , & curr_pos );
523
+ if (!pg_tde_read_one_map_entry (map_fd , & read_map_entry , & curr_pos ))
524
+ break ;
529
525
530
- /*
531
- * We either reach EOF or found an empty slot in the middle of the
532
- * file
533
- */
534
- if (prev_pos == curr_pos || found )
526
+ if (read_map_entry .flags == MAP_ENTRY_EMPTY )
535
527
break ;
536
528
}
537
529
@@ -583,17 +575,13 @@ pg_tde_delete_map_entry(const RelFileLocator *rlocator, char *db_map_path, off_t
583
575
*/
584
576
while (1 )
585
577
{
586
- TDEMapEntry read_map_entry ;
578
+ TDEMapEntry map_entry ;
587
579
off_t prev_pos = curr_pos ;
588
580
589
- found = pg_tde_read_one_map_entry (map_fd , rlocator , MAP_ENTRY_VALID , & read_map_entry , & curr_pos );
590
-
591
- /* We've reached EOF */
592
- if (curr_pos == prev_pos )
581
+ if (!pg_tde_read_one_map_entry (map_fd , & map_entry , & curr_pos ))
593
582
break ;
594
583
595
- /* We found a valid entry for the relation */
596
- if (found )
584
+ if (map_entry .flags != MAP_ENTRY_EMPTY && map_entry .spcOid == rlocator -> spcOid && map_entry .relNumber == rlocator -> relNumber )
597
585
{
598
586
TDEMapEntry empty_map_entry = {
599
587
.flags = MAP_ENTRY_EMPTY ,
@@ -603,6 +591,7 @@ pg_tde_delete_map_entry(const RelFileLocator *rlocator, char *db_map_path, off_t
603
591
};
604
592
605
593
pg_tde_write_one_map_entry (map_fd , & empty_map_entry , & prev_pos , db_map_path );
594
+ found = true;
606
595
break ;
607
596
}
608
597
}
@@ -616,7 +605,7 @@ pg_tde_delete_map_entry(const RelFileLocator *rlocator, char *db_map_path, off_t
616
605
617
606
/*
618
607
* Called when transaction is being completed; either committed or aborted.
619
- * By default, when a transaction creates an entry, we mark it as MAP_ENTRY_VALID .
608
+ * By default, when a transaction creates an entry, we mark it with the type .
620
609
* Only during the abort phase of the transaction that we are proceed on with
621
610
* marking the entry as MAP_ENTRY_FREE. This optimistic strategy that assumes
622
611
* that transaction will commit more often then getting aborted avoids
@@ -712,22 +701,15 @@ pg_tde_perform_rotate_key(TDEPrincipalKey *principal_key, TDEPrincipalKey *new_p
712
701
while (1 )
713
702
{
714
703
InternalKey * rel_key_data ;
715
- off_t old_prev_pos ,
716
- new_prev_pos ;
704
+ off_t new_prev_pos ;
717
705
TDEMapEntry read_map_entry ,
718
706
write_map_entry ;
719
707
RelFileLocator rloc ;
720
- bool found ;
721
-
722
- old_prev_pos = old_curr_pos ;
723
- found = pg_tde_read_one_map_entry (old_fd , NULL , MAP_ENTRY_VALID , & read_map_entry , & old_curr_pos );
724
708
725
- /* We either reach EOF */
726
- if (old_prev_pos == old_curr_pos )
709
+ if (!pg_tde_read_one_map_entry (old_fd , & read_map_entry , & old_curr_pos ))
727
710
break ;
728
711
729
- /* We didn't find a valid entry */
730
- if (found == false)
712
+ if (read_map_entry .flags == MAP_ENTRY_EMPTY )
731
713
continue ;
732
714
733
715
rloc .spcOid = read_map_entry .spcOid ;
@@ -975,8 +957,9 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
975
957
}
976
958
977
959
/*
978
- * Returns the index of the read map if we find a valid match; e.g. flags is set to
979
- * MAP_ENTRY_VALID and the relNumber and spcOid matches the one provided in rlocator.
960
+ * Returns the entry of the map if we find a valid match; e.g. flags is not
961
+ * set to MAP_ENTRY_EMPTY and the relNumber and spcOid matches the one
962
+ * provided in rlocator.
980
963
*/
981
964
static TDEMapEntry *
982
965
pg_tde_find_map_entry (const RelFileLocator * rlocator , uint32 key_type , char * db_map_path )
@@ -985,30 +968,20 @@ pg_tde_find_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_
985
968
TDEMapEntry * map_entry = palloc_object (TDEMapEntry );
986
969
off_t curr_pos = 0 ;
987
970
971
+ Assert (rlocator != NULL );
972
+
988
973
map_fd = pg_tde_open_file_read (db_map_path , & curr_pos );
989
974
990
- /*
991
- * Read until we find an empty slot. Otherwise, read until end. This seems
992
- * to be less frequent than vacuum. So let's keep this function here
993
- * rather than overloading the vacuum process.
994
- */
995
975
while (1 )
996
976
{
997
- bool found ;
998
- off_t prev_pos = curr_pos ;
999
-
1000
- found = pg_tde_read_one_map_entry (map_fd , rlocator , key_type , map_entry , & curr_pos );
1001
-
1002
- /* We've reached EOF */
1003
- if (curr_pos == prev_pos )
977
+ if (!pg_tde_read_one_map_entry (map_fd , map_entry , & curr_pos ))
1004
978
{
1005
979
close (map_fd );
1006
980
pfree (map_entry );
1007
981
return NULL ;
1008
982
}
1009
983
1010
- /* We found a valid entry for the relation */
1011
- if (found )
984
+ if ((map_entry -> flags & key_type ) && map_entry -> spcOid == rlocator -> spcOid && map_entry -> relNumber == rlocator -> relNumber )
1012
985
{
1013
986
close (map_fd );
1014
987
return map_entry ;
@@ -1118,26 +1091,17 @@ pg_tde_file_header_read(const char *tde_filename, int fd, TDEFileHeader *fheader
1118
1091
1119
1092
1120
1093
/*
1121
- * Returns true if a valid map entry if found. Otherwise, it only increments
1122
- * the offset and returns false. If the same offset value is set, it indicates
1123
- * to the caller that nothing was read.
1124
- *
1125
- * If a non-NULL rlocator is provided, the function compares the read value
1126
- * against the relNumber of rlocator. It sets found accordingly.
1127
- *
1128
- * The caller is reponsible for identifying that we have reached EOF by
1129
- * comparing old and new value of the offset.
1094
+ * Returns true if a map entry if found or false if we have reached the end of
1095
+ * the file.
1130
1096
*/
1131
1097
static bool
1132
- pg_tde_read_one_map_entry (int map_file , const RelFileLocator * rlocator , int flags , TDEMapEntry * map_entry , off_t * offset )
1098
+ pg_tde_read_one_map_entry (int map_file , TDEMapEntry * map_entry , off_t * offset )
1133
1099
{
1134
- bool found ;
1135
1100
off_t bytes_read = 0 ;
1136
1101
1137
1102
Assert (map_entry );
1138
1103
Assert (offset );
1139
1104
1140
- /* Read the entry at the given offset */
1141
1105
bytes_read = pg_pread (map_file , map_entry , MAP_ENTRY_SIZE , * offset );
1142
1106
1143
1107
/* We've reached the end of the file. */
@@ -1146,14 +1110,7 @@ pg_tde_read_one_map_entry(int map_file, const RelFileLocator *rlocator, int flag
1146
1110
1147
1111
* offset += bytes_read ;
1148
1112
1149
- /* We found a valid entry */
1150
- found = map_entry -> flags & flags ;
1151
-
1152
- /* If a valid rlocator is provided, let's compare and set found value */
1153
- if (rlocator != NULL )
1154
- found &= map_entry -> spcOid == rlocator -> spcOid && map_entry -> relNumber == rlocator -> relNumber ;
1155
-
1156
- return found ;
1113
+ return true;
1157
1114
}
1158
1115
1159
1116
/*
0 commit comments