Skip to content

Commit

Permalink
Make memory limit configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-meyer committed Feb 1, 2024
1 parent 982b299 commit 0a4fafa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
18 changes: 16 additions & 2 deletions config/config.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repositoryName: 'OAI-PMH 2.0 Data Provider'
#
# This has to be a valid email according to RFC 822 Address Specification.
#
adminEmail: [email protected]
adminEmail: '[email protected]'

#
# Database connection details
Expand All @@ -41,13 +41,27 @@ adminEmail: [email protected]
# database: 'mssql://oaipmh:[email protected]/oaipmh'
# database: 'mysql://root@localhost/oai?charset=utf8mb4'
# database: 'pgsql://oaipmh:secret@localhost:5432/oai_data_provider'
# database: 'sqlite3:////home/oaipmh/database.db'
# database: 'sqlite3:////opt/oaipmh/database.db'
#
# Run "composer doctrine:initialize-database" after switching to a new DB to
# test the settings and initialize the database!
#
database: 'sqlite3:///%BASEDIR%/data/sqlite3.db'

#
# Memory Limit
#
# This defines the maximum amount of memory the indexing process should use per
# run before it persists changes to the database, flushes it's internal storage
# and starts over with the next batch of records. Higher settings result in
# better performance, but depending on the size of your records even smaller
# values may be needed to prevent running out of memory.
# The value is interpreted as a percentage of the PHP "memory_limit".
#
# [0.2 - 0.8]
#
memoryLimit: 0.4

#
# Metadata formats, namespaces and schemas of your records
#
Expand Down
8 changes: 8 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* @property-read string $repositoryName
* @property-read string $adminEmail
* @property-read string $database
* @property-read float $memoryLimit
* @property-read array $metadataPrefix
* @property-read string $deletedRecords
* @property-read int $maxRecords
Expand Down Expand Up @@ -86,6 +87,13 @@ protected function getValidationConstraints(): Assert\Collection
new Assert\Type('string'),
new Assert\NotBlank()
],
'memoryLimit' => [
new Assert\Type('float'),
new Assert\Range([
'min' => 0.2,
'max' => 0.8
])
],
'metadataPrefix' => [
new Assert\Type('array'),
new Assert\All([
Expand Down
2 changes: 1 addition & 1 deletion src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function clearResultCache(): void
*
* @return int The memory limit in bytes or -1 if unlimited
*/
protected function getMemoryLimit(): int
protected function getPhpMemoryLimit(): int
{
$ini = trim(ini_get('memory_limit'));
$limit = (int) $ini;
Expand Down
8 changes: 5 additions & 3 deletions src/Console/CsvImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace OCC\OaiPmh2\Console;

use DateTime;
use OCC\OaiPmh2\Configuration;
use OCC\OaiPmh2\Console;
use OCC\OaiPmh2\Database;
use OCC\OaiPmh2\Entity\Format;
Expand Down Expand Up @@ -119,7 +120,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (!$this->validateInput($input, $output)) {
return Command::INVALID;
}
$memoryLimit = $this->getMemoryLimit();
$phpMemoryLimit = $this->getPhpMemoryLimit();
$memoryLimit = Configuration::getInstance()->memoryLimit;

/** @var array<string, string> */
$arguments = $input->getArguments();
Expand Down Expand Up @@ -161,8 +163,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$progressIndicator->advance();
$progressIndicator->setMessage('Importing... ' . (string) $count . ' records done.');

// Flush to database if memory usage reaches 30% of available limit.
if ((memory_get_usage() / $memoryLimit) > 0.3) {
// Flush to database if memory usage reaches limit.
if ((memory_get_usage() / $phpMemoryLimit) > $memoryLimit) {
Database::getInstance()->flush([Record::class]);
}
}
Expand Down

0 comments on commit 0a4fafa

Please sign in to comment.