Skip to content

Commit

Permalink
Handle records as entities
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-meyer committed Jan 6, 2024
1 parent 1e27542 commit ec7f72c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 42 deletions.
29 changes: 20 additions & 9 deletions src/Console/CsvImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ function (): array {
'Name of the CSV column which holds the records\' sets list.',
'sets'
);
$this->addOption(
'noValidation',
null,
InputOption::VALUE_NONE,
'Omit content validation (improves performance for large record sets).',
false
);
parent::configure();
}

Expand All @@ -117,6 +124,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$arguments = $input->getArguments();
/** @var Format */
$format = Database::getInstance()->getEntityManager()->getReference(Format::class, $arguments['format']);
/** @var bool */
$noValidation = $input->getOption('noValidation');
/** @var resource */
$file = fopen($arguments['file'], 'r');

Expand All @@ -129,23 +138,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$progressIndicator = new ProgressIndicator($output, 'verbose', 200, ['', '', '', '', '', '', '', '']);
$progressIndicator->start('Importing...');

while ($record = fgetcsv($file)) {
Database::getInstance()->addOrUpdateRecord(
$record[$columns['idColumn']],
while ($row = fgetcsv($file)) {
$record = new Record(
$row[$columns['idColumn']],
$format,
trim($record[$columns['contentColumn']]),
new DateTime($record[$columns['dateColumn']] ?? 'now'),
// TODO: Complete support for sets.
/* $record[$columns['setColumn']] ?? */ null,
true
null,
new DateTime($row[$columns['dateColumn']] ?? 'now')
);
if (strlen(trim($row[$columns['contentColumn']])) > 0) {
$record->setContent($row[$columns['contentColumn']], !$noValidation);
}
// TODO: Complete support for sets.
Database::getInstance()->addOrUpdateRecord($record, true);

++$count;
$progressIndicator->advance();
$progressIndicator->setMessage((string) $count . ' done.');

// Flush to database if memory usage reaches 90% of available limit.
if (memory_get_usage() / $memoryLimit > 0.9) {
if ((memory_get_usage() / $memoryLimit) > 0.9) {
Database::getInstance()->flush([Record::class]);
}
}
Expand Down
50 changes: 18 additions & 32 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,46 +92,32 @@ public function addOrUpdateMetadataFormat(Format $newFormat): void
/**
* Add or update record.
*
* @param string $identifier The record identifier
* @param Format $format The metadata prefix
* @param ?string $data The record's content
* @param ?DateTime $lastChanged The date of last change
* @param ?array<string, Set> $sets The record's associated sets
* @param Record $newRecord The record
* @param bool $bulkMode Should we operate in bulk mode (no flush)?
*
* @return void
*/
public function addOrUpdateRecord(
string $identifier,
Format $format,
?string $data = null,
?DateTime $lastChanged = null,
// TODO: Complete support for sets
?array $sets,
bool $bulkMode = false
): void
public function addOrUpdateRecord(Record $newRecord, bool $bulkMode = false): void
{
$record = $this->entityManager->find(Record::class, ['identifier' => $identifier, 'format' => $format]);
if (!isset($data) && Configuration::getInstance()->deletedRecords === 'no') {
if (isset($record)) {
$this->entityManager->remove($record);
$oldRecord = $this->entityManager->find(
Record::class,
[
'identifier' => $newRecord->getIdentifier(),
'format' => $newRecord->getFormat()
]
);
if (isset($oldRecord)) {
if ($newRecord->hasContent() || Configuration::getInstance()->deletedRecords !== 'no') {
$oldRecord->setContent($newRecord->getContent(), false);
$oldRecord->setLastChanged($newRecord->getLastChanged());
// TODO: Add full set support.
} else {
$this->entityManager->remove($oldRecord);
}
} else {
if (isset($record)) {
try {
$record->setContent($data);
$record->setLastChanged($lastChanged);
} catch (ValidationFailedException $exception) {
throw $exception;
}
} else {
try {
$record = new Record($identifier, $format, $data, $lastChanged);
} catch (ValidationFailedException $exception) {
throw $exception;
}
if ($newRecord->hasContent() || Configuration::getInstance()->deletedRecords !== 'no') {
$this->entityManager->persist($newRecord);
}
$this->entityManager->persist($record);
}
if (!$bulkMode) {
$this->entityManager->flush();
Expand Down
12 changes: 11 additions & 1 deletion src/Database/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ public function getSets(): array
return $this->sets->toArray();
}

/**
* Whether this record has any content.
*
* @return bool TRUE if content exists, FALSE otherwise
*/
public function hasContent(): bool
{
return isset($this->content);
}

/**
* Remove record from set.
*
Expand Down Expand Up @@ -184,6 +194,7 @@ public function removeSet(Set $set): void
public function setContent(?string $data = null, bool $validate = true): void
{
if (isset($data)) {
$data = trim($data);
if ($validate) {
try {
$data = $this->validate($data);
Expand Down Expand Up @@ -233,7 +244,6 @@ public function setLastChanged(?DateTime $dateTime = null): void
*/
protected function validate(string $xml): string
{
$xml = trim($xml);
$validator = Validation::createValidator();
$violations = $validator->validate(
$xml,
Expand Down

0 comments on commit ec7f72c

Please sign in to comment.