| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## File: Lingua::TT::DBFile::PackedArray.pm | ||
| 3 | ## Author: Bryan Jurish <TT/IO.pm> | ||
| 4 | ## Descript: TT I/O: Berkely DB: tied files: arrays (using DB_RECNO) | ||
| 5 | |||
| 6 | |||
| 7 | package Lingua::TT::DBFile::PackedArray; | ||
| 8 | 0 | ▶ | use Lingua::TT::DBFile; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use DB_File; | ||
| 10 | use Carp; | ||
| 11 | use strict; | ||
| 12 | |||
| 13 | ##============================================================================== | ||
| 14 | ## Globals & Constants | ||
| 15 | |||
| 16 | our @ISA = qw(Lingua::TT::DBFile); | ||
| 17 | |||
| 18 | ##============================================================================== | ||
| 19 | ## Constructors etc. | ||
| 20 | |||
| 21 | ## $dbf = CLASS_OR_OBJECT->new(%opts) | ||
| 22 | ## + %opts, %$doc: | ||
| 23 | ## ##-- overrides | ||
| 24 | ## packfmt => $fmt, ##-- pack format (constant width); default='L' | ||
| 25 | ## type => $type, ##-- one of 'HASH', 'BTREE', 'RECNO' (default: 'RECNO') | ||
| 26 | ## dbinfo => \%dbinfo, ##-- default: "DB_File::RECNOINFO"->new(); | ||
| 27 | ## ## + overrides 'flags', 'reclen' | ||
| 28 | ## ## | ||
| 29 | ## ##-- user options | ||
| 30 | ## file => $directory, ##-- default: undef (none) | ||
| 31 | ## mode => $mode, ##-- default: 0644 | ||
| 32 | ## flags => $flags, ##-- default: O_RDWR|O_CREAT | ||
| 33 | ## #type => $type, ##-- one of 'HASH', 'BTREE', 'RECNO' (default: 'RECNO') | ||
| 34 | ## dbopts => \%opts, ##-- db options (e.g. cachesize,bval,...) -- defaults to none (uses DB_File defaults) | ||
| 35 | ## ## | ||
| 36 | ## ##-- low-level data | ||
| 37 | ## packlen => $len, ##-- length data | ||
| 38 | ## data => $thingy, ##-- tied data (hash or array) | ||
| 39 | ## tied => $ref, ##-- reference returned by tie() | ||
| 40 | sub new { | ||
| 41 | my ($that,%opts) = @_; | ||
| 42 | my $dbf = $that->SUPER::new(type=>'RECNO',packfmt=>'L',%opts,file=>undef); | ||
| 43 | $dbf->{packlen} = length(pack($dbf->{packfmt})) if (!defined($dbf->{packlen})); | ||
| 44 | $dbf->{dbinfo}{reclen} = $dbf->{packlen}; | ||
| 45 | $dbf->{dbinfo}{flags} = R_FIXEDLEN; | ||
| 46 | $dbf->{dbinfo}{bval} = chr(0); | ||
| 47 | return $dbf->open($opts{file}) if (defined($opts{file})); | ||
| 48 | return $dbf; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | ##============================================================================== | ||
| 53 | ## Access | ||
| 54 | |||
| 55 | ## $packed = $pa->pack(@values) | ||
| 56 | sub pack { | ||
| 57 | return CORE::pack($_[0]{packfmt},@_[1..$#_]); | ||
| 58 | } | ||
| 59 | |||
| 60 | ## @values = $pa->unpack($packed) | ||
| 61 | sub unpack { | ||
| 62 | return CORE::unpack($_[0]{packfmt},$_[1]); | ||
| 63 | } | ||
| 64 | |||
| 65 | ##---------------------------------------------------------------------- | ||
| 66 | ## Access: Wrappers: p* | ||
| 67 | |||
| 68 | ## $status = $pa->pput($key,@values) | ||
| 69 | sub pput ($@) { | ||
| 70 | return $_[0]{tied}->put($_[1], CORE::pack($_[0]{packfmt},@_[2..$#_])); | ||
| 71 | } | ||
| 72 | |||
| 73 | ## $status = $pa->pget($key,@values) | ||
| 74 | sub pget ($\@) { | ||
| 75 | my ($rc,$val); | ||
| 76 | $rc = $_[0]{tied}->get($_[1], $val); | ||
| 77 | @{$_[1]} = CORE::unpack($_[0]{packfmt},$val); | ||
| 78 | return $rc; | ||
| 79 | } | ||
| 80 | |||
| 81 | ## @values = $pa->ppop(); | ||
| 82 | sub ppop ($) { | ||
| 83 | return CORE::unpack($_[0]{packfmt},$_[0]{tied}->pop); | ||
| 84 | } | ||
| 85 | |||
| 86 | ## @values = $pa->pshift(); | ||
| 87 | sub pshift ($) { | ||
| 88 | return CORE::unpack($_[0]{packfmt},$_[0]{tied}->shift); | ||
| 89 | } | ||
| 90 | |||
| 91 | ## undef = $pa->ppush(@values); | ||
| 92 | sub ppush ($@) { | ||
| 93 | return $_[0]{tied}->push(CORE::pack($_[0]{packfmt},@_[1..$#_])); | ||
| 94 | } | ||
| 95 | |||
| 96 | ## undef = $pa->punshift(@values); | ||
| 97 | sub punshift ($) { | ||
| 98 | return $_[0]{tied}->unshift(CORE::pack($_[0]{packfmt},@_[1..$#_])); | ||
| 99 | } | ||
| 100 | |||
| 101 | ##---------------------------------------------------------------------- | ||
| 102 | ## Access: Wrappers: r* | ||
| 103 | |||
| 104 | ## $packed_values = $pa->rput($key,\@values) | ||
| 105 | sub rput ($$) { | ||
| 106 | return $_[0]{data}[$_[1]] = CORE::pack($_[0]{packfmt},@{$_[1]}); | ||
| 107 | } | ||
| 108 | |||
| 109 | ## \@values = $pa->rget($key); | ||
| 110 | sub rget ($) { | ||
| 111 | return [CORE::unpack($_[0]{packfmt},$_[0]{data}[$_[1]])]; | ||
| 112 | } | ||
| 113 | |||
| 114 | ## undef = $pa->rpush(\@vals1, ...); | ||
| 115 | sub rpush ($@) { | ||
| 116 | return $_[0]{tied}->push(map {CORE::pack($_[0]{packfmt},@$_)} @_[1..$#_]); | ||
| 117 | } | ||
| 118 | |||
| 119 | ## undef = $pa->runshift(\@vals1, ...); | ||
| 120 | sub runshift ($@) { | ||
| 121 | return $_[0]{tied}->unshift(map {CORE::pack($_[0]{packfmt},@$_)} @_[1..$#_]); | ||
| 122 | } | ||
| 123 | |||
| 124 | ##---------------------------------------------------------------------- | ||
| 125 | ## Access: Wrappers: a* | ||
| 126 | |||
| 127 | ## $packed_values = $pa->aput($key,@values) | ||
| 128 | sub aput ($@) { | ||
| 129 | return $_[0]{data}[$_[1]] = CORE::pack($_[0]{packfmt},@_[2..$#_]); | ||
| 130 | } | ||
| 131 | |||
| 132 | ## @values = $pa->rget($key); | ||
| 133 | sub aget ($) { | ||
| 134 | return CORE::unpack($_[0]{packfmt},$_[0]{data}[$_[1]]); | ||
| 135 | } | ||
| 136 | |||
| 137 | ## undef = $pa->apush(@values); | ||
| 138 | ## undef = $pa->aunshift(@values); | ||
| 139 | BEGIN { | ||
| 140 | *apush = \&ppush; | ||
| 141 | *aunshift = \&punshift; | ||
| 142 | } | ||
| 143 | |||
| 144 | ##============================================================================== | ||
| 145 | ## Footer | ||
| 146 | 1; | ||
| 147 | |||
| 148 | __END__ | ||