Skip to content

Commit 4d8cd9c

Browse files
committed
Simplify pg_tde_find_map_entry() now that reading is simpler
Now that pg_tde_read_one_map_entry() is simpler it makes sense to also simplify the pg_tde_find_map_entry() function.
1 parent 82daaeb commit 4d8cd9c

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

contrib/pg_tde/src/access/pg_tde_tdemap.c

+17-23
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static WALKeyCacheRec *tde_wal_key_cache = NULL;
110110
static WALKeyCacheRec *tde_wal_key_last_rec = NULL;
111111

112112
static InternalKey *pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type);
113-
static TDEMapEntry *pg_tde_find_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_map_path);
113+
static bool pg_tde_find_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_map_path, TDEMapEntry *map_entry);
114114
static InternalKey *tde_decrypt_rel_key(TDEPrincipalKey *principal_key, TDEMapEntry *map_entry);
115115
static int pg_tde_open_file_basic(const char *tde_filename, int fileFlags, bool ignore_missing);
116116
static void pg_tde_file_header_read(const char *tde_filename, int fd, TDEFileHeader *fheader, off_t *bytes_read);
@@ -905,7 +905,7 @@ pg_tde_open_file_write(const char *tde_filename, const TDESignedPrincipalKeyInfo
905905
static InternalKey *
906906
pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
907907
{
908-
TDEMapEntry *map_entry;
908+
TDEMapEntry map_entry;
909909
TDEPrincipalKey *principal_key;
910910
LWLock *lock_pk = tde_lwlock_enc_keys();
911911
char db_map_path[MAXPGPATH] = {0};
@@ -921,10 +921,7 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
921921

922922
LWLockAcquire(lock_pk, LW_SHARED);
923923

924-
/* Read the map entry and get the index of the relation key */
925-
map_entry = pg_tde_find_map_entry(rlocator, key_type, db_map_path);
926-
927-
if (map_entry == NULL)
924+
if (!pg_tde_find_map_entry(rlocator, key_type, db_map_path, &map_entry))
928925
{
929926
LWLockRelease(lock_pk);
930927
return NULL;
@@ -948,44 +945,41 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
948945
errmsg("principal key not configured"),
949946
errhint("create one using pg_tde_set_key before using encrypted tables"));
950947

951-
rel_key = tde_decrypt_rel_key(principal_key, map_entry);
948+
rel_key = tde_decrypt_rel_key(principal_key, &map_entry);
952949

953950
LWLockRelease(lock_pk);
954951

955952
return rel_key;
956953
}
957954

958955
/*
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.
956+
* Returns true if we find a valid match; e.g. flags is not set to
957+
* MAP_ENTRY_EMPTY and the relNumber and spcOid matches the one provided in
958+
* rlocator.
962959
*/
963-
static TDEMapEntry *
964-
pg_tde_find_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_map_path)
960+
static bool
961+
pg_tde_find_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_map_path, TDEMapEntry *map_entry)
965962
{
966963
File map_fd;
967-
TDEMapEntry *map_entry = palloc_object(TDEMapEntry);
968964
off_t curr_pos = 0;
965+
bool found = false;
969966

970967
Assert(rlocator != NULL);
971968

972969
map_fd = pg_tde_open_file_read(db_map_path, &curr_pos);
973970

974-
while (1)
971+
while (pg_tde_read_one_map_entry(map_fd, map_entry, &curr_pos))
975972
{
976-
if (!pg_tde_read_one_map_entry(map_fd, map_entry, &curr_pos))
977-
{
978-
close(map_fd);
979-
pfree(map_entry);
980-
return NULL;
981-
}
982-
983973
if ((map_entry->flags & key_type) && map_entry->spcOid == rlocator->spcOid && map_entry->relNumber == rlocator->relNumber)
984974
{
985-
close(map_fd);
986-
return map_entry;
975+
found = true;
976+
break;
987977
}
988978
}
979+
980+
close(map_fd);
981+
982+
return found;
989983
}
990984

991985
bool

0 commit comments

Comments
 (0)