Aniki has come

40
Aniki has come id:karupanerura Gotanda.pm #7 vs Yokohama.pm #13

Transcript of Aniki has come

Aniki has comeid:karupanerura Gotanda.pm #7 vs Yokohama.pm #13

What is Aniki?

• Simple Architecture

• Object Mapping

• Relationship Support

• Pluggable / Extendable

• Fast

Concept of architecture:

“Do it simple stupid!”

Do it simple

• Aniki - provides main interface

• Aniki::Handler - provides DBI handling

• Aniki::Schema - provides Schema meta API

• Aniki::Result - provides SELECT query’s result

• Aniki::Row - provides access to row

Don’t do difficult somethings

• Delegate to:

• DBIx::Handler (DBIx::TransactionManager)

• DBIx::Schema::DSL (SQL::Translator)

• SQL::Maker (SQL::QueryMaker)

• SQL::NamedPlaceholder

Code Lines

• Teng: 1908[lines]

• Aniki: 2444[lines]

• DBIx::Class: 39710[lines]

Modules

• Teng: 18[modules]

• Aniki: 25[modules]

• DBIx::Class: 154[modules]

Rule of least surprise

• Write clearly a side effect

• e.g.) insert => execute insert only

• e.g.) insert_and_fetch_id

• e.g.) insert_and_fetch_row

• No write clearly, No do it.

“Don’t repeat your self”

Don’t repeat your self

• Abstraction Layers

• Aniki::Handler - connection handling

• Aniki::Reuslt::Collection - collection

• Aniki::Result::Row - row of result

Don’t repeat your self

• Object Mapping

• Relationship Support

• Plugin Architecture

• I’ll talk about these later

“YAGNI”

Implement it later!

• Relationship Support

• I’ll talk about it.

Object Mapping

Object Mapping

• Map a row to a row object

• Can change the class of object each tables

• Map rows to a collection object

• Also you can it too :)

• And, you can disable it, If you want.

Example:

# $rows is a Aniki::Result::Collectionmy $rows = $db->select(user => {});$rows->first; ## first of row

Example:

# $rows is a Arraymy $rows = $db->select(user => {}, { suppress_result_object => 1,});$rows->[0]; ## first of row

Example:

# $row is a Aniki::Result::Rowmy $row = $db->select(user => { id => 1,}, { limit => 1,})->first;$row->id; ## => 1

Example:

# $row is a Hashmy $row = $db->select(user => { id => 1,}, { limit => 1, suppress_row_objects => 1,})->first;$row->{id}; ## => 1

DEMO

Example:

# $user is a Aniki::Result::Row$db->update($user => { name => 1,});

Example:

# $user is a Aniki::Result::Row$db->delete($user);

DEMO

Relationship Support

Relationship Support

• Fetch related rows from ORM

• a.k.a.) prefetch

• Useful for suppress query executions.

Example:

$db->select(user => { id => [1..10],}, { prefetch => [qw/user_items/],});

Example:

$db->select(user => { id => [1..10],}, { prefetch => { user_items => [qw/item/], },});

DEMO

Plaggable / Extendable

Pluggable

• Mo[ou]se::Role is useful for plugin system

• You can write plugin as Mouse::Role for Aniki

Extendable

• Aniki

• Aniki::Handler

• e.g.) Aniki::Handler::WeightedRoundRobin

• Aniki::Result::Row

• Aniki::Result::Collection

Fast

Benchmark

• SELECT:

• 19% faster than Teng

• 148% faster than DBIx::Class

Benchmark

• INSERT(and fetch row):

• 18% faster than Teng

• 40% faster than DBIx::Class

• INSERT(and fetch id):

• 21% faster than Teng

Benchmark

• UPDATE(from row):

• 34% slower than Teng

• 49% faster than DBIx::Class

• UPDATE(from where condition):

• 5% faster than Teng

Benchmark

• DELETE(from row):

• 2% slower than Teng

• 422% faster than DBIx::Class

• DELETE(from where condition):

• 5% faster than Teng

Conclusion

Conclusion

• Aniki is Simple, Simple is best.

• Pluggable

• Extendable

• and, Fast

Thank you for listening