'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
var Class = require('./Class');
var toBool = require('./toBool');
var camelCase = require('./camelCase');
var LinkedList = require('./LinkedList');
var isStr = require('./isStr');
var each = require('./each');
var map = require('./map');
exports = Class({
    initialize: function HeapSnapshot(profile) {
        if (isStr(profile)) {
            profile = JSON.parse(profile);
        }
        this.nodes = new LinkedList();
        this.edges = new LinkedList();
        var snapshot = profile.snapshot;
        var meta = snapshot.meta;
        this.nodeFields = map(meta.node_fields, camelCase);
        this.nodeTypes = meta.node_types[this.nodeFields.indexOf('type')];
        this.edgeFields = map(meta.edge_fields, camelCase);
        this.edgeTypes = meta.edge_types[this.edgeFields.indexOf('type')];
        this._init(profile);
    },
    _init: function(profile) {
        var _this = this;
        var nodes = profile.nodes,
            edges = profile.edges,
            strings = profile.strings;
        var nodeFields = this.nodeFields,
            edgeFields = this.edgeFields;
        var curEdgeIdx = 0;
        var nodeFieldCount = nodeFields.length;
        var edgeFieldCount = edgeFields.length;
        var nodeMap = {};
        for (var i = 0, len = nodes.length; i < len; i += nodeFieldCount) {
            var node = new Node(this);
            node.init(nodes.slice(i, i + nodeFieldCount), strings);
            this.nodes.push(node);
            nodeMap[i] = node;
        }
        this.nodes.forEach(function(node) {
            var edgeCount = node.edgeCount;
            delete node.edgeCount;
            var maxEdgeIdx = curEdgeIdx + edgeCount * edgeFieldCount;
            for (var _i = curEdgeIdx; _i < maxEdgeIdx; _i += edgeFieldCount) {
                var edge = new Edge(_this, node);
                edge.init(
                    edges.slice(_i, _i + edgeFieldCount),
                    strings,
                    nodeMap
                );
                _this.edges.push(edge);
            }
            curEdgeIdx = maxEdgeIdx;
        });
    }
});
var Node = Class({
    initialize: function Node(heapSnapshot) {
        this._heapSnapshot = heapSnapshot;
    },
    init: function(fields, strings) {
        var _this2 = this;
        var heapSnapshot = this._heapSnapshot;
        var nodeFields = heapSnapshot.nodeFields,
            nodeTypes = heapSnapshot.nodeTypes;
        each(nodeFields, function(field, idx) {
            var val = fields[idx];
            switch (field) {
                case 'name':
                    val = strings[val];
                    break;
                case 'detachedness':
                    val = toBool(val);
                    break;
                case 'type':
                    val = nodeTypes[val];
                    break;
            }
            _this2[field] = val;
        });
    }
});
var Edge = Class({
    initialize: function Edge(heapSnapshot, fromNode) {
        this._heapSnapshot = heapSnapshot;
        this.fromNode = fromNode;
    },
    init: function(fields, strings, nodeMap) {
        var _this3 = this;
        var heapSnapshot = this._heapSnapshot;
        var edgeFields = heapSnapshot.edgeFields,
            edgeTypes = heapSnapshot.edgeTypes;
        each(edgeFields, function(field, idx) {
            var val = fields[idx];
            switch (field) {
                case 'nameOrIndex':
                    val = strings[val];
                    break;
                case 'type':
                    val = edgeTypes[val];
                    break;
                case 'toNode':
                    val = nodeMap[val];
                    break;
            }
            _this3[field] = val;
        });
    }
});
 
module.exports = exports;