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
|
use anyhow::Result;
use crossterm::event::{self, Event, KeyCode, KeyModifiers};
use std::{
sync::{
atomic::{AtomicBool, AtomicU64, Ordering},
Arc,
},
time::Duration,
};
pub type AbortSignal = Arc<AbortSignalInner>;
#[derive(Debug, Default)]
pub struct QueryIdGen {
counter: AtomicU64,
parents: parking_lot::RwLock<std::collections::HashMap<String, u64>>,
}
impl QueryIdGen {
pub fn root_id(&self) -> u64 {
self.counter.fetch_add(1, Ordering::SeqCst)
}
pub fn child_id(&self, parent_path: &[u64]) -> u64 {
if parent_path.is_empty() {
return self.root_id();
}
let key = parent_path
.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join("/");
let mut parents = self.parents.write();
let entry = parents.entry(key).or_insert(0);
let id = *entry;
*entry += 1;
id
}
}
pub fn format_query_path(path: &[u64]) -> String {
if path.is_empty() {
return String::new();
}
format!(
"[{}]",
path.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join("/")
)
}
tokio::task_local! {
static QUERY_SCOPE: String;
static QUERY_ID_GEN: QueryIdGen;
}
pub fn current_query_scope() -> Option<String> {
QUERY_SCOPE
.try_with(|v| if v.is_empty() { None } else { Some(v.clone()) })
.ok()
.flatten()
}
pub async fn with_query_scope<F: std::future::Future>(scope: String, future: F) -> F::Output {
QUERY_SCOPE.scope(scope, future).await
}
pub fn next_query_id(parent_path: &[u64]) -> u64 {
if parent_path.is_empty() {
return QUERY_ID_GEN.try_with(|g| g.root_id()).unwrap_or(0);
}
QUERY_ID_GEN
.try_with(|g| g.child_id(parent_path))
.unwrap_or(0)
}
pub struct AbortSignalInner {
ctrlc: AtomicBool,
ctrld: AtomicBool,
}
pub fn create_abort_signal() -> AbortSignal {
AbortSignalInner::new()
}
impl AbortSignalInner {
pub fn new() -> AbortSignal {
Arc::new(Self {
ctrlc: AtomicBool::new(false),
ctrld: AtomicBool::new(false),
})
}
pub fn aborted(&self) -> bool {
if self.aborted_ctrlc() {
return true;
}
if self.aborted_ctrld() {
return true;
}
false
}
pub fn aborted_ctrlc(&self) -> bool {
self.ctrlc.load(Ordering::SeqCst)
}
pub fn aborted_ctrld(&self) -> bool {
self.ctrld.load(Ordering::SeqCst)
}
pub fn reset(&self) {
self.ctrlc.store(false, Ordering::SeqCst);
self.ctrld.store(false, Ordering::SeqCst);
}
pub fn set_ctrlc(&self) {
self.ctrlc.store(true, Ordering::SeqCst);
}
pub fn set_ctrld(&self) {
self.ctrld.store(true, Ordering::SeqCst);
}
}
pub async fn wait_abort_signal(abort_signal: &AbortSignal) {
loop {
if abort_signal.aborted() {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(25)).await;
}
}
pub fn poll_abort_signal(abort_signal: &AbortSignal) -> Result<bool> {
if crossterm::event::poll(Duration::from_millis(25))? {
if let Event::Key(key) = event::read()? {
match key.code {
KeyCode::Char('c') if key.modifiers == KeyModifiers::CONTROL => {
abort_signal.set_ctrlc();
return Ok(true);
}
KeyCode::Char('d') if key.modifiers == KeyModifiers::CONTROL => {
abort_signal.set_ctrld();
return Ok(true);
}
_ => {}
}
}
}
Ok(false)
}
|