Skip to content

Commit

Permalink
Item10331: implement a trivial version of OP_ref - it will kill the m…
Browse files Browse the repository at this point in the history
…ongodb query if there is even one topic for which the array element does not exist

git-svn-id: http://svn.foswiki.org/trunk/MongoDBPlugin@10973 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
SvenDowideit authored and SvenDowideit committed Mar 8, 2011
1 parent 56d8363 commit ab6e90a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/Foswiki/Plugins/MongoDBPlugin.pm
Expand Up @@ -315,10 +315,10 @@ sub _updateDatabase {

Foswiki::Func::loadTemplate('mongodb_js');
my $foswiki_d2n_js = Foswiki::Func::expandTemplate('foswiki_d2n_js');

print STDERR "_updateDatabase\n".$foswiki_d2n_js."\n";

getMongoDB()->updateSystemJS('foswiki_d2n', $foswiki_d2n_js);

my $foswiki_getRef_js = Foswiki::Func::expandTemplate('foswiki_getRef_js');
getMongoDB()->updateSystemJS('foswiki_getRef', $foswiki_getRef_js);
}


Expand Down
28 changes: 24 additions & 4 deletions lib/Foswiki/Plugins/MongoDBPlugin/HoistMongoDB.pm
Expand Up @@ -397,6 +397,7 @@ my %js_func_map = (
'#minus' => '-',
'#pos' => '+',
'#neg' => '-',
'#ref' => 'REF'
);

sub convertFunction {
Expand All @@ -417,6 +418,15 @@ sub convertFunction {
my $regexoptions = '\'\'';
return "Regex('^'+$regex+'$', $regexoptions).test"; #(this.\$scope);";
}
if ($key eq '#ref') {
#TODO: like all accessses, this needs alot of undef protection.
my $addr = convertStringToJS( $$value[1] );
$addr =~ s/^this//;
return
'(foswiki_getRef('
. convertStringToJS( $$value[0] ) . ')'
. $addr . ')';
}
if ( ( $key eq '#div' )
or ( $key eq '#mult' )
or ( $key eq '#plus' )
Expand All @@ -425,7 +435,6 @@ sub convertFunction {
or ( $key eq '#neg' ) )
{

#die 'asd';
return
'('
. convertStringToJS( $$value[0] ) . ')'
Expand Down Expand Up @@ -628,7 +637,7 @@ sub convertToJavascript {
. ref($value) . "\n"
if MONITOR;
if ( ref($value) eq 'Regexp' ) {
$value =~ /\(\?-xism:(.*)\)/; #TODO: er, regex options?
$value =~ /\(\?[xism]*-[xism]*:(.*)\)/; #TODO: er, regex options?
$statement .=
"( /$1/.test(" . convertStringToJS($js_key) . ") )";

Expand Down Expand Up @@ -1494,9 +1503,20 @@ package Foswiki::Query::OP_in;

######################################
package Foswiki::Query::OP_ref;
use Assert;
#use Data::Dumper;

sub hoistMongoDB {
my $op = shift;
my $node = shift;ASSERT(ref($node) eq 'Foswiki::Query::Node');

#oh what a disaster.
# this has to be implemented as a compound query, so that we're querying against constants
#print STDERR "---OP_ref(".Dumper($node).")\n";

return {
'####delay_function' => 1,
'#ref' => [ $node->{lhs}, $node->{rhs} ]
};
}
######################################

=pod
Expand Down
16 changes: 10 additions & 6 deletions lib/Foswiki/Plugins/MongoDBPlugin/Meta.pm
Expand Up @@ -91,6 +91,7 @@ sub reload {
sub loadFromBSONData {
my $this = shift;
my $data = shift;
#print STDERR "======== loadFromBSONData (".$data->{_web}.")(".$data->{_topic}.")\n";

my @validKeys = keys(%Foswiki::Meta::VALIDATE);
#push( @validKeys, '_text' );
Expand All @@ -99,18 +100,21 @@ sub loadFromBSONData {
foreach my $key (@validKeys) {
next unless (defined($data->{$key}));
if ( $Foswiki::Meta::isArrayType{$key} ) {
#print STDERR "---- $key == many\n";
$this->{$key} = $data->{$key}->{'__RAW_ARRAY'};
#print STDERR "---- $key == many (".scalar(@{$data->{$key}->{'__RAW_ARRAY'}}).")\n";
##$this->{$key} = $data->{$key}->{'__RAW_ARRAY'};
if (defined($data->{$key}->{'__RAW_ARRAY'}) && scalar(@{$data->{$key}->{'__RAW_ARRAY'}})>0) {
$this->putAll($key, @{$data->{$key}->{'__RAW_ARRAY'}});
}
} else {
#$meta->{$key} = $savedMeta->{$key}[0];
$this->{$key} = [];
push(@{$this->{$key}}, $data->{$key});
# $this->{$key} = [];
# push(@{$this->{$key}}, $data->{$key});
$this->putAll($key, $data->{$key});
}
}
#use Data::Dumper;
#print STDERR "--- FIELD: ".Dumper($this->{TOPICINFO})."\n";
$this->{_text} = $data->{_text};
$this->{_indices} = $data->{_indices};
#$this->{_indices} = $data->{_indices};

$this->{_loadedRev} =
Foswiki::Store::cleanUpRevID( $this->{TOPICINFO}[0]->{version} );
Expand Down
8 changes: 7 additions & 1 deletion templates/mongodb_js.tmpl
@@ -1,3 +1,9 @@
%TMPL:DEF{foswiki_getRef_js}%function(host, collection, web, topic) {
conn = new Mongo(host);
current = conn.getCollection(collection);

return current.findOne({_web:web, _topic: topic});
}%TMPL:END%
%TMPL:DEF{foswiki_d2n_js}%function(dateString) {
var parseTime = function(date, defaultLocal) {

Expand Down Expand Up @@ -180,4 +186,4 @@
return 0;
}
return parsedDate.getTime(); //return seconds since 1970
}%TMPL:END%
}%TMPL:END%
18 changes: 18 additions & 0 deletions test/unit/HoistMongoDBsTests.pm
Expand Up @@ -1416,4 +1416,22 @@ sub test_hoist_not_in2 {
Foswiki::Plugins::MongoDBPlugin::HoistMongoDB::convertToJavascript($mongoDBQuery)
);
}

sub test_hoist_ref {
my $this = shift;
my $s = "'AnotherTopic'/number = 12";
my $queryParser = new Foswiki::Query::Parser();
my $query = $queryParser->parse($s);
my $mongoDBQuery =
Foswiki::Plugins::MongoDBPlugin::HoistMongoDB::hoist($query);

$this->do_Assert(
$query,
$mongoDBQuery,
{
'$where' => "(foswiki_getRef('AnotherTopic').FIELD.number.value) == 12",
}
);
}

1;

0 comments on commit ab6e90a

Please sign in to comment.