Skip to content

Commit

Permalink
Merge 0240d5c into 243e39f
Browse files Browse the repository at this point in the history
  • Loading branch information
njlg committed Jul 22, 2015
2 parents 243e39f + 0240d5c commit 63760a1
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 48 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ before_install:
- wget -qO- http://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add -
- sudo apt-get update
- sudo apt-get install rethinkdb
- perlbrew available
install:
- cpanm -n Test::Pod Test::Pod::Coverage
- cpanm -n Devel::Cover::Report::Coveralls
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ See http://njlg.info/perl-rethinkdb/

## Notes

* This version is compatible with RethinkDB 1.16.2-1
* This version is compatible with RethinkDB 2.0.4
* This is still in beta stage
* For examples see the tests in `t/*.t`
* For examples see the tests in `t/*.t` or see the documentation (link above)

## Todo

* Add sugar syntax for `attr` (e.g. `$doc->{attr}`), `slice` (e.g. `$doc->[3..6]`), and `nth` (e.g. `$doc->[3]`)
* Add sugar syntax for as many operators as possible (e.g. `+`, `-`, `/`, `*`)
* Performance testing and fixes
* Submit to [CPAN](http://www.cpan.org/) — Coming soon!
* Look into non-blocking IO
50 changes: 40 additions & 10 deletions external/ql2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ message VersionDummy { // We need to wrap it like this for some
V0_1 = 0x3f61ba36;
V0_2 = 0x723081e1; // Authorization key during handshake
V0_3 = 0x5f75e83e; // Authorization key and protocol during handshake
V0_4 = 0x400c2d20; // Queries execute in parallel
}

// The protocol to use after the handshake, specified in V0_3
Expand Down Expand Up @@ -118,9 +119,7 @@ message Response {
// the same token as this response, you will get
// more of the sequence. Keep sending [CONTINUE]
// queries until you get back [SUCCESS_SEQUENCE].
SUCCESS_FEED = 5; // Like [SUCCESS_PARTIAL] but for feeds.
WAIT_COMPLETE = 4; // A [NOREPLY_WAIT] query completed.
SUCCESS_ATOM_FEED = 6; // Like [SUCCESS_FEED] but a singleton.

// These response types indicate failure.
CLIENT_ERROR = 16; // Means the client is buggy. An example is if the
Expand All @@ -135,6 +134,31 @@ message Response {
// than numbers.
}
optional ResponseType type = 1;

// ResponseNotes are used to provide information about the query
// response that may be useful for people writing drivers or ORMs.
// Currently all the notes we send indicate that a stream has certain
// special properties.
enum ResponseNote {
// The stream is a changefeed stream (e.g. `r.table('test').changes()`).
SEQUENCE_FEED = 1;
// The stream is a point changefeed stream
// (e.g. `r.table('test').get(0).changes()`).
ATOM_FEED = 2;
// The stream is an order_by_limit changefeed stream
// (e.g. `r.table('test').order_by(index: 'id').limit(5).changes()`).
ORDER_BY_LIMIT_FEED = 3;
// The stream is a union of multiple changefeed types that can't be
// collapsed to a single type
// (e.g. `r.table('test').changes().union(r.table('test').get(0).changes())`).
UNIONED_FEED = 4;
// The stream is a changefeed stream and includes notes on what state
// the changefeed stream is in (e.g. objects of the form `{state:
// 'initializing'}`).
INCLUDES_STATES = 5;
}
repeated ResponseNote notes = 6;

optional int64 token = 2; // Indicates what [Query] this response corresponds to.

// [response] contains 1 RQL datum if [type] is [SUCCESS_ATOM], or many RQL
Expand Down Expand Up @@ -332,7 +356,7 @@ message Term {
SLICE = 30; // Sequence, NUMBER, NUMBER -> Sequence
SKIP = 70; // Sequence, NUMBER -> Sequence
LIMIT = 71; // Sequence, NUMBER -> Sequence
INDEXES_OF = 87; // Sequence, DATUM -> Sequence | Sequence, Function(1) -> Sequence
OFFSETS_OF = 87; // Sequence, DATUM -> Sequence | Sequence, Function(1) -> Sequence
CONTAINS = 93; // Sequence, DATUM -> BOOL | Sequence, Function(1) -> BOOL

// Stream/Object Ops
Expand Down Expand Up @@ -364,7 +388,9 @@ message Term {
// Half-open by default, but the openness of either side can be
// changed by passing 'closed' or 'open for `right_bound` or
// `left_bound`.
BETWEEN = 36; // StreamSelection, DATUM, DATUM, {index:!STRING, right_bound:STRING, left_bound:STRING} -> StreamSelection
BETWEEN_DEPRECATED = 36; // Deprecated version of between, which allows `null` to specify unboundedness
// With the newer version, clients should use `r.minval` and `r.maxval` for unboundedness
BETWEEN = 182; // StreamSelection, DATUM, DATUM, {index:!STRING, right_bound:STRING, left_bound:STRING} -> StreamSelection
REDUCE = 37; // Sequence, Function(2) -> DATUM
MAP = 38; // Sequence, Function(1) -> Sequence
// The arity of the function should be
Expand Down Expand Up @@ -520,11 +546,9 @@ message Term {
// statement).
BRANCH = 65; // BOOL, Top, Top -> Top
// Returns true if any of its arguments returns true (short-circuits).
// (Like `or` in most languages.)
ANY = 66; // BOOL... -> BOOL
OR = 66; // BOOL... -> BOOL
// Returns true if all of its arguments return true (short-circuits).
// (Like `and` in most languages.)
ALL = 67; // BOOL... -> BOOL
AND = 67; // BOOL... -> BOOL
// Calls its Function with each entry in the sequence
// and executes the array of terms that Function returns.
FOR_EACH = 68; // Sequence, Function(1) -> OBJECT
Expand Down Expand Up @@ -671,8 +695,10 @@ message Term {
NOVEMBER = 124; // -> 11
DECEMBER = 125; // -> 12

// Indicates to MERGE to replace the other object rather than merge it.
LITERAL = 137; // JSON -> Merging
// Indicates to MERGE to replace, or remove in case of an empty literal, the
// other object rather than merge it.
LITERAL = 137; // -> Merging
// JSON -> Merging

// SEQUENCE, STRING -> GROUPED_SEQUENCE | SEQUENCE, FUNCTION -> GROUPED_SEQUENCE
GROUP = 144;
Expand Down Expand Up @@ -711,6 +737,10 @@ message Term {
FILL = 167; // PSEUDOTYPE(GEOMETRY) -> PSEUDOTYPE(GEOMETRY)
GET_NEAREST = 168; // TABLE, PSEUDOTYPE(GEOMETRY) {index:!STRING, max_results:NUM, max_dist:NUM, geo_system:STRING, unit:STRING} -> ARRAY
POLYGON_SUB = 171; // PSEUDOTYPE(GEOMETRY), PSEUDOTYPE(GEOMETRY) -> PSEUDOTYPE(GEOMETRY)

// Constants for specifying key ranges
MINVAL = 180;
MAXVAL = 181;
}
optional TermType type = 1;

Expand Down
62 changes: 50 additions & 12 deletions lib/Rethinkdb.pm
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,24 @@ sub object {

# MATH AND LOGIC

sub all {
sub and {
my $self = shift;
my $args = \@_;

my $q = Rethinkdb::Query->new(
_type => $self->term->termType->all,
_type => $self->term->termType->and,
args => $args,
);

return $q;
}

sub any {
sub or {
my $self = shift;
my $args = \@_;

my $q = Rethinkdb::Query->new(
_type => $self->term->termType->any,
_type => $self->term->termType->or,
args => $args,
);

Expand Down Expand Up @@ -668,6 +668,28 @@ sub wait {
return $q;
}

sub minval {
my $self = shift;

my $q = Rethinkdb::Query->new(
_rdb => $self,
_type => $self->term->termType->minval,
);

return $q;
}

sub maxval {
my $self = shift;

my $q = Rethinkdb::Query->new(
_rdb => $self,
_type => $self->term->termType->maxval,
);

return $q;
}

sub true { Rethinkdb::_True->new; }
sub false { Rethinkdb::_False->new; }

Expand Down Expand Up @@ -834,19 +856,17 @@ Creates an object from a list of key-value pairs, where the keys must be
strings. C<r.object(A, B, C, D)> is equivalent to
C<r.expr([[A, B], [C, D]]).coerce_to('OBJECT')>.
=head2 all
=head2 and
r->all(true, false)->run;
r->and(true, false)->run;
Returns true if all of its arguments returns true, otherwise false. L<all> is
comparable to C<and> in most languages.
Compute the logical C<and> of two or more values.
=head2 any
=head2 or
r->any(true, false)->run;
r->or(true, false)->run;
Returns true if any of its arguments returns true, otherwise false. L<any> is
comparable to C<or> in most languages.
Compute the logical C<or> of two or more values.
=head2 random
Expand Down Expand Up @@ -1163,6 +1183,24 @@ temporarily unavailable after creation, rebalancing or reconfiguring. The
L<wait> command blocks until the given all the tables in database is fully up
to date.
=head2 minval
r->table('marvel')->between( r->minval, 7 )->run;
The special constants L<minval> is used for specifying a boundary, which
represent "less than any index key". For instance, if you use L<minval> as the
lower key, then L<Table/between> will return all documents whose primary keys
(or indexes) are less than the specified upper key.
=head2 maxval
r->table('marvel')->between( 8, r->maxval )->run;
The special constants L<maxval> is used for specifying a boundary, which
represent "greater than any index key". For instance, if you use L<maxval> as
the upper key, then L<Table/between> will return all documents whose primary
keys (or indexes) are greater than the specified lower key.
=head2 true
r->true->run;
Expand Down
23 changes: 17 additions & 6 deletions lib/Rethinkdb/Protocol.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use Rethinkdb::Base -base;
has 'v0_1' => 0x3f61ba36;
has 'v0_2' => 0x723081e1;
has 'v0_3' => 0x5f75e83e;
has 'v0_4' => 0x400c2d20;

package Rethinkdb::Protocol::Protocol;
use Rethinkdb::Base -base;
Expand Down Expand Up @@ -54,19 +55,26 @@ use Rethinkdb::Base -base;
package Rethinkdb::Protocol::Response;
use Rethinkdb::Base -base;
has 'responseType' => sub { Rethinkdb::Protocol::ResponseType->new; };
has 'responseNote' => sub { Rethinkdb::Protocol::ResponseNote->new; };

package Rethinkdb::Protocol::ResponseType;
use Rethinkdb::Base -base;
has 'success_atom' => 1;
has 'success_sequence' => 2;
has 'success_partial' => 3;
has 'success_feed' => 5;
has 'wait_complete' => 4;
has 'success_atom_feed' => 6;
has 'client_error' => 16;
has 'compile_error' => 17;
has 'runtime_error' => 18;

package Rethinkdb::Protocol::ResponseNote;
use Rethinkdb::Base -base;
has 'sequence_feed' => 1;
has 'atom_feed' => 2;
has 'order_by_limit_feed' => 3;
has 'unioned_feed' => 4;
has 'includes_states' => 5;

package Rethinkdb::Protocol::Datum;
use Rethinkdb::Base -base;
has 'datumType' => sub { Rethinkdb::Protocol::DatumType->new; };
Expand Down Expand Up @@ -122,7 +130,7 @@ has 'set_difference' => 91;
has 'slice' => 30;
has 'skip' => 70;
has 'limit' => 71;
has 'indexes_of' => 87;
has 'offsets_of' => 87;
has 'contains' => 93;
has 'get_field' => 31;
has 'keys' => 94;
Expand All @@ -132,7 +140,8 @@ has 'with_fields' => 96;
has 'pluck' => 33;
has 'without' => 34;
has 'merge' => 35;
has 'between' => 36;
has 'between_deprecated' => 36;
has 'between' => 182;
has 'reduce' => 37;
has 'map' => 38;
has 'filter' => 39;
Expand Down Expand Up @@ -179,8 +188,8 @@ has 'index_wait' => 140;
has 'index_rename' => 156;
has 'funcall' => 64;
has 'branch' => 65;
has 'any' => 66;
has 'all' => 67;
has 'or' => 66;
has 'and' => 67;
has 'for_each' => 68;
has 'func' => 69;
has 'asc' => 73;
Expand Down Expand Up @@ -256,6 +265,8 @@ has 'get_intersecting' => 166;
has 'fill' => 167;
has 'get_nearest' => 168;
has 'polygon_sub' => 171;
has 'minval' => 180;
has 'maxval' => 181;

1;

Expand Down
16 changes: 8 additions & 8 deletions lib/Rethinkdb/Query.pm
Original file line number Diff line number Diff line change
Expand Up @@ -357,13 +357,13 @@ sub nth {
return $q;
}

sub indexes_of {
sub offsets_of {
my $self = shift;
my ($args) = @_;

my $q = Rethinkdb::Query->new(
_parent => $self,
_type => $self->_termType->indexes_of,
_type => $self->_termType->offsets_of,
args => Rethinkdb::Util->_wrap_func($args),
);

Expand Down Expand Up @@ -895,7 +895,7 @@ sub and {

my $q = Rethinkdb::Query->new(
_parent => $self,
_type => $self->_termType->all,
_type => $self->_termType->and,
args => $args,
);

Expand All @@ -908,7 +908,7 @@ sub or {

my $q = Rethinkdb::Query->new(
_parent => $self,
_type => $self->_termType->any,
_type => $self->_termType->or,
args => $args,
);

Expand Down Expand Up @@ -1457,9 +1457,9 @@ Return the elements of a sequence within the specified range.
Get the nth element of a sequence.
=head2 indexes_of
=head2 offsets_of
r->expr(['a','b','c'])->indexes_of('c')->run;
r->expr(['a','b','c'])->offsets_of('c')->run;
Get the indexes of an element in a sequence. If the argument is a predicate,
get the indexes of all elements matching it.
Expand Down Expand Up @@ -1773,13 +1773,13 @@ Find the remainder when dividing two numbers.
r->expr(r->true)->and(r->false)->run;
Compute the logical "and" of two or more values.
Compute the logical C<and> of two or more values.
=head2 or
r->expr(r->true)->or(r->false)->run;
Compute the logical "or" of two or more values.
Compute the logical C<or> of two or more values.
=head2 eq
Expand Down
1 change: 0 additions & 1 deletion lib/Rethinkdb/Response.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ sub _init {
1 => 'success_atom',
2 => 'success_sequence',
3 => 'success_partial',
5 => 'success_feed',
4 => 'wait_complete',
16 => 'client_error',
17 => 'compile_error',
Expand Down
2 changes: 1 addition & 1 deletion t/control.t
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ my $res;
$res = r->table('marvel')->get_all( r->args( [ 'Spider-Man', 'Wolverine' ] ),
{ index => 'superhero' } )->run;

is $res->type, 1, 'Correct response type';
is $res->type, 2, 'Correct response type';
is_deeply [ sort map { $_->{superhero} } @{ $res->response } ],
[ 'Spider-Man', 'Wolverine' ], 'Correct response';

Expand Down
Loading

0 comments on commit 63760a1

Please sign in to comment.