← Index
NYTProf Performance Profile   « block view • line view • sub view »
For bin/hailo
  Run on Thu Oct 21 22:50:37 2010
Reported on Thu Oct 21 22:52:09 2010

Filename/mnt/stuff/src/my-cpan/hailo/lib/Hailo/Storage/Schema.pm
StatementsExecuted 67 statements in 1.18ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
111194µs1.91msHailo::Storage::Schema::::sthHailo::Storage::Schema::sth
111118µs2.61msHailo::Storage::Schema::::deployHailo::Storage::Schema::deploy
11158µs151µsHailo::Storage::Schema::::BEGIN@3Hailo::Storage::Schema::BEGIN@3
11113µs93µsHailo::Storage::Schema::::BEGIN@3.13Hailo::Storage::Schema::BEGIN@3.13
11113µs18µsHailo::Storage::Schema::::BEGIN@4Hailo::Storage::Schema::BEGIN@4
1119µs9µsHailo::Storage::Schema::::CORE:sortHailo::Storage::Schema::CORE:sort (opcode)
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Hailo::Storage::Schema;
2
3474µs3324µs
# spent 93µs (13+80) within Hailo::Storage::Schema::BEGIN@3.13 which was called: # once (13µs+80µs) by Hailo::Storage::Schema::BEGIN@3 at line 3 # spent 151µs (58+93) within Hailo::Storage::Schema::BEGIN@3 which was called: # once (58µs+93µs) by Hailo::Storage::BEGIN@7 at line 3
use 5.010;
# spent 151µs making 1 call to Hailo::Storage::Schema::BEGIN@3 # spent 93µs making 1 call to Hailo::Storage::Schema::BEGIN@3.13 # spent 80µs making 1 call to feature::import
42580µs224µs
# spent 18µs (13+5) within Hailo::Storage::Schema::BEGIN@4 which was called: # once (13µs+5µs) by Hailo::Storage::BEGIN@7 at line 4
use strict;
# spent 18µs making 1 call to Hailo::Storage::Schema::BEGIN@4 # spent 6µs making 1 call to strict::import
5
6## Soup to spawn the database itself / create statement handles
7
# spent 2.61ms (118µs+2.49) within Hailo::Storage::Schema::deploy which was called: # once (118µs+2.49ms) by Hailo::Storage::_engage at line 109 of lib/Hailo/Storage.pm
sub deploy {
82460µs my (undef, $dbd, $dbh, $order) = @_;
9 my @orders = (0 .. $order-1);
10
11 my $int_primary_key = "INTEGER PRIMARY KEY AUTOINCREMENT";
12 $int_primary_key = "INTEGER PRIMARY KEY AUTO_INCREMENT" if $dbd eq "mysql";
13 $int_primary_key = "SERIAL UNIQUE" if $dbd eq "Pg";
14
15 my $text = 'TEXT';
16 $text = 'VARCHAR(255)' if $dbd eq 'mysql';
17
18 my $text_primary = 'TEXT NOT NULL PRIMARY KEY';
19 $text_primary = 'TEXT NOT NULL' if $dbd eq 'mysql';
20
21 my @tables;
22
23 push @tables => <<"TABLE";
24CREATE TABLE info (
25 attribute $text_primary,
26 text TEXT NOT NULL
27);
28TABLE
29
30 push @tables => <<"TABLE";
31CREATE TABLE token (
32 id $int_primary_key,
33 spacing INTEGER NOT NULL,
34 text $text NOT NULL,
35 count INTEGER NOT NULL
36);
37TABLE
38
39 my $token_n = join ",\n ", map { "token${_}_id INTEGER NOT NULL REFERENCES token (id)" } @orders;
40 push @tables => <<"TABLE";
41CREATE TABLE expr (
42 id $int_primary_key,
43 $token_n
44);
45TABLE
46
47 push @tables => <<"TABLE";
48CREATE TABLE next_token (
49 id $int_primary_key,
50 expr_id INTEGER NOT NULL REFERENCES expr (id),
51 token_id INTEGER NOT NULL REFERENCES token (id),
52 count INTEGER NOT NULL
53);
54TABLE
55
56 push @tables => <<"TABLE";
57CREATE TABLE prev_token (
58 id $int_primary_key,
59 expr_id INTEGER NOT NULL REFERENCES expr (id),
60 token_id INTEGER NOT NULL REFERENCES token (id),
61 count INTEGER NOT NULL
62);
63TABLE
64
65 for my $i (@orders) {
6627µs push @tables => "CREATE INDEX expr_token${i}_id on expr (token${i}_id);"
67 }
68
69 my $columns = join(', ', map { "token${_}_id" } @orders);
70 push @tables => "CREATE INDEX expr_token_ids on expr ($columns);";
71
72 push @tables => 'CREATE INDEX token_text on token (text);';
73 push @tables => 'CREATE INDEX next_token_expr_id ON next_token (expr_id);';
74 push @tables => 'CREATE INDEX prev_token_expr_id ON prev_token (expr_id);';
75
76
77 for (@tables) {
7811114µs224.92ms $dbh->do($_);
# spent 2.49ms making 11 calls to DBI::db::do, avg 227µs/call # spent 2.43ms making 11 calls to DBD::SQLite::db::do, avg 221µs/call
79 }
80
81 return;
82}
83
84# create statement handle objects
85
# spent 1.91ms (194µs+1.72) within Hailo::Storage::Schema::sth which was called: # once (194µs+1.72ms) by Hailo::Storage::_build_sth at line 84 of lib/Hailo/Storage.pm
sub sth {
861698µs my (undef, $dbd, $dbh, $order) = @_;
87 my @orders = (0 .. $order-1);
88 my @columns = map { "token${_}_id" } 0 .. $order-1;
89 my $columns = join(', ', @columns);
90 my @ids = join(', ', ('?') x @columns);
91 my $ids = join(', ', @ids);
92
93 my $q_rand = 'RANDOM()';
94 $q_rand = 'RAND()' if $dbd eq 'mysql';
95
96 my $q_rand_id = "(abs($q_rand) % (SELECT max(id) FROM expr))";
97 $q_rand_id = "(random()*id+1)::int" if $dbd eq 'Pg';
98
99 my %state = (
100 set_info => qq[INSERT INTO info (attribute, text) VALUES (?, ?);],
101
102 random_expr => qq[SELECT * FROM expr WHERE id >= $q_rand_id LIMIT 1;],
103 token_id => qq[SELECT id FROM token WHERE spacing = ? AND text = ?;],
104 token_info => qq[SELECT spacing, text FROM token WHERE id = ?;],
105 token_similar => qq[SELECT id, spacing FROM token WHERE text = ? ORDER BY $q_rand LIMIT 1;] ,
106 add_token => qq[INSERT INTO token (spacing, text, count) VALUES (?, ?, 0)],
107 inc_token_count => qq[UPDATE token SET count = count + 1 WHERE id = ?],
108
109 # ->stats()
110 expr_total => qq[SELECT COUNT(*) FROM expr;],
111 token_total => qq[SELECT COUNT(*) FROM token;],
112 prev_total => qq[SELECT COUNT(*) FROM prev_token;],
113 next_total => qq[SELECT COUNT(*) FROM next_token;],
114
115 # Defaults, overriden in SQLite
116 last_expr_rowid => qq[SELECT id FROM expr ORDER BY id DESC LIMIT 1;],
117 last_token_rowid => qq[SELECT id FROM token ORDER BY id DESC LIMIT 1;],
118
119 next_token_count => qq[SELECT count FROM next_token WHERE expr_id = ? AND token_id = ?;],
120 prev_token_count => qq[SELECT count FROM prev_token WHERE expr_id = ? AND token_id = ?;],
121 next_token_inc => qq[UPDATE next_token SET count = count + 1 WHERE expr_id = ? AND token_id = ?],
122 prev_token_inc => qq[UPDATE prev_token SET count = count + 1 WHERE expr_id = ? AND token_id = ?],
123 next_token_add => qq[INSERT INTO next_token (expr_id, token_id, count) VALUES (?, ?, 1);],
124 prev_token_add => qq[INSERT INTO prev_token (expr_id, token_id, count) VALUES (?, ?, 1);],
125 next_token_get => qq[SELECT token_id, count FROM next_token WHERE expr_id = ?;],
126 prev_token_get => qq[SELECT token_id, count FROM prev_token WHERE expr_id = ?;],
127
128 token_count => qq[SELECT count FROM token WHERE id = ?;],
129
130 add_expr => qq[INSERT INTO expr ($columns) VALUES ($ids)],
131 expr_id => qq[SELECT id FROM expr WHERE ] . join(' AND ', map { "token${_}_id = ?" } @orders),
132 );
133
134 for (@orders) {
13528µs $state{"expr_by_token${_}_id"} = qq[SELECT * FROM expr WHERE token${_}_id = ? ORDER BY $q_rand LIMIT 1;];
136 }
137
138 # DBD specific queries / optimizations / munging
13913µs given ($dbd) {
14046µs when ('SQLite') {
141 # Optimize these for SQLite
142 $state{expr_total} = qq[SELECT seq FROM sqlite_sequence WHERE name = 'expr';];
143 $state{token_total} = qq[SELECT seq FROM sqlite_sequence WHERE name = 'token';];
144 $state{prev_total} = qq[SELECT seq FROM sqlite_sequence WHERE name = 'prev_token';];
145 $state{next_total} = qq[SELECT seq FROM sqlite_sequence WHERE name = 'next_token';];
146 }
147 }
148
149 # Sort to make error output easier to read if this fails. The
150 # order doesn't matter.
15119µs my @queries = sort keys %state;
# spent 9µs making 1 call to Hailo::Storage::Schema::CORE:sort
1521228µs523.27ms my %sth = map { $_ => $dbh->prepare($state{$_}) } @queries;
# spent 1.71ms making 26 calls to DBI::db::prepare, avg 66µs/call # spent 1.56ms making 26 calls to DBD::SQLite::db::prepare, avg 60µs/call
153
154 return \%sth;
155}
156
15713µs1;
158
159=head1 NAME
160
161Hailo::Storage::Schema - Deploy the database schema Hailo uses
162
163=head1 DESCRIPTION
164
165Implements functions to create the database schema and prepared
166database queries L<Hailo::Storage> needs.
167
168This class is internal to Hailo and has no public interface.
169
170=head1 AUTHOR
171
172E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@cpan.org>
173
174=head1 LICENSE AND COPYRIGHT
175
176Copyright 2010 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason
177
178This program is free software, you can redistribute it and/or modify
179it under the same terms as Perl itself.
180
181=cut
 
# spent 9µs within Hailo::Storage::Schema::CORE:sort which was called: # once (9µs+0s) by Hailo::Storage::Schema::sth at line 151
sub Hailo::Storage::Schema::CORE:sort; # opcode