-
-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shared memory hash table #1379
Open
levkk
wants to merge
15
commits into
pgcentralfoundation:develop
Choose a base branch
from
levkk:levkk-shared-mem-hash-table
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Shared memory hash table #1379
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
926201c
htab
levkk afb25ab
hmmm
levkk 31ddf43
wah
levkk edd3300
clean
levkk f47615a
alright!
levkk b69a6dc
alright!
levkk a168537
comment
levkk 8ccbd74
tests
levkk 6e088de
alright!
levkk f490b99
remove
levkk fe95ef3
finish up hashmap
levkk cd9d6cc
comment
levkk 34760d2
Feature flag
levkk 87558bc
Almost!
levkk e077414
Address comments
levkk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,22 +11,35 @@ use pgrx::prelude::*; | |||||
use pgrx::{pg_shmem_init, PgAtomic, PgLwLock, PgSharedMemoryInitialization}; | ||||||
use std::sync::atomic::AtomicBool; | ||||||
|
||||||
#[cfg(feature = "cshim")] | ||||||
use pgrx::PgHashMap; | ||||||
|
||||||
static ATOMIC: PgAtomic<AtomicBool> = PgAtomic::new(); | ||||||
static LWLOCK: PgLwLock<bool> = PgLwLock::new(); | ||||||
|
||||||
#[cfg(feature = "cshim")] | ||||||
static HASH_MAP: PgHashMap<i64, i64> = PgHashMap::new(500); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
#[pg_guard] | ||||||
pub extern "C" fn _PG_init() { | ||||||
// This ensures that this functionality works across PostgreSQL versions | ||||||
pg_shmem_init!(ATOMIC); | ||||||
pg_shmem_init!(LWLOCK); | ||||||
|
||||||
#[cfg(feature = "cshim")] | ||||||
pg_shmem_init!(HASH_MAP); | ||||||
} | ||||||
|
||||||
#[cfg(any(test, feature = "pg_test"))] | ||||||
#[pgrx::pg_schema] | ||||||
mod tests { | ||||||
#[allow(unused_imports)] | ||||||
use crate as pgrx_tests; | ||||||
|
||||||
#[cfg(feature = "cshim")] | ||||||
use crate::tests::shmem_tests::HASH_MAP; | ||||||
use crate::tests::shmem_tests::LWLOCK; | ||||||
|
||||||
use pgrx::prelude::*; | ||||||
|
||||||
#[pg_test] | ||||||
|
@@ -53,4 +66,55 @@ mod tests { | |||||
}); | ||||||
let _lock = LWLOCK.exclusive(); | ||||||
} | ||||||
|
||||||
#[cfg(feature = "cshim")] | ||||||
#[pg_test] | ||||||
pub fn test_pg_hash_map() { | ||||||
use rand::prelude::IteratorRandom; | ||||||
|
||||||
for i in 1..250 { | ||||||
assert_eq!(HASH_MAP.insert(i, i), Ok(None)); | ||||||
} | ||||||
|
||||||
assert_eq!(HASH_MAP.len(), 249); | ||||||
|
||||||
for i in 1..250 { | ||||||
assert_eq!(HASH_MAP.get(i), Some(i)); | ||||||
} | ||||||
|
||||||
assert_eq!(HASH_MAP.len(), 249); | ||||||
|
||||||
for i in 251..500 { | ||||||
assert_eq!(HASH_MAP.get(i), None); | ||||||
} | ||||||
|
||||||
assert_eq!(HASH_MAP.len(), 249); | ||||||
|
||||||
for i in 1..250 { | ||||||
assert_eq!(HASH_MAP.insert(i, i), Ok(Some(i))); | ||||||
} | ||||||
|
||||||
assert_eq!(HASH_MAP.len(), 249); | ||||||
|
||||||
for i in 1..250 { | ||||||
assert_eq!(HASH_MAP.remove(i), Some(i)); | ||||||
} | ||||||
|
||||||
assert_eq!(HASH_MAP.len(), 0); | ||||||
|
||||||
for i in 1..250 { | ||||||
assert_eq!(HASH_MAP.get(i), None); | ||||||
} | ||||||
|
||||||
assert_eq!(HASH_MAP.len(), 0); | ||||||
|
||||||
for _ in 0..25_000 { | ||||||
for key in 0..250 { | ||||||
let value = (0..1000).choose(&mut rand::thread_rng()).unwrap(); | ||||||
assert!(HASH_MAP.insert(key, value).is_ok()); | ||||||
} | ||||||
} | ||||||
|
||||||
assert_eq!(HASH_MAP.len(), 250); | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.