Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/***********************************************************************************************************************************
PostgreSQL Page Checksum
 
Adapted from PostgreSQL src/include/storage/checksum_impl.h.
***********************************************************************************************************************************/
#include <build.h>
 
#include <string.h>
 
#include "postgres/interface/static.vendor.h"
 
/***********************************************************************************************************************************
Page checksum calculation
***********************************************************************************************************************************/
// Number of checksums to calculate in parallel
#define PARALLEL_SUM                                                32
 
// Prime multiplier of FNV-1a hash
#define FNV_PRIME                                                   16777619
 
// Calculate one round of the checksum
#define CHECKSUM_ROUND(checksum, value)                                                                                            \
    do                                                                                                                             \
    {                                                                                                                              \
        const uint32_t tmp = (checksum) ^ (value);                                                                                 \
        checksum = tmp * FNV_PRIME ^ (tmp >> 17);                                                                                  \
    } while (0)
 
// Main calculation loop
#define CHECKSUM_CASE(pageSize)                                                                                                    \
    case pageSize:                                                                                                                 \
        for (uint32_t i = 0; i < (uint32) (pageSize / (sizeof(uint32) * PARALLEL_SUM)); i++)                                       \
            for (uint32_t j = 0; j < PARALLEL_SUM; j++)                                                                            \
                CHECKSUM_ROUND(sums[j], ((PgPageChecksum##pageSize *)page)->data[i][j]);                                           \
                                                                                                                                   \
        break;
 
/***********************************************************************************************************************************
Define unions that will make the code valid under strict aliasing for each page size
***********************************************************************************************************************************/
#define CHECKSUM_UNION(pageSize)                                                                                                   \
    typedef union                                                                                                                  \
    {                                                                                                                              \
        PageHeaderData phdr;                                                                                                       \
        uint32_t data[pageSize / (sizeof(uint32_t) * PARALLEL_SUM)][PARALLEL_SUM];                                                 \
    } PgPageChecksum##pageSize;
 
CHECKSUM_UNION(pgPageSize1);
CHECKSUM_UNION(pgPageSize2);
CHECKSUM_UNION(pgPageSize4);
CHECKSUM_UNION(pgPageSize8);
CHECKSUM_UNION(pgPageSize16);
CHECKSUM_UNION(pgPageSize32);
 
/**********************************************************************************************************************************/
FN_EXTERN uint16_t
pgPageChecksum(uint8_t *const page, const uint32_t blockNo, const PgPageSize pageSize)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM_P(BYTEDATA, page);
        FUNCTION_TEST_PARAM(UINT, blockNo);
        FUNCTION_TEST_PARAM(ENUM, pageSize);
    FUNCTION_TEST_END();
 
    // Save pd_checksum and temporarily set it to zero, so that the checksum calculation isn't affected by the old checksum stored
    // on the page. Restore it after, because actually updating the checksum is NOT part of the API of this function.
    const uint16_t checksumPrior = ((PageHeaderData *)page)->pd_checksum;
    ((PageHeaderData *)page)->pd_checksum = 0;
 
    // Initialize partial checksums to their corresponding offsets
    uint32_t sums[PARALLEL_SUM] =
    {
        0x5b1f36e9, 0xb8525960, 0x02ab50aa, 0x1de66d2a, 0x79ff467a, 0x9bb9f8a3, 0x217e7cd2, 0x83e13d2c,
        0xf8d4474f, 0xe39eb970, 0x42c6ae16, 0x993216fa, 0x7b093b5d, 0x98daff3c, 0xf718902a, 0x0b1c9cdb,
        0xe58f764b, 0x187636bc, 0x5d7b3bb1, 0xe73de7de, 0x92bec979, 0xcca6c0b2, 0x304a0979, 0x85aa43d4,
        0x783125bb, 0x6ca8eaa2, 0xe407eac6, 0x4b5cfc3e, 0x9fbf8c76, 0x15ca20be, 0xf2ca9fd3, 0x959bd756,
    };
 
    // Main checksum calculation
    switch (pageSize)
    {
        CHECKSUM_CASE(pgPageSize8);                                 // Default page size should be checked first
        CHECKSUM_CASE(pgPageSize1);
        CHECKSUM_CASE(pgPageSize2);
        CHECKSUM_CASE(pgPageSize4);
        CHECKSUM_CASE(pgPageSize16);
        CHECKSUM_CASE(pgPageSize32);
 
        default:
            pgPageSizeCheck(pageSize);
    }
 
    // Add in two rounds of zeroes for additional mixing
    for (uint32_t i = 0; i < 2; i++)
        for (uint32_t j = 0; j < PARALLEL_SUM; j++)
            CHECKSUM_ROUND(sums[j], 0);
 
    // Xor fold partial checksums together
    uint32 result = 0;
 
    for (uint32_t i = 0; i < PARALLEL_SUM; i++)
        result ^= sums[i];
 
    // Restore prior checksum
    ((PageHeaderData *)page)->pd_checksum = checksumPrior;
 
    // Mix in the block number to detect transposed pages
    result ^= blockNo;
 
    // Reduce to a uint16 (to fit in the pd_checksum field) with an offset of one. That avoids checksums of zero, which seems like a
    // good idea.
    FUNCTION_TEST_RETURN(UINT16, (uint16_t)((result % 65535) + 1));
}
 
/**********************************************************************************************************************************/
FN_EXTERN bool
pgPageSizeValid(const PgPageSize pageSize)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM(ENUM, pageSize);
    FUNCTION_TEST_END();
 
    switch (pageSize)
    {
        case pgPageSize1:
        case pgPageSize2:
        case pgPageSize4:
        case pgPageSize8:
        case pgPageSize16:
        case pgPageSize32:
            FUNCTION_TEST_RETURN(BOOL, true);
    }
 
    FUNCTION_TEST_RETURN(BOOL, false);
}
 
/**********************************************************************************************************************************/
FN_EXTERN void
pgPageSizeCheck(const PgPageSize pageSize)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM(ENUM, pageSize);
    FUNCTION_TEST_END();
 
    if (!pgPageSizeValid(pageSize))
    {
        THROW_FMT(
            FormatError, "page size is %u but only %i, %i, %i, %i, %i, and %i are supported", pageSize, pgPageSize1, pgPageSize2,
            pgPageSize4, pgPageSize8, pgPageSize16, pgPageSize32);
    }
 
    FUNCTION_TEST_RETURN_VOID();
}