Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
in progress
  • Loading branch information
am357 committed Aug 14, 2024
commit f8e7773b11b2b093e06a34c1de57bf9a1cd25de8
3 changes: 2 additions & 1 deletion partiql-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ default = []
serde = [
"dep:serde",
"indexmap/serde",
"smallvec/serde"
"smallvec/serde",
"dashmap/serde"
]
88 changes: 14 additions & 74 deletions partiql-common/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,15 @@
use dashmap::DashMap;
use indexmap::IndexMap;
use std::hash::Hash;
use std::sync::{Arc, MappedRwLockReadGuard, Mutex, RwLock, RwLockReadGuard};
use std::sync::{Arc, RwLock};

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

// #[derive(Debug, Clone)]
// pub struct NodeMap<T> {
// map: Arc<RwLock<IndexMap<NodeId, T>>>,
// }
//
// impl<T> Default for NodeMap<T> {
// fn default() -> Self {
// Self::new()
// }
// }
//
// impl<T> NodeMap<T> {
// pub fn new() -> Self {
// NodeMap {
// map: Arc::new(RwLock::new(IndexMap::new())),
// }
// }
//
// pub fn with_capacity(capacity: usize) -> Self {
// NodeMap {
// map: Arc::new(RwLock::new(IndexMap::with_capacity(capacity))),
// }
// }
//
// pub fn insert(&self, node_id: NodeId, value: T) -> Option<T> {
// let mut map = self.map.write().expect("NodeMap write lock");
// map.insert(node_id, value)
// }
//
// // pub fn get(&self, node_id: &NodeId) -> Option<T>
// // where
// // T: Clone,
// // {
// // let map = self.map.read().expect("NodeMap read lock");
// // map.get(node_id).cloned()
// // }
//
// pub fn get(&self, node_id: &NodeId) -> Option<MappedRwLockReadGuard<Option<&T>>> {
// let map = self.map.read().unwrap(); // Acquire a read lock
// Some(RwLockReadGuard::map(map, |m| m.get(node_id)))
// }
// }

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct NodeMap<T> {
map: Arc<DashMap<NodeId, T>>, // DashMap for thread-safe key-value storage
order: Arc<Mutex<Vec<NodeId>>>, // Mutex-protected Vec for maintaining insertion order
map: DashMap<NodeId, T>,
#[cfg_attr(feature = "serde", serde(skip))]
order: Arc<RwLock<Vec<NodeId>>>,
}

impl<T> Default for NodeMap<T> {
Expand All @@ -63,49 +19,33 @@ impl<T> Default for NodeMap<T> {
}

impl<T> NodeMap<T> {
// Constructor to create a new, empty NodeMap
pub fn new() -> Self {
NodeMap {
map: Arc::new(DashMap::new()),
order: Arc::new(Mutex::new(Vec::new())),
map: DashMap::new(),
order: Arc::new(RwLock::new(Vec::new())),
}
}

// Constructor to create a NodeMap with a specified capacity
pub fn with_capacity(capacity: usize) -> Self {
NodeMap {
map: Arc::new(DashMap::with_capacity(capacity)),
order: Arc::new(Mutex::new(Vec::with_capacity(capacity))),
map: DashMap::with_capacity(capacity),
order: Arc::new(RwLock::new(Vec::with_capacity(capacity))),
}
}

// The insert method to add a new key-value pair to the map
pub fn insert(&self, node_id: NodeId, value: T) -> Option<T> {
let mut order = self.order.lock().unwrap(); // Acquire a lock to modify the order
if !self.map.contains_key(&node_id) {
order.push(node_id); // Only add to order if it's a new key
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)
}
self.map.insert(node_id, value)
}

// The get method to retrieve a reference to a value by its NodeId
pub fn get(&self, node_id: &NodeId) -> Option<dashmap::mapref::one::Ref<'_, NodeId, T>> {
self.map.get(node_id)
}

// The unwrap_or method to get a value or return a default if not found
pub fn unwrap_or(&self, node_id: &NodeId, default: T) -> T
where
T: Clone,
{
self.map.get(node_id).map(|r| r.clone()).unwrap_or(default)
}

// Method to retrieve the keys in insertion order
pub fn keys_in_order(&self) -> Vec<NodeId> {
let order = self.order.lock().unwrap(); // Acquire a lock to read the order
order.clone() // Return a cloned version of the order
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
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
8 changes: 4 additions & 4 deletions partiql-parser/src/parse/partiql.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,10 @@ 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 start = state.locations.unwrap_or(&start_id, total.clone()).start.0.clone();
// let end = state.locations.get(&end_id).unwrap_or(&total).end.0.clone();
let end = state.locations.unwrap_or(&end_id, total.clone()).end.0.clone();
let start = state.locations.get(&start_id).map_or(total.start.0.clone(), |v| v.start.0.clone());
// let start = state.locations.get_or(&start_id, &total).start.0.clone();
let end = state.locations.get(&end_id).map_or(total.end.0.clone(), |v| v.end.0.clone());
// let end = state.locations.get_or(&end_id, &total).end.0.clone();
let range = start..end;
let join = state.node(ast::Join {
kind: ast::JoinKind::Cross,
Expand Down
2 changes: 1 addition & 1 deletion partiql-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use derivative::Derivative;
use indexmap::IndexSet;
use itertools::Itertools;
use miette::Diagnostic;
use partiql_common::node::{AutoNodeIdGenerator, NodeId, NodeIdGenerator, NodeMap};
use partiql_common::node::{AutoNodeIdGenerator, NodeId, NodeIdGenerator};
use std::collections::HashMap;
use std::fmt::{Debug, Display, Formatter};
use std::hash::{Hash, Hasher};
Expand Down