arthas 0.3.0

Arthas is an in-memory structure database.
Documentation
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713

use std::sync::atomic::Ordering;
use std::sync::atomic::AtomicBool;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::RwLock;
use std::fmt;
use serde_json::Value;
use item::{Id, FieldInt};
use super::comparision::Comparision;
use super::action::SearchAction;
use super::rc::RcChild;
use super::math::Math;
use super::rc::DataType;
use super::rc::RcNode;
use super::searcher::task::Sub;


pub type Group = Arc<RwLock<HashMap<Id, RcChild>>>;
pub type Groups = Vec<Group>;


#[derive(Clone)]
pub struct Node {
    pub _type: DataType,
    pub self_rc: Option<RcNode>,
    pub group: Group,
    pub parent: Option<RcNode>,
    pub left: Option<RcNode>,
    pub right: Option<RcNode>,
}

impl Node {
    pub fn new(id: Id, rc_child: RcChild) -> Node {
        let _type = rc_child.read().unwrap().get_type();

        let mut map = HashMap::new();
        map.insert(id, rc_child);

        Node {
            _type: _type,
            self_rc: None,
            group: Arc::new(RwLock::new(map)),
            parent: None,
            left: None,
            right: None,
        }
    }

    pub fn insert(&mut self,
                  field_int: FieldInt,
                  id: Id,
                  rc_child: RcChild,
                  is_min: &mut bool,
                  is_max: &mut bool)
                  -> Option<RcNode> {
        match self._type {
            DataType::Number | DataType::String => {
                if Math::gt(&rc_child.read().unwrap().get_value(), &self.get_value()) {
                    if *is_min {
                        thread_trace!("found gt, cancel min");
                        *is_min = false;
                    }

                    if self.right.is_some() {
                        thread_trace!("continue insert right");
                        self.insert_continue_right(field_int, id, rc_child, is_min, is_max)
                    } else {
                        thread_trace!("set right node");
                        self.set_right(field_int, id, rc_child, is_max)
                    }
                } else if Math::lt(&rc_child.read().unwrap().get_value(), &self.get_value()) {
                    if *is_max {
                        thread_trace!("found lt, cancel max");
                        *is_max = false;
                    }

                    if self.left.is_some() {
                        thread_trace!("continue insert left");
                        self.insert_continue_left(field_int, id, rc_child, is_min, is_max)
                    } else {
                        thread_trace!("set left node");
                        self.set_left(field_int, id, rc_child, is_min)
                    }
                } else {
                    thread_trace!("found group, insert to current");
                    self.insert_to_current(field_int, id, rc_child);
                    *is_min = false;
                    *is_max = false;
                    None
                }
            }
            _ => unreachable!(),
        }
    }

    fn insert_to_current(&mut self, field_int: FieldInt, id: Id, rc_child: RcChild) {
        {
            let child = rc_child.read().unwrap();
            child.item
                .write()
                .unwrap()
                .nodes
                .insert(field_int, self.self_rc.clone().unwrap());
        }

        self.group.write().unwrap().insert(id, rc_child);
    }

    fn insert_continue_right(&mut self,
                             field_int: FieldInt,
                             id: Id,
                             rc_child: RcChild,
                             is_min: &mut bool,
                             is_max: &mut bool)
                             -> Option<RcNode> {
        self.right
            .as_mut()
            .unwrap()
            .write()
            .unwrap()
            .insert(field_int, id, rc_child, is_min, is_max)
    }

    fn insert_continue_left(&mut self,
                            field_int: FieldInt,
                            id: Id,
                            rc_child: RcChild,
                            is_min: &mut bool,
                            is_max: &mut bool)
                            -> Option<RcNode> {
        self.left
            .as_mut()
            .unwrap()
            .write()
            .unwrap()
            .insert(field_int, id, rc_child, is_min, is_max)
    }

    fn set_left(&mut self,
                field_int: FieldInt,
                id: Id,
                rc_child: RcChild,
                is_min: &mut bool)
                -> Option<RcNode> {
        let rc_node = RcNode::new(id, rc_child.clone());
        let child = rc_child.read()
            .unwrap();
        child.item
            .write()
            .unwrap()
            .nodes
            .insert(field_int, rc_node.clone());

        {
            let mut node = rc_node.write().unwrap();
            node.parent = self.self_rc.clone();
            node.self_rc = Some(rc_node.clone());
        }

        self.left = Some(rc_node.clone());

        if *is_min { Some(rc_node) } else { None }
    }

    fn set_right(&mut self,
                 field_int: FieldInt,
                 id: Id,
                 rc_child: RcChild,
                 is_max: &mut bool)
                 -> Option<RcNode> {
        let rc_node = RcNode::new(id, rc_child.clone());
        let child = rc_child.read().unwrap();
        child.item
            .write()
            .unwrap()
            .nodes
            .insert(field_int, rc_node.clone());

        {
            let mut node = rc_node.write().unwrap();
            node.parent = self.self_rc.clone();
            node.self_rc = Some(rc_node.clone());
        }

        self.right = Some(rc_node.clone());

        if *is_max { Some(rc_node) } else { None }
    }

    pub fn search_root(&self, stopped: &AtomicBool, groups: &mut Groups, sub: &mut Sub) {
        let action_option = self.search_current(stopped, groups, sub);
        if action_option.is_some() {
            self.go_right_or_go_left(action_option.unwrap(), stopped, groups, sub);
        }
    }

    fn search_max_root(&self, stopped: &AtomicBool, groups: &mut Groups, sub: &mut Sub) {
        let action_option = self.search_max_current(stopped, groups, sub);
        if action_option.is_some() {
            self.search_min_or_max_go_right_or_go_left(action_option.unwrap(),
                                                       stopped,
                                                       groups,
                                                       sub);
        }
    }

    fn search_min_root(&self, stopped: &AtomicBool, groups: &mut Groups, sub: &mut Sub) {
        let action_option = self.search_min_current(stopped, groups, sub);
        if action_option.is_some() {
            self.search_min_or_max_go_right_or_go_left(action_option.unwrap(),
                                                       stopped,
                                                       groups,
                                                       sub);
        }
    }

    pub fn search_max(&self, stopped: &AtomicBool, groups: &mut Groups, sub: &mut Sub) {
        let action_option = self.search_max_current(stopped, groups, sub);
        if action_option.is_some() {
            let action = action_option.unwrap();

            let mut should_stop = true;

            if !action.fold_left && action.go_left {
                if self.left.is_some() {
                    self.go_left(stopped, groups, sub);
                }

                if self.parent.is_some() {
                    should_stop = false;
                    self.go_top_max(stopped, groups, sub);
                }
            }

            if should_stop {
                return stop(stopped, sub);
            }
        } else if !stopped.load(Ordering::SeqCst) {
            return stop(stopped, sub);
        }
    }

    pub fn search_min(&self, stopped: &AtomicBool, groups: &mut Groups, sub: &mut Sub) {
        let action_option = self.search_min_current(stopped, groups, sub);
        if action_option.is_some() {
            let action = action_option.unwrap();

            let mut should_stop = true;

            if !action.fold_right && action.go_right {
                if self.right.is_some() {
                    self.go_right(stopped, groups, sub);
                }

                if self.parent.is_some() {
                    should_stop = false;
                    self.go_top_min(stopped, groups, sub);
                }
            }

            if should_stop && !stopped.load(Ordering::SeqCst) {
                return stop(stopped, sub);
            }
        } else if !stopped.load(Ordering::SeqCst) {
            return stop(stopped, sub);
        }
    }

    fn go_right(&self, stopped: &AtomicBool, groups: &mut Groups, sub: &mut Sub) {
        thread_trace!("search min, go right");
        self.right.as_ref().unwrap().read().unwrap().search_min_root(stopped, groups, sub);
    }

    fn go_left(&self, stopped: &AtomicBool, groups: &mut Groups, sub: &mut Sub) {
        thread_trace!("search max, go left");
        self.left.as_ref().unwrap().read().unwrap().search_max_root(stopped, groups, sub);
    }

    fn go_top_max(&self, stopped: &AtomicBool, groups: &mut Groups, sub: &mut Sub) {
        thread_trace!("search max, go top");
        self.parent.as_ref().unwrap().read().unwrap().search_max(stopped, groups, sub);
    }

    fn go_top_min(&self, stopped: &AtomicBool, groups: &mut Groups, sub: &mut Sub) {
        thread_trace!("search min, go top");
        self.parent.as_ref().unwrap().read().unwrap().search_min(stopped, groups, sub);
    }

    fn search_min_current(&self,
                          stopped: &AtomicBool,
                          groups: &mut Groups,
                          sub: &mut Sub)
                          -> Option<SearchAction> {
        if stopped.load(Ordering::SeqCst) {
            thread_trace!("search min current, other threads had stopped, return none");
            return None;
        }

        let action_option = self.compare_self(&sub.comparisions);
        if action_option.is_none() {
            thread_trace!("search min current, action is none, return none");
            return None;
        }

        let action = action_option.unwrap();
        self.fold_min_action(&action, groups);

        if action.is_stopped() {
            thread_trace!("search min current, action is stopped, return none");
            return None;
        }

        thread_trace!("search min current, found some action: {:?}", action);
        Some(action)
    }

    fn search_max_current(&self,
                          stopped: &AtomicBool,
                          groups: &mut Groups,
                          sub: &mut Sub)
                          -> Option<SearchAction> {
        if stopped.load(Ordering::SeqCst) {
            thread_trace!("search min current, other threads had stopped, return none");
            return None;
        }

        let action_option = self.compare_self(&sub.comparisions);
        if action_option.is_none() {
            thread_trace!("search min current, action is none, return none");
            return None;
        }

        let action = action_option.unwrap();
        self.fold_max_action(&action, groups);

        if action.is_stopped() {
            thread_trace!("search min current, action is stopped, return none");
            return None;
        }

        thread_trace!("search min current, found some action: {:?}", action);
        Some(action)
    }

    fn search_current(&self,
                      stopped: &AtomicBool,
                      groups: &mut Groups,
                      sub: &mut Sub)
                      -> Option<SearchAction> {
        if stopped.load(Ordering::SeqCst) {
            thread_trace!("other threads had stopped, stopping current.");
            return None;
        }

        let action_option = self.compare_self(&sub.comparisions);
        if action_option.is_none() {
            stop(stopped, sub);
            thread_trace!("action is none, stopping sub.");
            return None;
        }

        let action = action_option.unwrap();
        self.fold_action(&action, groups);

        if action.is_stopped() {
            stop(stopped, sub);
            thread_trace!("action is stopped, stopping sub.");
            return None;
        }

        thread_trace!("found some action: {:?}", action);
        Some(action)
    }

    fn search_min_or_max_go_right_or_go_left(&self,
                                             action: SearchAction,
                                             stopped: &AtomicBool,
                                             groups: &mut Groups,
                                             sub: &mut Sub) {
        if action.go_right && self.right.is_some() {
            self.right
                .as_ref()
                .unwrap()
                .read()
                .unwrap()
                .search_min_root(stopped, groups, sub);
        }

        if action.go_left && self.left.is_some() {
            self.left
                .as_ref()
                .unwrap()
                .read()
                .unwrap()
                .search_min_root(stopped, groups, sub);

        }
    }

    fn go_right_or_go_left(&self,
                           action: SearchAction,
                           stopped: &AtomicBool,
                           groups: &mut Groups,
                           sub: &mut Sub) {
        let mut no_right_go = true;
        let mut no_left_go = true;

        if action.go_right && self.right.is_some() {
            no_right_go = false;
            self.right
                .as_ref()
                .unwrap()
                .read()
                .unwrap()
                .search_root(stopped, groups, sub);
        }

        if action.go_left && self.left.is_some() {
            no_left_go = false;
            self.left
                .as_ref()
                .unwrap()
                .read()
                .unwrap()
                .search_root(stopped, groups, sub);

        }

        if no_left_go && no_right_go {
            return stop(stopped, sub);
        }
    }

    fn fold_min_action(&self, action: &SearchAction, groups: &mut Groups) {
        if action.take {
            thread_trace!("fold min action, take current childs: {:?}",
                          &*self.group.read().unwrap());

            self.append_to(groups);
        }

        if action.fold_right {
            thread_trace!("fold min action, fold right");
            self.fold_right_top(groups);
        }
    }

    fn fold_max_action(&self, action: &SearchAction, groups: &mut Groups) {
        if action.take {
            thread_trace!("fold max action, take current childs: {:?}",
                          &*self.group.read().unwrap());

            self.append_to(groups);
        }

        if action.fold_left {
            thread_trace!("fold max action, fold left");
            self.fold_left_top(groups);
        }
    }

    fn fold_action(&self, action: &SearchAction, groups: &mut Groups) {
        if action.take {
            thread_trace!("fold action, take current childs: {:?}",
                          &*self.group.read().unwrap());

            self.append_to(groups);
        }

        if action.fold_right {
            thread_trace!("fold right");
            self.fold_right_to(groups);
        }

        if action.fold_left {
            thread_trace!("fold left");
            self.fold_left_to(groups);
        }
    }

    fn fold_left_top(&self, groups: &mut Groups) {
        self.fold_left_to(groups);

        if self.parent.is_some() {
            self.parent.as_ref().unwrap().read().unwrap().fold_current_left(groups);
        }
    }

    fn fold_right_top(&self, groups: &mut Groups) {
        self.fold_right_to(groups);

        if self.parent.is_some() {
            self.parent.as_ref().unwrap().read().unwrap().fold_current_right(groups);
        }
    }

    fn fold_current_left(&self, groups: &mut Groups) {
        self.append_to(groups);
        self.fold_left_to(groups);
    }

    fn fold_current_right(&self, groups: &mut Groups) {
        self.append_to(groups);
        self.fold_right_to(groups);
    }

    fn fold_left_to(&self, groups: &mut Groups) {
        if self.left.is_some() {
            self.left.as_ref().unwrap().read().unwrap().fold_to(groups);
        }
    }

    fn fold_right_to(&self, groups: &mut Groups) {
        if self.right.is_some() {
            self.right.as_ref().unwrap().read().unwrap().fold_to(groups);
        }
    }

    fn fold_to(&self, groups: &mut Groups) {
        self.append_to(groups);
        self.fold_right_to(groups);
        self.fold_left_to(groups);
    }

    fn append_to(&self, groups: &mut Groups) {
        groups.push(self.group.clone());
    }

    fn compare_self(&self, comparisions: &[Comparision]) -> Option<SearchAction> {
        let mut prev_action = None;

        for comparision in comparisions {
            let compared_action = comparision.compare(&self.get_value());
            if compared_action.is_none() {
                return None;
            }

            if prev_action.is_none() {
                prev_action = compared_action;
            } else {
                let merged_action = prev_action.as_ref().unwrap().merge(&compared_action.unwrap());
                if merged_action.is_none() {
                    return None;
                } else {
                    prev_action = merged_action;
                }
            }
        }

        prev_action
    }

    pub fn delete(&mut self, id: &str) -> (bool, Option<RcNode>, Option<RcNode>) {
        if self.group.read().unwrap().len() == 1 {
            let (clear, root) = self.delete_self();
            (clear, self.self_rc.take(), root)
        } else {
            let rc_child = self.group.write().unwrap().remove(id);
            if rc_child.is_some() {
                rc_child.unwrap().destroy();
            }

            (false, None, None)
        }
    }

    fn is_root(&self) -> bool {
        self.parent.is_none()
    }

    fn delete_self(&mut self) -> (bool, Option<RcNode>) {
        let mut clear = false;
        let mut root = None;

        if self.has_no_child_node() {
            if self.is_root() {
                clear = true;
            } else {
                self.parent.as_ref().unwrap().write().unwrap().delete_child_node(&self.self_rc);
            }
        } else if self.has_two_child_node() {
            let is_root = self.is_root();
            let need_link_rc_node = self.take_right_min_node();

            {
                let mut need_link_node = need_link_rc_node.write().unwrap();

                self.replace_parent_child_node(need_link_rc_node.clone());

                if is_root {
                    need_link_node.parent = None;
                } else {
                    need_link_node.parent = Some(self.parent.take().unwrap());
                }

                let left_rc_node = self.left.take().unwrap();
                left_rc_node.write().unwrap().parent = Some(need_link_rc_node.clone());
                need_link_node.left = Some(left_rc_node);

                if self.right.is_some() {
                    let right_rc_node = self.right.take().unwrap();
                    right_rc_node.write().unwrap().parent = Some(need_link_rc_node.clone());
                    need_link_node.right = Some(right_rc_node);
                }
            }

            if is_root {
                clear = true;
                root = Some(need_link_rc_node);
            }
        } else if self.left.is_some() {
            let rc_node = self.left.take().unwrap();

            if self.is_root() {
                rc_node.write().unwrap().parent = None;
                clear = true;
                root = Some(rc_node);
            } else {
                rc_node.write().unwrap().parent = self.parent.clone();
                self.replace_parent_child_node(rc_node);
            }
        } else if self.right.is_some() {
            let rc_node = self.right.take().unwrap();

            if self.is_root() {
                rc_node.write().unwrap().parent = None;
                clear = true;
                root = Some(rc_node);
            } else {
                rc_node.write().unwrap().parent = self.parent.clone();
                self.replace_parent_child_node(rc_node);
            }
        } else {
            unreachable!()
        }

        (clear, root)
    }

    fn right_child_no_left(&self) -> bool {
        self.right.as_ref().unwrap().read().unwrap().left.is_none()
    }

    fn left_child_no_left(&self) -> bool {
        self.left.as_ref().unwrap().read().unwrap().left.is_none()
    }

    fn take_left_min_node(&mut self) -> RcNode {
        if self.left_child_no_left() {
            self.left.take().unwrap()
        } else {
            self.left.as_ref().unwrap().write().unwrap().take_left_min_node()
        }
    }

    fn take_right_min_node(&mut self) -> RcNode {
        if self.right_child_no_left() {
            self.right.take().unwrap()
        } else {
            self.right.as_ref().unwrap().write().unwrap().take_left_min_node()
        }
    }

    fn replace_parent_child_node(&self, rc_node: RcNode) {
        let mut parent = self.parent.as_ref().unwrap().write().unwrap();
        parent.replace_child_node(&self.self_rc, rc_node);
    }

    fn replace_child_node(&mut self, from: &Option<RcNode>, to: RcNode) {
        if self.left == *from {
            self.left = Some(to);
        } else {
            self.right = Some(to);
        }
    }

    fn delete_child_node(&mut self, self_rc: &Option<RcNode>) {
        if self.left == *self_rc {
            self.left = None;
        } else {
            self.right = None;
        }
    }

    fn has_no_child_node(&self) -> bool {
        self.left.is_none() && self.right.is_none()
    }

    fn has_two_child_node(&self) -> bool {
        self.left.is_some() && self.right.is_some()
    }

    pub fn get_value(&self) -> Value {
        for child in self.group.read().unwrap().values() {
            return child.read().unwrap().get_value();
        }

        unreachable!()
    }
}

impl fmt::Debug for Node {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self.group)
    }
}

#[inline]
fn stop(stopped: &AtomicBool, sub: &mut Sub) {
    sub.stopped = true;
    stopped.store(true, Ordering::SeqCst);
}