Commit | Line | Data |
2c631ee0 |
1 | #!/bin/env perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
e8f4c506 |
6 | use DBI qw(looks_like_number); |
b2fc39a5 |
7 | use DBD::SQLite; |
f60f09e5 |
8 | use JSON::XS; |
b2fc39a5 |
9 | |
fc6614ee |
10 | use Getopt::Long; |
11 | |
12 | GetOptions( |
13 | 'json!' => \my $opt_json, |
ee2793c1 |
14 | 'dot!' => \my $opt_dot, |
b2fc39a5 |
15 | 'db=s' => \my $opt_db, |
e8f4c506 |
16 | 'verbose|v!' => \my $opt_verbose, |
17 | 'debug|d!' => \my $opt_debug, |
fc6614ee |
18 | ) or exit 1; |
94fab3d1 |
19 | |
f60f09e5 |
20 | my $j = JSON::XS->new->ascii->pretty(0); |
21 | |
b2fc39a5 |
22 | my $dbh = DBI->connect("dbi:SQLite:dbname=$opt_db","","", { |
23 | RaiseError => 1, PrintError => 0, AutoCommit => 0 |
24 | }); |
25 | $dbh->do("PRAGMA synchronous = OFF"); |
26 | $dbh->do("DROP TABLE IF EXISTS node"); |
27 | $dbh->do(q{ |
28 | CREATE TABLE node ( |
29 | id integer primary key, |
30 | name text, |
e8f4c506 |
31 | title text, |
b2fc39a5 |
32 | depth integer, |
5a78486c |
33 | parent_id integer, |
b2fc39a5 |
34 | |
35 | self_size integer, |
36 | kids_size integer, |
37 | kids_node_count integer, |
f60f09e5 |
38 | child_ids text, |
e78b28ca |
39 | attr_json text, |
40 | leaves_json text |
b2fc39a5 |
41 | ) |
42 | }); |
43 | my $node_ins_sth = $dbh->prepare(q{ |
e8f4c506 |
44 | INSERT INTO node VALUES (?,?,?,?,?, ?,?,?,?,?,?) |
b2fc39a5 |
45 | }); |
46 | |
2c631ee0 |
47 | my @stack; |
48 | my %seqn2node; |
49 | |
ee2793c1 |
50 | my $dotnode = sub { |
51 | my $name = shift; |
52 | $name =~ s/"/\\"/g; |
53 | return '"'.$name.'"'; |
54 | }; |
55 | |
56 | print "memnodes = [" if $opt_json; |
57 | |
58 | if ($opt_dot) { |
59 | print "digraph {\n"; # } |
60 | print "graph [overlap=false]\n"; # target="???", URL="???" |
61 | } |
62 | |
63 | |
94fab3d1 |
64 | sub enter_node { |
65 | my $x = shift; |
66 | if ($opt_json) { |
67 | print " " x $x->{depth}; |
b2fc39a5 |
68 | print qq({ "id": "$x->{id}", "name": "$x->{name}", "depth":$x->{depth}, "children":[ \n); |
94fab3d1 |
69 | } |
ee2793c1 |
70 | if ($opt_dot) { |
71 | #printf $fh qq{\tn%d [ %s ]\n}, $x->{id}, $dotnode->($x->{name}); |
72 | #print qq({ "id": "$x->{id}", "name": "$x->{name}", "depth":$x->{depth}, "children":[ \n); |
73 | } |
94fab3d1 |
74 | return; |
75 | } |
76 | |
77 | sub leave_node { |
78 | my $x = shift; |
b2fc39a5 |
79 | delete $seqn2node{$x->{id}}; |
ee2793c1 |
80 | |
94fab3d1 |
81 | my $self_size = 0; $self_size += $_ for values %{$x->{leaves}}; |
82 | $x->{self_size} = $self_size; |
ee2793c1 |
83 | |
84 | my $parent = $stack[-1]; |
85 | if ($parent) { |
2c631ee0 |
86 | # link to parent |
5a78486c |
87 | $x->{parent_id} = $parent->{id}; |
2c631ee0 |
88 | # accumulate into parent |
89 | $parent->{kids_node_count} += 1 + ($x->{kids_node_count}||0); |
94fab3d1 |
90 | $parent->{kids_size} += $self_size + $x->{kids_size}; |
5a78486c |
91 | push @{$parent->{child_id}}, $x->{id}; |
2c631ee0 |
92 | } |
93 | # output |
94 | # ... |
94fab3d1 |
95 | if ($opt_json) { |
96 | print " " x $x->{depth}; |
97 | my $size = $self_size + $x->{kids_size}; |
98 | print qq(], "data":{ "\$area": $size } },\n); |
99 | } |
ee2793c1 |
100 | if ($opt_dot) { |
101 | my @attr = (sprintf "label=%s", $dotnode->($x->{name})); |
102 | push @attr, "shape=point" if $x->{type} == 2; |
103 | printf qq{n%d [ %s ];\n}, $x->{id}, join(",", @attr); |
104 | printf qq{n%d -> n%d;\n}, $parent->{id}, $x->{id} if $parent; |
105 | } |
b2fc39a5 |
106 | if ($dbh) { |
f60f09e5 |
107 | my $attr_json = $j->encode($x->{attr}); |
e78b28ca |
108 | my $leaves_json = $j->encode($x->{leaves}); |
b2fc39a5 |
109 | $node_ins_sth->execute( |
e8f4c506 |
110 | $x->{id}, $x->{name}, $x->{title}, $x->{depth}, $x->{parent_id}, |
b2fc39a5 |
111 | $x->{self_size}, $x->{kids_size}, $x->{kids_node_count}, |
f60f09e5 |
112 | $x->{child_id} ? join(",", @{$x->{child_id}}) : undef, |
e78b28ca |
113 | $attr_json, $leaves_json, |
b2fc39a5 |
114 | ); |
115 | # XXX attribs |
116 | } |
94fab3d1 |
117 | return; |
2c631ee0 |
118 | } |
119 | |
94fab3d1 |
120 | |
2c631ee0 |
121 | while (<>) { |
122 | chomp; |
b2fc39a5 |
123 | my ($type, $id, $val, $name, $extra) = split / /, $_, 5; |
ee2793c1 |
124 | if ($type =~ s/^-//) { # Node type ($val is depth) |
2c631ee0 |
125 | while ($val < @stack) { |
94fab3d1 |
126 | leave_node(my $x = pop @stack); |
e8f4c506 |
127 | warn "N $id d$val ends $x->{id} d$x->{depth}: size $x->{self_size}+$x->{kids_size}\n" |
128 | if $opt_verbose; |
2c631ee0 |
129 | } |
130 | die 1 if $stack[$val]; |
ee2793c1 |
131 | my $node = $stack[$val] = { id => $id, type => $type, name => $name, extra => $extra, attr => {}, leaves => {}, depth => $val, self_size=>0, kids_size=>0 }; |
94fab3d1 |
132 | enter_node($node); |
b2fc39a5 |
133 | $seqn2node{$id} = $node; |
2c631ee0 |
134 | } |
135 | elsif ($type eq "L") { # Leaf name and memory size |
b2fc39a5 |
136 | my $node = $seqn2node{$id} || die; |
2c631ee0 |
137 | $node->{leaves}{$name} += $val; |
138 | } |
e8f4c506 |
139 | elsif (looks_like_number($type)) { # Attribute type, name and value |
b2fc39a5 |
140 | my $node = $seqn2node{$id} || die; |
e8f4c506 |
141 | my $attr = $node->{attr} || die; |
142 | if ($type == 1) { # NPattr_NAME |
143 | warn "Node $id already has attribute $type:$name (value $attr->{$type}{$name})\n" |
144 | if exists $attr->{$type}{$name}; |
145 | $attr->{$type}{$name} = $val || $id; |
146 | warn "A \@$id: '$name' $val\n"; |
147 | $node->{title} = $name if $type == 1 and !$val; |
148 | } |
149 | elsif (2 <= $type and $type <= 4) { # NPattr_PAD* |
150 | warn "Node $id already has attribute $type:$name (value $attr->{$type}[$val])\n" |
151 | if defined $attr->{$type}[$val]; |
152 | $attr->{$type}[$val] = $name; |
153 | } |
154 | else { |
155 | warn "Invalid attribute type '$type' on line $. ($_)"; |
156 | } |
2c631ee0 |
157 | } |
158 | else { |
159 | warn "Invalid type '$type' on line $. ($_)"; |
e8f4c506 |
160 | next; |
2c631ee0 |
161 | } |
b2fc39a5 |
162 | $dbh->commit if $dbh and $id % 10_000 == 0; |
2c631ee0 |
163 | } |
164 | |
165 | my $x; |
166 | while (@stack > 1) { |
94fab3d1 |
167 | leave_node($x = pop @stack) while @stack; |
b2fc39a5 |
168 | warn "EOF ends $x->{id} d$x->{depth}: size $x->{self_size}+$x->{kids_size}\n"; |
2c631ee0 |
169 | } |
94fab3d1 |
170 | print " ];\n" if $opt_json; |
ee2793c1 |
171 | print "}\n" if $opt_dot; |
94fab3d1 |
172 | |
b2fc39a5 |
173 | $dbh->commit if $dbh; |
174 | |
2c631ee0 |
175 | use Data::Dumper; |
176 | warn Dumper(\$x); |
177 | warn Dumper(\%seqn2node); |
178 | |
179 | =for |
180 | SV(PVAV) fill=1/1 [#1 @0] |
181 | : +64 sv =64 |
182 | : +16 av_max =80 |
183 | : AVelem-> [#2 @1] |
184 | : : SV(RV) [#3 @2] |
185 | : : : +24 sv =104 |
186 | : : : RV-> [#4 @3] |
187 | : : : : SV(PVAV) fill=-1/-1 [#5 @4] |
188 | : : : : : +64 sv =168 |
189 | : AVelem-> [#6 @1] |
190 | : : SV(IV) [#7 @2] |
191 | : : : +24 sv =192 |
192 | 192 at -e line 1. |
193 | =cut |
194 | __DATA__ |
195 | N 1 0 SV(PVAV) fill=1/1 |
196 | L 1 64 sv |
197 | L 1 16 av_max |
198 | N 2 1 AVelem-> |
199 | N 3 2 SV(RV) |
200 | L 3 24 sv |
201 | N 4 3 RV-> |
202 | N 5 4 SV(PVAV) fill=-1/-1 |
203 | L 5 64 sv |
204 | N 6 1 AVelem-> |
205 | N 7 2 SV(IV) |
206 | L 7 24 sv |