linkrs_core/
session_stats.rs1use std::sync::atomic::{AtomicU64, Ordering};
7
8#[derive(Debug)]
16pub struct SessionStatistics {
17 last_changes: AtomicU64,
19 total_changes: AtomicU64,
21 last_insert_vertex_id: AtomicU64,
23 last_insert_edge_id: AtomicU64,
25 has_vertex_id: AtomicU64,
27 has_edge_id: AtomicU64,
29}
30
31impl SessionStatistics {
32 pub fn new() -> Self {
34 Self {
35 last_changes: AtomicU64::new(0),
36 total_changes: AtomicU64::new(0),
37 last_insert_vertex_id: AtomicU64::new(0),
38 last_insert_edge_id: AtomicU64::new(0),
39 has_vertex_id: AtomicU64::new(0),
40 has_edge_id: AtomicU64::new(0),
41 }
42 }
43
44 pub fn record_changes(&self, count: u64) {
49 self.last_changes.store(count, Ordering::SeqCst);
50 self.total_changes.fetch_add(count, Ordering::SeqCst);
51 }
52
53 pub fn record_vertex_insert(&self, id: u64) {
58 if id > 0 {
59 self.last_insert_vertex_id.store(id, Ordering::SeqCst);
60 self.has_vertex_id.store(1, Ordering::SeqCst);
61 self.last_changes.store(1, Ordering::SeqCst);
62 self.total_changes.fetch_add(1, Ordering::SeqCst);
63 }
64 }
65
66 pub fn record_edge_insert(&self, id: u64) {
71 if id > 0 {
72 self.last_insert_edge_id.store(id, Ordering::SeqCst);
73 self.has_edge_id.store(1, Ordering::SeqCst);
74 self.last_changes.store(1, Ordering::SeqCst);
75 self.total_changes.fetch_add(1, Ordering::SeqCst);
76 }
77 }
78
79 pub fn last_changes(&self) -> u64 {
81 self.last_changes.load(Ordering::SeqCst)
82 }
83
84 pub fn total_changes(&self) -> u64 {
86 self.total_changes.load(Ordering::SeqCst)
87 }
88
89 pub fn last_insert_vertex_id(&self) -> Option<u64> {
93 if self.has_vertex_id.load(Ordering::SeqCst) != 0 {
94 Some(self.last_insert_vertex_id.load(Ordering::SeqCst))
95 } else {
96 None
97 }
98 }
99
100 pub fn last_insert_edge_id(&self) -> Option<u64> {
104 if self.has_edge_id.load(Ordering::SeqCst) != 0 {
105 Some(self.last_insert_edge_id.load(Ordering::SeqCst))
106 } else {
107 None
108 }
109 }
110
111 pub fn reset_last(&self) {
115 self.last_changes.store(0, Ordering::SeqCst);
116 self.has_vertex_id.store(0, Ordering::SeqCst);
117 self.has_edge_id.store(0, Ordering::SeqCst);
118 }
119
120 pub fn reset_all(&self) {
122 self.last_changes.store(0, Ordering::SeqCst);
123 self.total_changes.store(0, Ordering::SeqCst);
124 self.last_insert_vertex_id.store(0, Ordering::SeqCst);
125 self.last_insert_edge_id.store(0, Ordering::SeqCst);
126 self.has_vertex_id.store(0, Ordering::SeqCst);
127 self.has_edge_id.store(0, Ordering::SeqCst);
128 }
129}
130
131impl Default for SessionStatistics {
132 fn default() -> Self {
133 Self::new()
134 }
135}
136
137impl Clone for SessionStatistics {
138 fn clone(&self) -> Self {
139 Self {
140 last_changes: AtomicU64::new(self.last_changes.load(Ordering::SeqCst)),
141 total_changes: AtomicU64::new(self.total_changes.load(Ordering::SeqCst)),
142 last_insert_vertex_id: AtomicU64::new(
143 self.last_insert_vertex_id.load(Ordering::SeqCst),
144 ),
145 last_insert_edge_id: AtomicU64::new(self.last_insert_edge_id.load(Ordering::SeqCst)),
146 has_vertex_id: AtomicU64::new(self.has_vertex_id.load(Ordering::SeqCst)),
147 has_edge_id: AtomicU64::new(self.has_edge_id.load(Ordering::SeqCst)),
148 }
149 }
150}
151
152#[cfg(test)]
153mod tests {
154 use super::*;
155
156 #[test]
157 fn test_session_statistics_basic() {
158 let stats = SessionStatistics::new();
159
160 assert_eq!(stats.last_changes(), 0);
161 assert_eq!(stats.total_changes(), 0);
162 assert_eq!(stats.last_insert_vertex_id(), None);
163 assert_eq!(stats.last_insert_edge_id(), None);
164 }
165
166 #[test]
167 fn test_record_changes() {
168 let stats = SessionStatistics::new();
169
170 stats.record_changes(5);
171 assert_eq!(stats.last_changes(), 5);
172 assert_eq!(stats.total_changes(), 5);
173
174 stats.record_changes(3);
175 assert_eq!(stats.last_changes(), 3);
176 assert_eq!(stats.total_changes(), 8);
177 }
178
179 #[test]
180 fn test_record_vertex_insert() {
181 let stats = SessionStatistics::new();
182
183 stats.record_vertex_insert(100);
184 assert_eq!(stats.last_insert_vertex_id(), Some(100));
185 assert_eq!(stats.last_changes(), 1);
186 assert_eq!(stats.total_changes(), 1);
187
188 stats.record_vertex_insert(0);
189 assert_eq!(stats.last_insert_vertex_id(), Some(100));
190 }
191
192 #[test]
193 fn test_record_edge_insert() {
194 let stats = SessionStatistics::new();
195
196 stats.record_edge_insert(200);
197 assert_eq!(stats.last_insert_edge_id(), Some(200));
198 assert_eq!(stats.last_changes(), 1);
199 assert_eq!(stats.total_changes(), 1);
200 }
201
202 #[test]
203 fn test_reset() {
204 let stats = SessionStatistics::new();
205
206 stats.record_changes(5);
207 stats.record_vertex_insert(100);
208
209 stats.reset_last();
210 assert_eq!(stats.last_changes(), 0);
211 assert_eq!(stats.last_insert_vertex_id(), None);
212 assert_eq!(stats.total_changes(), 6);
213
214 stats.reset_all();
215 assert_eq!(stats.total_changes(), 0);
216 }
217}