Skip to content

Commit 4a63df7

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 bf71119 commit 4a63df7

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);
@@ -906,7 +906,7 @@ pg_tde_open_file_write(const char *tde_filename, const TDESignedPrincipalKeyInfo
906906
static InternalKey *
907907
pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
908908
{
909-
TDEMapEntry *map_entry;
909+
TDEMapEntry map_entry;
910910
TDEPrincipalKey *principal_key;
911911
LWLock *lock_pk = tde_lwlock_enc_keys();
912912
char db_map_path[MAXPGPATH] = {0};
@@ -922,10 +922,7 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
922922

923923
LWLockAcquire(lock_pk, LW_SHARED);
924924

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

952-
rel_key = tde_decrypt_rel_key(principal_key, map_entry);
949+
rel_key = tde_decrypt_rel_key(principal_key, &map_entry);
953950

954951
LWLockRelease(lock_pk);
955952

956953
return rel_key;
957954
}
958955

959956
/*
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.
957+
* Returns true if we find a valid match; e.g. flags is not set to
958+
* MAP_ENTRY_EMPTY and the relNumber and spcOid matches the one provided in
959+
* rlocator.
963960
*/
964-
static TDEMapEntry *
965-
pg_tde_find_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_map_path)
961+
static bool
962+
pg_tde_find_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_map_path, TDEMapEntry *map_entry)
966963
{
967964
File map_fd;
968-
TDEMapEntry *map_entry = palloc_object(TDEMapEntry);
969965
off_t curr_pos = 0;
966+
bool found = false;
970967

971968
Assert(rlocator != NULL);
972969

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

975-
while (1)
972+
while (pg_tde_read_one_map_entry(map_fd, map_entry, &curr_pos))
976973
{
977-
if (!pg_tde_read_one_map_entry(map_fd, map_entry, &curr_pos))
978-
{
979-
close(map_fd);
980-
pfree(map_entry);
981-
return NULL;
982-
}
983-
984974
if ((map_entry->flags & key_type) && map_entry->spcOid == rlocator->spcOid && map_entry->relNumber == rlocator->relNumber)
985975
{
986-
close(map_fd);
987-
return map_entry;
976+
found = true;
977+
break;
988978
}
989979
}
980+
981+
close(map_fd);
982+
983+
return found;
990984
}
991985

992986
bool

0 commit comments

Comments
 (0)