Исходный код СУБД 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
/*--------------------------------------------------------------------------
 *
 * test_dsm_registry.c
 *    Test the dynamic shared memory registry.
 *
 * Copyright (c) 2024-2026, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *      src/test/modules/test_dsm_registry/test_dsm_registry.c
 *
 * -------------------------------------------------------------------------
 */
#include "postgres.h"
 
#include "fmgr.h"
#include "storage/dsm_registry.h"
#include "storage/lwlock.h"
#include "utils/builtins.h"
 
PG_MODULE_MAGIC;
 
typedef struct TestDSMRegistryStruct
{
    int         val;
    LWLock      lck;
} TestDSMRegistryStruct;
 
typedef struct TestDSMRegistryHashEntry
{
    char        key[64];
    dsa_pointer val;
} TestDSMRegistryHashEntry;
 
static TestDSMRegistryStruct *tdr_dsm;
static dsa_area *tdr_dsa;
static dshash_table *tdr_hash;
 
static const dshash_parameters dsh_params = {
    offsetof(TestDSMRegistryHashEntry, val),
    sizeof(TestDSMRegistryHashEntry),
    dshash_strcmp,
    dshash_strhash,
    dshash_strcpy
};
 
static void
init_tdr_dsm(void *ptr, void *arg)
{
    TestDSMRegistryStruct *dsm = (TestDSMRegistryStruct *) ptr;
 
    if ((int) (intptr_t) arg != 5432)
        elog(ERROR, "unexpected arg value %d", (int) (intptr_t) arg);
 
    LWLockInitialize(&dsm->lck, LWLockNewTrancheId("test_dsm_registry"));
    dsm->val = 0;
}
 
static void
tdr_attach_shmem(void)
{
    bool        found;
 
    tdr_dsm = GetNamedDSMSegment("test_dsm_registry_dsm",
                                 sizeof(TestDSMRegistryStruct),
                                 init_tdr_dsm,
                                 &found, (void *) (intptr_t) 5432);
 
    if (tdr_dsa == NULL)
        tdr_dsa = GetNamedDSA("test_dsm_registry_dsa", &found);
 
    if (tdr_hash == NULL)
        tdr_hash = GetNamedDSHash("test_dsm_registry_hash", &dsh_params, &found);
}
 
PG_FUNCTION_INFO_V1(set_val_in_shmem);
Datum
set_val_in_shmem(PG_FUNCTION_ARGS)
{
    tdr_attach_shmem();
 
    LWLockAcquire(&tdr_dsm->lck, LW_EXCLUSIVE);
    tdr_dsm->val = PG_GETARG_INT32(0);
    LWLockRelease(&tdr_dsm->lck);
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(get_val_in_shmem);
Datum
get_val_in_shmem(PG_FUNCTION_ARGS)
{
    int         ret;
 
    tdr_attach_shmem();
 
    LWLockAcquire(&tdr_dsm->lck, LW_SHARED);
    ret = tdr_dsm->val;
    LWLockRelease(&tdr_dsm->lck);
 
    PG_RETURN_INT32(ret);
}
 
PG_FUNCTION_INFO_V1(set_val_in_hash);
Datum
set_val_in_hash(PG_FUNCTION_ARGS)
{
    TestDSMRegistryHashEntry *entry;
    char       *key = TextDatumGetCString(PG_GETARG_DATUM(0));
    char       *val = TextDatumGetCString(PG_GETARG_DATUM(1));
    bool        found;
 
    if (strlen(key) >= offsetof(TestDSMRegistryHashEntry, val))
        ereport(ERROR,
                (errmsg("key too long")));
 
    tdr_attach_shmem();
 
    entry = dshash_find_or_insert(tdr_hash, key, &found);
    if (found)
        dsa_free(tdr_dsa, entry->val);
 
    entry->val = dsa_allocate(tdr_dsa, strlen(val) + 1);
    strcpy(dsa_get_address(tdr_dsa, entry->val), val);
 
    dshash_release_lock(tdr_hash, entry);
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(get_val_in_hash);
Datum
get_val_in_hash(PG_FUNCTION_ARGS)
{
    TestDSMRegistryHashEntry *entry;
    char       *key = TextDatumGetCString(PG_GETARG_DATUM(0));
    text       *val = NULL;
 
    tdr_attach_shmem();
 
    entry = dshash_find(tdr_hash, key, false);
    if (entry == NULL)
        PG_RETURN_NULL();
 
    val = cstring_to_text(dsa_get_address(tdr_dsa, entry->val));
 
    dshash_release_lock(tdr_hash, entry);
 
    PG_RETURN_TEXT_P(val);
}