Skip to content

Commit

Permalink
Merge pull request #1170 from feikesteenbergen/feike/support_dequeue
Browse files Browse the repository at this point in the history
Implement PGRXSharedMemory for Deque
  • Loading branch information
eeeebbbbrrrr authored Jul 6, 2023
2 parents b84eee1 + 4465502 commit dbc9949
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pgrx-examples/shmem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl Default for Pgtest {
}
unsafe impl PGRXSharedMemory for Pgtest {}

static DEQUE: PgLwLock<heapless::Deque<Pgtest, 400>> = PgLwLock::new();
static VEC: PgLwLock<heapless::Vec<Pgtest, 400>> = PgLwLock::new();
static HASH: PgLwLock<heapless::FnvIndexMap<i32, i32, 4>> = PgLwLock::new();
static STRUCT: PgLwLock<Pgtest> = PgLwLock::new();
Expand All @@ -40,6 +41,7 @@ static ATOMIC: PgAtomic<std::sync::atomic::AtomicBool> = PgAtomic::new();

#[pg_guard]
pub extern "C" fn _PG_init() {
pg_shmem_init!(DEQUE);
pg_shmem_init!(VEC);
pg_shmem_init!(HASH);
pg_shmem_init!(STRUCT);
Expand Down Expand Up @@ -75,6 +77,50 @@ fn vec_pop() -> Option<Pgtest> {
VEC.exclusive().pop()
}

#[pg_extern]
fn deque_select() -> SetOfIterator<'static, Pgtest> {
SetOfIterator::new(DEQUE.share().iter().map(|i| *i).collect::<Vec<Pgtest>>().into_iter())
}

#[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().map(|i| *i).collect::<Vec<Pgtest>>();
vec.clear();
SetOfIterator::new(r.into_iter())
}

#[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();
Expand Down
1 change: 1 addition & 0 deletions pgrx/src/shmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ where
{
}
unsafe impl<T, const N: usize> PGRXSharedMemory for heapless::Vec<T, N> {}
unsafe impl<T, const N: usize> PGRXSharedMemory for heapless::Deque<T, N> {}
unsafe impl<K: Eq + Hash, V: Default, S, const N: usize> PGRXSharedMemory
for heapless::IndexMap<K, V, S, N>
{
Expand Down

0 comments on commit dbc9949

Please sign in to comment.