Skip to content

Commit 6f1bb54

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 5f279ad commit 6f1bb54

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
@@ -109,7 +109,7 @@ static WALKeyCacheRec *tde_wal_key_cache = NULL;
109109
static WALKeyCacheRec *tde_wal_key_last_rec = NULL;
110110

111111
static InternalKey *pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type);
112-
static TDEMapEntry *pg_tde_find_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_map_path);
112+
static bool pg_tde_find_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_map_path, TDEMapEntry *map_entry);
113113
static InternalKey *tde_decrypt_rel_key(TDEPrincipalKey *principal_key, TDEMapEntry *map_entry);
114114
static int pg_tde_open_file_basic(const char *tde_filename, int fileFlags, bool ignore_missing);
115115
static void pg_tde_file_header_read(const char *tde_filename, int fd, TDEFileHeader *fheader, off_t *bytes_read);
@@ -846,7 +846,7 @@ pg_tde_open_file_write(const char *tde_filename, const TDESignedPrincipalKeyInfo
846846
static InternalKey *
847847
pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
848848
{
849-
TDEMapEntry *map_entry;
849+
TDEMapEntry map_entry;
850850
TDEPrincipalKey *principal_key;
851851
LWLock *lock_pk = tde_lwlock_enc_keys();
852852
char db_map_path[MAXPGPATH] = {0};
@@ -862,10 +862,7 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
862862

863863
LWLockAcquire(lock_pk, LW_SHARED);
864864

865-
/* Read the map entry and get the index of the relation key */
866-
map_entry = pg_tde_find_map_entry(rlocator, key_type, db_map_path);
867-
868-
if (map_entry == NULL)
865+
if (!pg_tde_find_map_entry(rlocator, key_type, db_map_path, &map_entry))
869866
{
870867
LWLockRelease(lock_pk);
871868
return NULL;
@@ -889,44 +886,41 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
889886
errmsg("principal key not configured"),
890887
errhint("create one using pg_tde_set_key before using encrypted tables"));
891888

892-
rel_key = tde_decrypt_rel_key(principal_key, map_entry);
889+
rel_key = tde_decrypt_rel_key(principal_key, &map_entry);
893890

894891
LWLockRelease(lock_pk);
895892

896893
return rel_key;
897894
}
898895

899896
/*
900-
* Returns the entry of the map if we find a valid match; e.g. flags is not
901-
* set to MAP_ENTRY_EMPTY and the relNumber and spcOid matches the one
902-
* provided in rlocator.
897+
* Returns true if we find a valid match; e.g. flags is not set to
898+
* MAP_ENTRY_EMPTY and the relNumber and spcOid matches the one provided in
899+
* rlocator.
903900
*/
904-
static TDEMapEntry *
905-
pg_tde_find_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_map_path)
901+
static bool
902+
pg_tde_find_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_map_path, TDEMapEntry *map_entry)
906903
{
907904
File map_fd;
908-
TDEMapEntry *map_entry = palloc_object(TDEMapEntry);
909905
off_t curr_pos = 0;
906+
bool found = false;
910907

911908
Assert(rlocator != NULL);
912909

913910
map_fd = pg_tde_open_file_read(db_map_path, &curr_pos);
914911

915-
while (1)
912+
while (pg_tde_read_one_map_entry(map_fd, map_entry, &curr_pos))
916913
{
917-
if (!pg_tde_read_one_map_entry(map_fd, map_entry, &curr_pos))
918-
{
919-
close(map_fd);
920-
pfree(map_entry);
921-
return NULL;
922-
}
923-
924914
if ((map_entry->flags & key_type) && map_entry->spcOid == rlocator->spcOid && map_entry->relNumber == rlocator->relNumber)
925915
{
926-
close(map_fd);
927-
return map_entry;
916+
found = true;
917+
break;
928918
}
929919
}
920+
921+
close(map_fd);
922+
923+
return found;
930924
}
931925

932926
bool

0 commit comments

Comments
 (0)