=encoding euc-jp =head1 NAME =begin original DB_File - Perl5 access to Berkeley DB version 1.x =end original DB_File - Berkeley DB �С������ 1.x�ؤ� Perl5 �������� =head1 SYNOPSIS =begin original use DB_File; =end original use DB_File; =begin original [$X =] tie %hash, 'DB_File', [$filename, $flags, $mode, $DB_HASH] ; [$X =] tie %hash, 'DB_File', $filename, $flags, $mode, $DB_BTREE ; [$X =] tie @array, 'DB_File', $filename, $flags, $mode, $DB_RECNO ; =end original [$X =] tie %hash, 'DB_File', [$filename, $flags, $mode, $DB_HASH] ; [$X =] tie %hash, 'DB_File', $filename, $flags, $mode, $DB_BTREE ; [$X =] tie @array, 'DB_File', $filename, $flags, $mode, $DB_RECNO ; =begin original $status = $X->del($key [, $flags]) ; $status = $X->put($key, $value [, $flags]) ; $status = $X->get($key, $value [, $flags]) ; $status = $X->seq($key, $value, $flags) ; $status = $X->sync([$flags]) ; $status = $X->fd ; =end original $status = $X->del($key [, $flags]) ; $status = $X->put($key, $value [, $flags]) ; $status = $X->get($key, $value [, $flags]) ; $status = $X->seq($key, $value, $flags) ; $status = $X->sync([$flags]) ; $status = $X->fd ; =begin original # BTREE only $count = $X->get_dup($key) ; @list = $X->get_dup($key) ; %list = $X->get_dup($key, 1) ; $status = $X->find_dup($key, $value) ; $status = $X->del_dup($key, $value) ; =end original # BTREE �Τ� $count = $X->get_dup($key) ; @list = $X->get_dup($key) ; %list = $X->get_dup($key, 1) ; $status = $X->find_dup($key, $value) ; $status = $X->del_dup($key, $value) ; =begin original # RECNO only $a = $X->length; $a = $X->pop ; $X->push(list); $a = $X->shift; $X->unshift(list); @r = $X->splice(offset, length, elements); =end original # RECNO �Τ� $a = $X->length; $a = $X->pop ; $X->push(list); $a = $X->shift; $X->unshift(list); @r = $X->splice(offset, length, elements); =begin original # DBM Filters $old_filter = $db->filter_store_key ( sub { ... } ) ; $old_filter = $db->filter_store_value( sub { ... } ) ; $old_filter = $db->filter_fetch_key ( sub { ... } ) ; $old_filter = $db->filter_fetch_value( sub { ... } ) ; =end original # DBM �ե��륿 $old_filter = $db->filter_store_key ( sub { ... } ) ; $old_filter = $db->filter_store_value( sub { ... } ) ; $old_filter = $db->filter_fetch_key ( sub { ... } ) ; $old_filter = $db->filter_fetch_value( sub { ... } ) ; =begin original untie %hash ; untie @array ; =end original untie %hash ; untie @array ; =head1 DESCRIPTION =begin original B is a module which allows Perl programs to make use of the facilities provided by Berkeley DB version 1.x (if you have a newer version of DB, see L). It is assumed that you have a copy of the Berkeley DB manual pages at hand when reading this documentation. The interface defined here mirrors the Berkeley DB interface closely. =end original B �� Berkeley DB version 1.x �ˤ�ä��󶡤���Ƥ��뵡ǽ�� Perl �ץ�����ब�Ȥ���褦�ˤ���⥸�塼��Ǥ� (�⤷����˿������С������� DB ����äƤ���ΤǤ���С� L ������������)�� ���Υɥ�����Ȥ��ɤ�Ȥ������ʤ��� Berkeley DB�� �˥奢��ڡ����� �긵�ˤ����Ƥ��뤳�Ȥ�����Ȥ��Ƥ��ޤ��� �������������Ƥ��륤�󥿡��ե������� Berkeley DB ���󥿡��ե������� ���ʤ�ȿ�Ǥ��Ƥ��ޤ��� =begin original Berkeley DB is a C library which provides a consistent interface to a number of database formats. B provides an interface to all three of the database types currently supported by Berkeley DB. =end original Berkeley DB �Ϥ����Ĥ��Υǡ����١����ե����ޥåȤ��Ф��� ������Τ��륤�󥿡��ե��������󶡤��� C �饤�֥��Ǥ��� B �ϸ��� Berkeley DB �ˤ�äƥ��ݡ��Ȥ���Ƥ��뻰�����Ƥ� �ǡ����١��������פؤΥ��󥿥ե��������󶡤��Ƥ��ޤ��� =begin original The file types are: =end original ���Υե����륿���פˤϰʲ���Τ�����ޤ�: =over 5 =item B =begin original This database type allows arbitrary key/value pairs to be stored in data files. This is equivalent to the functionality provided by other hashing packages like DBM, NDBM, ODBM, GDBM, and SDBM. Remember though, the files created using DB_HASH are not compatible with any of the other packages mentioned. =end original ���Υǡ����١��������פϡ�Ǥ�դΥ���/�ͤ��Ȥ�ǡ����ե������ ��Ǽ���뤳�Ȥ��ǽ�ˤ��ޤ��� ����ϵ�ǽ���̤� DBM, NDBM, ODBM, GDBM, SDBM�Ȥ��ä�¾�Υϥå���� �����ʤ��ѥå��������󶡤��뵡ǽ��Ʊ���Ǥ��� ������ DB_HASH ��Ȥäƺ��줿�ե�����ϡ����޾夲��¾�Υѥå������� �ߴ������ʤ��Ȥ������Ȥ�˺��ʤ��Ǥ��������� =begin original A default hashing algorithm, which will be adequate for most applications, is built into Berkeley DB. If you do need to use your own hashing algorithm it is possible to write your own in Perl and have B use it instead. =end original �ǥե���ȤΥϥå��奢�르�ꥺ�ब Berkeley DB ���Ȥ߹��ޤ�Ƥ��ꡢ �ۤȤ�ɤΥ��ץꥱ��������Ŭ�礷�ޤ��� �⤷�ȼ��Υϥå��奢�르�ꥺ��� �Ȥ�ɬ�פ�����С�Perl ���ȼ��˽���ߡ�B ������ˤ���� �Ȥ��褦�ˤ��뤳�Ȥ����ޤ��� =item B =begin original The btree format allows arbitrary key/value pairs to be stored in a sorted, balanced binary tree. =end original btree �ե����ޥåȤ�Ǥ�դΥ���/�ͤ��Ȥ򡢥Х�󥹤��Ȥ줿 ��ʬ�ڤ˳�Ǽ�Ǥ��ޤ��� =begin original As with the DB_HASH format, it is possible to provide a user defined Perl routine to perform the comparison of keys. By default, though, the keys are stored in lexical order. =end original DB_HASH �ե����ޥåȤξ���Ʊ���褦�ˡ���������Ӥ�¹Ԥ���桼������� Perl �Υ롼������󶡤��뤳�Ȥ�����ޤ��� �������ǥե���ȤǤϡ�������ʸ��(lexical)�ν�˳�Ǽ����ޤ��� =item B =begin original DB_RECNO allows both fixed-length and variable-length flat text files to be manipulated using the same key/value pair interface as in DB_HASH and DB_BTREE. In this case the key will consist of a record (line) number. =end original DB_RECNO �ϸ���Ĺ�Ȳ���Ĺ��ξ���Υե�åȤʥƥ����ȥե������ DB_HASH �� DB_BTREE ��Ʊ������/�ͤ��ȤΥ��󥿡��ե�������Ȥäư����� �褦�ˤ��ޤ��� ���ξ�硢�����ϥ쥳����(��)�ֹ�ˤʤ�ޤ��� =back =head2 Using DB_File with Berkeley DB version 2 or greater (DB_File �� Berkeley DB �С������ 2 �ʾ�ǻȤ�) =begin original Although B is intended to be used with Berkeley DB version 1, it can also be used with version 2, 3 or 4. In this case the interface is limited to the functionality provided by Berkeley DB 1.x. Anywhere the version 2 or greater interface differs, B arranges for it to work like version 1. This feature allows B scripts that were built with version 1 to be migrated to version 2 or greater without any changes. =end original B �� Berkeley DB �С������ 1 ��Ȥ��褦�ˤʤäƤ��ޤ����� �С������ 2, 3, 4 �ǻȤ����Ȥ����ޤ��� ���ξ�硢���󥿡��ե������� Berkeley DB 1.x �ˤ�ä� �󶡤���Ƥ��뵡ǽ�˸��ꤵ��ޤ��� �С������ 2 �ʾ�Υ��󥿡��ե������ǰۤʤ�ս�ϡ� �ɤ��Ǥ�С������ 1 �Τ褦�˵�ǽ����褦 B ���ѹ����Ƥ��ޤ��� ���ε�ǽ�ˤ�ꡢ�С������ 1 �ǹ��ۤ��줿 B ������ץȤ��ѹ����뤳�Ȥʤ����С������ 2 �ʾ�� �ܹԤǤ��ޤ��� =begin original If you want to make use of the new features available in Berkeley DB 2.x or greater, use the Perl module B instead. =end original �⤷ Berkeley DB 2.x �ʾ�����Ѳ�ǽ�ʿ�������ǽ��Ȥ������ΤǤ���С� Perl �⥸�塼�� B ������˻ȤäƤ��������� =begin original B The database file format has changed multiple times in Berkeley DB version 2, 3 and 4. If you cannot recreate your databases, you must dump any existing databases with either the C or the C utility that comes with Berkeley DB. Once you have rebuilt DB_File to use Berkeley DB version 2 or greater, your databases can be recreated using C. Refer to the Berkeley DB documentation for further details. =end original B<����:> Berkeley DB version 2, 3, 4 �ǥǡ����١����ե������ �ե����ޥåȤϲ����ѹ�����Ƥ��ޤ��� �⤷�ǡ����١�����ƺ����Ǥ��ʤ��ΤǤ���С� Berkeley DB ���դ��Ƥ��� C ���뤤�� C �桼�ƥ���ƥ��Τɤ��餫�ǡ���¸�����ƤΥǡ����١����� ����פ��ʤ���Фʤ�ޤ��� ���١�Berkeley DB version 2 �ʾ��Ȥ������ DB_File ��ƺ�������С� C ��Ȥäƺƺ������뤳�Ȥ��Ǥ��ޤ��� ����ʤ�ܺ٤� Berkeley DB �ɥ�����Ȥ򻲾Ȥ��Ƥ��������� =begin original Please read L<"COPYRIGHT"> before using version 2.x or greater of Berkeley DB with DB_File. =end original DB_File �� Berkeley DB �ΥС������ 2.x �ʾ��Ȥ����� L<"COPYRIGHT"> �����ˤʤäƤ��������� =head2 Interface to Berkeley DB (Berkeley DB�ؤΥ��󥿡��ե�����) =begin original B allows access to Berkeley DB files using the tie() mechanism in Perl 5 (for full details, see L). This facility allows B to access Berkeley DB files using either an associative array (for DB_HASH & DB_BTREE file types) or an ordinary array (for the DB_RECNO file type). =end original B �� Berkeley DB �ե������ Perl 5 �� tie() ��ǽ��Ȥäƥ����������� ���Ȥ��ǽ�ˤ��ޤ��� (�����ʾܺ٤ˤĤ��Ƥ� L ����������)�� ���ε�ǽ�� DB_File ��Ϣ������(DB_HASH �� DB_BTREE �ե����륿����)��Ȥä��ꡢ �̾������(DB_RECNO �ե����륿����)��Ȥä� Berkeley DB �ե�����˥����������� ���Ȥ��ǽ�ˤ��ޤ��� =begin original In addition to the tie() interface, it is also possible to access most of the functions provided in the Berkeley DB API directly. See L. =end original tie() ���󥿡��ե������˲ä��ơ�Berkeley DB API �ˤ�ä��󶡤���롢�ۤȤ�� ���Ƥδؿ���ľ�ܥ����������뤳�Ȥ�Ǥ��ޤ��� L ������������ =head2 Opening a Berkeley DB Database File (Berkeley DB �ǡ����١����ե�����Υ����ץ�) =begin original Berkeley DB uses the function dbopen() to open or create a database. Here is the C prototype for dbopen(): =end original Berkeley DB �ϥǡ����١����Υ����ץ������� dbopen() �ؿ���Ȥ��ޤ��� dbopen() �� C �ץ��ȥ����פϰʲ����̤�Ǥ�: DB* dbopen (const char * file, int flags, int mode, DBTYPE type, const void * openinfo) =begin original The parameter C is an enumeration which specifies which of the 3 interface methods (DB_HASH, DB_BTREE or DB_RECNO) is to be used. Depending on which of these is actually chosen, the final parameter, I points to a data structure which allows tailoring of the specific interface method. =end original �ѥ�᡼�� C ������(enumeration)�ǡ����ĤΥ��󥿥ե������᥽�å� (DB_HASH, DB_BTREE, DB_RECNO)�Τɤ��Ȥ�������ꤷ�ޤ��� �ºݤˤɤ줬���Ф줿���ˤ�äơ��Ǹ�Υѥ�᡼�� I �ˤϻ��ꤵ�줿 ���󥿥ե������᥽�åɤ�Ŭ�礹��ǡ�����¤�Τ���ꤷ�ޤ��� =begin original This interface is handled slightly differently in B. Here is an equivalent call using B: =end original ���󥿡��ե������� B �Ǥϼ㴳�ۤʤ�ޤ��� B ��Ȥä�Ʊ���ƤӽФ��ϰʲ����̤�Ǥ�: tie %array, 'DB_File', $filename, $flags, $mode, $DB_HASH ; =begin original The C, C and C parameters are the direct equivalent of their dbopen() counterparts. The final parameter $DB_HASH performs the function of both the C and C parameters in dbopen(). =end original C, C, C �ѥ�᡼���� dbopen() �Ǥ��б����� ��ΤȤ��Τޤ�Ʊ���Ǥ��� �Ǹ�Υѥ�᡼�� $DB_HASH �� dbopen() �Ǥ� C �� C �ѥ�᡼����ξ���ε�ǽ��Ԥ��ޤ��� =begin original In the example above $DB_HASH is actually a pre-defined reference to a hash object. B has three of these pre-defined references. Apart from $DB_HASH, there is also $DB_BTREE and $DB_RECNO. =end original �嵭����Ǥ� $DB_HASH �ϼºݤˤ��餫����������줿�ϥå��奪�֥������Ȥؤ� ��ե���󥹤ˤʤ�ޤ��� DB_File �Ϥ����δ���������줿���ĤΥ�ե���󥹤� ���äƤ��ޤ��� $DB_HASH �Τۤ��� $DB_BTREE �� $DB_RECNO ������ޤ��� =begin original The keys allowed in each of these pre-defined references is limited to the names used in the equivalent C structure. So, for example, the $DB_HASH reference will only allow keys called C, C, C, C, C and C. =end original ���������������Ƥ����ե���󥹤ǵ�����륭���ϡ� ������ C ��¤�ΤǻȤ��Ƥ���̾���˸��ꤵ��ޤ��� ���Τ����㤨�� $DB_HASH �� C, C, C, C, C, C �����򥭡��Ȥ��Ƶ����ޤ��� =begin original To change one of these elements, just assign to it like this: =end original ���������Ǥ��ѹ�����ˤϡ��ʲ��Τ褦��������������Ǥ�: $DB_HASH->{'cachesize'} = 10000 ; =begin original The three predefined variables $DB_HASH, $DB_BTREE and $DB_RECNO are usually adequate for most applications. If you do need to create extra instances of these objects, constructors are available for each file type. =end original ���ĤΤ��餫����������줿�ѿ� $DB_HASH, $DB_BTREE ������ $DB_RECNO ������� ���ץꥱ��������Ŭ�����ޤ��� �⤷����쥪�֥������Ȥ����̤ʥ��󥹥��󥹤���ɬ�פ�����С� ���줾��Υե����륿���פΤ���˥��󥹥ȥ饯�������Ѥ��ޤ��� =begin original Here are examples of the constructors and the valid options available for DB_HASH, DB_BTREE and DB_RECNO respectively. =end original DB_HASH, DB_BTREE ������ DB_RECNO���б����륳�󥹥ȥ饯����Ŭ�ڤʥ��ץ����� ���줾������ʲ��˼����ޤ�: $a = new DB_File::HASHINFO ; $a->{'bsize'} ; $a->{'cachesize'} ; $a->{'ffactor'}; $a->{'hash'} ; $a->{'lorder'} ; $a->{'nelem'} ; $b = new DB_File::BTREEINFO ; $b->{'flags'} ; $b->{'cachesize'} ; $b->{'maxkeypage'} ; $b->{'minkeypage'} ; $b->{'psize'} ; $b->{'compare'} ; $b->{'prefix'} ; $b->{'lorder'} ; $c = new DB_File::RECNOINFO ; $c->{'bval'} ; $c->{'cachesize'} ; $c->{'psize'} ; $c->{'flags'} ; $c->{'lorder'} ; $c->{'reclen'} ; $c->{'bfname'} ; =begin original The values stored in the hashes above are mostly the direct equivalent of their C counterpart. Like their C counterparts, all are set to a default values - that means you don't have to set I of the values when you only want to change one. Here is an example: =end original �嵭�ǥϥå���˳�Ǽ������ͤ�C���б�������ʬ������Ʊ���Ǥ��� ������ C ���б���ʬ�Τ褦�ˡ����Ƥϥǥե���Ȥ����ꤵ��ޤ� - �Ĥޤ�������ѹ������������Ǥ�������Ƥ��ͤ����ꤹ�� ɬ�פ�̵���Ȥ������ȤǤ��� �ʲ�����򼨤��ޤ�: $a = new DB_File::HASHINFO ; $a->{'cachesize'} = 12345 ; tie %y, 'DB_File', "filename", $flags, 0777, $a ; =begin original A few of the options need extra discussion here. When used, the C equivalent of the keys C, C and C store pointers to C functions. In B these keys are used to store references to Perl subs. Below are templates for each of the subs: =end original �����Ĥ��Υ��ץ����ˤĤ��Ƥϡ��������������ɬ�פ�����ޤ��� ���Ѥ��뤵����C�Ǥ�C��C��C�Ȥ������ܤ� C�ؿ��ؤΥݥ��󥿤��Ǽ���ޤ��� B�Ǥϡ������Υ����� Perl���֥롼����ؤΥ�ե���󥹤��Ǽ���뤿��˻Ȥ��ޤ��� �ƥ��֥롼����Υƥ�ץ졼�Ȥϰʲ��˼����ޤ�: =begin original sub hash { my ($data) = @_ ; ... # return the hash value for $data return $hash ; } =end original sub hash { my ($data) = @_ ; ... # $data �Τ���Υϥå����ͤ��֤� return $hash ; } =begin original sub compare { my ($key, $key2) = @_ ; ... # return 0 if $key1 eq $key2 # -1 if $key1 lt $key2 # 1 if $key1 gt $key2 return (-1 , 0 or 1) ; } =end original sub compare { my ($key, $key2) = @_ ; ... # ����� $key1 eq $key2 �ʤ�� 0 # $key1 lt $key2 �ʤ�� -1 # $key1 gt $key2 �ʤ�� 1 return (-1 , 0 or 1) ; } =begin original sub prefix { my ($key, $key2) = @_ ; ... # return number of bytes of $key2 which are # necessary to determine that it is greater than $key1 return $bytes ; } =end original sub prefix { my ($key, $key2) = @_ ; ... # $key1 �����礭����Ƚ�ꤹ�뤿���ɬ�פ� # $key2 ��Ĺ����Ĺ�����֤��ޤ� return $bytes ; } =begin original See L for an example of using the C template. =end original C �ƥ�ץ졼�Ȥ�Ȥä���ˤĤ��Ƥ� L �� ������������ =begin original If you are using the DB_RECNO interface and you intend making use of C, you should check out L. =end original DB_RECNO ���󥿡��ե�������ȤäƤ��ơ�C ��Ȥ����Ȥ���ΤǤ���С� L ������å�����ɬ�פ�����ޤ��� =head2 Default Parameters (�ǥե���ȥѥ�᡼��) =begin original It is possible to omit some or all of the final 4 parameters in the call to C and let them take default values. As DB_HASH is the most common file format used, the call: =end original C �θƤӽФ��ǤκǸ�λͤĤΥѥ�᡼���Τ����Ĥ��� ���뤤�����Ƥ��ά�����ǥե�����ͤ�Ȥ�褦�ˤ��뤳�Ȥ��Ǥ��ޤ��� �ʲ��Τ褦�˸ƤӽФ��ȡ��Ǥ����Ū�ʥե�����ե����ޥåȤǤ��� DB_HASH ���Ȥ��ޤ�: =begin original tie %A, "DB_File", "filename" ; =end original tie %A, "DB_File", "filename" ; =begin original is equivalent to: =end original ����ϰʲ��Τ�Τ�Ʊ���Ǥ�: tie %A, "DB_File", "filename", O_CREAT|O_RDWR, 0666, $DB_HASH ; =begin original It is also possible to omit the filename parameter as well, so the call: =end original �ե�����̾(filename)�ѥ�᡼�����ά���뤳�Ȥ���ǽ�Ǥ��� �ʲ��Τ褦�� �ƤӽФ��ޤ�: tie %A, "DB_File" ; =begin original is equivalent to: =end original ����ϰʲ��Τ�Τ�Ʊ���Ǥ�: tie %A, "DB_File", undef, O_CREAT|O_RDWR, 0666, $DB_HASH ; =begin original See L for a discussion on the use of C in place of a filename. =end original �ե�����̾�ξ���C��Ȥ����ȤˤĤ��Ƥ������� L ������������ =head2 In Memory Databases (�����Υǡ����١���) =begin original Berkeley DB allows the creation of in-memory databases by using NULL (that is, a C<(char *)0> in C) in place of the filename. B uses C instead of NULL to provide this functionality. =end original Berkeley DB �ϥե�����̾�ξ��� NULL(�Ĥޤ� C �Ǥ� C<(char *)0>)�� �Ȥ����Ȥˤ�ꡢ�����Υǡ����١���(in-memory database)�� �������뤳�Ȥ��ǽ�ˤ��Ƥ��ޤ��� B �Ǥϡ����ε�ǽ���󶡤��뤿��� NULL ������� C ��Ȥ��ޤ��� =head1 DB_HASH =begin original The DB_HASH file format is probably the most commonly used of the three file formats that B supports. It is also very straightforward to use. =end original DB_HASH �ե�����ե����ޥåȤ� B �����ݡ��Ȥ��Ƥ��뻰�Ĥ� �ե�����ե����ޥåȤ���ǡ������餯�Ǥ����Ū�˻Ȥ��Ƥ��ޤ��� ��������˻Ȥ������狼��䤹����ΤǤ��� =head2 A Simple Example (��ñ����) =begin original This example shows how to create a database, add key/value pairs to the database, delete keys/value pairs and finally how to enumerate the contents of the database. =end original ������Ǥϥǡ����١����κ�������ǡ����١����ؤΥ���/�ͤ��Ȥ��ɲá� ����/�ͤ��Ȥκ���������ƺǸ�˥ǡ����١������˽��Ϥ�����ˡ�򼨤��ޤ��� use warnings ; use strict ; use DB_File ; our (%h, $k, $v) ; unlink "fruit" ; tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0666, $DB_HASH or die "Cannot open file 'fruit': $!\n"; =begin original # Add a few key/value pairs to the file $h{"apple"} = "red" ; $h{"orange"} = "orange" ; $h{"banana"} = "yellow" ; $h{"tomato"} = "red" ; =end original # �����Ĥ��Υ���/�ͤ��Ȥ�ե�������ɲ� $h{"apple"} = "red" ; $h{"orange"} = "orange" ; $h{"banana"} = "yellow" ; $h{"tomato"} = "red" ; =begin original # Check for existence of a key print "Banana Exists\n\n" if $h{"banana"} ; =end original # ������¸�ߤ��뤫�����å� print "Banana Exists\n\n" if $h{"banana"} ; =begin original # Delete a key/value pair. delete $h{"apple"} ; =end original # ����/�ͤ��Ȥ��� delete $h{"apple"} ; =begin original # print the contents of the file while (($k, $v) = each %h) { print "$k -> $v\n" } =end original # �ե���������Ƥ���Ϥ��� while (($k, $v) = each %h) { print "$k -> $v\n" } untie %h ; =begin original here is the output: =end original �ʲ��Τ褦�˽��Ϥ���ޤ�: Banana Exists orange -> orange tomato -> red banana -> yellow =begin original Note that the like ordinary associative arrays, the order of the keys retrieved is in an apparently random order. =end original �̾��Ϣ������(�ϥå���)��Ʊ�͡����Ф���륭���ν��֤ϸ����ܾ塢 �Ǥ����ˤʤ뤳�Ȥ����դ��Ƥ��������� =head1 DB_BTREE =begin original The DB_BTREE format is useful when you want to store data in a given order. By default the keys will be stored in lexical order, but as you will see from the example shown in the next section, it is very easy to define your own sorting function. =end original Ϳ����줿����ǥǡ������Ǽ�������Ȥ���DB_BTREE �ե����ޥåȤ������Ǥ��� �ǥե���ȤǤϥ�����ʸ����(lexical)��dz�Ǽ����ޤ��� ���������Υ��������Ǥ��㤫���狼��褦�ˡ� �����Ȥ��뤿����ȼ��δؿ����������Τϡ���Ƥ��ñ�Ǥ��� =head2 Changing the BTREE sort order (BTREE�����Ƚ���ѹ�) =begin original This script shows how to override the default sorting algorithm that BTREE uses. Instead of using the normal lexical ordering, a case insensitive compare function will be used. =end original ���Υ�����ץȤ� BTREE ���Ȥ��ǥե���ȤΥ������ѥ��르�ꥺ��� ��񤭤�����ˡ�򼨤��Ƥ��ޤ��� �̾��ʸ���ˤ������Ȥ�����ˡ���ʸ��/��ʸ���� �㤤��̵�뤷����Ӵؿ���Ȥ��ޤ��� use warnings ; use strict ; use DB_File ; my %h ; sub Compare { my ($key1, $key2) = @_ ; "\L$key1" cmp "\L$key2" ; } =begin original # specify the Perl sub that will do the comparison $DB_BTREE->{'compare'} = \&Compare ; =end original # ��Ӥ�Ԥ� Perl sub ����ꤷ�ޤ� $DB_BTREE->{'compare'} = \&Compare ; unlink "tree" ; tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0666, $DB_BTREE or die "Cannot open file 'tree': $!\n" ; =begin original # Add a key/value pair to the file $h{'Wall'} = 'Larry' ; $h{'Smith'} = 'John' ; $h{'mouse'} = 'mickey' ; $h{'duck'} = 'donald' ; =end original # �ե�����˥���/�ͤ��Ȥ��ɲ� $h{'Wall'} = 'Larry' ; $h{'Smith'} = 'John' ; $h{'mouse'} = 'mickey' ; $h{'duck'} = 'donald' ; =begin original # Delete delete $h{"duck"} ; =end original # ��� delete $h{"duck"} ; =begin original # Cycle through the keys printing them in order. # Note it is not necessary to sort the keys as # the btree will have kept them in order automatically. foreach (keys %h) { print "$_\n" } =end original # ���֤˥����򷫤��֤������Ϥ��ޤ� # btree ����ưŪ�˽��֤��ݤäƤ���Τ� # �����򥽡��Ȥ���ɬ�פ��ʤ����Ȥ����դ��Ƥ������� foreach (keys %h) { print "$_\n" } untie %h ; =begin original Here is the output from the code above. =end original �嵭�Υ����ɤϰʲ��Τ褦�˽��Ϥ��ޤ�: mouse Smith Wall =begin original There are a few point to bear in mind if you want to change the ordering in a BTREE database: =end original BTREE �ǡ����١����ǽ�����ѹ��������ΤǤ���С������Ĥ����դ��٤� �ݥ���Ȥ�����ޤ�: =over 5 =item 1. =begin original The new compare function must be specified when you create the database. =end original ��������Ӵؿ��ϥǡ����١������������Ȥ��˻��ꤵ��ʤ���Фʤ�ޤ��� =item 2. =begin original You cannot change the ordering once the database has been created. Thus you must use the same compare function every time you access the database. =end original ���٥ǡ����١�����������Ƥ��ޤä��������ѹ����뤳�ȤϤǤ��ޤ󤻤� ���Τ���ǡ����١����˥�����������Ȥ��ˤϡ����Ĥ�Ʊ����Ӵؿ��� �Ȥ�ʤ���Фʤ�ޤ��� =item 3 =begin original Duplicate keys are entirely defined by the comparison function. In the case-insensitive example above, the keys: 'KEY' and 'key' would be considered duplicates, and assigning to the second one would overwrite the first. If duplicates are allowed for (with the R_DUP flag discussed below), only a single copy of duplicate keys is stored in the database --- so (again with example above) assigning three values to the keys: 'KEY', 'Key', and 'key' would leave just the first key: 'KEY' in the database with three values. For some situations this results in information loss, so care should be taken to provide fully qualified comparison functions when necessary. For example, the above comparison routine could be modified to additionally compare case-sensitively if two keys are equal in the case insensitive comparison: =end original �����ν�ʣ�ϴ�������Ӵؿ��ˤ�ä��������ޤ��� �嵭����ʸ��/��ʸ������̤��ʤ���Ǥϡ�����: 'KEY'��'key' �� ��ʣ�����Τȹͤ���졢2 ���ܤΤ�Τ���������Ⱥǽ�Τ�Τ��� ����Ǥ��礦�� (�������������� R_DUPS �ե饰��)��ʣ�����������Τ� ����С���Ĥν�ʣ�����������ǡ����١����˳�Ǽ����ޤ� --- ���Τ��� (�ƤӾ嵭����Ǥ�)����: 'KEY'��'Key' ������ 'key' �ϡ��ǡ����١����� ���Ĥ��ͤ���ä��ǽ�Υ���: 'KEY' ������Ĥ��ޤ��� �����ˤ�äƤϤ���Ͼ���Υ����ˤʤ�ޤ��� ������ɬ�פʤȤ��ˤϴ����˽������줿 ��Ӥ��󶡤���褦�����դ��ʤ���Фʤ�ޤ��� �㤨�о嵭����ӥ롼����ϡ��⤷��ĤΥ�������ʸ��/��ʸ���� ���̤��ʤ��Ȥ���Ʊ���Ǥ���С��ɲäȤ�����ʸ��/��ʸ������̤��� ��Ӥ���褦���ѹ����뤳�Ȥ��Ǥ��ޤ�: =begin original sub compare { my($key1, $key2) = @_; lc $key1 cmp lc $key2 || $key1 cmp $key2; } =end original sub compare { my($key1, $key2) = @_; lc $key1 cmp lc $key2 || $key1 cmp $key2; } =begin original And now you will only have duplicates when the keys themselves are truly the same. (note: in versions of the db library prior to about November 1996, such duplicate keys were retained so it was possible to recover the original keys in sets of keys that compared as equal). =end original ��������С��������Τ�Τ�������Ʊ���Ȥ��ˤ����� ��ʣ����Ĥ��Ȥˤʤ�ޤ��� (����:1996 ǯ 11 ��ޤǤ� db �饤�֥��ΥС������Ǥϡ� ���Τ褦�ʽ�ʣ�����������Ĥ��졢���Τ���Ʊ���Ǥ������Ӥ���륭���� ����Ǹ��Υ������᤹���Ȥ��Ǥ��ޤ���)�� =back =head2 Handling Duplicate Keys (��ʣ���륭���μ�갷��) =begin original The BTREE file type optionally allows a single key to be associated with an arbitrary number of values. This option is enabled by setting the flags element of C<$DB_BTREE> to R_DUP when creating the database. =end original BTREE �ե����륿���פϥ��ץ����ǡ���ĤΥ�����Ǥ�դθĿ����ͤ� ��Ϣ�դ��뤳�Ȥ��ǽ�ˤ��Ƥ��ޤ��� ���Υ��ץ����ϥǡ����١������������Ȥ��� C<$DB_BTREE> �Υե饰���Ǥ� R_DUP �����ꤹ�뤳�Ȥˤ���ǽ�ˤʤ�ޤ��� =begin original There are some difficulties in using the tied hash interface if you want to manipulate a BTREE database with duplicate keys. Consider this code: =end original BTREE �ǡ����١������ʣ������������äư��������ʤ�С�tie ����Ƥ��� �ϥå��奤�󥿡��ե�������Ȥ���硢�����Ĥ������꤬����ޤ��� �ʲ��Υ����ɤˤĤ��ƹͤ��ƤߤƤ�������: use warnings ; use strict ; use DB_File ; my ($filename, %h) ; $filename = "tree" ; unlink $filename ; =begin original # Enable duplicate records $DB_BTREE->{'flags'} = R_DUP ; =end original # �쥳���ɤν�ʣ���ǽ�Ȥ��ޤ� $DB_BTREE->{'flags'} = R_DUP ; tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE or die "Cannot open $filename: $!\n"; =begin original # Add some key/value pairs to the file $h{'Wall'} = 'Larry' ; $h{'Wall'} = 'Brick' ; # Note the duplicate key $h{'Wall'} = 'Brick' ; # Note the duplicate key and value $h{'Smith'} = 'John' ; $h{'mouse'} = 'mickey' ; =end original # �����Ĥ��Υ���/�ͤ��Ȥ�ե�������ɲä��ޤ� $h{'Wall'} = 'Larry' ; $h{'Wall'} = 'Brick' ; # �����ν�ʣ������ $h{'Wall'} = 'Brick' ; # �������ͤν�ʣ������ $h{'Smith'} = 'John' ; $h{'mouse'} = 'mickey' ; =begin original # iterate through the associative array # and print each key/value pair. foreach (sort keys %h) { print "$_ -> $h{$_}\n" } =end original # Ϣ�������ʬ�����֤��� # �ƥ���/�ͤ��Ȥ���Ϥ��ޤ� foreach (sort keys %h) { print "$_ -> $h{$_}\n" } untie %h ; =begin original Here is the output: =end original ����ϰʲ��Τ褦�˽��Ϥ��ޤ�: Smith -> John Wall -> Larry Wall -> Larry Wall -> Larry mouse -> mickey =begin original As you can see 3 records have been successfully created with key C - the only thing is, when they are retrieved from the database they I to have the same value, namely C. The problem is caused by the way that the associative array interface works. Basically, when the associative array interface is used to fetch the value associated with a given key, it will only ever retrieve the first value. =end original ����C��3�ĤΥ쥳���ɤ�����˺����Ǥ����褦�˸����ޤ� - ���줬ͣ�������Τ��ȤǤ��� �ǡ����١���������Ф��ȡ��ޤ�� C �Ȥ���̾���� Ʊ���ͤ����äƤ��뤫�Τ褦�˸����ޤ��� �����Ϣ�����󥤥󥿡��ե���������ǽ������ˡ�ˤ�ä�ȯ�����ޤ��� ����Ū��Ϣ�����󥤥󥿡��ե�������Ϳ����줿�����˴�Ϣ�դ���줿�ͤ� ���Ф�����˻Ȥ��ޤ��� ����Ϻǽ���ͤ�������Ф��ޤ��� =begin original Although it may not be immediately obvious from the code above, the associative array interface can be used to write values with duplicate keys, but it cannot be used to read them back from the database. =end original ����Ͼ嵭�Υ����ɤǤϡ����ޤ����ΤˤϤʤäƤ��ޤ��󤬡� Ϣ�����󥤥󥿡��ե������Ͻ�ʣ���륭�����ͤ�񤭤��ळ�ȤϤǤ��ޤ��� ���������ǡ����١��������ͤ��ɤߤ��ि��ˤϻȤ��ޤ��� =begin original The way to get around this problem is to use the Berkeley DB API method called C. This method allows sequential access to key/value pairs. See L for details of both the C method and the API in general. =end original �����������򤹤뤿��ˤ� C �Ȥ��� Berkeley DB API �᥽�åɤ� �Ȥ��ޤ��� ���Υ᥽�åɤϽ��֤˥���/�ͤ��Ȥ˥����������뤳�Ȥ��ǽ�ˤ��ޤ��� C �᥽�åɤξܺ٤�API���̤ˤĤ��Ƥ� L �� ������������ =begin original Here is the script above rewritten using the C API method. =end original C API �᥽�åɤ�Ȥäƾ嵭�Υ�����ץȤ��ľ������Τ� �ʲ����̤�Ǥ�: use warnings ; use strict ; use DB_File ; my ($filename, $x, %h, $status, $key, $value) ; $filename = "tree" ; unlink $filename ; =begin original # Enable duplicate records $DB_BTREE->{'flags'} = R_DUP ; =end original # �쥳���ɤν�ʣ���ǽ�Ȥ��ޤ� $DB_BTREE->{'flags'} = R_DUP ; $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE or die "Cannot open $filename: $!\n"; =begin original # Add some key/value pairs to the file $h{'Wall'} = 'Larry' ; $h{'Wall'} = 'Brick' ; # Note the duplicate key $h{'Wall'} = 'Brick' ; # Note the duplicate key and value $h{'Smith'} = 'John' ; $h{'mouse'} = 'mickey' ; =end original # �����Ĥ��Υ���/�ͤ��Ȥ�ե�������ɲä��ޤ� $h{'Wall'} = 'Larry' ; $h{'Wall'} = 'Brick' ; # �����ν�ʣ������ $h{'Wall'} = 'Brick' ; # �������ͤν�ʣ������ $h{'Smith'} = 'John' ; $h{'mouse'} = 'mickey' ; =begin original # iterate through the btree using seq # and print each key/value pair. $key = $value = 0 ; for ($status = $x->seq($key, $value, R_FIRST) ; $status == 0 ; $status = $x->seq($key, $value, R_NEXT) ) { print "$key -> $value\n" } =end original # Ϣ�������ʬ�����֤��� # �ƥ���/�ͤ��Ȥ���Ϥ��ޤ��� $key = $value = 0 ; for ($status = $x->seq($key, $value, R_FIRST) ; $status == 0 ; $status = $x->seq($key, $value, R_NEXT) ) { print "$key -> $value\n" } undef $x ; untie %h ; =begin original that prints: =end original ����ϰʲ��Τ褦�˽��Ϥ��ޤ�: Smith -> John Wall -> Brick Wall -> Brick Wall -> Larry mouse -> mickey =begin original This time we have got all the key/value pairs, including the multiple values associated with the key C. =end original ���٤ϥ��� C �˴�Ϣ�դ���줿ʣ�����ͤ�ޤ�ơ����ƤΥ���/�ͤ� �������ޤ����� =begin original To make life easier when dealing with duplicate keys, B comes with a few utility methods. =end original ��ʣ���륭���ΰ��������ñ�ˤ��뤿�ᡢB �Ϥ����Ĥ��� �桼�ƥ���ƥ��᥽�åɤ������Ƥ��ޤ��� =head2 The get_dup() Method (get_dup() �᥽�å�) =begin original The C method assists in reading duplicate values from BTREE databases. The method can take the following forms: =end original C �᥽�åɤ� BTREE �ǡ����١��������ʣ�����ͤ��ɤ߹��ळ�Ȥ� �����ޤ��� ���Υ᥽�åɤϰʲ��Τ褦�ʷ����Ȥ뤳�Ȥ��Ǥ��ޤ�: =begin original $count = $x->get_dup($key) ; @list = $x->get_dup($key) ; %list = $x->get_dup($key, 1) ; =end original $count = $x->get_dup($key) ; @list = $x->get_dup($key) ; %list = $x->get_dup($key, 1) ; =begin original In a scalar context the method returns the number of values associated with the key, C<$key>. =end original �����饳��ƥ����ȤǤ� C<$key> �Ȥ��������˴�Ϣ�դ���줿 �ͤο����֤��ޤ��� =begin original In list context, it returns all the values which match C<$key>. Note that the values will be returned in an apparently random order. =end original �ꥹ�ȥ���ƥ����ȤǤ� C<$key> ���б������ͤ������֤��ޤ��� �ͤϸ������塢�Ǥ����ʽ��֤��֤ä����Ȥ������Ȥ����դ��Ƥ��������� =begin original In list context, if the second parameter is present and evaluates TRUE, the method returns an associative array. The keys of the associative array correspond to the values that matched in the BTREE and the values of the array are a count of the number of times that particular value occurred in the BTREE. =end original �ꥹ�ȥ���ƥ����ȤǤϡ�2 ���ܤΰ��������꿿��ɾ�������С� �᥽�åɤ�Ϣ��������֤��ޤ��� ����Ϣ������Υ����� BTREE �ǤΥޥå����� �ͤ��б���������������ͤϤ����ͤ� BTREE �˸�������Ǥ��� =begin original So assuming the database created above, we can use C like this: =end original ������Ȥ˾嵭�Ǻ��������ǡ����١����ˤĤ��ƹͤ���ȡ��ʲ��Τ褦�� C ��Ȥ����Ȥ��Ǥ��ޤ�: use warnings ; use strict ; use DB_File ; my ($filename, $x, %h) ; $filename = "tree" ; =begin original # Enable duplicate records $DB_BTREE->{'flags'} = R_DUP ; =end original # �쥳���ɤν�ʣ���ǽ�Ȥ��ޤ� $DB_BTREE->{'flags'} = R_DUP ; $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE or die "Cannot open $filename: $!\n"; my $cnt = $x->get_dup("Wall") ; print "Wall occurred $cnt times\n" ; my %hash = $x->get_dup("Wall", 1) ; print "Larry is there\n" if $hash{'Larry'} ; print "There are $hash{'Brick'} Brick Walls\n" ; my @list = sort $x->get_dup("Wall") ; print "Wall => [@list]\n" ; @list = $x->get_dup("Smith") ; print "Smith => [@list]\n" ; @list = $x->get_dup("Dog") ; print "Dog => [@list]\n" ; =begin original and it will print: =end original ����ϰʲ��Τ褦�˽��Ϥ��ޤ�: =begin original Wall occurred 3 times Larry is there There are 2 Brick Walls Wall => [Brick Brick Larry] Smith => [John] Dog => [] =end original Wall occurred 3 times Larry is there There are 2 Brick Walls Wall => [Brick Brick Larry] Smith => [John] Dog => [] =head2 The find_dup() Method (find_dup() �᥽�å�) $status = $X->find_dup($key, $value) ; =begin original This method checks for the existence of a specific key/value pair. If the pair exists, the cursor is left pointing to the pair and the method returns 0. Otherwise the method returns a non-zero value. =end original ���Υ᥽�åɤϡ����륭��/�ͤ��Ȥ�¸�ߤ��뤫�ɤ���������å����ޤ��� �⤷�����Ȥ�����Х�������Ϥ����Ȥ򼨤����᥽�åɤ� 0 ���֤��ޤ��� �����Ǥʤ���Х᥽�åɤ� 0 �ʳ����ͤ��֤��ޤ��� =begin original Assuming the database from the previous example: =end original �����㤫��Υǡ����١����ˤĤ��ƹͤ����: use warnings ; use strict ; use DB_File ; my ($filename, $x, %h, $found) ; $filename = "tree" ; =begin original # Enable duplicate records $DB_BTREE->{'flags'} = R_DUP ; =end original # �쥳���ɤν�ʣ���ǽ�Ȥ��ޤ� $DB_BTREE->{'flags'} = R_DUP ; $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE or die "Cannot open $filename: $!\n"; $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ; print "Larry Wall is $found there\n" ; $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ; print "Harry Wall is $found there\n" ; undef $x ; untie %h ; =begin original prints this =end original ����ϰʲ��Τ褦�˽��Ϥ��ޤ�: Larry Wall is there Harry Wall is not there =head2 The del_dup() Method (del_dup() �᥽�å�) $status = $X->del_dup($key, $value) ; =begin original This method deletes a specific key/value pair. It returns 0 if they exist and have been deleted successfully. Otherwise the method returns a non-zero value. =end original ���Υ᥽�åɤϻ��ꤵ�줿����/�ͤ��Ȥ������ޤ��� �⤷¸�ߤ�������˺���Ǥ����� 0 ���֤��ޤ��� �����Ǥʤ���� 0 �ʳ����ͤ��֤��ޤ��� =begin original Again assuming the existence of the C database =end original �Ƥ� C �ǡ����١���������Ȥ��ޤ��� use warnings ; use strict ; use DB_File ; my ($filename, $x, %h, $found) ; $filename = "tree" ; =begin original # Enable duplicate records $DB_BTREE->{'flags'} = R_DUP ; =end original # �쥳���ɤν�ʣ���ǽ�Ȥ��ޤ� $DB_BTREE->{'flags'} = R_DUP ; $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE or die "Cannot open $filename: $!\n"; $x->del_dup("Wall", "Larry") ; $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ; print "Larry Wall is $found there\n" ; undef $x ; untie %h ; =begin original prints this =end original ����ϰʲ��Τ褦�˽��Ϥ��ޤ�: Larry Wall is not there =head2 Matching Partial Keys (��ʬ�����Υޥå���) =begin original The BTREE interface has a feature which allows partial keys to be matched. This functionality is I available when the C method is used along with the R_CURSOR flag. =end original BTREE ���󥿡��ե������ϥޥå������뤿�����ʬ����������Ȥ��� ��ǽ����äƤ��ޤ��� ���ε�ǽ�� R_CURSOR �ե饰�Ȱ��� C �᥽�åɤ� �Ȥä��Ȥ��� I<�Τ�> ͭ���Ǥ��� $x->seq($key, $value, R_CURSOR) ; =begin original Here is the relevant quote from the dbopen man page where it defines the use of the R_CURSOR flag with seq: =end original dbopen �ޥ˥奢��ڡ��������Ϣ���� seq �Ǥ� R_CURSOR �ե饰�λȤ����� �Ĥ���������Ƥ�����ʬ�ΰ��Ѥ�ʲ��˼����ޤ�: =begin original Note, for the DB_BTREE access method, the returned key is not necessarily an exact match for the specified key. The returned key is the smallest key greater than or equal to the specified key, permitting partial key matches and range searches. =end original DB_BTREE ���������᥽�åɤˤĤ��Ƥ��֤���륭����ɬ��������ꤵ�줿 �����˴����˥ޥå�����櫓�ǤϤʤ����Ȥ����ա� �֤���륭���ϻ��ꤵ�줿��������礭����Ʊ���Ǥ��� �Ǿ��Υ����ǡ���ʬ�����ޥå����ϰϸ������ǽ�Ȥ��Ƥ��ޤ��� =begin original In the example script below, the C sub uses this feature to find and print the first matching key/value pair given a partial key. =end original �ʲ�����Υ�����ץȤǤϡ�C ���֥롼����ϡ����ε�ǽ��Ȥä� ���ꤵ�줿��ʬ�����˥ޥå�����ǽ�Υ���/�ͤ��Ȥ򸫤Ĥ����Ϥ��ޤ��� use warnings ; use strict ; use DB_File ; use Fcntl ; my ($filename, $x, %h, $st, $key, $value) ; sub match { my $key = shift ; my $value = 0; my $orig_key = $key ; $x->seq($key, $value, R_CURSOR) ; print "$orig_key\t-> $key\t-> $value\n" ; } $filename = "tree" ; unlink $filename ; $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE or die "Cannot open $filename: $!\n"; =begin original # Add some key/value pairs to the file $h{'mouse'} = 'mickey' ; $h{'Wall'} = 'Larry' ; $h{'Walls'} = 'Brick' ; $h{'Smith'} = 'John' ; =end original # �ե�����ˤ����Ĥ��Υ���/�ͤ��Ȥ��ɲä��ޤ� $h{'mouse'} = 'mickey' ; $h{'Wall'} = 'Larry' ; $h{'Walls'} = 'Brick' ; $h{'Smith'} = 'John' ; $key = $value = 0 ; print "IN ORDER\n" ; for ($st = $x->seq($key, $value, R_FIRST) ; $st == 0 ; $st = $x->seq($key, $value, R_NEXT) ) { print "$key -> $value\n" } print "\nPARTIAL MATCH\n" ; match "Wa" ; match "A" ; match "a" ; undef $x ; untie %h ; =begin original Here is the output: =end original ����ϰʲ��Τ褦�˽��Ϥ��ޤ�: IN ORDER Smith -> John Wall -> Larry Walls -> Brick mouse -> mickey PARTIAL MATCH Wa -> Wall -> Larry A -> Smith -> John a -> mouse -> mickey =head1 DB_RECNO =begin original DB_RECNO provides an interface to flat text files. Both variable and fixed length records are supported. =end original DB_RECNO �ϥե�åȤʥƥ����ȥե�����ؤΥ��󥿡��ե��������󶡤��ޤ��� ����Ĺ�쥳���ɤȸ���Ĺ�쥳���ɤ�ξ���򥵥ݡ��Ȥ��ޤ��� =begin original In order to make RECNO more compatible with Perl, the array offset for all RECNO arrays begins at 0 rather than 1 as in Berkeley DB. =end original RECNO �� Perl �Ȥθߴ�����������뤿�ᡢRECNO ����Τ�������󥪥ե��åȤ� Berkeley DB �Ǥ� 1 �ǤϤʤ� 0 ����Ϥޤ�ޤ��� =begin original As with normal Perl arrays, a RECNO array can be accessed using negative indexes. The index -1 refers to the last element of the array, -2 the second last, and so on. Attempting to access an element before the start of the array will raise a fatal run-time error. =end original �̾�� Perl �����Ʊ���褦�ˡ�RECNO ����ˤ���Υ���ǥå����� �Ȥäƥ����������뤳�Ȥ��Ǥ��ޤ��� ����ǥå��� -1 ������κǸ�����Ǥ� -2 �ϸ������ 2 ���ܤ�Ȥ��ä��褦�˻��Ȥ��ޤ��� ����κǽ�����������Ǥ˥����������褦�Ȥ���� ��̿Ū�ʥ�󥿥��२�顼�򵯤����ޤ��� =head2 The 'bval' Option ('bval' ���ץ����) =begin original The operation of the bval option warrants some discussion. Here is the definition of bval from the Berkeley DB 1.85 recno manual page: =end original bval ���ץ��������Ϥ����Ĥ��������ݾڤ��ޤ��� �ʲ��� Berkeley DB 1.85 recno �ޥ˥奢��ڡ�������� bval ������Ǥ�: =begin original The delimiting byte to be used to mark the end of a record for variable-length records, and the pad charac- ter for fixed-length records. If no value is speci- fied, newlines (``\n'') are used to mark the end of variable-length records and fixed-length records are padded with spaces. =end original ����Ĺ�쥳���ɤ��ᡢ�쥳���ɤν�����ޡ������뤿��˻Ȥ��� ���ڤ�Х��ȡ������Ƹ���Ĺ�쥳���ɤΤ����������ʸ���� ���ꤵ��ʤ���С�����("\n") ������Ĺ�쥳���ɤν����򼨤������ �Ȥ�졢����Ĺ�쥳���ɤˤ϶��������ޤ��� =begin original The second sentence is wrong. In actual fact bval will only default to C<"\n"> when the openinfo parameter in dbopen is NULL. If a non-NULL openinfo parameter is used at all, the value that happens to be in bval will be used. That means you always have to specify bval when making use of any of the options in the openinfo parameter. This documentation error will be fixed in the next release of Berkeley DB. =end original 2 ���ܤ�ʸ�ϴְ�äƤ��ޤ��� �ºݤˤϡ�dbopen �Ǥ� openinfo �ѥ�᡼���� NULL �Ǥ���Ȥ�������bval �ϥǥե���Ȥ� C<"\n"> �Ȥʤ�ޤ��� NULL �ʳ��� openinfo �ѥ�᡼�����Ȥ���ȡ�bval ���Ȥ���Ȥ��� ���ޤ��ޤ����ͤˤʤäƤ��ޤ��ޤ��� �Ĥޤ� openinfo �ѥ�᡼���ǥ��ץ�������Ѥ���Ȥ� �ˤϡ�bval ����ꤷ�ʤ���Фʤ�ޤ��� ���Υɥ�����ȤΥ��顼�� Berkeley DB �� ���Υ�꡼���ǽ��������Ǥ��礦�� =begin original That clarifies the situation with regards Berkeley DB itself. What about B? Well, the behavior defined in the quote above is quite useful, so B conforms to it. =end original Berkeley DB ���ȤˤĤ��Ƥξ��������餫�ˤʤ�ޤ����� ����Ǥ� B �Ϥɤ��Ǥ��礦? �嵭�ΰ��Ѥ��������Ƥ���ư���ϤȤƤ������Ǥ�; ������ B �Ϥ���˽��äƤ��ޤ��� =begin original That means that you can specify other options (e.g. cachesize) and still have bval default to C<"\n"> for variable length records, and space for fixed length records. =end original �Ĥޤ� bval �����Ĺ�쥳���ɤˤ� "C<\n"> �򡢸���Ĺ�쥳���ɤˤ� �����ǥե���Ȥˤ����ޤޡ�¾�Υ��ץ����(�㤨�� cachesize)�� ���ꤹ�뤳�Ȥ��Ǥ��ޤ��� =begin original Also note that the bval option only allows you to specify a single byte as a delimiter. =end original �ޤ� bval ���ץ����϶��ڤ�Ȥ��� 1 �Х��Ȥ���������Ǥ��뤳�Ȥˤ� ���դ��Ƥ��������� =head2 A Simple Example (��ñ����) =begin original Here is a simple example that uses RECNO (if you are using a version of Perl earlier than 5.004_57 this example won't work -- see L for a workaround). =end original �ʲ��� RECNO ��Ȥä���ñ����򼨤��ޤ�(�⤷ 5.004_57 �������� �С������� Perl ��ȤäƤ���ȡ����Υ���ץ��ư���ޤ��� -- ��褹�뤿��ˤ� L ����������)�� use warnings ; use strict ; use DB_File ; my $filename = "text" ; unlink $filename ; my @h ; tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_RECNO or die "Cannot open file 'text': $!\n" ; =begin original # Add a few key/value pairs to the file $h[0] = "orange" ; $h[1] = "blue" ; $h[2] = "yellow" ; =end original # �����Ĥ��Υ������ͤ��Ȥ�ե�����˲ä��ޤ� $h[0] = "orange" ; $h[1] = "blue" ; $h[2] = "yellow" ; push @h, "green", "black" ; my $elements = scalar @h ; print "The array contains $elements entries\n" ; my $last = pop @h ; print "popped $last\n" ; unshift @h, "white" ; my $first = shift @h ; print "shifted $first\n" ; =begin original # Check for existence of a key print "Element 1 Exists with value $h[1]\n" if $h[1] ; =end original # ������¸�ߤ�����å����ޤ� print "Element 1 Exists with value $h[1]\n" if $h[1] ; =begin original # use a negative index print "The last element is $h[-1]\n" ; print "The 2nd last element is $h[-2]\n" ; =end original # �ޥ��ʥ��Υ���ǥå�����Ȥ��ޤ� print "The last element is $h[-1]\n" ; print "The 2nd last element is $h[-2]\n" ; untie @h ; =begin original Here is the output from the script: =end original ������ץȤν��Ϥϰʲ��Τ褦�ˤʤ�ޤ�: The array contains 5 entries popped black shifted white Element 1 Exists with value blue The last element is green The 2nd last element is yellow =head2 Extra RECNO Methods (���̤� RECNO �᥽�å�) =begin original If you are using a version of Perl earlier than 5.004_57, the tied array interface is quite limited. In the example script above C, C, C, C or determining the array length will not work with a tied array. =end original 5.004_57 ������ΥС������� Perl ��ȤäƤ���ΤǤ���С�tie ���줿 ����Υ��󥿡��ե����������˸��ꤵ��ޤ��� �嵭�Υ�����ץ���Ǥ� C, C, C, C �� Ĺ����Ƚ�ꤹ�뤳�Ȥ� tie ���줿����ǤϤ��ޤ���ǽ���ޤ��� =begin original To make the interface more useful for older versions of Perl, a number of methods are supplied with B to simulate the missing array operations. All these methods are accessed via the object returned from the tie call. =end original �Ť� Perl �ΥС������Τ���Ρ����󥿡��ե���������Ȥ��䤹�����뤿�ᡢ C �Ǥϡ�����줿�������򥷥ߥ�졼�Ȥ��롢�����Ĥ��Υ᥽�åɤ� �󶡤���Ƥ��ޤ��� ����餹�٤ƤΥ᥽�åɤ�tie�ƤӽФ����֤���� ���֥������Ȥ�𤷤ƥ�����������ޤ��� =begin original Here are the methods: =end original �ʲ��Υ᥽�åɤ�����ޤ�: =over 5 =item B<$X-Epush(list) ;> =begin original Pushes the elements of C to the end of the array. =end original ����κǸ�� C �����Ǥ��ɲä��ޤ��� =item B<$value = $X-Epop ;> =begin original Removes and returns the last element of the array. =end original ����κǸ�����Ǥ������֤��ޤ��� =item B<$X-Eshift> =begin original Removes and returns the first element of the array. =end original ����κǽ�����Ǥ������֤��ޤ��� =item B<$X-Eunshift(list) ;> =begin original Pushes the elements of C to the start of the array. =end original �������Ƭ�� C �����Ǥ��ɲä��ޤ��� =item B<$X-Elength> =begin original Returns the number of elements in the array. =end original ��������ǿ����֤��ޤ��� =item B<$X-Esplice(offset, length, elements);> =begin original Returns a splice of the array. =end original ����� splice ���֤��ޤ��� =back =head2 Another Example (�⤦1�Ĥ���) =begin original Here is a more complete example that makes use of some of the methods described above. It also makes use of the API interface directly (see L). =end original �ʲ��ˡ��嵭�����������᥽�åɤΤ����Ĥ���Ȥä��⤦��Ĵ�������� �����ޤ��� ����� API ���󥿡��ե�������ľ�ܻȤäƤ��ޤ��� (L �⤴��������)�� use warnings ; use strict ; my (@h, $H, $file, $i) ; use DB_File ; use Fcntl ; $file = "text" ; unlink $file ; $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0666, $DB_RECNO or die "Cannot open file $file: $!\n" ; =begin original # first create a text file to play with $h[0] = "zero" ; $h[1] = "one" ; $h[2] = "two" ; $h[3] = "three" ; $h[4] = "four" ; =end original # �ǽ�ˡ������ƥ����ȥե������������ޤ� $h[0] = "zero" ; $h[1] = "one" ; $h[2] = "two" ; $h[3] = "three" ; $h[4] = "four" ; =begin original # Print the records in order. # # The length method is needed here because evaluating a tied # array in a scalar context does not return the number of # elements in the array. =end original # �쥳���ɤ���֤˽��Ϥ��ޤ��� # # �����饳��ƥ����Ȥ�tie���줿�����ɾ�����Ƥ⡢ # ��������ǿ����֤��ʤ��Τǡ�length�᥽�åɤ�ɬ�פǤ��� print "\nORIGINAL\n" ; foreach $i (0 .. $H->length - 1) { print "$i: $h[$i]\n" ; } =begin original # use the push & pop methods $a = $H->pop ; $H->push("last") ; print "\nThe last record was [$a]\n" ; =end original # push �� pop �᥽�åɤ�Ȥ��ޤ� $a = $H->pop ; $H->push("last") ; print "\nThe last record was [$a]\n" ; =begin original # and the shift & unshift methods $a = $H->shift ; $H->unshift("first") ; print "The first record was [$a]\n" ; =end original # ������ shift �� unshift �᥽�åɤ�Ȥ��ޤ� $a = $H->shift ; $H->unshift("first") ; print "The first record was [$a]\n" ; =begin original # Use the API to add a new record after record 2. $i = 2 ; $H->put($i, "Newbie", R_IAFTER) ; =end original # �쥳���� 2 �θ�˿������쥳���ɤ��ɲä��뤿�� API ��Ȥ��ޤ��� $i = 2 ; $H->put($i, "Newbie", R_IAFTER) ; =begin original # and a new record before record 1. $i = 1 ; $H->put($i, "New One", R_IBEFORE) ; =end original # �����ƿ������쥳���ɤ�쥳���� 1 ������ $i = 1 ; $H->put($i, "New One", R_IBEFORE) ; =begin original # delete record 3 $H->del(3) ; =end original # �쥳���� 3 ���� $H->del(3) ; =begin original # now print the records in reverse order print "\nREVERSE\n" ; for ($i = $H->length - 1 ; $i >= 0 ; -- $i) { print "$i: $h[$i]\n" } =end original # ���٤ϵդν��֤ǥ쥳���ɤ���Ϥ��ޤ� print "\nREVERSE\n" ; for ($i = $H->length - 1 ; $i >= 0 ; -- $i) { print "$i: $h[$i]\n" } =begin original # same again, but use the API functions instead print "\nREVERSE again\n" ; my ($s, $k, $v) = (0, 0, 0) ; for ($s = $H->seq($k, $v, R_LAST) ; $s == 0 ; $s = $H->seq($k, $v, R_PREV)) { print "$k: $v\n" } =end original # Ʊ�����Ȥ�⤦���١������������ API �ؿ���Ȥ��ޤ��� print "\nREVERSE again\n" ; my ($s, $k, $v) = (0, 0, 0) ; for ($s = $H->seq($k, $v, R_LAST) ; $s == 0 ; $s = $H->seq($k, $v, R_PREV)) { print "$k: $v\n" } undef $H ; untie @h ; =begin original and this is what it outputs: =end original ����ϰʲ��Τ褦�˽��Ϥ��ޤ�: ORIGINAL 0: zero 1: one 2: two 3: three 4: four The last record was [four] The first record was [zero] REVERSE 5: last 4: three 3: Newbie 2: one 1: New One 0: first REVERSE again 5: last 4: three 3: Newbie 2: one 1: New One 0: first =begin original Notes: =end original ����: =over 5 =item 1. =begin original Rather than iterating through the array, C<@h> like this: =end original �ʲ��Τ褦������ C<@h> ���Τ򷫤��֤��ΤǤϤʤ�: =begin original foreach $i (@h) =end original foreach $i (@h) =begin original it is necessary to use either this: =end original �ʲ��Τ褦�ˤ���ɬ�פ�����ޤ�: =begin original foreach $i (0 .. $H->length - 1) =end original foreach $i (0 .. $H->length - 1) =begin original or this: =end original ���뤤�ϰʲ��Τ褦�ˤ��ޤ�: =begin original for ($a = $H->get($k, $v, R_FIRST) ; $a == 0 ; $a = $H->get($k, $v, R_NEXT) ) =end original for ($a = $H->get($k, $v, R_FIRST) ; $a == 0 ; $a = $H->get($k, $v, R_NEXT) ) =item 2. =begin original Notice that both times the C method was used the record index was specified using a variable, C<$i>, rather than the literal value itself. This is because C will return the record number of the inserted line via that parameter. =end original C �᥽�åɤ��Ȥ�줿�Ȥ���ξ���Ȥ�쥳���ɥ���ǥå��� �˥�ƥ���ͤǤϤʤ����ѿ� C<$i> ���Ȥ��Ƥ��뤳�Ȥ� ���դ��Ƥ��������� ����� C �ϥѥ�᡼���ˤ�ä��ɲä��줿 �ԤΥ쥳�����ֹ���֤�����Ǥ��� =back =head1 THE API INTERFACE (API ���󥿡��ե�����) =begin original As well as accessing Berkeley DB using a tied hash or array, it is also possible to make direct use of most of the API functions defined in the Berkeley DB documentation. =end original tie ���줿�ϥå���������Ȥä� Berkeley DB �˥���������������Ǥʤ��� Berkeley DB �ɥ�����Ȥ��������Ƥ� API �ؿ��ΤۤȤ�ɤ� ľ�ܻȤ����Ȥ�Ǥ��ޤ��� =begin original To do this you need to store a copy of the object returned from the tie. =end original ����򤹤뤿��ˤ� tie �����֤���륪�֥������ȤΥ��ԡ��� ��äƤ���ɬ�פ�����ޤ��� $db = tie %hash, "DB_File", "filename" ; =begin original Once you have done that, you can access the Berkeley DB API functions as B methods directly like this: =end original ���٤�������С��ʲ��Τ褦�� B �᥽�åɤΤ褦�� Berkeley DB API �ؿ���ľ�ܥ����������뤳�Ȥ��Ǥ��ޤ�: $db->put($key, $value, R_NOOVERWRITE) ; =begin original B If you have saved a copy of the object returned from C, the underlying database file will I be closed until both the tied variable is untied and all copies of the saved object are destroyed. =end original B<����:> C �����֤���륪�֥������ȤΥ��ԡ�����¸�������ϡ� tie ���줿�ѿ��η���դ����Ϥ�����(untie)����¸���줿���֥������Ȥ� �˲������ޤǡ����ˤʤäƤ���ǡ����١����ե�����ϥ������� I<����ޤ���>�� use DB_File ; $db = tie %hash, "DB_File", "filename" or die "Cannot tie filename: $!" ; ... undef $db ; untie %hash ; =begin original See L for more details. =end original �ܺ٤ˤĤ��Ƥ� L �������������� =begin original All the functions defined in L are available except for close() and dbopen() itself. The B method interface to the supported functions have been implemented to mirror the way Berkeley DB works whenever possible. In particular note that: =end original close() �� dbopen() ���Ȥ���� L ��������줿���Ƥδؿ��� ���Ѥ��뤳�Ȥ��Ǥ��ޤ��� ���ݡ��Ȥ����ؿ��ؤ� B �᥽�åɥ��󥿡��ե������ϡ� ��ǽ�ʸ¤� Berkeley DB ����ˡ��Ʊ���褦�˼�������Ƥ��ޤ��� �ä˰ʲ����������դ��Ƥ�������: =over 5 =item * =begin original The methods return a status value. All return 0 on success. All return -1 to signify an error and set C<$!> to the exact error code. The return code 1 generally (but not always) means that the key specified did not exist in the database. =end original �᥽�åɤϥ��ơ������ͤ��֤��ޤ��� ��������� 0 ���֤��ޤ��� ���顼��ɽ������ˤ� -1 ���֤�����̩�ʥ��顼�����ɤ� C<$!> �����ꤷ�ޤ��� �꥿���󥳡���1��(��ˤǤϤ���ޤ���)����Ū�ˤϻ��ꤵ�줿������ �ǡ����١�����¸�ߤ��ʤ����Ȥ��̣���ޤ��� =begin original Other return codes are defined. See below and in the Berkeley DB documentation for details. The Berkeley DB documentation should be used as the definitive source. =end original ¾�ˤ�꥿���󥳡��ɤ��������Ƥ��ޤ��� �ܺ٤ˤĤ��Ƥϰʲ������������ Berkeley DB �ɥ�����Ȥ����������� Berkeley DB �ɥ�����Ȥ�����Ū�ʾ��󸻤Ǥ��� =item * =begin original Whenever a Berkeley DB function returns data via one of its parameters, the equivalent B method does exactly the same. =end original Berkeley DB �ؿ����ǡ�����ѥ�᡼���ΰ�Ĥ�𤷤��֤��Ȥ��ˤϤ��ĤǤ⡢ Ʊ�ͤ� B �᥽�åɤ�����Ʊ��̾����¸�ߤ��ޤ��� =item * =begin original If you are careful, it is possible to mix API calls with the tied hash/array interface in the same piece of code. Although only a few of the methods used to implement the tied interface currently make use of the cursor, you should always assume that the cursor has been changed any time the tied hash/array interface is used. As an example, this code will probably not do what you expect: =end original ���ʤ������դ���С�tie ���줿�ϥå���/���󥤥󥿡��ե������� API ��Ʊ���ץ���������˺����뤳�Ȥ�Ǥ��ޤ��� tie ���줿���󥿡��ե�������������뤿��˻Ȥ���᥽�åɤΤ��� �ۤ�ξ�������������������Ѥ��ޤ��󤬡�tie ���줿 �ϥå���/���󥤥󥿡��ե��������Ȥ�줿�Ȥ��ϡ� ���Ĥ⥫�����뤬�ѹ�����Ƥ���ȹͤ��ʤ���Фʤ�ޤ��� �㤨�С����Υ����ɤϤ��֤���Ԥ��Ƥ����褦�ˤ�ư���ޤ���: $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE or die "Cannot tie $filename: $!" ; =begin original # Get the first key/value pair and set the cursor $X->seq($key, $value, R_FIRST) ; =end original # �ǽ�Υ���/�ͤ��Ȥ��������������������ꤷ�ޤ� $X->seq($key, $value, R_FIRST) ; =begin original # this line will modify the cursor $count = scalar keys %x ; =end original # ���ιԤϥ���������ѹ����Ƥ��ޤ��ޤ� $count = scalar keys %x ; =begin original # Get the second key/value pair. # oops, it didn't, it got the last key/value pair! $X->seq($key, $value, R_NEXT) ; =end original # 2 ���ܤΥ���/�ͤ��Ȥ���Ф��ޤ��� # ���顢�����ǤϤ���ޤ��󡣺Ǹ�Υ���/�ͤ��Ȥ���Ф��ޤ���! $X->seq($key, $value, R_NEXT) ; =begin original The code above can be rearranged to get around the problem, like this: =end original �����������򤹤뤿��˾嵭�Υ����ɤ�ʲ��Τ褦�˽񤭤����뤳�Ȥ��Ǥ��ޤ�: $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE or die "Cannot tie $filename: $!" ; =begin original # this line will modify the cursor $count = scalar keys %x ; =end original # ���ιԤϥ���������ѹ����Ƥ��ޤ��ޤ��� $count = scalar keys %x ; =begin original # Get the first key/value pair and set the cursor $X->seq($key, $value, R_FIRST) ; =end original # �ǽ�Υ������ͤ��Ȥ��������������������ꤷ�ޤ� $X->seq($key, $value, R_FIRST) ; =begin original # Get the second key/value pair. # worked this time. $X->seq($key, $value, R_NEXT) ; =end original # 2 ���ܤΥ���/�ͤ��Ȥ���Ф��ޤ��� # ���٤Ϥ��ޤ������ޤ��� $X->seq($key, $value, R_NEXT) ; =back =begin original All the constants defined in L for use in the flags parameters in the methods defined below are also available. Refer to the Berkeley DB documentation for the precise meaning of the flags values. =end original flag �ѥ�᡼�������ѤǤ���褦�� L ���������Ƥ������Ƥ������ �ʲ��Υ᥽�åɤǤ�Ȥ����Ȥ��Ǥ��ޤ��� flags ���ͤ����Τʰ�̣�ˤĤ��Ƥ� Berkeley DB �ɥ�����Ȥ����������� =begin original Below is a list of the methods available. =end original ���ѤǤ���᥽�åɤΰ�����ʲ��˼����ޤ��� =over 5 =item B<$status = $X-Eget($key, $value [, $flags]) ;> =begin original Given a key (C<$key>) this method reads the value associated with it from the database. The value read from the database is returned in the C<$value> parameter. =end original ���� (C<$key>) ��Ϳ������ȡ����Υ᥽�åɤϥǡ����١������� ��Ϣ�����ͤ��ɤߤ��ߤޤ��� �ǡ����١��������ɤߤ�����ͤ� C<$value>�ѥ�᡼�����֤���ޤ��� =begin original If the key does not exist the method returns 1. =end original ������¸�ߤ��ʤ���С��᥽�åɤ�1���֤��ޤ��� =begin original No flags are currently defined for this method. =end original ���Υ᥽�åɤˤĤ��Ƥϡ����ߤʤ�� flag ����ݤ���ޤ��� =item B<$status = $X-Eput($key, $value [, $flags]) ;> =begin original Stores the key/value pair in the database. =end original �ǡ����١����˥���/�ͤ��Ȥߤ��Ǽ���ޤ��� =begin original If you use either the R_IAFTER or R_IBEFORE flags, the C<$key> parameter will have the record number of the inserted key/value pair set. =end original R_IAFTER �ޤ��� R_IBEFORE �ե饰��Ȥ��ȡ�C<$key> �ѥ�᡼���� �������륭��/�ͤ��ȤΥ쥳�����ֹ������ޤ��� =begin original Valid flags are R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE and R_SETCURSOR. =end original Ŭ�ڤ� flag �� R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE, R_SETCURSOR �Ǥ��� =item B<$status = $X-Edel($key [, $flags]) ;> =begin original Removes all key/value pairs with key C<$key> from the database. =end original �ǡ����١������饭�� C<$key> �������ƤΥ���/�ͤ��Ȥ������ޤ��� =begin original A return code of 1 means that the requested key was not in the database. =end original �꥿���󥳡���1���׵ᤵ�줿�������ǡ����١�����¸�ߤ��ʤ��ä����Ȥ� ��̣���ޤ��� =begin original R_CURSOR is the only valid flag at present. =end original R_CURSOR ���������ޤΤ��Ȥ�ͣ��Ŭ�ڤ� flag �Ǥ��� =item B<$status = $X-Efd ;> =begin original Returns the file descriptor for the underlying database. =end original ���ˤʤäƤ���ǡ����١����Τ���Υե�����ǥ�������ץ��� �֤��ޤ��� =begin original See L for an explanation for why you should not use C to lock your database. =end original �ǡ����١�������å����뤿��� C ��Ȥ��٤��ǤϤʤ���ͳ�� �����ˤĤ��Ƥ� L ������������ =item B<$status = $X-Eseq($key, $value, $flags) ;> =begin original This interface allows sequential retrieval from the database. See L for full details. =end original ���Υ��󥿡��ե������ϥǡ����١���������֤˼��Ф����Ȥ� ��ǽ�ˤ��ޤ��� �����ʾܺ٤ˤĤ��Ƥ� L ������������ =begin original Both the C<$key> and C<$value> parameters will be set to the key/value pair read from the database. =end original C<$key> �� C<$value> �ѥ�᡼����ξ���ϥǡ����١������� �ɤߤ��ޤ�륭��/�ͤ��Ȥ����ꤵ��ޤ��� =begin original The flags parameter is mandatory. The valid flag values are R_CURSOR, R_FIRST, R_LAST, R_NEXT and R_PREV. =end original flags �ѥ�᡼����ɬ�ܤǤ��� Ŭ�ڤʥե饰���ͤ� R_CURSOR, R_FIRST, R_LAST, R_NEX, R_PREV�Ǥ��� =item B<$status = $X-Esync([$flags]) ;> =begin original Flushes any cached buffers to disk. =end original ����å��夵�줿�Хåե���ǥ������˥ե�å��夷�ޤ��� =begin original R_RECNOSYNC is the only valid flag at present. =end original ���� R_RECNOSYNC ������Ŭ�ڤʥե饰�Ǥ��� =back =head1 DBM FILTERS (DBM�ե��륿) =begin original A DBM Filter is a piece of code that is be used when you I want to make the same transformation to all keys and/or values in a DBM database. =end original DBM �ե��륿�ϥ����ɤν��ޤ�ǤǤ��ꡢDBM �ǡ����١�������� ���ƤΥ�����/�ޤ����ͤ�Ʊ���Ѵ���Ԥ������� I<���> �פäƤ��� �Ȥ��˻Ȥ��ޤ��� =begin original There are four methods associated with DBM Filters. All work identically, and each is used to install (or uninstall) a single DBM Filter. Each expects a single parameter, namely a reference to a sub. The only difference between them is the place that the filter is installed. =end original DBM �ե��륿�˴�Ϣ���ƤϻͤĤΥ᥽�åɤ�����ޤ��� ����Ʊ�ͤ˵�ǽ���ޤ��� ���줾��ϰ�Ĥ� DBM �ե��륿�򥤥󥹥ȡ��� (�ޤ��ϥ��󥤥󥹥ȡ���)���뤿��˻Ȥ��ޤ��� �ƥ᥽�åɤϰ�ĤΥѥ�᡼�����Ĥޤ� sub �ؤΥ�ե���󥹤���Ԥ��ޤ��� ���줾���ͣ��ΰ㤤�ϥե��륿�����󥹥ȡ��뤵�����Ǥ��� =begin original To summarise: =end original �ޤȤ��Ȱʲ��Τ褦�ˤʤ�ޤ�: =over 5 =item B =begin original If a filter has been installed with this method, it will be invoked every time you write a key to a DBM database. =end original ���Υ᥽�åɤǥե��륿�����󥹥ȡ��뤵���ȡ� DBM �ǡ����١����˥�����񤭤��ि�Ӥˡ����줬�ƤФ�ޤ��� =item B =begin original If a filter has been installed with this method, it will be invoked every time you write a value to a DBM database. =end original ���Υ᥽�åɤǥե��륿�����󥹥ȡ��뤵���ȡ�DBM �ǡ����١����� �ͤ�񤭤��ि�Ӥˡ����줬�ƤФ�ޤ��� =item B =begin original If a filter has been installed with this method, it will be invoked every time you read a key from a DBM database. =end original ���Υ᥽�åɤǥե��륿�����󥹥ȡ��뤵���ȡ�DBM �ǡ����١������� �������ɤߤ��ि�Ӥˡ����줬�ƤФ�ޤ��� =item B =begin original If a filter has been installed with this method, it will be invoked every time you read a value from a DBM database. =end original ���Υ᥽�åɤǥե��륿�����󥹥ȡ��뤵���ȡ�DBM �ǡ����١������� �ͤ��ɤߤ��ि�Ӥˡ����줬�ƤФ�ޤ��� =back =begin original You can use any combination of the methods, from none, to all four. =end original ����̵������ͤ����Ƥޤǡ�������ͳ���Ȥ߹�碌�ƻȤ����Ȥ��Ǥ��ޤ��� =begin original All filter methods return the existing filter, if present, or C in not. =end original ���ƤΥե��륿�᥽�åɤϡ��⤷����д�¸�Υե��륿�� ̵����� C ���֤��ޤ��� =begin original To delete a filter pass C to it. =end original �ե��륿��������ˤ� C ���Ϥ��Ƥ��������� =head2 The Filter (�ե��륿) =begin original When each filter is called by Perl, a local copy of C<$_> will contain the key or value to be filtered. Filtering is achieved by modifying the contents of C<$_>. The return code from the filter is ignored. =end original �ƥե��륿��Perl����ƤӽФ����Ȥ���C<$_> �Υ������륳�ԡ��ˤ� �ե��륿����륭���ޤ����ͤ�����ޤ��� �ե��륿��󥰤� C<$_> �����Ƥ� �ѹ����뤳�Ȥˤ��¸�����ޤ��� �ե��륿���������ͤ�̵�뤵��ޤ��� =head2 An Example -- the NULL termination problem. (�� -- NULL����������) =begin original Consider the following scenario. You have a DBM database that you need to share with a third-party C application. The C application assumes that I keys and values are NULL terminated. Unfortunately when Perl writes to DBM databases it doesn't use NULL termination, so your Perl application will have to manage NULL termination itself. When you write to the database you will have to use something like this: =end original �ʲ��Υ��ʥꥪ�ˤĤ��ƹͤ��ƤߤƤ��������� �����ɥѡ��ƥ��� C ���ץꥱ�������ȶ�ͭ����ɬ�פ����� DBM �ǡ����١�������äƤ���Ȥ��ޤ��� ���� C ���ץꥱ�������� I<���Ƥ�> ������ �ͤ� NULL �ǽ�����ΤȲ��ꤷ�Ƥ��ޤ��� �Թ��ˤ� Perl �� DBM �ǡ����١����˽񤭤���Ȥ���NULL ������Ȥ��ޤ��� ���Τ��ᡢ���ʤ��� Perl ���ץꥱ�������ϼ�ʬ���Ȥ� NULL ������ �������ʤ���Фʤ�ޤ��� �ǡ����١����˽񤭤���Ȥ����ʲ��Τ褦�� ��ˡ��Ȥ�ɬ�פ�����ޤ�: $hash{"$key\0"} = "$value\0" ; =begin original Similarly the NULL needs to be taken into account when you are considering the length of existing keys/values. =end original Ʊ�ͤ�¸�ߤ��륭��/�ͤ�Ĺ����ͤ���Ȥ���NULL ���θ������� ɬ�פ�����ޤ��� =begin original It would be much better if you could ignore the NULL terminations issue in the main application code and have a mechanism that automatically added the terminating NULL to all keys and values whenever you write to the database and have them removed when you read from the database. As I'm sure you have already guessed, this is a problem that DBM Filters can fix very easily. =end original �ᥤ��Υ��ץꥱ�������ץ������Ǥ� NULL ����������̵�뤹�� ���Ȥ��Ǥ����ǡ����١����˽񤭤���Ȥ��ˤϤ��ĤǤ⼫ưŪ�����Ƥ� �������ͤ˽����� NULL ����Ϳ�����ǡ����١��������ɤߤ���Ȥ��ˤϡ� ������������褦�ʵ�������Ĥʤ�С������餷�����ȤǤ��� ���ˤ��狼�꤫�Ȼפ��ޤ�������������� DBM �ե��륿�ˤ�ä� �ȤƤ��ñ�˽������뤳�Ȥ��Ǥ��ޤ��� use warnings ; use strict ; use DB_File ; my %hash ; my $filename = "filt" ; unlink $filename ; my $db = tie %hash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, $DB_HASH or die "Cannot open $filename: $!\n" ; =begin original # Install DBM Filters $db->filter_fetch_key ( sub { s/\0$// } ) ; $db->filter_store_key ( sub { $_ .= "\0" } ) ; $db->filter_fetch_value( sub { s/\0$// } ) ; $db->filter_store_value( sub { $_ .= "\0" } ) ; =end original # DBM�ե��륿�Υ��󥹥ȡ��� $db->filter_fetch_key ( sub { s/\0$// } ) ; $db->filter_store_key ( sub { $_ .= "\0" } ) ; $db->filter_fetch_value( sub { s/\0$// } ) ; $db->filter_store_value( sub { $_ .= "\0" } ) ; $hash{"abc"} = "def" ; my $a = $hash{"ABC"} ; # ... undef $db ; untie %hash ; =begin original Hopefully the contents of each of the filters should be self-explanatory. Both "fetch" filters remove the terminating NULL, and both "store" filters add a terminating NULL. =end original �Ǥ���ʤ�гƥե��륿�����Ƥϼ�������Ū�Ǥ���٤��Ǥ��� ξ���� "fetch" �ե��륿�� NULL ������������� ξ���� "store" �ե��륿�� NULL ��������Ϳ���ޤ��� =head2 Another Example -- Key is a C int. (�⤦��Ĥ��� -- ������ C int) =begin original Here is another real-life example. By default, whenever Perl writes to a DBM database it always writes the key and value as strings. So when you use this: =end original �ºݤξ��̤Ǥ����⤦��ġ� �ǥե���ȤǤ� Perl �� DBM �ǡ����١����� �񤭤���Ȥ��Ϥ��ĤǤ⡢�������ͤ�ʸ����Ȥ��ƽ񤭤��ߤޤ��� ���Τ���ʲ��Τ褦�ˤ����: $hash{12345} = "something" ; =begin original the key 12345 will get stored in the DBM database as the 5 byte string "12345". If you actually want the key to be stored in the DBM database as a C int, you will have to use C when writing, and C when reading. =end original ����12345 �� 5 �Х��Ȥ�ʸ���� "12345" �Ȥ��� DBM �ǡ����١����� ��Ǽ����ޤ��� �⤷������ DBM �ǡ����١����� C �� int �ǥ����� ��Ǽ�������ΤǤ���С��񤭤���Ȥ��� C �����ɤߤ���Ȥ��� C ����ɬ�פ�����ޤ��� =begin original Here is a DBM Filter that does it: =end original �ʲ��Ϥ���򤪤��ʤ� DBM �ե��륿�Ǥ�: use warnings ; use strict ; use DB_File ; my %hash ; my $filename = "filt" ; unlink $filename ; my $db = tie %hash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, $DB_HASH or die "Cannot open $filename: $!\n" ; $db->filter_fetch_key ( sub { $_ = unpack("i", $_) } ) ; $db->filter_store_key ( sub { $_ = pack ("i", $_) } ) ; $hash{123} = "def" ; # ... undef $db ; untie %hash ; =begin original This time only two filters have been used -- we only need to manipulate the contents of the key, so it wasn't necessary to install any value filters. =end original �������ĤΥե��륿��Ȥ��ޤ��� -- ���������Ƥ򰷤����Ȥ�����ɬ�� ���ä��Τǡ��ͤΥե��륿�򥤥󥹥ȡ��뤹��ɬ�פ�����ޤ��� =head1 HINTS AND TIPS (�ҥ�ȤȾ���) =head2 Locking: The Trouble with fd (���å�: fd �ǤΥȥ�֥�) =begin original Until version 1.72 of this module, the recommended technique for locking B databases was to flock the filehandle returned from the "fd" function. Unfortunately this technique has been shown to be fundamentally flawed (Kudos to David Harris for tracking this down). Use it at your own peril! =end original ���Υ⥸�塼��ΥС������ 1.72 �ޤǡ�B �ǡ����١�������å����� ����ο侩�����ƥ��˥å��ϡ�"fd" �ؿ������֤����ե�����ϥ�ɥ�� flock ���뤳�ȤǤ����� ��ǰ�ʤ��顢���Υƥ��˥å��Ϻ���Ū��̵���Ǥ��� ���Ȥ����餫�ˤʤ�ޤ���(������ͤ��ߤ᤿������ David Harris �ˤ���ޤ�)�� ���ʤ����Ȥ���Ǥ�Ǥ����ȤäƤ�������! =begin original The locking technique went like this. =end original ���å��Υƥ��˥å��ϰʲ��Τ褦�ʤ�ΤǤ����� $db = tie(%db, 'DB_File', 'foo.db', O_CREAT|O_RDWR, 0644) || die "dbcreat foo.db $!"; $fd = $db->fd; open(DB_FH, "+<&=$fd") || die "dup $!"; flock (DB_FH, LOCK_EX) || die "flock: $!"; ... $db{"Tom"} = "Jerry" ; ... flock(DB_FH, LOCK_UN); undef $db; untie %db; close(DB_FH); =begin original In simple terms, this is what happens: =end original ��ñ�ˤ����С����Τ褦�ʤ��Ȥ�������ޤ�: =over 5 =item 1. =begin original Use "tie" to open the database. =end original �ǡ����١����򥪡��ץ󤹤뤿�� "tie" ��ȤäƤ��������� =item 2. =begin original Lock the database with fd & flock. =end original fd & flock �ǥǡ����١�������å����Ƥ��������� =item 3. =begin original Read & Write to the database. =end original �ǡ����١������ɤ߹��� & �񤭹��ߡ� =item 4. =begin original Unlock and close the database. =end original �ǡ����١����Υ��å��������ƥ��������� =back =begin original Here is the crux of the problem. A side-effect of opening the B database in step 2 is that an initial block from the database will get read from disk and cached in memory. =end original ����������ε޽꤬����ޤ��� ���ƥå� 2 �� B �򥪡��ץ󤹤������� �Ȥ��ƥǡ����١����������Ƭ�֥��å��ϥǥ����������ɤ߹��ޤ졢 ����˥���å��夵��ޤ��� =begin original To see why this is a problem, consider what can happen when two processes, say "A" and "B", both want to update the same B database using the locking steps outlined above. Assume process "A" has already opened the database and has a write lock, but it hasn't actually updated the database yet (it has finished step 2, but not started step 3 yet). Now process "B" tries to open the same database - step 1 will succeed, but it will block on step 2 until process "A" releases the lock. The important thing to notice here is that at this point in time both processes will have cached identical initial blocks from the database. =end original �����������ͳ��狼�뤿�ᡢ��ĤΥץ����� "A" �� "B" ��ξ����Ʊ�� B �ǡ����١�����嵭�Υ��å�����Ȥäƹ����������Ȥ��� ����������Τ���ͤ��Ƥߤޤ��礦�� �ץ����� "A" �����˥ǡ����١����򥪡��ץ󤷡� ������å�����äƤ��뤱��ɤ⡢�ޤ��ǡ����١�����ºݤˤ� �������Ƥ��ʤ��Ȥ��ޤ�(���ƥå�2�ޤǽ���äƤ��Ƥ⡢�ޤ� ���ƥå� 3 �򳫻Ϥ��Ƥ��ޤ���)�� �����ǥץ����� "B" ��Ʊ���ǡ����١����� �����ץ󤷤褦�Ȥ��ޤ� - ���ƥå� 1 ���������ޤ��� �������ץ����� "A" �� ���å���������ޤǥ��ƥå� 2 �ǥ֥��å�����ޤ��� �����ǵ���Ĥ��ʤ���Фʤ�ʤ����פʤ��Ȥϡ����λ�����ξ���Υץ������� �ǡ����١�������Υ���å��夵�줿Ʊ����Ƭ�֥��å�����äƤ��뤳�ȤǤ��� =begin original Now process "A" updates the database and happens to change some of the data held in the initial buffer. Process "A" terminates, flushing all cached data to disk and releasing the database lock. At this point the database on disk will correctly reflect the changes made by process "A". =end original �����ƥץ����� "A" ���ǡ����١����򹹿��������ޤ�����Ƭ�Хåե������ �ǡ����򤤤��餫�ѹ������Ȥ��ޤ��� �ץ����� "A" ����λ�������ƤΥ���å��夵�줿�ǡ�����ǥ������� �ե�å��夷���ǡ����١������å��������ޤ��� ���λ����ǡ��ǥ�������Υǡ����١����� �ץ����� "A" �ˤ�äƹԤ�줿�ѹ���������ȿ�Ǥ��Ƥ��ޤ��� =begin original With the lock released, process "B" can now continue. It also updates the database and unfortunately it too modifies the data that was in its initial buffer. Once that data gets flushed to disk it will overwrite some/all of the changes process "A" made to the database. =end original ���å�����������ȡ��ץ����� "B" ��������³���뤳�Ȥ��Ǥ��ޤ��� �����ǡ����١����򹹿�����������ǰ�ʤ��Ȥ���Ƭ�Хåե��ˤ��� �ǡ������ѹ������Ȥ��ޤ��� �ǡ������ǥ������˥ե�å��夵���ȡ��ץ����� "A" ���ǡ����١����� �Ф��ƹԤä�������/���Ƥ��ѹ����񤭤��Ƥ��ޤ��ޤ��� =begin original The result of this scenario is at best a database that doesn't contain what you expect. At worst the database will corrupt. =end original ���Υ��ʥꥪ�η�̤ϡ������Ǥ�ǡ����١����ˤϡ����ʤ���ͽ�����Ƥ��� ��Τ����äƤ��ޤ��� ������Хǡ����١��������������ʤäƤ��ޤ��ޤ��� =begin original The above won't happen every time competing process update the same B database, but it does illustrate why the technique should not be used. =end original �嵭�Τ��Ȥ϶��礹��ץ�������Ʊ�� B �ǡ����١����򹹿����� �٤�ȯ������櫓�ǤϤ���ޤ��� �������ʤ����Υƥ��˥å���Ȥ��٤��� �ʤ��������餫�ˤ��Ƥ��ޤ��� =head2 Safe ways to lock a database (�ǡ����١�������å������������ˡ) =begin original Starting with version 2.x, Berkeley DB has internal support for locking. The companion module to this one, B, provides an interface to this locking functionality. If you are serious about locking Berkeley DB databases, I strongly recommend using B. =end original version 2.x ���顢Berkeley DB �������ǥ��å��򥵥ݡ��Ȥ��Ƥ��ޤ��� ����˿�̩�ʴط������롢B �ϡ����Υ��å���ǽ�ؤ� ���󥿡��ե��������󶡤��Ƥ��ޤ��� Berkeley DB �ǡ����١�������å����뤳�Ȥ˿����Ǥ���С� B ��Ȥ����Ȥ򶯤��侩���ޤ��� =begin original If using B isn't an option, there are a number of modules available on CPAN that can be used to implement locking. Each one implements locking differently and has different goals in mind. It is therefore worth knowing the difference, so that you can pick the right one for your application. Here are the three locking wrappers: =end original �⤷ B ��Ȥ����Ȥ�����ˤʤ���С����å���������뤿��� �Ȥ����Ȥ�����롢�����Ĥ��Υ⥸�塼��� CPAN ����������뤳�Ȥ��Ǥ��ޤ��� ���줾��ϰۤʤ���ˡ�ǥ��å�����������ۤʤ르��������ꤷ�Ƥ��ޤ��� ���Τ��ᡢ���ʤ��Υ��ץꥱ�������ˤ��ä���Τ����٤�褦�ˡ� ���ΰ㤤���Τ뤳�Ȥˤϲ��ͤ�����ޤ��� �ʲ��˻��ĤΥ��å���Ԥ���åѡ��򼨤��ޤ�: =over 5 =item B =begin original A B wrapper which creates copies of the database file for read access, so that you have a kind of a multiversioning concurrent read system. However, updates are still serial. Use for databases where reads may be lengthy and consistency problems may occur. =end original �ޥ���С�����󲽤��줿�¹��ɤ߹��ߥ��������ΰ���¸����뤿�ᡢ �ɤ߹��ߥ��������Τ���Υǡ����١����ե�����Υ��ԡ���������� B ��åѡ��� �����������ϥ��ꥢ��Ǥ��� �ɤ߹��ߤ�Ĺ��������������꤬�����뤫�⤷��ʤ��ǡ����١����� �ȤäƤ��������� =item B =begin original A B wrapper that has the ability to lock and unlock the database while it is being used. Avoids the tie-before-flock problem by simply re-tie-ing the database when you get or drop a lock. Because of the flexibility in dropping and re-acquiring the lock in the middle of a session, this can be massaged into a system that will work with long updates and/or reads if the application follows the hints in the POD documentation. =end original ���줬�Ȥ��Ƥ���֡��ǡ����١�������å���������å�����ǽ�Ϥ� ���� B ��åѡ��� ���å�����������ꡢ�Ϥ����Ȥ����ǡ����١����� �Ƥ�tie���뤳�Ȥˤ�ꡢflock ������ tie ������򤱤ޤ��� ���å���������ǤΥ��å����������ƤӼ�������Ȥ��ν������Τ��ᡢ ���ץꥱ������� POD �ɥ�����Ȥˤ���ҥ�Ȥ˽����С� Ĺ������������/���뤤���ɤ߹��ߤ�Ԥ������ƥ�����ƤϤ�뤳�Ȥ��Ǥ��ޤ��� =item B =begin original An extremely lightweight B wrapper that simply flocks a lockfile before tie-ing the database and drops the lock after the untie. Allows one to use the same lockfile for multiple databases to avoid deadlock problems, if desired. Use for databases where updates are reads are quick and simple flock locking semantics are enough. =end original �ǡ����١�����tie�������˥��å��ե������ñ��� flock ���� untie�θ�˥��å���Ϥ��������˷ڤ� B ��åѡ��� �ǥåɥ��å��������򤹤뤿�ᡢ�������������ʣ���Υǡ����١����� Ʊ�����å��ե������Ȥ����Ȥ��ǽ�ˤ��Ƥ��ޤ��� �������ɤ߹��ߤ�®����ñ��� flock ���å��Υ��ޥ�ƥ����ǽ�ʬ�� ����ǡ����١��������Ѥ��Ƥ��������� =back =head2 Sharing Databases With C Applications (C���ץꥱ�������ȤΥǡ����١����ζ�ͭ) =begin original There is no technical reason why a Berkeley DB database cannot be shared by both a Perl and a C application. =end original Berkeley DB �ǡ����١����� Perl �� C ���ץꥱ�������ȤǶ�ͭ�Ǥ��ʤ��Ȥ��� ����Ū����ͳ�ϲ��⤢��ޤ��� =begin original The vast majority of problems that are reported in this area boil down to the fact that C strings are NULL terminated, whilst Perl strings are not. See L for a generic way to work around this problem. =end original ��������𤵤�Ƥ��������礭������� Perl ʸ����Ϥ����Ǥʤ��Τˡ� C ʸ���� NULL �����Ǥ���Ȥ������ȤǤ��� �����������򤹤����Ū����ˡ�ˤĤ��Ƥ� L ������������ =begin original Here is a real example. Netscape 2.0 keeps a record of the locations you visit along with the time you last visited them in a DB_HASH database. This is usually stored in the file F<~/.netscape/history.db>. The key field in the database is the location string and the value field is the time the location was last visited stored as a 4 byte binary value. =end original �ºݤ����ʲ��˼����ޤ��� Netscape 2.0 �� DB_HASH �ǡ����١����ˡ����ʤ���ˬ�줿���(location)�� ���ʤ����Ǹ�ˤ�����ˬ�줿���֤Ȥ���˥쥳���ɤˤ�����¸���Ƥ��ޤ��� ������̾�ե�����F<~/.netscape/history.db> �˳�Ǽ����ޤ��� ���Υǡ����١����Υ����ե�����ɤϾ��(location)��ʸ����Ǥ��ꡢ �ͤΥե�����ɤϺǸ��ˬ�줿���郎4�Х��ȤΥХ��ʥ��ͤȤ��� ��Ǽ����ޤ��� =begin original If you haven't already guessed, the location string is stored with a terminating NULL. This means you need to be careful when accessing the database. =end original �Ĥޤ����ʸ����� NULL �դ��dz�Ǽ����ޤ��� �ǡ����١����˥�����������Ȥ��ˤϤ���˵���Ĥ��ʤ���Фʤ�ޤ��� =begin original Here is a snippet of code that is loosely based on Tom Christiansen's I script (available from your nearest CPAN archive in F). =end original �ʲ�����ʬŪ�ʥץ������򼨤��ޤ��� �����Tom Christiansen�� I ������ץȤ������Τʤ��� �١����ˤ��Ƥ��ޤ� (����Ϥ��ʤ��˺Ǥ�ᤤCPAN���������֤� F �Ǽ������뤳�Ȥ��Ǥ��ޤ�)�� use warnings ; use strict ; use DB_File ; use Fcntl ; my ($dotdir, $HISTORY, %hist_db, $href, $binary_time, $date) ; $dotdir = $ENV{HOME} || $ENV{LOGNAME}; $HISTORY = "$dotdir/.netscape/history.db"; tie %hist_db, 'DB_File', $HISTORY or die "Cannot open $HISTORY: $!\n" ;; =begin original # Dump the complete database while ( ($href, $binary_time) = each %hist_db ) { =end original # �ǡ����١������Τ����� while ( ($href, $binary_time) = each %hist_db ) { =begin original # remove the terminating NULL $href =~ s/\x00$// ; =end original # ������ NULL ���� $href =~ s/\x00$// ; =begin original # convert the binary time into a user friendly string $date = localtime unpack("V", $binary_time); print "$date $href\n" ; } =end original # �Х��ʥ�� time ��桼���ե��ȥ��ʸ������Ѵ����� $date = localtime unpack("V", $binary_time); print "$date $href\n" ; } =begin original # check for the existence of a specific key # remember to add the NULL if ( $binary_time = $hist_db{"http://mox.perl.com/\x00"} ) { $date = localtime unpack("V", $binary_time) ; print "Last visited mox.perl.com on $date\n" ; } else { print "Never visited mox.perl.com\n" } =end original # ����Υ�����¸�ߤ��뤫������å� # NULL ���դ���Τ�˺��ʤ��Dz����� if ( $binary_time = $hist_db{"http://mox.perl.com/\x00"} ) { $date = localtime unpack("V", $binary_time) ; print "Last visited mox.perl.com on $date\n" ; } else { print "Never visited mox.perl.com\n" } untie %hist_db ; =head2 The untie() Gotcha (untie() �����Ǥ�������) =begin original If you make use of the Berkeley DB API, it is I strongly recommended that you read L. =end original Berkeley DB API ��Ȥ��ΤǤ���С�L ���ɤळ�Ȥ� I<����> ���������ᤷ�ޤ��� =begin original Even if you don't currently make use of the API interface, it is still worth reading it. =end original �⤷���ΤȤ����� API ���󥿡��ե�������Ȥ�ʤ��ΤǤ��äƤ⡢ �ɤ���ͤϤ���ޤ��� =begin original Here is an example which illustrates the problem from a B perspective: =end original B �θ��Ϥ�������򼨤����ʲ��˼����ޤ�: use DB_File ; use Fcntl ; my %x ; my $X ; $X = tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_TRUNC or die "Cannot tie first time: $!" ; $x{123} = 456 ; untie %x ; tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_CREAT or die "Cannot tie second time: $!" ; untie %x ; =begin original When run, the script will produce this error message: =end original �¹Ԥ���ȡ����Υ�����ץȤϰʲ��Υ��顼��å�������Ф��ޤ�: Cannot tie second time: Invalid argument at bad.file line 14. =begin original Although the error message above refers to the second tie() statement in the script, the source of the problem is really with the untie() statement that precedes it. =end original �嵭�Υ��顼��å������Ǥϥ��ץ�ץȤǤ�2 ���ܤ� tie() ���ơ��ȥ��Ȥ� �ؤ��Ƥ���ˤ⤫����餺����������θ����������Ϥ������ˤ��� untie() ���ơ��ȥ��Ȥˤ���ޤ��� =begin original Having read L you will probably have already guessed that the error is caused by the extra copy of the tied object stored in C<$X>. If you haven't, then the problem boils down to the fact that the B destructor, DESTROY, will not be called until I references to the tied object are destroyed. Both the tied variable, C<%x>, and C<$X> above hold a reference to the object. The call to untie() will destroy the first, but C<$X> still holds a valid reference, so the destructor will not get called and the database file F will remain open. The fact that Berkeley DB then reports the attempt to open a database that is already open via the catch-all "Invalid argument" doesn't help. =end original L ���ɤ�Ǥ���д��ˤ�ʬ����Τ褦�ˡ����顼�� C<$X> �� ��Ǽ���줿 tie ���줿���֥������Ȥ�;ʬ��ʣ���ˤ�äƵ�������Ƥ��ޤ��� �ɤ�Ǥ��ʤ���С��פ��������� tie ����Ƥ��륪�֥������Ȥؤ� I<���Ƥ�> ��ե���󥹤��˲�����ʤ���С�B �ǥ��ȥ饯�� DESTROY ���ƤФ�ʤ��Ȥ������Ȥˤ���ޤ��� �嵭��tie���줿�ѿ� C<%X> �� C<$X> ��ξ�� �Ȥ⥪�֥������ȤؤΥ�ե���󥹤���äƤ��ޤ��� untie() ��ƤӽФ��Ⱥǽ�Τ�Τ��˲�����ޤ����������� C<$X> �Ϥޤ�Ŭ�ڤʥ�ե���󥹤���äƤ��ꡢ ���Τ���ǥ��ȥ饯���ϸƤФ줺��F �ϥ����ץ󤵤줿�ޤޤǤ��� ���� Berkeley DB �ϡ����Ǥ˥����ץ󤷤Ƥ���ǡ����١����� �������Ȥ��� ��catch-all ��"Invalid argument"(��Ŭ�ڤʰ���)�� ��𤷤ޤ��������ν����ˤ�ʤ�ޤ��� =begin original If you run the script with the C<-w> flag the error message becomes: =end original ���Υ�����ץȤ� C<-w> �ե饰���դ��Ƽ¹Ԥ���ȡ��ʲ��Τ褦�� ���顼��å������ˤʤ�ޤ�: untie attempted while 1 inner references still exist at bad.file line 12. Cannot tie second time: Invalid argument at bad.file line 14. =begin original which pinpoints the real problem. Finally the script can now be modified to fix the original problem by destroying the API object before the untie: =end original ����������������ؤ��Ƥ��ޤ��� �ǽ�Ū�ˤ� API ���֥������Ȥ� untie ������ �˲����뤳�Ȥˤ�긵�������������褦���ѹ����뤳�Ȥ��Ǥ��ޤ�: ... $x{123} = 456 ; undef $X ; untie %x ; $X = tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_CREAT ... =head1 COMMON QUESTIONS (�褯�������) =head2 Why is there Perl source in my database? (��Υǡ����١����� Perl �����������äƤ���ΤϤʤ��Ǥ���?) =begin original If you look at the contents of a database file created by DB_File, there can sometimes be part of a Perl script included in it. =end original DB_File �ˤ�äƺ������줿�ǡ����١�������Ȥ򸫤�ȡ�������� Perl ������ץȤΰ��������äƤ��뤳�Ȥ�����ޤ��� =begin original This happens because Berkeley DB uses dynamic memory to allocate buffers which will subsequently be written to the database file. Being dynamic, the memory could have been used for anything before DB malloced it. As Berkeley DB doesn't clear the memory once it has been allocated, the unused portions will contain random junk. In the case where a Perl script gets written to the database, the random junk will correspond to an area of dynamic memory that happened to be used during the compilation of the script. =end original ����� Berkeley DB ������ǥǡ����١����ե�����˽񤭤��ޤ��Хåե��� ��뤿���ưŪ�����ȤäƤ��뤿��Ǥ��� ưŪ�Ǥ��뤿��ˡ������ DB �� malloc �������� �Ȥ��Ƥ����ޤޤˤʤ�ޤ��� Berkeley DB �ϥ��������Ȥ��줿����򥯥ꥢ���ʤ��Τǡ� �Ȥ��ʤ���ʬ�ˤϥǥ����ʥ��ߤ����äƤ��ޤ��� Perl ������ץȤ��ǡ����١����˽񤭹��ޤ�Ƥ����硢 ���Υǥ����ʥ��ߤ���������ץȤ򥳥�ѥ��뤹��Ȥ������ޤ��޻Ȥ�줿 ưŪ������ΰ���б����Ƥ����ΤǤ��礦�� =begin original Unless you don't like the possibility of there being part of your Perl scripts embedded in a database file, this is nothing to worry about. =end original Perl ������ץȤΰ��������Ǥ�ǡ����١����ե����������ޤ�뤫�⤷��ʤ��� �������Ȥ����Ǥʤ���С����⿴�ۤ��뤳�ȤϤ���ޤ��� =head2 How do I store complex data structures with DB_File? (DB_File ��ʣ���ʥǡ�����¤���Ǽ����ˤϤɤ�����Ф����Ǥ���?) =begin original Although B cannot do this directly, there is a module which can layer transparently over B to accomplish this feat. =end original B ��ľ�ܤ����Ǥ��ޤ��󤬡����ε�ǽ��¸����뤿�� B �� ���Ʃ��Ū�˽Ťͤ뤳�ȤΤǤ���⥸�塼�뤬����ޤ��� =begin original Check out the MLDBM module, available on CPAN in the directory F. =end original CPAN �� F �ǥ��쥯�ȥ꤫������Ǥ��� MLDBM �⥸�塼����ƤߤƤ��������� =head2 What does "Invalid Argument" mean? ("Invalid Argument" �äƤɤ�������̣�Ǥ���?) =begin original You will get this error message when one of the parameters in the C call is wrong. Unfortunately there are quite a few parameters to get wrong, so it can be difficult to figure out which one it is. =end original C �θƤӽФ��ǥѥ�᡼���ΰ�Ĥ��ְ�äƤ���ȡ����Υ��顼��å������� �������ޤ��� ��ǰ�ʤ���ѥ�᡼�����ְ㤦���ȤϤ��ޤ�ʤ��Τǡ� ���줬��ʤΤ���ȯ�����뤳�Ȥ��񤷤����⤷��ޤ��� =begin original Here are a couple of possibilities: =end original �ʲ��ˤ����Ĥ��β�ǽ���򤷤ᤷ�ޤ�: =over 5 =item 1. =begin original Attempting to reopen a database without closing it. =end original �ǡ����١����򥯥������ʤ��˺ƥ����ץ󤷤褦�Ȥ����� =item 2. =begin original Using the O_WRONLY flag. =end original O_WRONLY �ե饰��ȤäƤ��롣 =back =head2 What does "Bareword 'DB_File' not allowed" mean? ("Bareword 'DB_File' not allowed"�äƤɤ�������̣�Ǥ���?) =begin original You will encounter this particular error message when you have the C pragma (or the full strict pragma) in your script. Consider this script: =end original ��������Υ��顼��å������ϡ�������ץȤ���� C �ץ饰�� (���뤤�Ϥ��٤Ƥ� strict �ץ饰��)������Ȥ���ȯ�����ޤ��� �ʲ��Υ�����ץȤˤĤ��ƹͤ��ƤߤƤ�������: use warnings ; use strict ; use DB_File ; my %x ; tie %x, DB_File, "filename" ; =begin original Running it produces the error in question: =end original �����¹Ԥ���ȼ���Υ��顼�򵯤����ޤ�: Bareword "DB_File" not allowed while "strict subs" in use =begin original To get around the error, place the word C in either single or double quotes, like this: =end original ���Υ��顼����򤹤�ˤϡ��ʲ��Τ褦�� C �Ȥ���ñ��򥷥󥰥� �ޤ��ϥ��֥륯�����Ȥ��������Ƥ�������: tie %x, "DB_File", "filename" ; =begin original Although it might seem like a real pain, it is really worth the effort of having a C in all your scripts. =end original ����Ϥ��ʤ�ζ��ˤ˸����ޤ�����C �򤢤ʤ������ƤΥ�����ץȤ� ����褦�Ȥ��뤳�Ȥ������˲��ͤΤ��뤳�ȤǤ��� =head1 REFERENCES (����ʸ��) =begin original Articles that are either about B or make use of it. =end original B �䤽�λȤ����ˤĤ��Ƥε����ϰʲ����̤ꡣ =over 5 =item 1. I, Tim Kientzle (tkientzle@ddj.com), Dr. Dobb's Journal, Issue 295, January 1999, pp 34-41 =back =head1 HISTORY =begin original Moved to the Changes file. =end original Changes �ե�����˰�ư���ޤ����� =head1 BUGS =begin original Some older versions of Berkeley DB had problems with fixed length records using the RECNO file format. This problem has been fixed since version 1.85 of Berkeley DB. =end original �����Τ����Ĥ��� Berkeley DB �ϡ�RECNO �ե�����ե����ޥåȤ�Ȥä� ����Ĺ�쥳���ɤ����꤬����ޤ����� ��������� Berkeley DB �� �С������ 1.85 ���齤������Ƥ��ޤ��� =begin original I am sure there are bugs in the code. If you do find any, or can suggest any enhancements, I would welcome your comments. =end original ���Υץ������˥Х�������ȻפäƤ��ޤ��� �������Ĥ����顢���뤤�ϳ�ĥ�ˤĤ��Ƥʤˤ���ƤǤ���ΤǤ���С� �����Ȥ��Ԥ����Ƥ��ޤ��� =head1 AVAILABILITY (������) =begin original B comes with the standard Perl source distribution. Look in the directory F. Given the amount of time between releases of Perl the version that ships with Perl is quite likely to be out of date, so the most recent version can always be found on CPAN (see L for details), in the directory F. =end original B ��ɸ��� Perl source �ǥ����ȥ�ӥ塼�����Ȱ�������ޤ��� F ������������ Perl �Ȥ��ä���˽Ф��줿�С������� ��꡼������λ��֤Τ���˸Ť��ʤäƤ��뤫�⤷��ޤ��� �����Ǻǿ��ΥС������Ͼ�� CPAN(�ܺ٤ˤĤ��Ƥ� L ����������)�Υǥ��쥯�ȥ� F �� �������뤳�Ȥ��Ǥ��ޤ��� =begin original This version of B will work with either version 1.x, 2.x or 3.x of Berkeley DB, but is limited to the functionality provided by version 1. =end original ���ΥС������� B �� Berkeley DB �� 1.x, 2.x, 3.x �Τ�����Ǥ� ư���ޤ��� �������С������ 1 �ˤ�ä��󶡤���Ƥ��뵡ǽ�˸��ꤵ��ޤ��� =begin original The official web site for Berkeley DB is F. All versions of Berkeley DB are available there. =end original Berkeley DB �θ��������֥����Ȥ� F �Ǥ��� �����ǤϤ��٤ƤΥС������� Berkeley DB ��������뤳�Ȥ��Ǥ��ޤ��� =begin original Alternatively, Berkeley DB version 1 is available at your nearest CPAN archive in F. =end original ���뤤�ϡ�Berkeley DB �С������ 1 �ϺǴ��� CPAN ���������֤Ǥ� F �Ǽ����Ǥ��ޤ��� =begin original If you are running IRIX, then get Berkeley DB version 1 from F. It has the patches necessary to compile properly on IRIX 5.3. =end original IRIX ��ȤäƤ���ΤǤ���С�Berkeley DB �С������ 1 �� F �Ǽ������뤳�Ȥ��Ǥ��ޤ��� IRIX 5.3 ��Ŭ�ڤ˥���ѥ��뤹�뤿���ɬ�פʥѥå������äƤ��ޤ��� =head1 COPYRIGHT Copyright (c) 1995-2007 Paul Marquess. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =begin original Although B is covered by the Perl license, the library it makes use of, namely Berkeley DB, is not. Berkeley DB has its own copyright and its own license. Please take the time to read it. =end original B �� Perl �饤���󥹤ˤ�äƥ��С�����ޤ��������줬���Ѥ��� �饤�֥�ꡢ�Ĥޤ� Berkeley DB �Ϥ����ǤϤ���ޤ��� Berkeley DB �Ϥ��켫�Ȥ�������ȼ��Υ饤���󥹤���äƤ��ޤ��� ������ɤ�Ǥ��������� =begin original Here are are few words taken from the Berkeley DB FAQ (at F) regarding the license: =end original Berkeley DB FAQ F) ����饤���󥹤ˤĤ��Ƥΰ����򼨤��ޤ�: =begin original Do I have to license DB to use it in Perl scripts? =end original Perl ������ץȤǻȤ������ DB ��饤���󥹤���ɬ�פ�����ޤ���? =begin original No. The Berkeley DB license requires that software that uses Berkeley DB be freely redistributable. In the case of Perl, that software is Perl, and not your scripts. Any Perl scripts that you write are your property, including scripts that make use of Berkeley DB. Neither the Perl license nor the Berkeley DB license place any restriction on what you may do with them. =end original �������� Berkeley DB �饤���󥹤� Berkeley DB �����Ѥ��륽�եȥ������ϡ� ��ͳ�˺����۲�ǽ�Ǥ��뤳�Ȥ�ɬ�פȤ��ޤ��� Perl �ξ�硢�Ĥޤ꥽�եȥ������� Perl �Ǥ��ꡢ���ʤ��� ������ץȤǤϤ���ޤ��� ���ʤ����񤤤����Ƥ� Perl ������ץȤ� Berkeley DB ��Ȥä���Τ� �ޤ�ơ����ʤ��λ񻺤Ǥ��� Perl �饤���󥹤� Berkeley DB �饤���󥹤� ���ʤ���������ȤäƤǤ��뤳�Ȥ򲿤����¤��ޤ��� =begin original If you are in any doubt about the license situation, contact either the Berkeley DB authors or the author of DB_File. See L<"AUTHOR"> for details. =end original �⤷�饤���󥹤ξ����˵��䤬����С�Berkeley DB �κ�Ԥ��뤤�� DB_File �� ��Ԥ˥��󥿥��Ȥ��Ƥ��������� ������ L<"AUTHOR"> �������������� =head1 SEE ALSO L, L, L, L, L, L =head1 AUTHOR =begin original The DB_File interface was written by Paul Marquess Epmqs@cpan.orgE. =end original DB_File ���󥿡��ե������� Paul Marquess Epmqs@cpan.orgE �ˤ�äƺ�������ޤ����� =begin meta Translate: hippo2000 Update: SHIRAKATA Kentaro (1.805-) Status: completed =end meta =cut