From 4aaf6c32a266726cb4cd7540b465aa6b9b67d5e9 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Fri, 5 Jan 2024 07:59:22 -0800 Subject: [PATCH] places-setup: make transaction size configurable (fix #943) Signed-off-by: Varun Patil --- lib/Command/PlacesSetup.php | 7 +++++++ lib/Service/Places.php | 8 ++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/Command/PlacesSetup.php b/lib/Command/PlacesSetup.php index 82446522..23d26b74 100644 --- a/lib/Command/PlacesSetup.php +++ b/lib/Command/PlacesSetup.php @@ -44,6 +44,7 @@ class PlacesSetup extends Command ->setName('memories:places-setup') ->setDescription('Setup reverse geocoding') ->addOption('recalculate', 'r', InputOption::VALUE_NONE, 'Only recalculate places for existing files') + ->addOption('transaction-size', null, InputOption::VALUE_REQUIRED, 'Reduce this value if your database crashes', 50) ; } @@ -52,6 +53,12 @@ class PlacesSetup extends Command $this->output = $output; $recalculate = $input->getOption('recalculate'); + if (($this->places->txnSize = (int) $input->getOption('transaction-size')) < 1) { + $this->output->writeln('Transaction size must be at least 1'); + + return 1; + } + $this->output->writeln('Attempting to set up reverse geocoding'); // Detect the GIS type diff --git a/lib/Service/Places.php b/lib/Service/Places.php index 9bf4a2a6..d3af7334 100644 --- a/lib/Service/Places.php +++ b/lib/Service/Places.php @@ -13,12 +13,16 @@ const GIS_TYPE_NONE = 0; const GIS_TYPE_MYSQL = 1; const GIS_TYPE_POSTGRES = 2; const APPROX_PLACES = 635189; -const DB_TRANSACTION_SIZE = 50; const PLANET_URL = 'https://github.com/pulsejet/memories-assets/releases/download/geo-0.0.3/planet_coarse_boundaries.zip'; class Places { + /** + * Number of places to process in a single transaction. + */ + public int $txnSize = 50; + public function __construct( protected IConfig $config, protected IDBConnection $connection, @@ -240,7 +244,7 @@ class Places // Function to commit the current transaction $transact = function () use (&$txnCount): void { - if (++$txnCount >= DB_TRANSACTION_SIZE) { + if (++$txnCount >= $this->txnSize) { $this->connection->commit(); $this->connection->beginTransaction(); $txnCount = 0;