forked from pgcentralfoundation/pgrx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
174 lines (147 loc) · 4.58 KB
/
lib.rs
File metadata and controls
174 lines (147 loc) · 4.58 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC.
//LICENSE
//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc.
//LICENSE
//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <contact@pgcentral.org>
//LICENSE
//LICENSE All rights reserved.
//LICENSE
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
use pgrx::atomics::*;
use pgrx::lwlock::PgLwLock;
use pgrx::prelude::*;
use pgrx::shmem::*;
use pgrx::{pg_shmem_init, warning};
use serde::*;
use std::sync::atomic::Ordering;
pgrx::pg_module_magic!(name, version);
// types behind a `LwLock` must derive/implement `Copy` and `Clone`
#[derive(Copy, Clone)]
// This is for general Postgres type support -- not strictly necessary if the type is not exposed via SQL
#[derive(PostgresType, Serialize, Deserialize)]
#[derive(Default)]
pub struct Pgtest {
value1: i32,
value2: i32,
}
unsafe impl PGRXSharedMemory for Pgtest {}
static DEQUE: PgLwLock<AssertPGRXSharedMemory<heapless::Deque<Pgtest, 400>>> =
unsafe { PgLwLock::new(c"shmem_deque") };
static VEC: PgLwLock<AssertPGRXSharedMemory<heapless::Vec<Pgtest, 400>>> =
unsafe { PgLwLock::new(c"shmem_vec") };
static HASH: PgLwLock<AssertPGRXSharedMemory<heapless::FnvIndexMap<i32, i32, 4>>> =
unsafe { PgLwLock::new(c"shmem_hash") };
static STRUCT: PgLwLock<Pgtest> = unsafe { PgLwLock::new(c"shmem_struct") };
static PRIMITIVE: PgLwLock<i32> = unsafe { PgLwLock::new(c"shmem_primtive") };
static ATOMIC: PgAtomic<std::sync::atomic::AtomicBool> = unsafe { PgAtomic::new(c"shmem_atomic") };
#[pg_guard]
pub extern "C-unwind" fn _PG_init() {
if unsafe { !pgrx::pg_sys::process_shared_preload_libraries_in_progress } {
pgrx::error!("this extension must be loaded via shared_preload_libraries.");
}
pg_shmem_init!(DEQUE = unsafe { AssertPGRXSharedMemory::new(Default::default()) });
pg_shmem_init!(VEC = unsafe { AssertPGRXSharedMemory::new(Default::default()) });
pg_shmem_init!(HASH = unsafe { AssertPGRXSharedMemory::new(Default::default()) });
pg_shmem_init!(STRUCT);
pg_shmem_init!(PRIMITIVE);
pg_shmem_init!(ATOMIC);
}
#[pg_extern]
fn vec_select() -> SetOfIterator<'static, Pgtest> {
SetOfIterator::new(VEC.share().iter().copied().collect::<Vec<Pgtest>>())
}
#[pg_extern]
fn vec_count() -> i32 {
VEC.share().len() as i32
}
#[pg_extern]
fn vec_drain() -> SetOfIterator<'static, Pgtest> {
let mut vec = VEC.exclusive();
let r = vec.iter().copied().collect::<Vec<Pgtest>>();
vec.clear();
SetOfIterator::new(r)
}
#[pg_extern]
fn vec_push(value: Pgtest) {
VEC.exclusive().push(value).unwrap_or_else(|_| warning!("Vector is full, discarding update"));
}
#[pg_extern]
fn vec_pop() -> Option<Pgtest> {
VEC.exclusive().pop()
}
#[pg_extern]
fn deque_select() -> SetOfIterator<'static, Pgtest> {
SetOfIterator::new(DEQUE.share().iter().copied().collect::<Vec<Pgtest>>())
}
#[pg_extern]
fn deque_count() -> i32 {
DEQUE.share().len() as i32
}
#[pg_extern]
fn deque_drain() -> SetOfIterator<'static, Pgtest> {
let mut vec = DEQUE.exclusive();
let r = vec.iter().copied().collect::<Vec<Pgtest>>();
vec.clear();
SetOfIterator::new(r)
}
#[pg_extern]
fn deque_push_back(value: Pgtest) {
DEQUE
.exclusive()
.push_back(value)
.unwrap_or_else(|_| warning!("Deque is full, discarding update"));
}
#[pg_extern]
fn deque_push_front(value: Pgtest) {
DEQUE
.exclusive()
.push_front(value)
.unwrap_or_else(|_| warning!("Deque is full, discarding update"));
}
#[pg_extern]
fn deque_pop_back() -> Option<Pgtest> {
DEQUE.exclusive().pop_back()
}
#[pg_extern]
fn deque_pop_front() -> Option<Pgtest> {
DEQUE.exclusive().pop_front()
}
#[pg_extern]
fn hash_insert(key: i32, value: i32) {
HASH.exclusive().insert(key, value).unwrap();
}
#[pg_extern]
fn hash_get(key: i32) -> Option<i32> {
HASH.share().get(&key).cloned()
}
#[pg_extern]
fn struct_get() -> Pgtest {
*STRUCT.share()
}
#[pg_extern]
fn struct_set(value1: i32, value2: i32) {
*STRUCT.exclusive() = Pgtest { value1, value2 };
}
#[pg_extern]
fn primitive_get() -> i32 {
*PRIMITIVE.share()
}
#[pg_extern]
fn primitive_set(value: i32) {
*PRIMITIVE.exclusive() = value;
}
#[pg_extern]
fn atomic_get() -> bool {
ATOMIC.get().load(Ordering::Relaxed)
}
#[pg_extern]
fn atomic_set(value: bool) -> bool {
ATOMIC.get().swap(value, Ordering::Relaxed)
}
#[cfg(test)]
pub mod pg_test {
pub fn setup(_options: Vec<&str>) {}
pub fn postgresql_conf_options() -> Vec<&'static str> {
vec!["shared_preload_libraries='shmem'"]
}
}