Skip to content

Commit

Permalink
Item9750: use the now generalised getField(), and perltidy
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/MongoDBPlugin@10590 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
SvenDowideit authored and SvenDowideit committed Jan 21, 2011
1 parent 64084cf commit 70ada39
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 135 deletions.
2 changes: 1 addition & 1 deletion lib/Foswiki/Plugins/MongoDBPlugin/DB.pm
Expand Up @@ -29,7 +29,7 @@ use Time::HiRes ();

#I wish
#use constant MONITOR => $Foswiki::cfg{MONITOR}{'Foswiki::Plugins::MongoDBPlugin'} || 0;
use constant MONITOR => 1;
use constant MONITOR => 0;

sub new {
my $class = shift;
Expand Down
139 changes: 5 additions & 134 deletions lib/Foswiki/Store/QueryAlgorithms/MongoDB.pm
Expand Up @@ -18,8 +18,12 @@ speed and memory size. It also depends on the complexity of the query.
=cut

package Foswiki::Store::QueryAlgorithms::MongoDB;

use Foswiki::Store::Interfaces::QueryAlgorithm ();
our @ISA = ( 'Foswiki::Store::Interfaces::QueryAlgorithm' );

use strict;
use constant MONITOR => 1;
use constant MONITOR => 0;

BEGIN {

Expand Down Expand Up @@ -282,139 +286,6 @@ sub convertQueryToJavascript {
HERE
}

# The getField function is here to allow for Store specific optimisations
# such as direct database lookups.
sub getField {
my ( $this, $node, $data, $field ) = @_;

my $result;
if ( UNIVERSAL::isa( $data, 'Foswiki::Meta' ) ) {

# The object being indexed is a Foswiki::Meta object, so
# we have to use a different approach to treating it
# as an associative array. The first thing to do is to
# apply our "alias" shortcuts.
my $realField = $field;
if ( $Foswiki::Query::Node::aliases{$field} ) {
$realField = $Foswiki::Query::Node::aliases{$field};
}
if ( $realField =~ s/^META:// ) {
if ( $Foswiki::Query::Node::isArrayType{$realField} ) {

# Array type, have to use find
my @e = $data->find($realField);
$result = \@e;
}
else {
$result = $data->get($realField);
}
}
elsif ( $realField eq 'name' ) {

# Special accessor to compensate for lack of a topic
# name anywhere in the saved fields of meta
return $data->topic();
}
elsif ( $realField eq 'text' ) {

# Special accessor to compensate for lack of the topic text
# name anywhere in the saved fields of meta
return $data->text();
}
elsif ( $realField eq 'web' ) {

# Special accessor to compensate for lack of a web
# name anywhere in the saved fields of meta
return $data->web();
}
elsif ( $realField eq 'hash' ) {

#return the topic object.
return $data;
}
else {

# The field name isn't an alias, check to see if it's
# the form name
my $form = $data->get('FORM');
if ( $form && $field eq $form->{name} ) {

# SHORTCUT;it's the form name, so give me the fields
# as if the 'field' keyword had been used.
# TODO: This is where multiple form support needs to reside.
# Return the array of FIELD for further indexing.
my @e = $data->find('FIELD');
return \@e;
}
else {

# SHORTCUT; not a predefined name; assume it's a field
# 'name' instead.
# SMELL: Needs to error out if there are multiple forms -
# or perhaps have a heuristic that gives access to the
# uniquely named field.
$result = $data->get( 'FIELD', $field );
$result = $result->{value} if $result;
}
}
}
elsif ( ref($data) eq 'ARRAY' ) {

# Array objects are returned during evaluation, e.g. when
# a subset of an array is matched for further processing.

# Indexing an array object. The index will be one of:
# 1. An integer, which is an implicit index='x' query
# 2. A name, which is an implicit name='x' query
if ( $field =~ /^\d+$/ ) {

# Integer index
$result = $data->[$field];
}
else {

# String index
my @res;

# Get all array entries that match the field
foreach my $f (@$data) {
my $val = getField( undef, $node, $f, $field );
push( @res, $val ) if defined($val);
}
if ( scalar(@res) ) {
$result = \@res;
}
else {

# The field name wasn't explicitly seen in any of the records.
# Try again, this time matching 'name' and returning 'value'
foreach my $f (@$data) {
next unless ref($f) eq 'HASH';
if ( $f->{name}
&& $f->{name} eq $field
&& defined $f->{value} )
{
push( @res, $f->{value} );
}
}
if ( scalar(@res) ) {
$result = \@res;
}
}
}
}
elsif ( ref($data) eq 'HASH' ) {

# A hash object may be returned when a sub-object of a Foswiki::Meta
# object has been matched.
$result = $data->{ $node->{params}[0] };
}
else {
$result = $node->{params}[0];
}
return $result;
}

# Get a referenced topic
# See Foswiki::Store::QueryAlgorithms.pm for details
sub getRefTopic {
Expand Down

0 comments on commit 70ada39

Please sign in to comment.