Skip to content

Commit

Permalink
Open one MongoDB connection instead of three
Browse files Browse the repository at this point in the history
Now the lex_stats call opens a single MongoDB connection and reuses it
for all three queries. The efficiency boost might be minor, but it's
also a slight code simplification so it's just as well to do it now.
  • Loading branch information
rmunn committed Sep 28, 2023
1 parent 64de497 commit 0e6e7a8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/Api/Model/Languageforge/Lexicon/Dto/LexStatsDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class LexStatsDto
*/
public static function encode($project)
{
$dbName = $project->databaseName();
$num_entries = MongoQueries::countEntries($dbName, "lexicon");
$num_entries_with_pictures = MongoQueries::countEntriesWithPictures($dbName, "lexicon");
$num_unresolved_comments = MongoQueries::countUnresolvedComments($dbName, "lexiconComments");
$db = MongoStore::connect($project->databaseName());
$num_entries = MongoQueries::countEntries($db, "lexicon");
$num_entries_with_pictures = MongoQueries::countEntriesWithPictures($db, "lexicon");
$num_unresolved_comments = MongoQueries::countUnresolvedComments($db, "lexiconComments");
return [
"num_entries" => $num_entries,
"num_entries_with_pictures" => $num_entries_with_pictures,
Expand Down
9 changes: 3 additions & 6 deletions src/Api/Model/Shared/Mapper/MongoQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@

class MongoQueries
{
public static function countEntries($databaseName, $collectionName)
public static function countEntries($db, $collectionName)
{
$db = MongoStore::connect($databaseName);
$coll = $db->selectCollection($collectionName);
return $coll->count();
}

public static function countEntriesWithPictures($databaseName, $collectionName)
public static function countEntriesWithPictures($db, $collectionName)
{
$db = MongoStore::connect($databaseName);
$coll = $db->selectCollection($collectionName);
$query = [
"senses" => ['$exists' => true, '$ne' => []],
Expand All @@ -22,9 +20,8 @@ public static function countEntriesWithPictures($databaseName, $collectionName)
return $coll->count($query);
}

public static function countUnresolvedComments($databaseName, $collectionName)
public static function countUnresolvedComments($db, $collectionName)
{
$db = MongoStore::connect($databaseName);
$coll = $db->selectCollection($collectionName);
$query = [
"status" => ['$exists' => true, '$ne' => "resolved"],
Expand Down

0 comments on commit 0e6e7a8

Please sign in to comment.