Skip to content

Commit

Permalink
Item11042: perltidy
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/MongoDBPlugin@12710 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
PaulHarvey authored and PaulHarvey committed Oct 5, 2011
1 parent ea74fa8 commit 9d1c174
Showing 1 changed file with 47 additions and 35 deletions.
82 changes: 47 additions & 35 deletions lib/Foswiki/Plugins/MongoDBPlugin/DB.pm
Expand Up @@ -31,7 +31,6 @@ use Tie::IxHash ();
use Foswiki::Func;
use Digest::MD5 qw(md5_hex);


#lets declare it ok to run queries on slaves.
#http://search.cpan.org/~kristina/MongoDB-0.42/lib/MongoDB/Cursor.pm#slave_okay
$MongoDB::Cursor::slave_okay = 1;
Expand Down Expand Up @@ -59,13 +58,15 @@ sub query {
my $collectionName = shift;
my $ixhQuery = shift;
my $queryAttrs = shift || {};

if (not $self->databaseNameSafeToUse($web)) {
print STDERR "ERROR: sorry, $web cannot be cached to MongoDB as there is another web with the same spelling, but different case already cached\n";

if ( not $self->databaseNameSafeToUse($web) ) {
print STDERR
"ERROR: sorry, $web cannot be cached to MongoDB as there is another web with the same spelling, but different case already cached\n";
return;
}

if ($collectionName eq 'current') {
if ( $collectionName eq 'current' ) {

#remove all the history versions from the result.
if ( ref($ixhQuery) eq 'Tie::IxHash' ) {
$ixhQuery->Unshift( '_history' => { '$exists' => 0 } );
Expand All @@ -77,8 +78,10 @@ sub query {

my $startTime = [Time::HiRes::gettimeofday];

my $collection = $self->_getCollection( $web, $collectionName);
print STDERR "searching mongo ($web -> ".$self->getDatabaseName($web).". $collectionName) : "
my $collection = $self->_getCollection( $web, $collectionName );
print STDERR "searching mongo ($web -> "
. $self->getDatabaseName($web)
. ". $collectionName) : "
. Dumper($ixhQuery) . " , "
. Dumper($queryAttrs) . "\n"
if MONITOR;
Expand All @@ -87,13 +90,20 @@ sub query {
#print STDERR "----------------------------------------------------------------------------------\n" if DEBUG;
my $db = $self->_getDatabase($web);

if ( exists $Foswiki::cfg{Plugins}{MongoDBPlugin}{ProfilingLevel}
and defined $Foswiki::cfg{Plugins}{MongoDBPlugin}{ProfilingLevel}) {
$db->run_command({'profile' => $Foswiki::cfg{Plugins}{MongoDBPlugin}{ProfilingLevel}});
if ( exists $Foswiki::cfg{Plugins}{MongoDBPlugin}{ProfilingLevel}
and defined $Foswiki::cfg{Plugins}{MongoDBPlugin}{ProfilingLevel} )
{
$db->run_command(
{
'profile' =>
$Foswiki::cfg{Plugins}{MongoDBPlugin}{ProfilingLevel}
}
);
}

my $long_count =
$db->run_command( { "count" => $collectionName, "query" => $ixhQuery } );

#use Devel::Peek;
#Dump($long_count);
#print STDERR "----------------------------------------------------------------------------------\n";
Expand All @@ -102,9 +112,10 @@ sub query {
#die $long_count if ($long_count =~ /assert/);

my $cursor = $collection->query( $ixhQuery, $queryAttrs );

#TODO: this is to make sure we're getting the cursor->count before anyone uses the cursor.
my $count = $long_count;
if (($collectionName eq 'current') and ( $count > 100 )) {
if ( ( $collectionName eq 'current' ) and ( $count > 100 ) ) {
$cursor->{noCache} = 1;
$cursor = $cursor->fields( { _web => 1, _topic => 1 } );
}
Expand Down Expand Up @@ -139,14 +150,15 @@ sub query {
}

sub update {
my $self = shift;
my $web = shift;

if (not $self->databaseNameSafeToUse($web)) {
print STDERR "ERROR: sorry, $web cannot be cached to MongoDB as there is another web with the same spelling, but different case already cached\n";
my $self = shift;
my $web = shift;

if ( not $self->databaseNameSafeToUse($web) ) {
print STDERR
"ERROR: sorry, $web cannot be cached to MongoDB as there is another web with the same spelling, but different case already cached\n";
return;
}

my $collectionName = shift;
my $address = shift;
my $hash = shift;
Expand Down Expand Up @@ -200,11 +212,8 @@ sub update {
{ 'CREATEINFO.date' => 1 },
{ name => 'CREATEINFO.date' }
);
$self->ensureIndex(
$collection,
{ 'address' => 1 },
{ name => 'address' }
);
$self->ensureIndex( $collection, { 'address' => 1 },
{ name => 'address' } );

#TODO: maybe should use the auto indexed '_id' (or maybe we can use this as a tuid - unique foreach rev of each topic..)
#then again, atm, its totally random, so may be good for sharding.
Expand Down Expand Up @@ -337,11 +346,12 @@ sub updateSystemJS {
my $functionname = shift;
my $sourcecode = shift;

if (not $self->databaseNameSafeToUse($web)) {
print STDERR "ERROR: sorry, $web cannot be cached to MongoDB as there is another web with the same spelling, but different case already cached\n";
if ( not $self->databaseNameSafeToUse($web) ) {
print STDERR
"ERROR: sorry, $web cannot be cached to MongoDB as there is another web with the same spelling, but different case already cached\n";
return;
}

my $collection = $self->_getCollection( $web, 'system.js' );

use MongoDB::Code;
Expand All @@ -353,24 +363,24 @@ sub updateSystemJS {
value => $code
}
);

#update our webmap.
$collection = $self->_getCollection( 'webs', 'map' );
$collection->save(
{
_id => $web,
_id => $web,
hash => $self->getDatabaseName($web)
}
);
);
}

#######################################################
sub getDatabaseName {
my $self = shift;
my $web = shift;
return $web if ($web eq 'webs');
return 'web_'.md5_hex($web);

return $web if ( $web eq 'webs' );
return 'web_' . md5_hex($web);

#using webname as database name, so we need to sanitise
#replace / with __ and pre-pend foswiki__ ?
Expand Down Expand Up @@ -425,12 +435,14 @@ sub _getCollection {
my $self = shift;
my $web = shift;
my $collectionName = shift;

return $self->{collections}{$web}{$collectionName} if (defined($self->{collections}{$web}{$collectionName}));

return $self->{collections}{$web}{$collectionName}
if ( defined( $self->{collections}{$web}{$collectionName} ) );

my $db = $self->_getDatabase($web);
$self->{collections}{$web}{$collectionName} = $db->get_collection($collectionName);

$self->{collections}{$web}{$collectionName} =
$db->get_collection($collectionName);

return $self->{collections}{$web}{$collectionName};
}

Expand Down

0 comments on commit 9d1c174

Please sign in to comment.