@@ -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 ,
@@ -616,7 +604,7 @@ pg_tde_delete_map_entry(const RelFileLocator *rlocator, char *db_map_path, off_t
616
604
617
605
/*
618
606
* 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 .
607
+ * By default, when a transaction creates an entry, we mark it with the type .
620
608
* Only during the abort phase of the transaction that we are proceed on with
621
609
* marking the entry as MAP_ENTRY_FREE. This optimistic strategy that assumes
622
610
* that transaction will commit more often then getting aborted avoids
@@ -712,22 +700,15 @@ pg_tde_perform_rotate_key(TDEPrincipalKey *principal_key, TDEPrincipalKey *new_p
712
700
while (1 )
713
701
{
714
702
InternalKey * rel_key_data ;
715
- off_t old_prev_pos ,
716
- new_prev_pos ;
703
+ off_t new_prev_pos ;
717
704
TDEMapEntry read_map_entry ,
718
705
write_map_entry ;
719
706
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
707
725
- /* We either reach EOF */
726
- if (old_prev_pos == old_curr_pos )
708
+ if (!pg_tde_read_one_map_entry (old_fd , & read_map_entry , & old_curr_pos ))
727
709
break ;
728
710
729
- /* We didn't find a valid entry */
730
- if (found == false)
711
+ if (read_map_entry .flags == MAP_ENTRY_EMPTY )
731
712
continue ;
732
713
733
714
rloc .spcOid = read_map_entry .spcOid ;
@@ -975,8 +956,9 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
975
956
}
976
957
977
958
/*
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.
959
+ * Returns the entry of the map if we find a valid match; e.g. flags is not
960
+ * set to MAP_ENTRY_EMPTY and the relNumber and spcOid matches the one
961
+ * provided in rlocator.
980
962
*/
981
963
static TDEMapEntry *
982
964
pg_tde_find_map_entry (const RelFileLocator * rlocator , uint32 key_type , char * db_map_path )
@@ -985,30 +967,20 @@ pg_tde_find_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_
985
967
TDEMapEntry * map_entry = palloc_object (TDEMapEntry );
986
968
off_t curr_pos = 0 ;
987
969
970
+ Assert (rlocator != NULL );
971
+
988
972
map_fd = pg_tde_open_file_read (db_map_path , & curr_pos );
989
973
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
974
while (1 )
996
975
{
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 )
976
+ if (!pg_tde_read_one_map_entry (map_fd , map_entry , & curr_pos ))
1004
977
{
1005
978
close (map_fd );
1006
979
pfree (map_entry );
1007
980
return NULL ;
1008
981
}
1009
982
1010
- /* We found a valid entry for the relation */
1011
- if (found )
983
+ if ((map_entry -> flags & key_type ) && map_entry -> spcOid == rlocator -> spcOid && map_entry -> relNumber == rlocator -> relNumber )
1012
984
{
1013
985
close (map_fd );
1014
986
return map_entry ;
@@ -1118,26 +1090,17 @@ pg_tde_file_header_read(const char *tde_filename, int fd, TDEFileHeader *fheader
1118
1090
1119
1091
1120
1092
/*
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.
1093
+ * Returns true if a map entry if found or false if we have reached the end of
1094
+ * the file.
1130
1095
*/
1131
1096
static bool
1132
- pg_tde_read_one_map_entry (int map_file , const RelFileLocator * rlocator , int flags , TDEMapEntry * map_entry , off_t * offset )
1097
+ pg_tde_read_one_map_entry (int map_file , TDEMapEntry * map_entry , off_t * offset )
1133
1098
{
1134
- bool found ;
1135
1099
off_t bytes_read = 0 ;
1136
1100
1137
1101
Assert (map_entry );
1138
1102
Assert (offset );
1139
1103
1140
- /* Read the entry at the given offset */
1141
1104
bytes_read = pg_pread (map_file , map_entry , MAP_ENTRY_SIZE , * offset );
1142
1105
1143
1106
/* We've reached the end of the file. */
@@ -1146,14 +1109,7 @@ pg_tde_read_one_map_entry(int map_file, const RelFileLocator *rlocator, int flag
1146
1109
1147
1110
* offset += bytes_read ;
1148
1111
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 ;
1112
+ return true;
1157
1113
}
1158
1114
1159
1115
/*
0 commit comments