LCOV - code coverage report
Current view: top level - libs/postgres_ffi/src - nonrelfile_utils.rs (source / functions) Coverage Total Hit
Test: fcf55189004bd3119eed75e2873a97da8078700c.info Lines: 58.6 % 87 51
Test Date: 2024-06-25 12:07:31 Functions: 60.0 % 30 18

            Line data    Source code
       1              : //!
       2              : //! Common utilities for dealing with PostgreSQL non-relation files.
       3              : //!
       4              : use crate::pg_constants;
       5              : use crate::transaction_id_precedes;
       6              : use bytes::BytesMut;
       7              : use log::*;
       8              : 
       9              : use super::bindings::MultiXactId;
      10              : 
      11            0 : pub fn transaction_id_set_status(xid: u32, status: u8, page: &mut BytesMut) {
      12            0 :     trace!(
      13            0 :         "handle_apply_request for RM_XACT_ID-{} (1-commit, 2-abort, 3-sub_commit)",
      14              :         status
      15              :     );
      16              : 
      17            0 :     let byteno: usize =
      18            0 :         ((xid % pg_constants::CLOG_XACTS_PER_PAGE) / pg_constants::CLOG_XACTS_PER_BYTE) as usize;
      19            0 : 
      20            0 :     let bshift: u8 =
      21            0 :         ((xid % pg_constants::CLOG_XACTS_PER_BYTE) * pg_constants::CLOG_BITS_PER_XACT as u32) as u8;
      22            0 : 
      23            0 :     page[byteno] =
      24            0 :         (page[byteno] & !(pg_constants::CLOG_XACT_BITMASK << bshift)) | (status << bshift);
      25            0 : }
      26              : 
      27            0 : pub fn transaction_id_get_status(xid: u32, page: &[u8]) -> u8 {
      28            0 :     let byteno: usize =
      29            0 :         ((xid % pg_constants::CLOG_XACTS_PER_PAGE) / pg_constants::CLOG_XACTS_PER_BYTE) as usize;
      30            0 : 
      31            0 :     let bshift: u8 =
      32            0 :         ((xid % pg_constants::CLOG_XACTS_PER_BYTE) * pg_constants::CLOG_BITS_PER_XACT as u32) as u8;
      33            0 : 
      34            0 :     (page[byteno] >> bshift) & pg_constants::CLOG_XACT_BITMASK
      35            0 : }
      36              : 
      37              : // See CLOGPagePrecedes in clog.c
      38            0 : pub const fn clogpage_precedes(page1: u32, page2: u32) -> bool {
      39            0 :     let mut xid1 = page1 * pg_constants::CLOG_XACTS_PER_PAGE;
      40            0 :     xid1 += pg_constants::FIRST_NORMAL_TRANSACTION_ID + 1;
      41            0 :     let mut xid2 = page2 * pg_constants::CLOG_XACTS_PER_PAGE;
      42            0 :     xid2 += pg_constants::FIRST_NORMAL_TRANSACTION_ID + 1;
      43            0 : 
      44            0 :     transaction_id_precedes(xid1, xid2)
      45            0 :         && transaction_id_precedes(xid1, xid2 + pg_constants::CLOG_XACTS_PER_PAGE - 1)
      46            0 : }
      47              : 
      48              : // See SlruMayDeleteSegment() in slru.c
      49            0 : pub fn slru_may_delete_clogsegment(segpage: u32, cutoff_page: u32) -> bool {
      50            0 :     let seg_last_page = segpage + pg_constants::SLRU_PAGES_PER_SEGMENT - 1;
      51            0 : 
      52            0 :     assert_eq!(segpage % pg_constants::SLRU_PAGES_PER_SEGMENT, 0);
      53              : 
      54            0 :     clogpage_precedes(segpage, cutoff_page) && clogpage_precedes(seg_last_page, cutoff_page)
      55            0 : }
      56              : 
      57              : // Multixact utils
      58              : 
      59           60 : pub fn mx_offset_to_flags_offset(xid: MultiXactId) -> usize {
      60           60 :     ((xid / pg_constants::MULTIXACT_MEMBERS_PER_MEMBERGROUP as u32)
      61           60 :         % pg_constants::MULTIXACT_MEMBERGROUPS_PER_PAGE as u32
      62           60 :         * pg_constants::MULTIXACT_MEMBERGROUP_SIZE as u32) as usize
      63           60 : }
      64              : 
      65           30 : pub fn mx_offset_to_flags_bitshift(xid: MultiXactId) -> u16 {
      66           30 :     (xid as u16) % pg_constants::MULTIXACT_MEMBERS_PER_MEMBERGROUP
      67           30 :         * pg_constants::MXACT_MEMBER_BITS_PER_XACT
      68           30 : }
      69              : 
      70              : /* Location (byte offset within page) of TransactionId of given member */
      71           30 : pub fn mx_offset_to_member_offset(xid: MultiXactId) -> usize {
      72           30 :     mx_offset_to_flags_offset(xid)
      73           30 :         + (pg_constants::MULTIXACT_FLAGBYTES_PER_GROUP
      74           30 :             + (xid as u16 % pg_constants::MULTIXACT_MEMBERS_PER_MEMBERGROUP) * 4) as usize
      75           30 : }
      76              : 
      77           60 : fn mx_offset_to_member_page(xid: u32) -> u32 {
      78           60 :     xid / pg_constants::MULTIXACT_MEMBERS_PER_PAGE as u32
      79           60 : }
      80              : 
      81           30 : pub fn mx_offset_to_member_segment(xid: u32) -> i32 {
      82           30 :     (mx_offset_to_member_page(xid) / pg_constants::SLRU_PAGES_PER_SEGMENT) as i32
      83           30 : }
      84              : 
      85              : #[cfg(test)]
      86              : mod tests {
      87              :     use super::*;
      88              : 
      89              :     #[test]
      90            6 :     fn test_multixid_calc() {
      91            6 :         // Check that the mx_offset_* functions produce the same values as the
      92            6 :         // corresponding PostgreSQL C macros (MXOffsetTo*). These test values
      93            6 :         // were generated by calling the PostgreSQL macros with a little C
      94            6 :         // program.
      95            6 :         assert_eq!(mx_offset_to_member_segment(0), 0);
      96            6 :         assert_eq!(mx_offset_to_member_page(0), 0);
      97            6 :         assert_eq!(mx_offset_to_flags_offset(0), 0);
      98            6 :         assert_eq!(mx_offset_to_flags_bitshift(0), 0);
      99            6 :         assert_eq!(mx_offset_to_member_offset(0), 4);
     100            6 :         assert_eq!(mx_offset_to_member_segment(1), 0);
     101            6 :         assert_eq!(mx_offset_to_member_page(1), 0);
     102            6 :         assert_eq!(mx_offset_to_flags_offset(1), 0);
     103            6 :         assert_eq!(mx_offset_to_flags_bitshift(1), 8);
     104            6 :         assert_eq!(mx_offset_to_member_offset(1), 8);
     105            6 :         assert_eq!(mx_offset_to_member_segment(123456789), 2358);
     106            6 :         assert_eq!(mx_offset_to_member_page(123456789), 75462);
     107            6 :         assert_eq!(mx_offset_to_flags_offset(123456789), 4780);
     108            6 :         assert_eq!(mx_offset_to_flags_bitshift(123456789), 8);
     109            6 :         assert_eq!(mx_offset_to_member_offset(123456789), 4788);
     110            6 :         assert_eq!(mx_offset_to_member_segment(u32::MAX - 1), 82040);
     111            6 :         assert_eq!(mx_offset_to_member_page(u32::MAX - 1), 2625285);
     112            6 :         assert_eq!(mx_offset_to_flags_offset(u32::MAX - 1), 5160);
     113            6 :         assert_eq!(mx_offset_to_flags_bitshift(u32::MAX - 1), 16);
     114            6 :         assert_eq!(mx_offset_to_member_offset(u32::MAX - 1), 5172);
     115            6 :         assert_eq!(mx_offset_to_member_segment(u32::MAX), 82040);
     116            6 :         assert_eq!(mx_offset_to_member_page(u32::MAX), 2625285);
     117            6 :         assert_eq!(mx_offset_to_flags_offset(u32::MAX), 5160);
     118            6 :         assert_eq!(mx_offset_to_flags_bitshift(u32::MAX), 24);
     119            6 :         assert_eq!(mx_offset_to_member_offset(u32::MAX), 5176);
     120            6 :     }
     121              : }
        

Generated by: LCOV version 2.1-beta