Skip to content
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

Make NodeMap Thread-safe #487

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion partiql-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ pretty = "0.12"
serde = { version = "1.*", features = ["derive"], optional = true }
smallvec = { version = "1.*" }
thiserror = "1.0"
dashmap = "6.0"

[features]
default = []
serde = [
"dep:serde",
"indexmap/serde",
"smallvec/serde"
"smallvec/serde",
"dashmap/serde"
]
47 changes: 44 additions & 3 deletions partiql-common/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,52 @@
use indexmap::IndexMap;
use std::hash::Hash;
use dashmap::DashMap;
use std::sync::{Arc, RwLock};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

pub type NodeMap<T> = IndexMap<NodeId, T>;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct NodeMap<T> {
map: DashMap<NodeId, T>,
#[cfg_attr(feature = "serde", serde(skip))]
order: Arc<RwLock<Vec<NodeId>>>,
}

impl<T> Default for NodeMap<T> {
fn default() -> Self {
Self::new()
}
}

impl<T> NodeMap<T> {
pub fn new() -> Self {
NodeMap {
map: DashMap::new(),
order: Arc::new(RwLock::new(Vec::new())),
}
}

pub fn with_capacity(capacity: usize) -> Self {
NodeMap {
map: DashMap::with_capacity(capacity),
order: Arc::new(RwLock::new(Vec::with_capacity(capacity))),
}
}

pub fn insert(&self, node_id: NodeId, value: T) -> Option<T> {
let mut order = self.order.write().expect("NodeMap order write lock");
if self.map.contains_key(&node_id) {
self.map.insert(node_id, value)
} else {
order.push(node_id);
self.map.insert(node_id, value)
}
}

pub fn get(&self, node_id: &NodeId) -> Option<dashmap::mapref::one::Ref<'_, NodeId, T>> {
self.map.get(node_id)
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down
3 changes: 3 additions & 0 deletions partiql-eval/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ delegate = "0.12"
[dev-dependencies]
criterion = "0.4"

[features]
default = []

[[bench]]
name = "bench_eval"
harness = false
1 change: 0 additions & 1 deletion partiql-eval/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ pub type EvalResult = Result<Evaluated, EvalErr>;
/// Represents result of evaluation as an evaluated entity.
#[non_exhaustive]
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Evaluated {
pub result: Value,
}
Expand Down
1 change: 1 addition & 0 deletions partiql-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ bigdecimal = "~0.2.0"
rust_decimal = { version = "1.25.0", default-features = false, features = ["std"] }

bitflags = "2"
dashmap = "6.0.1"

lalrpop-util = "0.20"
logos = "0.12"
Expand Down
4 changes: 2 additions & 2 deletions partiql-parser/src/parse/partiql.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ FromClause: ast::AstNode<ast::FromClause> = {
ast::FromSource::Join(node) => node.id,
};

let start = state.locations.get(&start_id).unwrap_or(&total).start.0.clone();
let end = state.locations.get(&end_id).unwrap_or(&total).end.0.clone();
let start = state.locations.get(&start_id).map_or(total.start.0.clone(), |v| v.start.0.clone());
let end = state.locations.get(&end_id).map_or(total.end.0.clone(), |v| v.end.0.clone());
let range = start..end;
let join = state.node(ast::Join {
kind: ast::JoinKind::Cross,
Expand Down
Loading