SlideShare a Scribd company logo
Operation Oriented Web Applications
                        Yokohama.pm#7
                            @kazeburo
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Log::Minimal
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
#!/usr/bin/env perl

use strict;
use warnings;
use Log::Minimal;

critf("%s","foo");
warnf("%d %s", 1, "foo");

sub hoge {
    infoff("foo");
    debugff("bar");
};
hoge();

local $Log::Minimal::AUTODUMP = 1;
infof({ key => 'val' });
warnf("data is %s", { key => 'val' });
$ LM_DEBUG=1 perl /tmp/logminimal.pl


2011-05-11T15:57:49 [CRITICAL] foo at /tmp/logminimal.pl line 7
2011-05-11T15:57:49 [WARN] 1 foo at /tmp/logminimal.pl line 8


2011-05-11T15:57:49 [INFO] foo at /tmp/logminimal.pl line 11 ,/tmp/
logminimal.pl line 14
2011-05-11T15:57:49 [DEBUG] bar at /tmp/logminimal.pl line 12 ,/tmp/
logminimal.pl line 14


2011-05-11T15:57:49 [INFO] {'key' => 'val'} at /tmp/logminimal.pl line 17
2011-05-11T15:57:49 [WARN] data is {'key' => 'val'} at /tmp/logminimal.pl
line 18
local $Log::Minimal::PRINT = sub {
    my ( $time, $type, $message, $trace) = @_;
    print STDERR “[$type] $message $trace”;
};




local $Log::Minimal::LOG_LEVEL = "WARN";
infof("foo"); #print nothing
warnf(“xaicron++”);
local $Log::Minimal::AUTODUMP = 1;
warnf(“response => %s”,[ 200, [‘Content-Type’,‘text/
plain’],[‘OK’]]);

# 2011-05-11T15:56:14 [WARN] response => [200,
['Content-Type','text/plain'],['OK']] at ..




sub myerror {
    local $Log::Minimal::TRACE_LEVEL = 1;
    infof(@_);
}
myerror(“foo”);
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
use Log::Minimal;
use Plack::Builder;

builder {
    enable "Log::Minimal", autodump => 1;
    sub {
        my $env = shift;
        warnf("warn message");
        debugf("debug message");
        ...
    }
};



$ plackup -a demo.psgi
HTTP::Server::PSGI: Accepting connections at http://0:5000/
2011-05-11T16:32:24 [WARN] [/foo/bar/baz] warn message at /
tmp/demo.psgi line 8
2011-05-11T16:32:24 [DEBUG] [/foo/bar/baz] debug message at /
tmp/demo.psgi line 9
DBIx::Sunny
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
selectrow_arrayref($query, {}, @bind);




selectrow_hashref($query, {}, @bind);




selectall_arrayref($query, { Slice => {} }, @bind);




prepare($query) && execute(@bind)
use DBIx::Sunny;

my $dbh = DBIx::Sunny->connect(...);




use DBI;
use DBIx::Sunny;

my $dbh = DBIx->connect(...,{
    RootClass => ‘DBIx::Sunny’
});
Operation Oriented Web Applications / Yokohama pm7
DBIx::Sunny::Schema
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
package NoNoPaste::Data;
use parent qw/DBIx::Sunny::Schema/;

__PACKAGE__->query(
    'add_entry',
    id => 'Str',
    nick => { isa => 'Str', default => 'anonymouse' },
    body => 'Str',
    q{INSERT INTO entries ( id, nick, body, ctime )
        values ( ?, ?, ?, NOW() )},
);

__PACKAGE__->select_row(
    'entry',
    'id' => 'Uint',
    q{SELECT id,nick,body,ctime FROM entries WHERE id =?};
);

__PACKAGE__->select_all(
    'entries_multi',
    'id' => { isa => 'ArrayRef[Uint]' },
    q{SELECT id,nick,body,ctime FROM entries WHERE id IN (?)}
);
use parent qw/DBIx::Sunny::Schema/;

__PACKAGE__->                   (
     '          ',
          =>     /       ,
     [     =>        /       ,[..]],
     ‘    ’,
);
prepare && execute && fetchrow_arrayref->[0];




prepare && execute && fetchrow_hashref;




prepare && execute && push @result, $_ while fetchrow_hash;




prepare && execute
my $dbh = DBI->connect(...);
my $master = NoNoPaste::Data->new( dbh => $dbh );

# readonly   query
my $slave = NoNoPaste::Data->new( dbh => $dbh,
                                 readonly => 1 );
#
my $row = $master->add_entry(
    id => $id,
    nick => $nick,
    body => $body,
);

#
my $rows = $slave->entry_list( offset => $offset );
Operation Oriented Web Applications / Yokohama pm7
GreenBuckets
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
use Digest::MurmurHash qw/murmur_hash/;

for ( 1..100 ) {
    say murmur_hash(sprintf "test%03d", $_ );
}
use Digest::MurmurHash qw/murmur_hash/;

for ( 1..100 ) {
    say murmur_hash(sprintf "test%03d", $_ );
}
CREATE TABLE objects (
    id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
    fid INT UNSIGNED NOT NULL,
    bucket_id INT UNSIGNED NOT NULL,
    rid SMALLINT UNSIGNED NOT NULL,
    gid SMALLINT UNSIGNED NOT NULL,
    filename VARCHAR(1024),
    INDEX (fid, bucket_id),
    INDEX (bucket_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin




SELECT * FROM objects WHERE fid = murmur($filename) AND
bucket_id = $bucket AND filename = $filename
CREATE TABLE entries_int (
    id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
    fid INT UNSIGNED NOT NULL,
    bid INT UNSIGNED NOT NULL,
    filename VARCHAR(255) NOT NULL,
    INDEX (fid, bid),
    INDEX (bid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin




CREATE TABLE entries_char (
    id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
    filename VARCHAR(255) NOT NULL,
    bid INT UNSIGNED NOT NULL,
    UNIQUE INDEX (filename, bid),
    INDEX (bid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin
.--------------------------------------------------.
| table_name   | total_kb | data_kb    | index_kb |
+--------------+-----------+-----------+-----------+
| entries_char | 3360.0000 | 1552.0000 | 1808.0000 |
| entries_int | 2080.0000 | 1552.0000 | 528.0000 |
'--------------+-----------+-----------+-----------'

               (filename      32    )
@nodes = sort {
    murmur_hash(join "/", $a->{node_id},$bucket,$filename)
    <=>
    murmur_hash(join "/", $b->{node_id},$bucket,$filename)
} @nodes;
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7

More Related Content

Operation Oriented Web Applications / Yokohama pm7

  • 1. Operation Oriented Web Applications Yokohama.pm#7 @kazeburo
  • 7. #!/usr/bin/env perl use strict; use warnings; use Log::Minimal; critf("%s","foo"); warnf("%d %s", 1, "foo"); sub hoge { infoff("foo"); debugff("bar"); }; hoge(); local $Log::Minimal::AUTODUMP = 1; infof({ key => 'val' }); warnf("data is %s", { key => 'val' });
  • 8. $ LM_DEBUG=1 perl /tmp/logminimal.pl 2011-05-11T15:57:49 [CRITICAL] foo at /tmp/logminimal.pl line 7 2011-05-11T15:57:49 [WARN] 1 foo at /tmp/logminimal.pl line 8 2011-05-11T15:57:49 [INFO] foo at /tmp/logminimal.pl line 11 ,/tmp/ logminimal.pl line 14 2011-05-11T15:57:49 [DEBUG] bar at /tmp/logminimal.pl line 12 ,/tmp/ logminimal.pl line 14 2011-05-11T15:57:49 [INFO] {'key' => 'val'} at /tmp/logminimal.pl line 17 2011-05-11T15:57:49 [WARN] data is {'key' => 'val'} at /tmp/logminimal.pl line 18
  • 9. local $Log::Minimal::PRINT = sub { my ( $time, $type, $message, $trace) = @_; print STDERR “[$type] $message $trace”; }; local $Log::Minimal::LOG_LEVEL = "WARN"; infof("foo"); #print nothing warnf(“xaicron++”);
  • 10. local $Log::Minimal::AUTODUMP = 1; warnf(“response => %s”,[ 200, [‘Content-Type’,‘text/ plain’],[‘OK’]]); # 2011-05-11T15:56:14 [WARN] response => [200, ['Content-Type','text/plain'],['OK']] at .. sub myerror { local $Log::Minimal::TRACE_LEVEL = 1; infof(@_); } myerror(“foo”);
  • 16. use Log::Minimal; use Plack::Builder; builder {     enable "Log::Minimal", autodump => 1;     sub {         my $env = shift;         warnf("warn message");         debugf("debug message");         ...     } }; $ plackup -a demo.psgi HTTP::Server::PSGI: Accepting connections at http://0:5000/ 2011-05-11T16:32:24 [WARN] [/foo/bar/baz] warn message at / tmp/demo.psgi line 8 2011-05-11T16:32:24 [DEBUG] [/foo/bar/baz] debug message at / tmp/demo.psgi line 9
  • 21. selectrow_arrayref($query, {}, @bind); selectrow_hashref($query, {}, @bind); selectall_arrayref($query, { Slice => {} }, @bind); prepare($query) && execute(@bind)
  • 22. use DBIx::Sunny; my $dbh = DBIx::Sunny->connect(...); use DBI; use DBIx::Sunny; my $dbh = DBIx->connect(...,{ RootClass => ‘DBIx::Sunny’ });
  • 28. package NoNoPaste::Data; use parent qw/DBIx::Sunny::Schema/; __PACKAGE__->query( 'add_entry', id => 'Str', nick => { isa => 'Str', default => 'anonymouse' }, body => 'Str', q{INSERT INTO entries ( id, nick, body, ctime ) values ( ?, ?, ?, NOW() )}, ); __PACKAGE__->select_row( 'entry', 'id' => 'Uint', q{SELECT id,nick,body,ctime FROM entries WHERE id =?}; ); __PACKAGE__->select_all( 'entries_multi', 'id' => { isa => 'ArrayRef[Uint]' }, q{SELECT id,nick,body,ctime FROM entries WHERE id IN (?)} );
  • 29. use parent qw/DBIx::Sunny::Schema/; __PACKAGE__-> ( ' ', => / , [ => / ,[..]], ‘ ’, );
  • 30. prepare && execute && fetchrow_arrayref->[0]; prepare && execute && fetchrow_hashref; prepare && execute && push @result, $_ while fetchrow_hash; prepare && execute
  • 31. my $dbh = DBI->connect(...); my $master = NoNoPaste::Data->new( dbh => $dbh ); # readonly query my $slave = NoNoPaste::Data->new( dbh => $dbh, readonly => 1 ); # my $row = $master->add_entry( id => $id, nick => $nick, body => $body, ); # my $rows = $slave->entry_list( offset => $offset );
  • 50. use Digest::MurmurHash qw/murmur_hash/; for ( 1..100 ) { say murmur_hash(sprintf "test%03d", $_ ); }
  • 51. use Digest::MurmurHash qw/murmur_hash/; for ( 1..100 ) { say murmur_hash(sprintf "test%03d", $_ ); }
  • 52. CREATE TABLE objects ( id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, fid INT UNSIGNED NOT NULL, bucket_id INT UNSIGNED NOT NULL, rid SMALLINT UNSIGNED NOT NULL, gid SMALLINT UNSIGNED NOT NULL, filename VARCHAR(1024), INDEX (fid, bucket_id), INDEX (bucket_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin SELECT * FROM objects WHERE fid = murmur($filename) AND bucket_id = $bucket AND filename = $filename
  • 53. CREATE TABLE entries_int ( id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, fid INT UNSIGNED NOT NULL, bid INT UNSIGNED NOT NULL, filename VARCHAR(255) NOT NULL, INDEX (fid, bid), INDEX (bid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin CREATE TABLE entries_char ( id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, filename VARCHAR(255) NOT NULL, bid INT UNSIGNED NOT NULL, UNIQUE INDEX (filename, bid), INDEX (bid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin
  • 54. .--------------------------------------------------. | table_name | total_kb | data_kb | index_kb | +--------------+-----------+-----------+-----------+ | entries_char | 3360.0000 | 1552.0000 | 1808.0000 | | entries_int | 2080.0000 | 1552.0000 | 528.0000 | '--------------+-----------+-----------+-----------' (filename 32 )
  • 55. @nodes = sort { murmur_hash(join "/", $a->{node_id},$bucket,$filename) <=> murmur_hash(join "/", $b->{node_id},$bucket,$filename) } @nodes;

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n