ktats****@users*****
ktats****@users*****
2009年 9月 18日 (金) 11:52:07 JST
Index: docs/modules/KiokuDB-0.32/KiokuDB/Tutorial.pod diff -u docs/modules/KiokuDB-0.32/KiokuDB/Tutorial.pod:1.3 docs/modules/KiokuDB-0.32/KiokuDB/Tutorial.pod:1.4 --- docs/modules/KiokuDB-0.32/KiokuDB/Tutorial.pod:1.3 Mon Sep 14 06:06:54 2009 +++ docs/modules/KiokuDB-0.32/KiokuDB/Tutorial.pod Fri Sep 18 11:52:07 2009 @@ -2,63 +2,111 @@ =pod -=head1 åå +=head1 NAME + +(åå) + +=begin original KiokuDB::Tutorial - Getting started with L<KiokuDB> -=head1 ã¤ã³ã¹ãã¼ã« +=end original + +KiokuDB::Tutorial - L<KiokuDB>ãå§ããã + +=head1 Install + +(ã¤ã³ã¹ãã¼ã«) + +=begin original The easiest way to install L<KiokuDB> and a number of backends is L<Task::KiokuDB>. +=end original + L<KiokuDB>ã¨ããã¯ã¨ã³ããä¸ç·ã«ã¤ã³ã¹ãã¼ã«ããã«ã¯ãL<Task::KiokuDB>ãã¤ã³ã¹ãã¼ã«ããã®ãä¸çªç°¡åã§ãã +=begin original + L<KiokuDB> depends on L<Moose> and a few other modules out of the box, but no specific storage module. -L<KiokuDB>ã¯L<Moose>ã¨ãããã¤ãã®ä»ã®åµé çãªã¢ã¸ã¥ã¼ã«ã«ä¾åãã¦ãã¾ãã -ã§ãããç¹å®ã®ã¹ãã¬ã¼ã¸ã¢ã¸ã¥ã¼ã«ã«ã¯ä¾åãã¦ãã¾ããã +=end original + +L<KiokuDB>ã¯L<Moose>ã¨ãããã¤ãã®ããã«ä½¿ããã¢ã¸ã¥ã¼ã«ã«ä¾åãã¦ãã¾ããã +ç¹å®ã®ã¹ãã¬ã¼ã¸ã¢ã¸ã¥ã¼ã«ã«ã¯ä¾åãã¦ãã¾ããã + +=begin original L<KiokuDB> is a frontend to several backends, much like L<DBI> uses DBDs to connect to actual databases. +=end original + L<KiokuDB>ã¯è¤æ°ã®ããã¯ã¨ã³ãã®ããã³ãã¨ã³ãã§ãã L<DBI>ãå®éã®ãã¼ã¿ãã¼ã¹ã¸ã®æ¥ç¶ã«DBDã使ã£ã¦ããã®ã«ä¼¼ã¦ãã¾ãã +=begin original + For development and testing you can use the L<KiokuDB::Backend::Hash> backend, which is an in memory store, but for production L<KiokuDB::Backend::BDB> or L<KiokuDB::Backend::DBI> are the recommended backends. -éçºç¨ãããã¹ãã¨ãã¦ãã¡ã¢ãªã¼ã¹ãã¢ã®L<KiokuDB::Backend::Hash>ããã¯ã¨ã³ãã使ããã¨ãã§ãã¾ãã +=end original + +éçºç¨ããã¹ãã¨ãã¦ãã¡ã¢ãªã«ä¿åããL<KiokuDB::Backend::Hash>ããã¯ã¨ã³ãã使ããã¨ãã§ãã¾ãã ãããã¯ã·ã§ã³ã«ã¯ãL<KiokuDB::Backend::DBD>ãL<KiokuDB::Backend::DBI>ãããã¯ã¨ã³ãã¨ãã¦æ¨å¥¨ãã¾ãã +=begin original + See below for instructions on getting L<KiokuDB::Backend::BDB> installed. +=end original + L<KiokuDB::Backend::DBD>ãã¤ã³ã¹ãã¼ã«ãã¦ã以ä¸ã®ã¤ã³ã¹ãã©ã¯ã·ã§ã³ãè¦ã¦ãã ããã -=head1 ãã£ã¬ã¯ããªã®ä½æ +=head1 CREATING A DIRECTORY + +(ãã£ã¬ã¯ããªã®ä½æ) + +=begin original A KiokuDB directory is the object that contains all the common functionality regardless of the backend. +=end original + KiokuDBãã£ã¬ã¯ããªã¯ãªãã¸ã§ã¯ãã§ãããã¯ã¨ã³ã以å¤ã®ãã¹ã¦ã®å ±éã®æ©è½ãå«ã¿ã¾ãã +=begin original + The simplest directory ready for use can be created like this: +=end original + ããã«ä½¿ãããã£ã¨ãåç´ãªãã£ã¬ã¯ããªã¯æ¬¡ã®ããã«ä½ãã¾ã: my $dir = KiokuDB->new( backend => KiokuDB::Backend::Hash->new ); +=begin original + We will revisit other more interesting backend configuration later in this document, but for now this will do. +=end original + ãã®ããã¥ã¡ã³ãã®æå¾ã«ãä»ã®ãã£ã¨é¢ç½ãããã¯ã¨ã³ãã®è¨å®ãç´¹ä»ãã¾ããã ã¨ããããããã£ã¦ã¿ã¾ãã +=begin original + You can also use DSN strings to connect to the various backends: +=end original + ä»ã®ããã¯ã¨ã³ãã«æ¥ç¶ããããã«DSNæååã使ããã¨ãã§ãã¾ãã KiokuDB->connect("hash"); @@ -67,15 +115,23 @@ KiokuDB->connect("bdb:dir=foo", create => 1); +=begin original + Or use a configuration file +=end original + ã¾ãã¯ãè¨å®ãã¡ã¤ã«ã§ã KiokuDB->connect("/path/to/my_db.yml"); KiokuDB->connect("/path/to/dir"); +=begin original + With a configuration file like this: +=end original + è¨å®ãã¡ã¤ã«ã¯æ¬¡ã®ããã«ãªãã¾ã: backend: @@ -83,24 +139,38 @@ dsn: dbi:SQLite:dbname=/tmp/test.db create: 1 -=head1 DBIããã¯ã¨ã³ãã使ã +=head1 USING THE DBI BACKEND + +(DBIããã¯ã¨ã³ãã使ã) + +=begin original During this tutorial we will be using the DBI backend for two reasons. The first is L<DBI>'s ubiquity - almost everyone has used and knows how to install and use it. The second the possibility of easily looking behind the scenes, to more clearly demonstrate what L<KiokuDB> is doing. +=end original + 2ã¤ã®çç±ã§ããã®ãã¥ã¼ããªã¢ã«ã§ã¯ãDBIããã¯ã¨ã³ãã使ãã¾ãã -1ã¤ç®ã®çç±ã¯ï¼L<DBI>ãã©ãã«ã§ãããããã§ã - ã»ã¨ãã©ãã¹ã¦ã®äººãã¤ã³ã¹ãã¼ã«æ¹æ³ã -使ãæ¹ãç¥ã£ã¦ãã¾ãã2ã¤ç®ã®çç±ã¯ãç°¡åã«è£èå°ãè¦ããã¨ãåºããããã§ãã +1ã¤ç®ã®çç±ã¯ãL<DBI>ãã©ãã«ã§ãããããã§ã - ã»ã¨ãã©ãã¹ã¦ã®äººãã¤ã³ã¹ãã¼ã«æ¹æ³ã +使ãæ¹ãç¥ã£ã¦ãã¾ãã2ã¤ç®ã®çç±ã¯ãç°¡åã«è£èå°ãè¦ããã¨ãåºæ¥ãããã§ãã L<KiokuDB>ã®ä½¿ããæ¹ããããããããããã¢ã³ã¹ãã¬ã¼ãã§ããããã§ãã +=begin original + That said, the examples will work with all backends exactly the same. +=end original + ãã®ä¾ã§ãã¹ã¦ã®ããã¯ã¨ã³ããã¾ã£ããåãããã«åãã¾ãã +=begin original + The C<$dir> variable used below is created like this: +=end original + 以ä¸ã§ä½¿ãC<$dir>å¤æ°ã¯ä¸è¨ã®ããã«ä½ããã¾ã: my $dir = KiokuDB->connect( @@ -108,10 +178,14 @@ create => 1, ); +=begin original + Note that if you are connecting with a username and password you must specify these as named arguments: -ã¦ã¼ã¶ã¼åã¨ãã¹ã¯ã¼ãã§æ¥ç¶ããå ´åï¼ååä»ãã®å¼æ°ãæå®ããªãã¨ããã¾ãã: +=end original + +ã¦ã¼ã¶ã¼åã¨ãã¹ã¯ã¼ãã§æ¥ç¶ããå ´åãååä»ãã®å¼æ°ãæå®ããªãã¨ããã¾ãã: my $dir = KiokuDB->connect( $dsn, @@ -119,10 +193,16 @@ password => $password, ); -=head1 ãªãã¸ã§ã¯ãã®ã¤ã³ãµã¼ã +=head1 INSERTING OBJECTS + +(ãªãã¸ã§ã¯ãã®ã¤ã³ãµã¼ã) + +=begin original Let's start by defining a simple class using L<Moose>: +=end original + L<Moose>ã使ã£ãç°¡åãªã¯ã©ã¹ãå®ç¾©ãã¦ã¿ã¾ããã: package Person; @@ -133,89 +213,135 @@ is => "rw", ); +=begin original + We can instantiate it: +=end original + ãããã¤ã³ã¹ã¿ã³ãåãã¾ã: my $obj = Person->new( name => "Homer Simpson" ); +=begin original + and insert the object to the database as follows: +=end original + ä¸è¨ã®ããã«ãªãã¸ã§ã¯ãããã¼ã¿ãã¼ã¹ã«å ¥ãã¾ã: my $scope = $dir->new_scope; my $homer_id = $dir->store($obj); +=begin original + This is very trivial use of L<KiokuDB>, but it illustrates a few important things. +=end original + ããã¯ãL<KiokuDB>ã®ã¨ã¦ãæ®éã®ä½¿ãæ¹ã§ããã§ãããããã¤ãéè¦ãªãã¨ã示ãã¦ãã¾ãã +=begin original + First, no schema is necessary. L<KiokuDB> can use L<Moose> to introspect your object without needing to predefine anything like tables. +=end original + ã¾ããã¹ãã¼ãã¯å¿ è¦ããã¾ãããL<KiokuDB>㯠ãã¼ãã«ã®ãããªä½ããäºåã«å®ç¾©ããå¿ è¦ã¯ããã¾ããã ãªãã¸ã§ã¯ãã®æ å ±ãåãåºãããã«ãL<Moose>ã使ããã¨ãã§ãã¾ãã +=begin original + Second, every object in the database has an ID. If you don't choose an ID for an object, L<KiokuDB> will assign a UUID instead. The ID is like a primary key in a relational database. If you want to choose an ID for your object, you can do something like: +=end original + 2çªç®ã«ããã¼ã¿ãã¼ã¹ã«å ¥ã£ã¦ãããã¹ã¦ã®ãªãã¸ã§ã¯ãã«ã¯IDãããã¾ãã ãªãã¸ã§ã¯ãã«IDãé¸ã°ãªãããã°ãL<KiokuDB>ã代ããã«UUIDãå²ãå½ã¦ã¾ãã IDã¯ãªã¬ã¼ã·ã§ãã«ãã¼ã¿ãã¼ã¹ã®ãã©ã¤ããªã¼ãã¼ã®ãããªãã®ã§ãã -èªåã§ãªãã¸ã§ã¯ãã«IDãæ¯ããããã°ï¼æ¬¡ã®ããã«ãããã¨ãã§ãã¾ã: +èªåã§ãªãã¸ã§ã¯ãã«IDãæ¯ããããã°ã次ã®ããã«ãããã¨ãã§ãã¾ã: $dir->store( homer => $obj ); +=begin original + and C<$obj>'s ID will be C<homer>. If you don't provide an ID a UUID will be assigned automatically. +=end original + ããã§ãC<$obj>ã®IDã¯C<homer>ã«ãªãã¾ããIDãä¸ããªããã°ãUUIDãèªåçã«ãµããã¾ãã +=begin original + Third, all L<KiokuDB> operations need to be performed within a B<scope>. The scope does not apply to a simple example like the above, but becomes necessary once weak references are used. We will look into that in more detail later. +=end original + 3çªç®ã«ããã¹ã¦ã®L<KiokuDB>æä½ã¯B<scope>å ã§è¡ãå¿ è¦ãããã¾ãã -ã¹ã³ã¼ãã¯ä¸ã®ãããªç°¡åãªä¾ã«ã¯é©ãã¾ãããï¼weakãªãã¡ã¬ã³ã¹ã使ãããããã«ãªãã¨ã +ã¹ã³ã¼ãã¯ä¸ã®ãããªç°¡åãªä¾ã«ã¯é©ãã¾ããããweakãªãã¡ã¬ã³ã¹ã使ãããããã«ãªãã¨ã å¿ è¦ã«ãªãã¾ããå¾ã§ãã詳細ã«è¦ã¦ããã¾ãã -=head1 ãªãã¸ã§ã¯ãã®èªã¿åºã +=head1 LOADING OBJECTS + +(ãªãã¸ã§ã¯ãã®èªã¿åºã) + +=begin original So now that Homer has been inserted into the database, we can fetch him out of there using the ID we got from C<store>. +=end original + ãã¦ããã¼ã¿ãã¼ã¹ã«Homerãå ¥ãã¾ãããC<store>ããå¾ãIDã§åãåºãã¾ãã my $homer = $dir->lookup($homer_id); +=begin original + Assuming that C<$scope> and C<$obj> are still in scope, C<$homer> and C<$obj> will actually be the same reference: -C<$scope>ã¨C<$obj>ã¯ãã¹ã³ã¼ãå ã«ããã¨ãã¾ããC<$homer>ã¨C<$obj>ã¯å®éã«ãåããªãã¡ã¬ã³ã¹ã§ãã +C<$scope>ã¨C<$obj>ã¯ãã¹ã³ã¼ãå ã«ããã¨ãã¾ããC<$homer>ã¨C<$obj>ã¯å®éã«ãåããªãã¡ã¬ã³ã¹ã«ãªãã¾ãã refaddr($homer) == refaddr($obj) +=begin original + This is because L<KiokuDB> tracks which objects are "live" in the B<live object set> (L<KiokuDB::LiveObjects>). -B<çåãã¦ãããªãã¸ã§ã¯ã> (L<KiokuDB::LiveObjects>)å ã®ãªãã¸ã§ã¯ãã +=end original + +B<çåãã¦ãããªãã¸ã§ã¯ãã»ãã> (L<KiokuDB::LiveObjects>)å ã®ãªãã¸ã§ã¯ãã "çå"ãã¦ããããL<KiokuDB>ã追跡ãã¦ããããã§ãã +=begin original + If C<$obj> and C<$scope> are no longer in scope you'd need to create a new scope, and then fetch the object from the database again: -C<$obj>ã¨C<$scope>ã¯ããã¹ã³ã¼ãã«ããªããã°ãæ°ããã¹ã³ã¼ããä½ããªããã°ããã¾ããã +=end original + +C<$obj>ã¨C<$scope>ããããã¹ã³ã¼ãã«ããªããã°ãæ°ããã¹ã³ã¼ããä½ããªããã°ããã¾ããã åã³ãã¼ã¿ãã¼ã¹ãããªãã¸ã§ã¯ããåãåºãã¾ã: my $scope = $dir->new_scope; my $homer = $dir->lookup($homer_id); +=begin original + In this case since the original instance of Homer is no longer live, but has been garbage collected by Perl, L<KiokuDB> will fetch it from the backend. @@ -223,20 +349,26 @@ Perlã«ããã¬ãã¼ã¸ã³ã¬ã¯ãããã¦ãã¾ãã L<KiokuDB>ã¯ã¤ã³ã¹ã¿ã³ã¹ãããã¯ã¨ã³ãããåå¾ãã¾ãã -=head1 ä½ãä¿åããã¦ããã +=head1 WHAT WAS STORED + +(ä½ãä¿åãããã) + +=begin original Let's peek into the database momentarily. Launch the SQL command line tool to your database: -ããã«ãã¼ã¿ãã¼ã¹ãè¦ãã¦ã¿ã¾ãããããã¼ã¿ãã¼ã¹ã«å¯¾ãã¦SQLã³ãã³ãã©ã¤ã³ãã¼ã«ã -å©ãã¦ã¿ã¾ããã: +=end original +ããã«ãã¼ã¿ãã¼ã¹ãè¦ãã¦ã¿ã¾ããããSQLã³ãã³ãã©ã¤ã³ãã¼ã«ãèµ·åãã¾ããã: % sqlite3 kiokudb_tutorial.db SQLite version 3.4.0 Enter ".help" for instructions sqlite> +=begin original + The database schema has two tables, C<entries> and C<gin_index>: ãã¼ã¿ãã¼ã¹ã®ã¹ãã¼ãã«ã¯2ã¤ã®ãã¼ãã«ãããã¾ããC<entries>ã¨C<gin_index>ã§ã: @@ -244,11 +376,17 @@ sqlite> .tables entries gin_index +=begin original + C<gin_index> is used for more complex queries, and we'll get back to it at the end of the tutorial. +=end original + C<gin_index>ã¯ããè¤éãªã¯ã¨ãªã«ä½¿ããã¾ãããã¥ã¼ããªã¢ã«ã®æå¾ã«æ±ãã¾ãã +=begin original + Let's have a closer look at C<entries>: C<entries>ãããè¦ã¾ããã: @@ -263,56 +401,94 @@ PRIMARY KEY (id) ); +=begin original + The main columns are C<id> and C<data>. In L<KiokuDB> every object has an ID which serves as a primary key and a BLOB of data associated with it. +=end original + ã¡ã¤ã³ã®ã«ã©ã ã¯C<id>ã¨C<data>ã§ããL<KiokuDB>ã«ããããã¹ã¦ã®ãªãã¸ã§ã¯ãã«ã¯IDãããã ãã©ã¤ããªãã¼ã¨BLOBãã¼ã¿ãé¢é£ä»ãããã¦ãã¾ãã +=begin original + Since the default serializer for the DBI backend is L<KiokuDB::Serializer::JSON>, we can peek at the data. +=end original + DBIããã¯ã¨ã³ãã®ããã©ã«ãã®ã·ãªã¢ã©ã¤ã¶ã¼ã¯L<KiokuDB::Serializer::JSON>ã§ãã®ã§ã ãã¼ã¿ãè¦ãè¦ããã¨ãã§ãã¾ãã +=begin original + First we'll set C<sqlite>'s output mode to C<line>. This is easier to read with large columns: +=end original + æåã«ãC<sqlite>ã®åºåã¢ã¼ããC<line>ã«ã»ãããã¦ãã ããã大ããã«ã©ã ã§ãè¦ããããªãã¾ã: sqlite> .mode line +=begin original + And select the data from the table: +=end original + ãã¼ãã«ãããã¼ã¿ãåå¾ãã¾ã: sqlite> select id, data from entries; id = 201C5B55-E759-492F-8F20-A529C7C02C8B data = {"__CLASS__":"Person","data":{"name":"Homer Simpson"},"id":"201C5B55-E759-492F-8F20-A529C7C02C8B","root":true} +=begin original + As you can see the C<name> attribute is stored under the C<data> key inside the blob, as is the object's class. +=end original + ä¸è¨ã®ããã«ãC<name>å±æ§ã¯blobå ã®C<data>ãã¼ã«ãªãã¸ã§ã¯ãã®ã¯ã©ã¹ã¨ãã¦ä¿åããã¦ãã¾ãã +=begin original + The C<data> column contains all of the data necessary to recreate the object. +=end original + C<data>ã«ã©ã ã¯ãªãã¸ã§ã¯ããåä½æããã®ã«å¿ è¦ãªãã¹ã¦ã®ãã¼ã¿ãå«ãã§ãã¾ãã +=begin original + All the other columns are used solely for lookups. Later on we'll show how to create more search columns. -ä»ã®ãã¹ã¦ã®ã«ã©ã ã¯åã«æ¤ç´¢ã®ããã«ä½¿ããã¾ããå¾ã§ï¼ã©ã®ããã«æ¤ç´¢ç¨ã®ã«ã©ã ãä½ãã®ããè¦ãã¾ãã +=end original + +ä»ã®ãã¹ã¦ã®ã«ã©ã ã¯åã«æ¤ç´¢ã®ããã«ä½¿ããã¾ããå¾ã§ãã©ã®ããã«æ¤ç´¢ç¨ã®ã«ã©ã ãä½ãã®ããè¦ãã¾ãã + +=begin original When using L<KiokuDB::Backend::BDB> the on-disk format is actually a hash of C<id> to C<data>. +=end original + L<KiokuDB::Backend::DBD>ã使ã£ãå ´åã¯ããã£ã¹ã¯ä¸ã®ãã©ã¼ãããã¯ãå®éã«ã¯ãC<id>ããC<data>ã®ããã·ã¥ã«ãªãã¾ãã -=head1 ãªãã¸ã§ã¯ãã®é¢ä¿æ§ +=head1 OBJECT RELATIONSHIPS + +(ãªãã¸ã§ã¯ãã®ãªã¬ã¼ã·ã§ã³ã·ãã) + +=begin original Let's extend the C<Person> class to hold some more interesting data than just a C<name>: +=end original + C<Person>ã¯ã©ã¹ã«C<name>ãããããã£ã¨é¢ç½ããã¼ã¿ã追å ãã¦ã¿ã¾ããã: package Person; @@ -323,20 +499,32 @@ weak_ref => 1, ); +=begin original + This new C<spouse> attribute will hold a reference to another person object. +=end original + C<spouse>å±æ§ã¯ä»ã®Personãªãã¸ã§ã¯ãã®ãªãã¡ã¬ã³ã¹ãæã¡ã¾ãã +=begin original + Let's first create and insert another object: +=end original + ã¾ãã¯ãä»ã®ãªãã¸ã§ã¯ããä½ãã¾ããã: my $marge_id = $dir->store( Person->new( name => "Marge Simpson" ), ); +=begin original + Now that we have both objects in the database, let's link them together: +=end original + ãã¼ã¿ãã¼ã¹ã«ä¸¡æ¹ã®ãªãã¸ã§ã¯ããæããã¾ãã2ã¤ãä¸ç·ã«ãªã³ã¯ãã¾ããã: { @@ -350,26 +538,42 @@ $dir->store( $marge, $homer ); } +=begin original + Now we have created a persistent B<object graph>, that is several objects which point to each other. +=end original + ä»ãæ°¸ç¶çãªB<ãªãã¸ã§ã¯ãã°ã©ã>ãä½ãã¾ãããããã¯ãè¤æ°ã®ãªãã¸ã§ã¯ããäºãã«åç §ãã¦ãã¾ãã +=begin original + The reason C<spouse> had the C<weak_ref> option was so that this circular structure will not leak. +=end original + C<spouse>ã«ã¯C<weak_ref>ãªãã·ã§ã³ãããã¾ããã®ã§ããã®å¾ªç°æ§é ã¯ãªã¼ã¯ãã¾ããã +=begin original + When then objects are updated in the database, L<KiokuDB> sees that their C<spouse> attribute contains references, and this relationship will be encoded using their unique ID in storage. +=end original + ãã¼ã¿ãã¼ã¹ã§ãªãã¸ã§ã¯ããæ´æ°ãããããL<LinkDB>ã¯C<spouse>å±æ§ãå«ããªãã¡ã¬ã³ã¹ãè¦ã¦ã ãã®é¢ä¿ã¯ã¹ãã¬ã¼ã¸å ã§ã¦ãã¼ã¯ãªIDã使ã£ã¦ã¨ã³ã³ã¼ãããã¾ãã +=begin original + To load the graph, we can do something like this: -ãã®ã°ã©ãããã¼ãããããã«ï¼æ¬¡ã®ããã«ã§ãã¾ã: +=end original + +ãã®ã°ã©ãããã¼ãããããã«ã次ã®ããã«ã§ãã¾ã: { my $scope = $dir->new_scope; @@ -389,39 +593,61 @@ refaddr($marge) == refaddr($marge->spouse->spouse); # true } +=begin original + When L<KiokuDB> is loading the initial object, all the objects the object depends on will also be loaded. The C<spouse> attribute contains a reference to another object (by ID), and this link is resolved at inflation time. +=end original + L<KiokuDB>ãæåã®ãªãã¸ã§ã¯ãããã¼ããããããã®ãªãã¸ã§ã¯ããä¾åãã¦ãã ãã¹ã¦ã®ãªãã¸ã§ã¯ãããã¼ãããã¾ããC<spouse>å±æ§ã¯ä»ã®ãªãã¸ã§ã¯ãã(IDã§) -æã£ã¦ããã®ã§ï¼ã¤ã³ãã¬ã¼ã·ã§ã³æã«ãã®ãªã³ã¯ã解決ãã¾ãã +æã£ã¦ããã®ã§ãã¤ã³ãã¬ã¼ã·ã§ã³æã«ãã®ãªã³ã¯ã解決ãã¾ãã + +=head2 The purpose of C<new_scope> + +(C<new_scope>ã®ç®ç) -=head2 C<new_scope>ã®ç®ç +=begin original This is where C<new_scope> becomes important. As objects are inflated from the database, they are pushed onto the live object scope, in order to increase their reference count. +=end original + C<new_scope>ãéè¦ã«ãªãã¨ããã§ãããªãã¸ã§ã¯ãã¯ãã¼ã¿ãã¼ã¹ããã¤ã³ãã¬ã¼ãããã ãªãã¡ã¬ã³ã¹ã«ã¦ã³ããå¢ããããã«ãçåãã¦ãããªãã¸ã§ã¯ãã¹ã³ã¼ãã«è¿½å ããã¾ãã +=begin original + If this was not done, by the time C<$homer> was returned from C<lookup> his C<spouse> attribute would have been cleared because there is no other reference to Marge. +=end original + ãããããã¦ããªããã°ãC<lookup>ããC<$homer>ãæ»ã£ã¦ããæã«ã C<spouse>å±æ§ãã¯ãªã¢ããã¾ãããã¼ã¸ããªãã¨ãããªãä»ã®ãªãã¡ã¬ã³ã¹ããªãããã§ãã +=begin original + If, on the other hand the circular structure was not weak, it would have to be broken manually, which is very error prone. -ãããä¸æ¹ã§ï¼å¾ªç°æ§é ãweakã§ãªããã°ï¼æã§å£ããªããã°ããã¾ããã +=end original + +ãããä¸æ¹ã§ã循ç°æ§é ãweakã§ãªããã°ãæã§å£ããªããã°ããã¾ããã ããã¯ãã¨ã¦ãã¨ã©ã¼ã«ãªããããã§ãã +=begin original + By using this idiom: +=end original + 次ã®ã¤ãã£ãªã ã使ã£ã¦: { @@ -430,31 +656,53 @@ # do all KiokuDB work in here } +=begin original + You are ensuring that the objects live at least as long as is necessary. +=end original + å°ãªãã¨ãå¿ è¦ã§ããæéã¯ãªãã¸ã§ã¯ããçãã¦ãããã¨ã確ä¿ã§ãã¾ãã +=begin original + In a web application context usually you create one new scope per request. +=end original + Webã¢ããªã±ã¼ã·ã§ã³ã®ã³ã³ããã¹ãã§ã¯ãæ®éãªã¯ã¨ã¹ããã¨ã«æ°ããã¹ã³ã¼ããä½ãã¾ãã +=begin original + While scopes can nest, this is not a requirement. +=end original + ã¹ã³ã¼ãããã¹ãã§ãããªããå¿ é ã§ã¯ããã¾ããã +=begin original + You are free to create as many or as few scopes as you like, as long as there is at least one, but note that child scopes refer to their parents to ensure that all objects that were already live at the time that a scope is created are still alive. +=end original + å°ãªãã¨ãä¸ã¤ã®ã¹ã³ã¼ããããã°ã好ããªã ãå¤ãã®ãã¾ãã¯ãå°ãªãã¹ã³ã¼ããä½ããã¨ãã§ãã¾ãã ãã®æã«ä½ãããã¹ã³ã¼ãã«ãã§ã«ãããã¹ã¦ã®ãªãã¸ã§ã¯ãã確å®ã«ãã親ãåç §ãã¦ããåä¾ã®ã¹ã³ã¼ããã¾ã çãã¦ãã¾ãã -=head1 ãã¼ã¿ãã¼ã¹å ã®ãªãã¡ã¬ã³ã¹ +=head1 REFERENCES IN THE DATABASE + +(ãã¼ã¿ãã¼ã¹å ã®ãªãã¡ã¬ã³ã¹) + +=begin original Now that we have an object graph in the database let's have another look at what's inside. +=end original + ãã¦ããã¼ã¿ãã¼ã¹ã«ãªãã¸ã§ã¯ãã°ã©ããããã¾ããå é¨ãã©ããªã£ã¦ãããè¦ã¦ã¿ã¾ãããã sqlite> select id, data from entries; @@ -464,38 +712,60 @@ id = 05A8D61C-6139-4F51-A748-101010CC8B02 data = {"__CLASS__":"Person","data":{"name":"Marge Simpson","spouse":{"$ref":"201C5B55-E759-492F-8F20-A529C7C02C8B.data"}},"id":"05A8D61C-6139-4F51-A748-101010CC8B02","root":true} +=begin original + You'll notice the C<spouse> field has a JSON object with a C<$ref> field inside it holding the UUID of the target object. +=end original + C<spouse>ãã£ã¼ã«ããJSONãªãã¸ã§ã¯ãã¨ãããã¨ã«æ°ã¥ãã§ãããã ããã¦ããã®å é¨ã®C<$ref>ãã£ã¼ã«ãã«ã¯ã対象ã®ãªãã¸ã§ã¯ãã®UUIDãããã¾ãã +=begin original + When data is loaded L<KiokuDB> queues up references to unloaded objects and then loads them in order to materialize the memory resident object graph. +=end original + ãã¼ã¿ããã¼ããããã¨ãL<KiokuDB>ã¯ãã¼ãããã¦ããªããªãã¸ã§ã¯ãã¸ã®ãªãã¡ã¬ã³ã¹ã ãã¥ã¼ã«å ¥ãã¦ããªãã¸ã§ã¯ãã°ã©ããã¡ã¢ãªã«å¸¸é§ãããããã«ããããããã¼ããã¾ãã +=begin original + If you're curious about why the data is represented this way, this format is called C<JSPON>, or JavaScript Persistent Object Notation (L<http://www.jspon.org/>). When using L<KiokuDB::Backend::Storable> the L<KiokuDB::Entry> and L<KiokuDB::Reference> objects are serialized with their storable hooks instead. -ãã¼ã¿ããã®ãããªæ¹æ³ã§è¡¨ç¾ããã¦ããçç±ã«ã¤ãã¦ç¥ããããã°ï¼ +=end original + +ãã¼ã¿ããã®ãããªæ¹æ³ã§è¡¨ç¾ããã¦ããçç±ã«ã¤ãã¦ç¥ããããã°ã ãã®ãã©ã¼ãããã¯ãC<JPSON>ã JavaScript Persistent Object notation(L<http://www.jpson.org>)ã¨å¼ã°ãã¦ãã¾ãã L<KiokuDB::Backend::Storable>ã使ãã¨ãL<KiokuDB::Entry>ã¨L<KiokuDB::Reference>ãªãã¸ã§ã¯ã㯠代ããã«ãstorableããã¯ã§ã·ãªã¢ã©ã¤ãºããã¾ãã -=head1 ãªãã¸ã§ã¯ãã»ãã +=head1 OBJECT SETS + +(ãªãã¸ã§ã¯ãã»ãã) + +=begin original More complex relationships (not necessarily 1 to 1) are fairly easy to model with L<Set::Object>. +=end original + ããè¤éãªãªã¬ã¼ã·ã§ã³ã·ãã(1対1ã«éããªã)ã¯ãL<Set::Object>ã§ããªãç°¡åã«ã¢ãã«åã§ãã¾ãã +=begin original + Let's extend the C<Person> class to add such a relationship: +=end original + C<Person>ã¯ã©ã¹ãæ¡å¼µãã¦ãã®ãããªãªã¬ã¼ã·ã§ã³ã·ããã足ãã¦ã¿ã¾ããã: package Person; @@ -505,8 +775,12 @@ is => "rw", ); +=begin original + L<KiokuDB::Set> objects are L<KiokuDB> specific wrappers for L<Set::Object>. +=end original + L<KiokuDB::Set>ãªãã¸ã§ã¯ãã¯ãL<Set::Object>ã®L<KiokuDB>ç¨ã®ã©ããã¼ã§ãã @@ -520,73 +794,110 @@ $dir->store($homer); +=begin original + The C<set> convenience function creates a new L<KiokuDB::Set::Transient> object. A transient set is one which started its life in memory space. +=end original + C<set>ã¨ãã便å©ãªé¢æ°ã¯æ°ããL<KiokuDB::Set::Transient>ãªãã¸ã§ã¯ããä½ãã¾ãã ä¸æçãªã»ããã¯ã¡ã¢ãªã¹ãã¼ã¹ã«åå¨ãããã®ã§ãã +=begin original + The C<weak_set> convenience function also exists, creating a transient set with L<Set::Object::Weak> used internally to help avoid circular structures (for instance if setting a C<parent> attribute in our example). +=end original + C<weak_set>ã¨ãã便å©ãªé¢æ°ãããã¾ãã -循ç°æ§é (ä¾ãã°ï¼ä»ã®ä¾ã«C<parent>å±æ§ã追å ãã)ãé¿ããããã«å é¨ã§ä½¿ããã¦ããã +循ç°æ§é (ä¾ãã°ãä»ã®ä¾ã«C<parent>å±æ§ã追å ãã)ãé¿ããããã«å é¨ã§ä½¿ããã¦ããã L<Set::Object::Weak>ã§ä¸æçãªã»ãããä½ãã¾ãã +=begin original + The set object behaves pretty much like a normal L<Set::Object>: +=end original + ãã®ãªãã¸ã§ã¯ãã¯æ®éã®L<Set::Object>ã¨ã»ã¨ãã©åãããã«æ¯ãèãã¾ãã my @kids = $dir->lookup($homer_id)->children->members; +=begin original + The main difference is that sets coming from the database are deferred by default, that is the objects in C<@kids> are not loaded until they are actually needed. +=end original + 主ãªéãã¯ãã»ããããã¼ã¿ãã¼ã¹ããæ¥ãã®ãããã©ã«ãã§é 延ããã¦ãããã¨ã§ãã C<@kids>ã«ãããªãã¸ã§ã¯ãã¯ãå®éã«å¿ è¦ã«ãªãã¨ãã¾ã§ãã¼ãããã¾ããã +=begin original + This allows large object graphs to exist in the database, while only being partially loaded, without breaking the encapsulation of user objects. This behavior is implemented in L<KiokuDB::Set::Deferred> and L<KiokuDB::Set::Loaded>. +=end original + ãã®ãã¨ã«ãããã¦ã¼ã¶ã¼ã®ãªãã¸ã§ã¯ãã®ã«ãã»ã«åãå£ããã¨ç¡ãã«ã é¨åçã«ãã¼ããããã®ã§ããã¼ã¿ãã¼ã¹ã«å·¨å¤§ãªãªãã¸ã§ã¯ãã°ã©ãããã£ã¦ãåé¡ã«ãªãã¾ããã ãã®æ¯ãèãã¯L<KiokuDB::Set::Deffered>ã¨L<KiokuDB::Set::Loaded>ã§å®è£ ããã¦ãã¾ãã +=begin original This set object is optimized to make most operations defer loading. For instance, if you intersect two deferred sets, only the members of the intersection set will need to be loaded. +=end original + ãã®ã»ãããªãã¸ã§ã¯ãã¯ãé 延ãã¼ãã®æä½ã«æé©åããã¦ãã¾ãã ä¾ãã°ã2ã¤ã®é 延ã»ããã横æãããªãã横æããã»ããã®ã¿ããã¼ããããå¿ è¦ãããã¾ãã =head1 THE TYPEMAP +=begin original + Storing an object with L<KiokuDB> involves passing it to L<KiokuDB::Collapser>, the object that "flattens" objects into L<KiokuDB::Entry> before the entries are inserted into the backend. +=end original + L<KiokuDB>ã«ãªãã¸ã§ã¯ããä¿åãããéã«ãL<KiokuDB::Collapser>ãééãã¾ãã ã¨ã³ããªã¼ãããã¯ã¨ã³ãã«ã¤ã³ãµã¼ããããåã«ãL<KiokuDB::Entry>ã«ã "å¹³ãã"ããããªãã¸ã§ã¯ããå ¥ãã¾ãã +=begin original + The collapser uses a L<KiokuDB::TypeMap> object that tells it how objects of each type should be collapsed. -collapserã«ã¯ï¼L<KiokuDB::TypeMap>ãªãã¸ã§ã¯ãã使ãã¾ãããã®ãªãã¸ã§ã¯ãã¯ã +=end original + +collapserã«ã¯ãL<KiokuDB::TypeMap>ãªãã¸ã§ã¯ãã使ãã¾ãããã®ãªãã¸ã§ã¯ãã¯ã ããããã®ã¿ã¤ãã®ãªãã¸ã§ã¯ããã©ã®ããã«ç ´æããããæãã¾ãã +=begin original + During retrieval of objects the same typemap is used to reinflate objects back into working objects. +=end original + ãªãã¸ã§ã¯ããåã£ã¦ããéããªãã¸ã§ã¯ããåã¤ã³ãã¬ã¼ããã¦ã ã¯ã¼ãã³ã°ãªãã¸ã§ã¯ãã«ããã®ã«ãåãtypemapã使ããã¾ãã +=begin original + Trying to store an object that is not in the typemap is an error. The reason behind this is that many objects depend on runtime states (for instance C<DBI> handles need a socket, objects based on XS modules have an internal pointer as @@ -594,31 +905,45 @@ even a small bit of unreported fragility is usually enough to create large, hard to debug problems. +=end original + typemapã«ãªããªãã¸ã§ã¯ããä¿åãããã¨ããã¨ã¨ã©ã¼ã«ãªãã¾ãã -ã©ã³ã¿ã¤ã ã®ç¶æ ã«ä¾åããå¤ãã®ãªãã¸ã§ã¯ããããããã§ã(ä¾ãã°ï¼C<DBI>ã¯ã½ã±ããããªãã¸ã§ã¯ãã +ã©ã³ã¿ã¤ã ã®ç¶æ ã«ä¾åããå¤ãã®ãªãã¸ã§ã¯ããããããã§ã(ä¾ãã°ãC<DBI>ã¯ã½ã±ããããªãã¸ã§ã¯ãã XSãã¼ã¹ã®ã¢ã¸ã¥ã¼ã«ã¯æ°å¤ã®ãããªå é¨çãªãã¤ã³ã¿ãæã¡ã¾ã)ã 大åã®ãªãã¸ã§ã¯ãã¯å®å ¨ã«ã·ãªã¢ã©ã¤ãºã§ããã«ããããããã ããããªå ±åãããªãããããã大ããªãããã°ã®é£ããåé¡ãä½ãã®ã¯ãããã¡ãªãã¨ã§ãã +=begin original + An exception to this rule is L<Moose> based objects, because they have sufficient meta information available through L<Moose>'s powerful reflection support in order to be safely serialized. +=end original + ãã®ã«ã¼ã«ã®ä¾å¤ã¯ãL<Moose>ãã¼ã¹ã®ãªãã¸ã§ã¯ãã§ããL<Moose>ã®å¼·å¤§ãª ãªãã¬ã¯ã·ã§ã³ãµãã¼ããéãã¦ãååãªã¡ã¿æ å ±ãå©ç¨ã§ããã®ã§ã å®å ¨ã«ã·ãªã¢ã©ã¤ãºåºæ¥ã¾ãã +=begin original + Additionally, the standard backends provide a default typemap for common objects (L<DateTime>, L<Path::Class>, etc), which by default is merged with any custom typemap you pass to L<KiokuDB>. -å ãã¦ï¼æ¨æºã®ããã¯ã¨ã³ãã¯å ±éã®ãªãã¸ã§ã¯ã(L<DateTime>, L<Path::Class>ãªã©>)ç¨ã« +=end original + +å ãã¦ãæ¨æºã®ããã¯ã¨ã³ãã¯å ±éã®ãªãã¸ã§ã¯ã(L<DateTime>, L<Path::Class>ãªã©>)ç¨ã« ããã©ã«ãã®typemapãæä¾ãã¦ãã¾ããL<KiokuDB>ã«ã©ããªã«ã¹ã¿ã ã®typemapã渡ããã¦ãã ããã©ã«ãã¨ãã¼ã¸ããã¾ãã +=begin original + So, in order to actually get L<KiokuDB> to store things like L<Class::Accessor> based objects, you can do something like this: +=end original + ããã§ãå®éã«L<KiokuDB>ã«L<Class::Accessor>ãã¼ã¹ã®ãªãã¸ã§ã¯ãã®ãããªãã®ãä¿åãããã«ã¯ã 次ã®ããã«ãã¾ã: @@ -631,25 +956,37 @@ ), ); +=begin original + L<KiokuDB::TypeMap::Entry::Naive> is a type map entry that performs naive collapsing of the object, by simply walking it recursively. +=end original + L<KiokuDB::TypeMap::Entry::Naive>ã¯åç´ã«å帰çã«ãã©ããã¨ã§ã ãªãã¸ã§ã¯ãã®ãã¤ã¼ããªç ´æãè¡ãã¾ãã +=begin original + When the collapser encounters an object it will ask L<KiokuDB::TypeMap::Resolver> for a collapsing routine based on the class of the object. +=end original + collapser ã¯ããªãã¸ã§ã¯ããè¦ã¤ããã¨ãL<KiokuDB::TypeMap::Resolver>ã«ã ãªãã¸ã§ã¯ãã®ã¯ã©ã¹ã«å¿ãããç ´æã«ã¼ãã³ãå°ãã¾ãã +=begin original + This lookup is typically performed by C<ref $object>, not using inheritance, because a typemap entry that is safe to use with a superclass isn't necessarily safe to use with a subclass. If you B<do> want inherited entries, specify C<isa_entries>: -ãã®æ¤ç´¢ã¯ãå ¸åçã«ã¯ï¼C<ref $object>ã§è¡ãããç¶æ¿ã使ãã¾ããã +=end original + +ãã®æ¤ç´¢ã¯ãå ¸åçã«ã¯ãC<ref $object>ã§è¡ãããç¶æ¿ã使ãã¾ããã ã¹ã¼ãã¼ã¯ã©ã¹ã§å®å ¨ã«ä½¿ããã¦ããtypemapã¨ã³ããªã¼ã¯ã å¿ ããããµãã¯ã©ã¹ã§å®å ¨ã«ä½¿ããã¨ã¯éããªãããã§ãã ç¶æ¿ãããã¨ã³ããªã¼ã«B<ããã>ãªããC<isa_entries>ãæå®ãã¦ãã ããã @@ -660,22 +997,30 @@ }, ); +=begin original + If no normal (C<ref> keyed) entry is found for an object, the isa entries are searched for a superclass of that object. Subclass entries are tried before superclass entries. The result of this lookup is cached, so it only happens once per class. -ãªãã¸ã§ã¯ãã«é常ã®(C<ref> keyed)ã¨ã³ããªã¼ãè¦ã¤ãããªããã°ï¼ +=end original + +ãªãã¸ã§ã¯ãã«é常ã®(C<ref> keyed)ã¨ã³ããªã¼ãè¦ã¤ãããªããã°ã isaã¨ã³ããªã¼ããªãã¸ã§ã¯ãã¹ã¼ãã¼ã¯ã©ã¹ã®ããã«æ¢ããã¾ãã ãµãã¯ã©ã¹ã¨ã³ããªã¼ã¯ã¹ã¼ãã¼ã¯ã©ã¹ã¨ã³ããªã¼ããåã«è©¦ããã¾ãã ãã®æ¤ç´¢ã®çµæã¯ãã£ãã·ã¥ãããã®ã§ãã¯ã©ã¹ãã¨ã«ä¸åããèµ·ããã¾ããã =head2 Typemap Entries +=begin original + If you want to do custom serialization hooks, you can specify hooks to collapse your object: -ã«ã¹ã¿ã ã®ã·ãªã¢ã©ã¤ãºã®ããã¯ã欲ãããã°ï¼èªåã®ãªãã¸ã§ã¯ããç ´æããããã® +=end original + +ã«ã¹ã¿ã ã®ã·ãªã¢ã©ã¤ãºã®ããã¯ã欲ãããã°ãèªåã®ãªãã¸ã§ã¯ããç ´æããããã® ããã¯ãæå®ã§ãã¾ãã @@ -696,12 +1041,20 @@ }, ); +=begin original + These hooks are called as methods on the object to be collapsed. +=end original + ãããã®ããã¯ã¯ãªãã¸ã§ã¯ããç ´æããã¨ãã«ãã¡ã½ããã¨ãã¦å¼ã°ãã¾ãã +=begin original + For instance the L<Path::Class> related typemap ISA entry is: +=end original + ä¾ãã°ãtypemapã®ISAã«é¢é£ããL<Path::Class>ã¯: 'Path::Class::Entity' => KiokuDB::TypeMap::Entry::Callback->new( @@ -710,23 +1063,35 @@ expand => "new", ); +=begin original + The C<intrinsic> flag is discussed in the next section. +=end original + C<intrinsic>ãã©ã°ã¯æ¬¡ã®ã»ã¯ã·ã§ã³ã§è¿°ã¹ã¾ãã +=begin original + Another option for typemap entries is L<KiokuDB::TypeMap::Entry::Passthrough>, which is appropriate when you know the backend's serialization can handle that data type natively. +=end original + typemapã¨ã³ããªã®ããä¸ã¤ã®é¸æã¯L<KiokuDB::Typemap::Entry::Passthrough>ã§ãã ããã¯ã¨ã³ãã®ã·ãªã¢ã©ã¤ãºããã¤ãã£ãã«ãã¼ã¿ã¿ã¤ããæ±ããã¨ãã§ããã¨åãã£ã¦ããã°ã ããã¯é©åã§ãã +=begin original + For example, if your object has a L<Storable> hook which you know is appropriate (e.g. contains no sub objects that need to be collapsible) and your backend uses L<KiokuDB::Backend::Serialize::Storable>. L<DateTime> is an example of a class with such storable hopes: +=end original + ä¾ãã°ããªãã¸ã§ã¯ãã«é©åãªL<Storable>ããã¯ããã(ç ´å£ããå¿ è¦ã®ãããµããªãã¸ã§ã¯ããå«ã¾ãªã)ã ããã¯ã¨ã³ãã«ã¯ãL<KiokuDB::Backend::Serialize::Storable>ã使ãå ´åã§ãã L<DateTime>ã¯ãã®ããã«storableãæãã¯ã©ã¹ã®ä¾ã§ã: @@ -735,38 +1100,58 @@ =head2 Intrinsic vs. First Class +=begin original + In L<KiokuDB> every object is normally assigned an ID, and if the object is shared by several objects this relationship will be preserved. +=end original + L<KiokuDB>ã§ã¯ããã¹ã¦ã®ãªãã¸ã§ã¯ãã«ãé常ãIDãå²ãå½ã¦ããã¾ãã ãªãã¸ã§ã¯ããè¤æ°ã®ãªãã¸ã§ã¯ãã«å ±æããã¦ããå ´åããã®ãªã¬ã¼ã·ã§ã³ã¯ç¶æããã¾ãã +=begin original + However, for some objects this is not the desired behavior. These are objects that represent values, like L<DateTime>, L<Path::Class> entries, L<URI> objects, etc. +=end original + ããããããã¤ãã®ãªãã¸ã§ã¯ãã¯æã¾ããæ¯ãèãããã¾ããã ãããã¯ãL<DateTime>ããL<Path::Class>ã¨ã³ããªãL<URI>ãªãã¸ã§ã¯ãã®ãããªãã®ã§ã å¤ã表ç¾ãã¾ãã +=begin original + L<KiokuDB> can be asked to collapse such objects B<intrinsicly>, that is instead of creating a new L<KiokuDB::Entry> with its own ID for the object, the object gets collapsed directly into its parent's structures. +=end original + L<KiokuDB>ã¯B<intrinsicly>ã«ããã®ãããªãªãã¸ã§ã¯ããã ãã®ãªãã¸ã§ã¯ãã«ããèªèº«ã®IDã¨æ°ããL<KiokuDB::Entry>ãä½ã代ããã«ã ç ´æããããè¦æ±ã§ãã¾ãããªãã¸ã§ã¯ããç´æ¥ç ´æã§ããã°ã親ã®æ§é ã®ä¸ã«å ¥ãã¾ãã +=begin original + This means that shared references that are collapsed intrinsically will be loaded back from the database as two distinct copies, so updates to one will not affect the other. +=end original + ç ´æãããå ±æããããªãã¡ã¬ã³ã¹ã¯ããã¨ãã¨2ã¤ã®åºå¥ãããã³ãã¼ã¨ã㦠ãã¼ã¿ã¼ãã¼ã¹ãããã¼ãããã¾ããã§ãã®ã§ãä¸ã¤ãã¢ãããã¼ããã¦ãã ããä¸æ¹ã«ã¯å½±é¿ãããã¾ããã +=begin original + For instance, when we run the following code: +=end original + ä¾ãã°ãä¸è¨ã®ãããªã³ã¼ããåãããã¨ãã¦: use Path::Class; @@ -779,58 +1164,86 @@ $dir->store( $obj_1, $obj_2 ); +=begin original + While the following is true when the data is being inserted, it will no longer be true when C<$obj_1> and C<$obj_2> are loaded from the database: +=end original + ãã¼ã¿ãã¤ã³ãµã¼ããããã¨ãã«ã¯ãä¸è¨ã¯çã§ããã -C<$obj_1>ã¨C<$obj_2>ããã¼ã¿ã¼ãã¼ã¹ãããã¼ããããã¨ï¼ãã¯ãçã§ã¯ããã¾ãã: +C<$obj_1>ã¨C<$obj_2>ããã¼ã¿ã¼ãã¼ã¹ãããã¼ããããã¨ããã¯ãçã§ã¯ããã¾ãã: refaddr($obj_1->file) == refaddr($obj_2->file) +=begin original + This is because both C<$obj_1> and C<$obj_2> each got its own copy of C<$path>. +=end original + C<$obj_1>ã¨C<$obj_2>ã®ä¸¡æ¹ãC<$path>ã®ã³ãã¼ã ããã§ãã +=begin original + This behavior is usually more appropriate for objects that aren't mutated, but are instead cloned and replaced, and for which creating a first class entry in the backend with its own ID is undesired. +=end original + ãã®ç¾è±¡ã¯ãé常ãå¤ç°ããããè¤è£½ããããç½®ãæããããããããªãã¸ã§ã¯ãã«é©ãã¦ãã¾ãã ãã®ãããªãªãã¸ã§ã¯ãã®ããã«ã¯ãæåã®ã¯ã©ã¹ã¨ã³ããªãç¬èªã®IDã§ããã¯ã¨ã³ãã«ä½ãããã®ã¯ã æã¾ãã¦ããªãããã§ãã =head2 The Default Typemap +=begin original + Each backend comes with a default typemap, with some built in entries for common CPAN modules' objects. L<KiokuDB::TypeMap::Default> contains more details. +=end original ããããã®ããã¯ã¨ã³ãã«ã¯ãããã©ã«ãã®typemapãã¤ãã¦ãã¾ãã ããã«ã¯ãå ±éã®CPANã¢ã¸ã¥ã¼ã«ãªãã¸ã§ã¯ãã®ããã«ãããã¤ãå ±éã®ãã«ãã¤ã³ã®ã¨ã³ããªãããã¾ãã L<KiokuDB::TypeMap::Default>ã«ãã詳細ãããã¾ãã -=head1 åç´ãªæ¤ç´¢ +=head1 SIMPLE SEARCHES + +(åç´ãªæ¤ç´¢) + +=begin original Most backends support an inefficient but convenient simple search, which scans the entries and matches fields. +=end original + ã»ã¨ãã©ã®ããã¯ã¨ã³ããå¹ççã§ã¯ãªããã®ã®ã便å©ãªåç´ãªæ¤ç´¢ãããã¾ãã -ããã¯ãã¨ã³ããªãã¹ãã£ã³ãã¦ï¼ãã£ã¼ã«ãã«ãããããã¾ãã +ããã¯ãã¨ã³ããªãã¹ãã£ã³ãã¦ããã£ã¼ã«ãã«ãããããã¾ãã + +=begin original If you want to make use of this API we suggest using L<KiokuDB::Backend::DBI> since simple searching is implemented using an SQL where clause, which is much more efficient (you do have to set up the column manually though). +=end original ãã®APIã使ããããªããL<KiokuDB::Backend::DBI>ã使ããã¨ããããããã¾ãã åç´äºæ¤ç´¢ã¯SQLã®whereç¯ã使ã£ã¦å®è£ ã§ããããå¹ççã ããã§ãã (ãã ããæã§ã«ã©ã ãã»ããã¢ããããªãã¨ããã¾ããã) +=begin original + Calling the C<search> method with a hash reference as the only argument invokes the simple search functionality, returning a L<Data::Stream::Bulk> with the results: +=end original + C<search>ã¡ã½ããã«å¼æ°ã¨ãã¦ããã·ã¥ãªãã¡ã¬ã³ã¹ã®ã¿ã渡ãã¦å¼ã³ã¾ãã åç´ãªæ¤ç´¢æ©è½ãå¼ã³åºãããL<Data::Stream::Bulk>ãçµæã¨ä¸ç·ã«æ»ã£ã¦ãã¾ã: @@ -842,21 +1255,33 @@ } } +=begin original + This exact API is intentionally still underdefined. In the future it will be compatible with L<DBIx::Class> 0.09's syntax. +=end original + æ£ç¢ºãªAPIã¯ã¾ã 決ãããã¦ãã¾ãããå°æ¥çã«ãL<DBIx::Class> 0.09ã®ã·ã³ã¿ãã¯ã¹ã¨ äºæã«ããã¤ããã§ãã =head2 DBI SEARCH COLUMNS +=begin original + In order to make use of the simple search API we need to configure columns for our DBI backend. +=end original + ãã®ç°¡åãªæ¤ç´¢APIã使ãã«ã¯ãDBIããã¯ã¨ã³ãã«ããããè¨å®ããªããã°ããã¾ããã +=begin original + Let's create a 'name' column to search by: +=end original + æ¤ç´¢ããããã«ã'name'ã«ã©ã ãä½ãã¾ããã: my $dir = KiokuDB->connect( @@ -872,15 +1297,23 @@ ], ); +=begin original + You can either alter the schema manually, or use C<kioku dump> to back up your data, delete the database, connect with C<< create => 1 >> and then use C<kioku load>. +=end original + ã¹ãã¼ããæã§å¤æ´ãããã¨ãã§ãã¾ãããã¾ãããã¼ã¿ãããã¯ã¢ããããã®ã«ãC<kioku dump>ã使ãã ãã¼ã¿ãã¼ã¹ãåé¤ããC<< create => 1 >>ã§æ¥ç¶ããC<kioku load>ã使ããã¨ãåºæ¥ã¾ãã +=begin original + To populate this column we'll need to load Homer and update him: +=end original + ãã®ã«ã©ã ãåãè¾¼ãããã«ãHomerããã¼ããã¦ãæ´æ°ããå¿ è¦ãããã¾ã: { @@ -888,8 +1321,12 @@ $dir->update( $dir->lookup( $homer_id ) ); } +=begin original + And this is what it looks in the database: +=end original + ãã¼ã¿ãã¼ã¹ã§ã¯æ¬¡ã®ããã«ãªãã¾ã: id = 201C5B55-E759-492F-8F20-A529C7C02C8B @@ -897,53 +1334,79 @@ =head1 GETTING STARTED WITH BDB +(BDBãå§ããã) + +=begin original + The most mature backend for L<KiokuDB> is L<KiokuDB::Backend::BDB>. It performs very well, and supports many features, like L<Search::GIN> integration to provide customized indexing of your objects and transactions. +=end original + L<KiokuDB>ã§ãã£ã¨ãæçããããã¯ã¨ã³ãã¯ãL<KiokuDB::Backend::DBD>ã§ã(訳注:DBIã®ã»ããå®å®ãã¦ããã¨YAPC::Asia 2009ã§èãã¾ãã)ã ååã«åãã¾ãããå¤ãã®æ©è½ããµãã¼ããã¾ãã ãªãã¸ã§ã¯ãã®ã¤ã³ããã¯ã¹ã®ã«ã¹ã¿ãã¤ãºããã©ã³ã¶ã¯ã·ã§ã³ãæä¾ãã L<Search::GIN>ã®ãããªã¤ã³ãã°ã¬ã¼ã·ã§ã³ãããã¾ãã +=begin original + L<KiokuDB::Backend::DBI> is newer and not as tested, but also supports transactions and L<Search::GIN> based queries. It performs quite well too, but isn't as fast as L<KiokuDB::Backend::BDB>. -L<KiokuDB::Backend::DBI>ã¯ããæ°ããã§ããï¼ããã¾ã§ãã¹ãããã¦ãã¾ããã +=end original + +L<KiokuDB::Backend::DBI>ã¯ããæ°ããã§ãããããã¾ã§ãã¹ãããã¦ãã¾ããã ã§ããããã©ã³ã¶ã¯ã·ã§ã³ããµãã¼ããã¾ãããã¯ã¨ãªãã¼ã¹ã®L<Search::GIN>ãããã¾ãã ãããããªããªãããåãã¾ããã§ãããL<KiokuDB::Backend::BDB>ã¨åããããéãã¯ããã¾ãã (訳注:YAPC::Asia 2009ã§ã¯ãã»ã¼å¤ãããªãã¨èãã¾ãã) =head2 Installing L<KiokuDB::Backend::BDB> +=begin original + L<KiokuDB::Backend::BDB> needs the L<BerkeleyDB> module, and a recent version of Berkeley DB itself, which can be found here: L<http://www.oracle.com/technology/software/products/berkeley-db/db/index.html>. +=end original + L<KiokuDB::Backend::BDB>ã¯ãL<BerkeleyDB>ã¢ã¸ã¥ã¼ã«ãå¿ è¦ã§ãã ã¾ããæè¿ã®ãã¼ã¸ã§ã³ã®Berkeley DBèªèº«ãå¿ è¦ã§ããBerkeley DBã¯ã以ä¸ã®URLã«ããã¾ãã L<http://www.oracle.com/technology/software/products/berkeley-db/db/index.html>. +=begin original + BerkeleyDB (the library) normally installs into C</usr/local/BerkeleyDB.4.7>, while L<BerkeleyDB> (the module) looks for it in C</usr/local/BerkeleyDB>, so adding a symbolic link should make installation easy. +=end original + BerkeleyDB(ã©ã¤ãã©ãª)ã¯é常ãC</usr/local/BerkeleyDB.4.7>ã«ã¤ã³ã¹ãã¼ã«ããã¾ãã ã§ãããL<BerkeleyDB>(ã¢ã¸ã¥ã¼ã«)ã¯ãC</usr/local/BerkeleyDB>ãè¦ããã¨ãã¾ãã ã§ãã®ã§ãã·ã³ããªãã¯ãªã³ã¯ãä½ã£ã¦ããã°ãã¤ã³ã¹ãã¼ã«ãç°¡åã«ãªãã¾ãã +=begin original + Once you have L<BerkeleyDB> installed, L<KiokuDB::Backend::BDB> should install without problem and you can use it with L<KiokuDB>. +=end original + L<BerkeleyDB>ãã¤ã³ã¹ãã¼ã«ã§ããã°ãL<KiokuDB::Backend::BDB>ã¯åé¡ãªãã¤ã³ã¹ãã¼ã«ã§ããã¯ãã§ãã L<KiokuDB>ã¨ä¸ç·ã«ä½¿ããã¨ãã§ãã¾ãã =head2 Using L<KiokuDB::Backend::BDB> +=begin original + To use the BDB backend we must first create the storage. To do this the C<create> flag must be passed: +=end original + BDBããã¯ã¨ã³ãã使ãããã«ãã¹ãã¬ã¼ã¸ãä½ããªããã°ããã¾ããã ãã®ããã«ãC<create>ãã©ã°ã渡ããªããã°ããã¾ããã @@ -954,60 +1417,98 @@ }, ); +=begin original + The BDB backend uses L<BerkeleyDB::Manager> to do a lot of the L<BerkeleyDB> gruntwork. The L<BerkeleyDB::Manager> object will be instantiated using the arguments provided in the C<manager> attribute. +=end original + BDBããã¯ã¨ã³ãã¯ãL<BerkeleyDB::Manager>ã使ã£ã¦ãããããã®L<BerkeleyDB>ã®ä¸åããè¡ãã¾ãã L<BerkeleyDB::Manager>ãªãã¸ã§ã¯ãã¯C<manager>å±æ§ã§æä¾ãããå¼æ°ã使ã£ã¦ãã¤ã³ã¹ã¿ã³ã¹åããã¾ãã +=begin original + Now that the storage is created we can make use of this backend, much like before: +=end original + ããã§ãã¹ãã¬ã¼ã¸ãã¤ãããã¾ããããã®ããã¯ã¨ã³ããã以åã¨åæ§ã«ä½¿ãã¾ãã my $dir = KiokuDB->new( backend => $backend ); +=begin original + Subsequent opens will not require the C<create> argument to be true, but it doesn't hurt. +=end original + ãã®å¾ã®ãªã¼ãã³ã«ã¯ãC<create>å±æ§ãçã§ããå¿ è¦ã¯ããã¾ããããçã§ãã£ã¦ãç¹ã«å®³ã¯ããã¾ããã +=begin original + This C<connect> call is equivalent to the above: +=end original + ãã®C<connect>ã¯ä¸è¨ã®ãã®ã¨åãã§ã: my $dir = KiokuDB->connect( "bdb:dir=path/to/storage", create => 1 ); -=head1 ãã©ã³ã¶ã¯ã·ã§ã³ +=head1 TRANSACTIONS + +(ãã©ã³ã¶ã¯ã·ã§ã³) + +=begin original Some backends (ones which do the L<KiokuDB::Backend::Role::TXN> role) can be used with transactions. +=end original + ããã¤ãã®ããã¯ã¨ã³ã(L<KiokuDB::Backend::Role::TXN>ãã¼ã«ããããã®)ã¯ããã©ã³ã¶ã¯ã·ã§ã³ã使ãããã®ãããã¾ãã +=begin original + If you are familiar with L<DBIx::Class> this should be very familiar: +=end original + L<DBIx::Class>ã«æ £ãã¦ãããªãããããããã§ããã: $dir->txn_do(sub { $dir->store($obj); }); +=begin original + This will create a L<BerkeleyDB> level transaction, and all changes to the database are committed if the block was executed cleanly. +=end original + L<BerkeleyDB>ã¬ãã«ã®ãã©ã³ã¶ã¯ã·ã§ã³ãä½ãã¾ãããã¼ã¿ãã¼ã¹ã¸ã®ãã¹ã¦ã®å¤æ´ã¯ ãããã¯ã綺éºã«å®è¡ãããããã³ãããããã¾ãã +=begin original + If any error occurred the transaction will be rolled back, and the changes will not be visible to subsequent reads. +=end original + ä½ããã®ã¨ã©ã¼ãèµ·ããã°ããã©ã³ã¶ã¯ã·ã§ã³ã¯ãã¼ã«ããã¯ããã¾ãã å¤æ´ã¯æ¬¡ã®èªã¿è¾¼ã¿ã§ã¯ãè¦ãã¾ããã +=begin original + Note that L<KiokuDB> does B<not> touch live instances, so if you do something like +=end original + L<KiokuDB>çãã¦ããã¤ã³ã¹ã¿ã³ã¹ã«ã¯è§¦ãã¾ãããã§ãã®ã§ã次ã®ããã«ãã㨠$dir->txn_do(sub { @@ -1019,50 +1520,76 @@ die "an error"; }); +=begin original + the C<name> attribute is B<not> rolled back, it is simply the C<store> operation that gets reverted. +=end original + C<name>å±æ§ã¯ãã¼ã«ããã¯B<ããã¾ãã>ãC<store>ãªãã¬ã¼ã·ã§ã³ã ãããå ã«æ»ãã¾ãã +=begin original + Transactions will nest properly, and with most backends they generally increase write performance as well. +=end original + ãã©ã³ã¶ã¯ã·ã§ã³ã¯é©åã«ãã¹ãã§ãã¾ããã¾ããã»ã¨ãã©ã®ããã¯ã¨ã³ãã§ãä¸è¬çã« æ¸ãè¾¼ã¿ã®ããã©ã¼ãã³ã¹ãè¯ããªãã¾ãã =head1 QUERIES +(ã¯ã¨ãª) + +=begin original + L<KiokuDB::Backend::BDB::GIN> is a subclass of L<KiokuDB::Backend::BDB> that provides L<Search::GIN> integration. +=end original + L<KiokuDB::Backend::BDB::GIN>ã¯L<KiokuDB::Backend::BDB>ã®ãµãã¯ã©ã¹ã§ã L<Serach::GIN>ã¤ã³ãã°ã¬ã¼ã·ã§ã³ãæä¾ãã¦ãã¾ãã +=begin original + L<Search::GIN> is a framework to index and query objects, inspired by Postgres' internal GIN api. GIN stands for Generalized Inverted Indexes. +=end original + L<Search::GIN>ã¯ã¤ã³ããã¯ã¹ã¨ã¯ã¨ãªã¼ãªãã¸ã§ã¯ãã®ãã¬ã¼ã ã¯ã¼ã¯ã§ãã Postgresã®å é¨GIN apiã«ã¤ã³ã¹ãã¤ã¢ããã¾ããã GINã¯ãGeneralized Inverted Indexesã®ç¥ã§ãã +=begin original + Using L<Search::GIN> arbitrary search keys can be indexed for your objects, and these objects can then be looked up using queries. +=end original + L<Search::GIN>ã使ãã¨ãä»»æã®æ¤ç´¢ãã¼ããªãã¸ã§ã¯ãã«ã¿ã¤ãã¦ã¤ã³ããã¯ã¹ã§ãã¾ãã ããã¦ããããã®ãªãã¸ã§ã¯ããã¯ã¨ãªã§æ¤ç´¢ã§ãã¾ãã +=begin original + For instance, one of the pre canned searches L<Search::GIN> supports out of the box is class indexing. Let's use L<Search::GIN::Extract::Callback> to do custom indexing of our objects: -ä¾ãã°ãL<Search::GIN>ãç¬åµçã«ãµãã¼ããããäºãããæ¤ç´¢ã®ä¸ã¤ã«ãã¯ã©ã¹ã¤ã³ããã¯ã¹ãããã¾ãã +=end original + +ä¾ãã°ãL<Search::GIN>ããµãã¼ããããããã«ä½¿ãããäºãããæ¤ç´¢ã®ä¸ã¤ã«ãã¯ã©ã¹ã¤ã³ããã¯ã¹ãããã¾ãã L<Search::GIN::Extract::Callback> ã使ã£ã¦ããªãã¸ã§ã¯ãã«ã«ã¹ã¿ã ã®ã¤ã³ããã¯ã¹ãä½ãã¾ããã: my $dir = KiokuDB->new( backend => KiokuDB::Backend::BDB::GIN->new( extract => Search::GIN::Extract::Callback->new( extract => sub { - my ( $object, $extractor, @args ) = @_; + my ( $obj, $extractor, @args ) = @_; if ( $obj->isa("Person") ) { return { @@ -1070,6 +1597,8 @@ name => $obj->name, }; } + + return; }, ), ), @@ -1077,8 +1606,12 @@ $dir->store( @random_objects ); +=begin original + To look up the objects, we use the a manual key lookup query: +=end original + ãªãã¸ã§ã¯ããæ¤ç´¢ããããã«ãããã¥ã¢ã«ãã¼æ¤ç´¢ã¯ã¨ãªã使ãã¾ã: my $query = Search::GIN::Query::Manual->new( @@ -1089,9 +1622,13 @@ my $stream = $dir->search($query); +=begin original + The result is L<Data::Stream::Bulk> object that represents the search results. It can be iterated as follows: +=end original + çµæã¨ãã¦ãæ¤ç´¢çµæã表ãL<Data::Stream::Bulk>ãªãã¸ã§ã¯ããè¿ãã¾ãã 次ã®ããã«ã¤ãã¬ã¼ãã§ãã¾ãã @@ -1101,23 +1638,35 @@ } } +=begin original + Or even more simply, if you don't mind loading the whole resultset into memory: +=end original + ã¾ããããåç´ã«ãã¡ã¢ãªã«å ¨çµæããã¼ããã¦ããã¾ããªããªã: my @people = $stream->all; +=begin original + L<Search::GIN> is very much in its infancy, and is very under documented. However it does work for simple searches such as this and contains pre canned solutions like L<Search::GIN::Extract::Class>. +=end original + L<Search::GIN>ã¯ã¾ã æªæçã§ããããã¥ã¡ã³ããæ¸ãã¦ããã¨ããã§ãã ã§ããããã®ãããªåç´ãªæ¤ç´¢ã¯åãã¾ãããL<Search::GIN::Extract::Class>ã®ãã㪠äºããã解決ãå«ãã§ãã¾ãã +=begin original + In short, it works today, but watch this space for new developments. -ã¤ã¾ããç¾å¨åãã¾ããï¼æ°ããéçºè ã¯ããã«æ³¨ç®ãã¦ãã¦ãã ããã +=end original + +ã¤ã¾ããç¾å¨ã¯åãã¾ãããæ°ããéçºãããã¨ãã«ã¯ãããã«æ³¨æãã¦ãã ããã =head1 翻訳ã«ã¤ã㦠@@ -1126,7 +1675,7 @@ Perlããã¥ã¡ã³ãæ¥æ¬èªè¨³ Project ã«ã¦ã Perlã¢ã¸ã¥ã¼ã«ãããã¥ã¡ã³ãã®ç¿»è¨³ãè¡ã£ã¦ããã¾ãã - http://perldocjp.sourceforge.jp + http://perldocjp.sourceforge.jp/ http://sourceforge.jp/projects/perldocjp/ http://www.freeml.com/ctrl/html/MLInfoForm/perld****@freem***** - http://www.perldoc.jp + http://www.perldoc.jp/