ArangoDB2

 view release on metacpan or  search on metacpan

lib/ArangoDB2/Cursor.pm  view on Meta::CPAN


# count
#
# get count of results
sub count
{
    my($self) = @_;

    return $self->data && $self->data->{count};
}

# delete
#
# DELETE /_api/cursor/{cursor-identifier}
sub delete
{
    my($self) = @_;
    # need data
    return unless $self->data
        and $self->data->{hasMore};

    return $self->arango->http->delete(
        $self->api_path('cursor', $self->data->{id}),
    );
}

# each
#
# iterate over results calling callback function
sub each
{
    my($self, $func) = @_;
    # require code ref
    die "Invalid Args"
        unless ref $func eq 'CODE';
    # need data
    return unless $self->data;

    my $i=0;

    while () {
        $func->($i++, $_) for @{$self->data->{result}};
        last unless $self->get;
    }

    return;
}

# fullCount
#
# get fullCount for LIMIT result
sub fullCount
{
    my($self) = @_;

    return $self->data && $self->data->{extra} && $self->data->{extra}->{fullCount};
}

# get
#
# PUT /_api/cursor/{cursor-identifier}
#
# get next batch of results from api
sub get
{
    my($self) = @_;
    # need data
    return unless $self->data
        and $self->data->{hasMore};
    # request next batch
    my $res = $self->arango->http->put(
        $self->api_path('cursor', $self->data->{id}),
    ) or return;
    # update internal state
    $self->{data} = $res;
    $self->{i} = 0;

    return $res;
}

# next
#
# get next result
sub next
{
    my($self) = @_;
    # need data
    return unless $self->data;
    # increment counter, starting at 0
    my $i = $self->{i}++;
    # return next result if it exists
    return $self->data->{result}->[$i]
        if exists $self->data->{result}->[$i];
    # try to get more data
    $self->get or return;
    # try read again
    $i = $self->{i}++;
    # return next result if it exists
    return $self->data->{result}->[$i];
}

1;

__END__

=head1 NAME

ArangoDB2::Cursor - ArangoDB cursor API methods

=head1 METHODS

=over 4

=item new

=item all

=item count

=item delete



( run in 0.659 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )