Skip to content

Commit

Permalink
Clustering: Cap clustering batch size at 10k
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed Feb 9, 2023
1 parent c4ba824 commit 75e1641
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/BackgroundJobs/ClusterFacesJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ClusterFacesJob extends QueuedJob {
private IJobList $jobList;
private LoggerInterface $logger;

public const BATCH_SIZE = 10000;
public function __construct(ITimeFactory $time, Logger $logger, IJobList $jobList, FaceClusterAnalyzer $clusterAnalyzer) {
parent::__construct($time);
$this->logger = $logger;
Expand All @@ -35,7 +36,7 @@ protected function run($argument) {
/** @var string $userId */
$userId = $argument['userId'];
try {
$this->clusterAnalyzer->calculateClusters($userId);
$this->clusterAnalyzer->calculateClusters($userId, self::BATCH_SIZE);
} catch (\JsonException|Exception $e) {
$this->logger->error('Failed to calculate face clusters', ['exception' => $e]);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/Service/FaceClusterAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(FaceDetectionMapper $faceDetections, FaceClusterMapp
* @throws \OCP\DB\Exception
* @throws \JsonException
*/
public function calculateClusters(string $userId): void {
public function calculateClusters(string $userId, int $batchSize = -1): void {
$this->logger->debug('ClusterDebug: Retrieving face detections for user ' . $userId);

$unclusteredDetections = $this->faceDetections->findUnclusteredByUserId($userId);
Expand All @@ -50,6 +50,10 @@ public function calculateClusters(string $userId): void {
return;
}

if ($batchSize > 0 && count($unclusteredDetections) > $batchSize) {
$unclusteredDetections = array_slice($unclusteredDetections, 0, $batchSize);
}

$this->logger->debug('ClusterDebug: Found ' . count($unclusteredDetections) . " unclustered detections. Calculating clusters.");

$sampledDetections = [];
Expand Down

0 comments on commit 75e1641

Please sign in to comment.