ArangoDB2

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

    * Add support for transaction API methods
    * Add support for graph API methods
    * major refactor and cleanup

0.04    2014-11-01

    * Add support for index API methods

0.03    2014-11-01

    * Add support for query/cursor API methods

0.02    2014-10-31

    * Add support for edge API methods

0.01    2014-10-31

    * Initial Release

MANIFEST  view on Meta::CPAN

MANIFEST.SKIP
README
t/00-load.t
t/01-arango.t
t/02-admin.t
t/03-database.t
t/04-collection.t
t/05-document.t
t/06-edge.t
t/07-query.t
t/08-cursor.t
t/09-index.t
t/10-transaction.t
t/11-graph.t
t/12-graph-vertex-collection.t
t/13-graph-edge-definition.t
t/14-graph-vertex.t
t/15-graph-vertex.t
t/16-traversal.t
t/17-replication.t
t/18-user.t

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

# 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

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

# 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
#

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

    # 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

lib/ArangoDB2/HTTP/Curl.pm  view on Meta::CPAN

    }
    # decode response, assuming JSON
    my $res = eval { $JSON->decode($$response) };
    # if content is not valid JSON then return entire content
    return $$response unless $res;
    # res may be array in rare cases
    if (!(ref $res eq 'HASH')) {
        return $res;
    }
    # if there is a result object and no error and this is not a
    # cursor result then only return the result object
    elsif ( ($res->{result} || $res->{graph} || $res->{graphs})
            && !$res->{error} && !defined $res->{hasMore} )
    {
        return $res->{result} || $res->{graph} || $res->{graphs};
    }
    # otherwise return entire response
    else {
        return $res;
    }
}

lib/ArangoDB2/HTTP/LWP.pm  view on Meta::CPAN


    if ($response->is_success) {
        my $res = eval { $JSON->decode($response->content) };
        # if content is not valid JSON then return entire content
        return $response->content unless $res;
        # res may be array in rare cases
        if (!(ref $res eq 'HASH')) {
            return $res;
        }
        # if there is a result object and no error and this is not a
        # cursor result then only return the result object
        elsif ( ($res->{result} || $res->{graph} || $res->{graphs})
                && !$res->{error} && !defined $res->{hasMore} )
        {
            return $res->{result} || $res->{graph} || $res->{graphs};
        }
        # otherwise return entire response
        else {
            return $res;
        }
    }

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

sub batchSize { shift->_get_set('batchSize', @_) }

# count
#
# boolean flag that indicates whether the number of documents in the
# result set should be returned.
sub count { shift->_get_set_bool('count', @_) }

# execute
#
# POST /_api/cursor
sub execute
{
    my($self, $bind, $args) = @_;
    # process args
    $args = $self->_build_args($args, [qw(
        batchSize fullCount count query ttl
    )]);
    # fullCount is an exception that belongs under options
    $args->{options}->{fullCount} = delete $args->{fullCount}
        if exists $args->{fullCount};
    # set bindVars if bind is passed
    $args->{bindVars} = $bind
        if defined $bind;
    # make request
    my $res = $self->arango->http->post(
        $self->api_path('cursor'),
        undef,
        $JSON->encode($args),
    ) or return;

    return ArangoDB2::Cursor->new($self->arango, $self->database, $res);
}

# fullCount
#
# include result count greater than LIMIT

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

    );
}

# query
#
# AQL query
sub query { shift->_get_set('query', @_) }

# ttl
#
# an optional time-to-live for the cursor (in seconds)
sub ttl { shift->_get_set('ttl', @_) }

1;

__END__

=head1 NAME

ArangoDB2::Query - ArangoDB query API methods

t/07-query.t  view on Meta::CPAN

    for (1 .. 10);

$query->query('FOR f IN foobar RETURN f');
# explain
$res = $query->explain;
ok($res->{plan}, "explain");
# parse
$res = $query->parse;
ok($res->{collections}, "parse");
# execute
my $cursor = $query->batchSize(1)->execute;
ok(@{$cursor->data->{result}} == 1, "got cursor");

# delete
$res = $cursor->delete;
ok($res, "delete");
# delete
$res = $collection->delete();
# delete database
$res = $arango->database($dbname)->delete();

done_testing();

t/08-cursor.t  view on Meta::CPAN

my $res;

my $arango = ArangoDB2->new("http://localhost:8529", $ENV{ARANGO_USER}, $ENV{ARANGO_PASS});

my $dbname = "ngukvderybvfgjutecbxzsfhyujmnvgf";
my $database = $arango->database($dbname);
my $collection = $database->collection('foobar');
my $document = $collection->document();
my $query = $database->query();

# we can't get a real cursor without doing a query
# so we will just create one to test the methods
my $cursor = ArangoDB2::Cursor->new($arango, $database, {});

# test required methods
my @methods = qw(
    all
    count
    delete
    each
    fullCount
    get
    next
);

for my $method (@methods) {
    can_ok($cursor, $method);
}

# skip tests against the actual ArangoDB2 server unless
# LIVE_TEST env param is set
if (!$ENV{LIVE_TEST}) {
    diag("Skipping live API tests - set LIVE_TEST=1 to enable");
    done_testing();
    exit;
}

t/08-cursor.t  view on Meta::CPAN

});

# create collection
$res = $collection->create();
# create documents
$res = $document->create({test => "test"})
    for (1 .. 10);

$query->query('FOR f IN foobar RETURN f');
# execute
$cursor = $query->batchSize(1)->execute;
ok(@{$cursor->data->{result}} == 1, "got cursor");
my $data = $cursor->data->{result}->[0];
is($data, $cursor->next, "next");
# there should be nine more records
for my $i (1..9) {
    ok($cursor->next, "next: record");
}
# now there should be no more records
ok(!$cursor->next, "next: no more records");
# do query again
$cursor = $query->batchSize(1)->execute;
ok(@{$cursor->all} == 10, "all: count correct");
# do query again
$cursor = $query->batchSize(1)->execute;
my $i = 0;
$cursor->each(sub { $i++ });
ok($i == 10, "each: count correct");
# get count
$cursor = $query->count(1)->execute;
ok($cursor->count == 10, "count");
# redo query with limit
$query = $database->query('FOR f IN foobar LIMIT 5 RETURN f');
$cursor = $query->count(1)->execute;
ok($cursor->count == 5, "count with LIMIT");
# redo query with fullCount
$cursor = $query->fullCount(1)->batchSize(1)->execute;
ok($cursor->fullCount == 10, "fullCount with LIMIT");

# delete
$res = $cursor->delete;
ok($res, "delete");
# delete
$res = $collection->delete();
# delete database
$res = $arango->database($dbname)->delete();

done_testing();



( run in 0.281 second using v1.01-cache-2.11-cpan-4d50c553e7e )