/usr/lib/perl5/DR/Tarantool/Tuple.pm is in libdr-tarantool-perl 0.42-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | use utf8;
use strict;
use warnings;
=head1 NAME
DR::Tarantool::Tuple - a tuple container for L<DR::Tarantool>
=head1 SYNOPSIS
my $tuple = new DR::Tarantool::Tuple([ 1, 2, 3]);
my $tuple = new DR::Tarantool::Tuple([ 1, 2, 3], $space);
my $tuple = unpack DR::Tarantool::Tuple([ 1, 2, 3], $space);
$tuple->next( $other_tuple );
$f = $tuple->raw(0);
$f = $tuple->name_field;
=head1 DESCRIPTION
A tuple contains normalized (unpacked) fields. You can access the fields
by their indexes (see L<raw> function) or by their names (if they are
described in the space).
Each tuple can contain references to L<next> tuple and L<iter>ator,
so that if the server returns more than one tuple, all of them
can be accessed.
=head1 METHODS
=cut
package DR::Tarantool::Tuple;
use DR::Tarantool::Iterator;
use Scalar::Util 'weaken', 'blessed';
use Carp;
$Carp::Internal{ (__PACKAGE__) }++;
=head2 new
A constructor.
my $t = DR::Tarantool::Tuple->new([1, 2, 3]);
my $t = DR::Tarantool::Tuple->new([1, 2, 3], $space);
=cut
sub new :method {
my ($class, $tuple, $space) = @_;
$class = ref $class if ref $class;
# hack to replace default autoload
$class = $space->tuple_class if $space and $class eq __PACKAGE__;
croak 'wrong space' if defined $space and !blessed $space;
croak 'tuple must be ARRAYREF [of ARRAYREF]' unless 'ARRAY' eq ref $tuple;
croak "tuple can't be empty" unless @$tuple;
$tuple = [ $tuple ] unless 'ARRAY' eq ref $tuple->[0];
my $iterator = DR::Tarantool::Iterator->new(
$tuple,
data => $space,
item_class => ref($class) || $class,
item_constructor => '_new'
);
return bless {
idx => 0,
iterator => $iterator,
} => ref($class) || $class;
}
sub _new {
my ($class, $item, $idx, $iterator) = @_;
return bless {
idx => $idx,
iterator => $iterator,
} => ref($class) || $class;
}
=head2 unpack
Another way to construct a tuple.
my $t = DR::Tarantool::Tuple->unpack([1, 2, 3], $space);
=cut
sub unpack :method {
my ($class, $tuple, $space) = @_;
croak 'wrong space' unless blessed $space;
return undef unless defined $tuple;
croak 'tuple must be ARRAYREF [of ARRAYREF]' unless 'ARRAY' eq ref $tuple;
return undef unless @$tuple;
if ('ARRAY' eq ref $tuple->[0]) {
my @tu;
push @tu => $space->unpack_tuple($_) for @$tuple;
return $class->new(\@tu, $space);
}
return $class->new($space->unpack_tuple($tuple), $space);
}
=head2 raw
Return raw data from the tuple.
my $array = $tuple->raw;
my $field = $tuple->raw(0);
=cut
sub raw :method {
my ($self, $fno) = @_;
my $item = $self->{iterator}->raw_item( $self->{idx} );
return $item unless defined $fno;
croak 'wrong field number' unless $fno =~ /^-?\d+$/;
return undef if $fno < -@$item;
return undef if $fno >= @$item;
return $item->[ $fno ];
}
=head2 next
Append or return the next tuple, provided there is more than one
tuple in the result set.
my $next_tuple = $tuple->next;
=cut
sub next :method {
my ($self, $tuple) = @_;
my $iterator = $self->{iterator};
my $idx = $self->{idx} + 1;
# if tuple is exists next works like 'iterator->push'
if ('ARRAY' eq ref $tuple) {
$iterator->push( $tuple );
$idx = $iterator->count - 1;
}
return undef unless $idx < $iterator->count;
my $next = bless {
idx => $idx,
iterator => $iterator,
} => ref($self);
return $next;
}
=head2 iter
Return an iterator object associated with the tuple.
my $iterator = $tuple->iter;
my $iterator = $tuple->iter('MyTupleClass', 'new');
while(my $t = $iterator->next) {
# the first value of $t and $tuple are the same
...
}
=head3 Arguments
=over
=item package (optional)
=item method (optional)
If 'package' and 'method' are present, $iterator->L<next> method
constructs objects using C<< $package->$method( $next_tuple ) >>
If 'method' is not present and 'package' is present, the iterator
blesses the raw array with 'package'.
=back
=cut
sub iter :method {
my ($self, $class, $method) = @_;
my $iterator = $self->{iterator};
if ($class) {
return $self->{iterator}->clone(
item_class => $class,
item_constructor => sub {
my ($c, $item, $idx) = @_;
if ($method) {
my $bitem = bless {
idx => $idx,
iterator => $iterator,
} => ref($self);
return $c->$method( $bitem );
}
return bless [ @$item ] => ref($c) || $c;
}
);
}
return $self->{iterator};
}
=head2 tail
Return the tail of the tuple (array of unnamed fields). The function always
returns B<ARRAYREF> (as L<raw>).
=cut
sub tail {
my ($self) = @_;
my $space = $self->{iterator}->data;
my $raw = $self->raw;
return [ @$raw[ $space->tail_index .. $#$raw ] ] if $space;
return $raw;
}
sub DESTROY { }
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2011 Dmitry E. Oboukhov <unera@debian.org>
Copyright (C) 2011 Roman V. Nikolaev <rshadow@rambler.ru>
This program is free software, you can redistribute it and/or
modify it under the terms of the Artistic License.
=head1 VCS
The project is placed git repo on github:
L<https://github.com/dr-co/dr-tarantool/>.
=cut
1;
|