rs_parse_snapshot/
common.rs

1use crate::define::define::{
2    EdgePropertyType, Heapsnapshot, JsValueType, NodePropertyType, EDGE_OTHERS_PROPERTY,
3    EDGE_TYPES_PROPERTY, NODE_OTHERS_PROPERTY, NODE_TYPES_PROPERTY,
4};
5use std::collections::HashMap;
6use std::fs::read_to_string;
7
8pub fn get_node_property(node_start: usize, col: usize, snapshot: &Heapsnapshot) -> JsValueType {
9    // node_start 代表当前 node 的起始索引, col 代表属性的偏移量
10    let offset = node_start + col; // 当前属性在 nodes 中的索引
11    let node_val = snapshot.nodes[offset];
12    let node_property_type: NodePropertyType = if col == 0 {
13        NodePropertyType::Arr(NODE_TYPES_PROPERTY)
14    } else {
15        NodePropertyType::Str(NODE_OTHERS_PROPERTY[col - 1])
16    };
17    let node_property_val: JsValueType = match node_property_type {
18        NodePropertyType::Str(property_type) => {
19            if property_type == "string" {
20                if node_val >= snapshot.strings.len() {
21                    JsValueType::JsString("".to_string())
22                } else {
23                    JsValueType::JsString(snapshot.strings[node_val].to_string())
24                }
25            } else if property_type == "number" {
26                JsValueType::JsNumber(node_val)
27            } else {
28                panic!("unknkow property_type {} ", property_type)
29            }
30        }
31        NodePropertyType::Arr(property_type_arr) => {
32            if node_val >= property_type_arr.len() {
33                JsValueType::JsString("".to_string())
34            } else {
35                JsValueType::JsString(property_type_arr[node_val].to_string())
36            }
37        }
38    };
39    node_property_val
40}
41pub fn read_to_snapshot(path: &str) -> (Heapsnapshot, usize) {
42    let snapshot_string = if path.starts_with("{") {
43        String::from(path)
44    } else {
45        read_to_string(path).expect("file not found")
46    };
47
48    let heap_snap_shot =
49        serde_json::from_str::<Heapsnapshot>(&snapshot_string).expect("type format error");
50    let node_fields_len = heap_snap_shot.snapshot.meta.node_fields.len();
51    (heap_snap_shot, node_fields_len)
52}
53
54pub fn find_char_boundary(s: &str, index: usize) -> usize {
55    if s.len() <= index {
56        return index;
57    }
58
59    let mut new_index = index;
60    while !s.is_char_boundary(new_index) {
61        new_index += 1;
62    }
63
64    new_index
65}
66pub fn get_edges_property(edge_start: usize, col: usize, snapshot: &Heapsnapshot) -> JsValueType {
67    // edge_start 代表当前 edge 的起始索引, col 代表属性的偏移量
68    let offset = edge_start + col;
69    let edge_val = snapshot.edges[offset];
70    let edge_property_type: EdgePropertyType = if col == 0 {
71        EdgePropertyType::Arr(EDGE_TYPES_PROPERTY)
72    } else {
73        EdgePropertyType::Str(EDGE_OTHERS_PROPERTY[col - 1].clone())
74    };
75    let edge_property_val: JsValueType = match edge_property_type {
76        EdgePropertyType::Str(property_type) => {
77            if property_type == "string" || property_type == "string_or_number" {
78                if edge_val >= snapshot.strings.len() {
79                    JsValueType::JsString("".to_string())
80                } else {
81                    JsValueType::JsString(snapshot.strings[edge_val].to_string())
82                }
83            } else if property_type == "number" {
84                JsValueType::JsNumber(edge_val)
85            } else if property_type == "node" {
86                get_node_property(edge_val, 2, snapshot)
87            } else {
88                panic!("unknkow property_type {} ", property_type)
89            }
90        }
91        EdgePropertyType::Arr(property_type_arr) => {
92            JsValueType::JsString(property_type_arr[edge_val].to_string())
93        }
94    };
95    edge_property_val
96}
97
98pub fn get_ordinal(id_to_ordinal: &HashMap<usize, usize>, node_id: usize) -> usize {
99    *id_to_ordinal.get(&node_id).unwrap()
100}