ArangoDB
view release on metacpan or search on metacpan
lib/ArangoDB/Collection.pm view on Meta::CPAN
my $api = API_IMPORT . '?type=documents&collection=' . $self->{id};
my $data = join "\n", map { $JSON->encode($_) } @$documents;
my $res = eval { $self->{connection}->http_post( $api, $data, 1 ) };
if ($@) {
$self->_server_error_handler( $@, 'Failed to bulk import to the collection(%s)' );
}
return $res;
}
=pod
=head1 METHODS FOR EDGE HANDLING
=head2 save_edge($from,$to[,$data])
Save edge to the collection. Returns instance of L<ArangoDB::Edge>.
=over 4
=item $from
The document that start-point of the edge.
=item $to
The document that end-point of the edge.
=item $data
Document data.
=back
$collection->save_edge($document1,$document2, { rel => 'has-a' });
=cut
sub save_edge {
my ( $self, $from, $to, $data ) = @_;
my $api = API_EDGE . '?collection=' . $self->{id} . '&from=' . $from . '&to=' . $to;
my $edge = eval {
my $res = $self->{connection}->http_post( $api, $data );
$self->{db}->edge( $res->{_id} );
};
if ($@) {
$self->_server_error_handler( $@, "Failed to save the new edge to the collection(%s)" );
}
return $edge;
}
=pod
=head1 METHODS FOR SIMPLE QUERY HANDLING
=head2 all([$options])
Send 'all' simple query. Returns instance of L<ArangoDB::Cursor>.
This will return all documents of in the collection.
my $cursor = $collection->all({ limit => 100 });
$options is query option(HASH reference).The attributes of $options are:
=over 4
=item limit
The maximal amount of documents to return. (optional)
=item skip
The documents to skip in the query. (optional)
=back
=cut
sub all {
my ( $self, $options ) = @_;
$options ||= {};
my $data = { collection => $self->{id} };
for my $key ( grep { exists $options->{$_} } qw{limit skip} ) {
$data->{$key} = $options->{$key};
}
my $res = eval { $self->{connection}->http_put( API_SIMPLE_ALL, $data ) };
if ($@) {
$self->_server_error_handler( $@, 'Failed to call Simple API(all) for the collection(%s)' );
}
return ArangoDB::Cursor->new( $self->{connection}, $res );
}
=pod
=head2 by_example($example[,$options])
Send 'by_example' simple query. Returns instance of L<ArangoDB::Cursor>.
This will find all documents matching a given example.
my $cursor = $collection->by_example({ age => 20 });
=over 4
=item $example
The exmaple.
=item $options
Query option(HASH reference).The attributes of $options are:
=over 4
=item limit
The maximal amount of documents to return. (optional)
=item skip
The documents to skip in the query. (optional)
=back
=back
=cut
sub by_example {
my ( $self, $example, $options ) = @_;
$options ||= {};
my $data = { collection => $self->{id}, example => $example };
map { $data->{$_} = $options->{$_} } grep { exists $options->{$_} } qw(limit skip);
my $res = eval { $self->{connection}->http_put( API_SIMPLE_EXAMPLE, $data ) };
if ($@) {
$self->_server_error_handler( $@, 'Failed to call Simple API(by_example) for the collection(%s)' );
}
return ArangoDB::Cursor->new( $self->{connection}, $res );
}
=pod
=head2 first_example($example)
Send 'first_example' simple query. Returns instance of L<ArangoDB::Document>.
This will return the first document matching a given example.
$example is the exmaple.
my $document = $collection->by_example({ age => 20 });
=cut
sub first_example {
my ( $self, $example ) = @_;
my $data = { collection => $self->{id}, example => $example };
my $res = eval { $self->{connection}->http_put( API_SIMPLE_FIRST, $data ) };
if ($@) {
$self->_server_error_handler( $@, 'Failed to call Simple API(first_example) for the collection(%s)' );
}
return ArangoDB::Document->new( $self->{connection}, $res->{document} );
}
=pod
=head2 range($attr,$lower,$upper[,$options])
Send 'range' simple query. Returns instance of L<ArangoDB::Cursor>.
It looks for documents in the collection with attribute between two values.
Note: You must declare a skip-list index on the attribute in order to be able to use a range query.
my $cursor = $collection->range('age', 20, 29, { closed => 1 } );
=over 4
=item $attr
The attribute path to check.
=item $lower
The lower bound.
=item $upper
The upper bound.
=item $options
Query option(HASH reference).The attributes of $options are:
=over 4
=item closed
If true, use intervall including $lower and $upper, otherwise exclude $upper, but include $lower
=item limit
The maximal amount of documents to return. (optional)
=item skip
The documents to skip in the query. (optional)
=back
=back
=cut
sub range {
my ( $self, $attr, $lower, $upper, $options ) = @_;
$options ||= {};
my $data = { collection => $self->{id}, attribute => $attr, left => $lower, right => $upper, };
map { $data->{$_} = $options->{$_} } grep { exists $options->{$_} } qw(closed limit skip);
$data->{closed} = $data->{closed} ? JSON::true : JSON::false;
my $res = eval { $self->{connection}->http_put( API_SIMPLE_RANGE, $data ) };
if ($@) {
$self->_server_error_handler( $@, 'Failed to call Simple API(range) for the collection(%s)' );
}
return ArangoDB::Cursor->new( $self->{connection}, $res );
}
=pod
=head2 near($latitude,$longitude[,$options])
Send 'near' simple query. Returns instance of L<ArangoDB::Cursor>.
The default will find at most 100 documents near a given coordinate.
The returned list is sorted according to the distance, with the nearest document coming first.
$cursor = $collection->near(0,0, { limit => 20 } );
=over 4
=item $latitude
The latitude of the coordinate.
=item $longitude
The longitude of the coordinate.
=item $options
Query option(HASH reference).The attributes of $options are:
=over 4
=item distance
If given, the attribute key used to store the C<distance> to document(optional).
C<distance> is the distance between the given point and the document in meter.
=item limit
The maximal amount of documents to return. (optional)
=item skip
The documents to skip in the query. (optional)
=item geo
If given, the identifier of the geo-index to use. (optional)
=back
=back
=cut
sub near {
my ( $self, $latitude, $longitude, $options ) = @_;
$options ||= {};
my $data = { collection => $self->{id}, latitude => $latitude, longitude => $longitude, };
map { $data->{$_} = $options->{$_} } grep { exists $options->{$_} } qw(distance limit skip geo);
my $res = eval { $self->{connection}->http_put( API_SIMPLE_NEAR, $data ) };
if ($@) {
$self->_server_error_handler( $@, 'Failed to call Simple API(near) for the collection(%s)' );
}
return ArangoDB::Cursor->new( $self->{connection}, $res );
}
=pod
=head2 within($latitude,$longitude,$radius[,$options])
Send 'within' simple query. Returns instance of L<ArangoDB::Cursor>.
This will find all documents with in a given radius around the coordinate (latitude, longitude).
The returned list is sorted by distance.
$cursor = $collection->within(0,0, 10 * 1000, { distance => 'distance' } );
=over 4
=item $latitude
The latitude of the coordinate.
=item $longitude
The longitude of the coordinate.
=item $radius
The maximal radius(meter).
=item $options
Query option(HASH reference).The attributes of $options are:
=over 4
=item distance
If given, the attribute name used to store the C<distance> to document(optional).
C<distance> is the distance between the given point and the document in meter.
=item limit
The maximal amount of documents to return. (optional)
=item skip
The documents to skip in the query. (optional)
=item geo
If given, the identifier of the geo-index to use. (optional)
=back
=back
=cut
sub within {
my ( $self, $latitude, $longitude, $radius, $options ) = @_;
$options ||= {};
my $data = { collection => $self->{id}, latitude => $latitude, longitude => $longitude, radius => $radius, };
map { $data->{$_} = $options->{$_} }
grep { exists $options->{$_} } qw(distance limit skip geo);
my $res = eval { $self->{connection}->http_put( API_SIMPLE_WITHIN, $data ) };
if ($@) {
$self->_server_error_handler( $@, 'Failed to call Simple API(within) for the collection(%s)' );
}
return ArangoDB::Cursor->new( $self->{connection}, $res );
}
=pod
( run in 0.609 second using v1.01-cache-2.11-cpan-39bf76dae61 )