Skip to content

Commit 3d3df79

Browse files
committed
Fix running out of memory
1 parent de236a7 commit 3d3df79

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
}
4343
],
4444
"autoload": {
45+
"files": [
46+
"src/helpers/Functions.php"
47+
],
4548
"psr-4": {
4649
"Commands\\": "src/Commands/",
4750
"controllers\\": "src/controllers/",

src/Commands/Database/ExportCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5151
'items' => $this->items->getRaw(),
5252
];
5353

54-
file_put_contents($input->getArgument('path'), json_encode($data));
54+
$encoder = new \Violet\StreamingJsonEncoder\BufferJsonEncoder($data);
55+
$stream = new \Violet\StreamingJsonEncoder\JsonStream($encoder);
56+
file_put_contents($input->getArgument('path'), $stream);
5557

5658
return self::SUCCESS;
5759
}

src/daos/mysql/Items.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use DateTime;
1010
use DateTimeImmutable;
1111
use helpers\Configuration;
12+
use function helpers\functions\map;
1213
use Monolog\Logger;
1314

1415
/**
@@ -670,7 +671,7 @@ public function getRaw(): array {
670671
$stmt = static::$stmt;
671672
$items = $this->database->exec('SELECT * FROM ' . $this->configuration->dbPrefix . 'items');
672673

673-
return array_map(function($row) {
674+
return map(function($row) {
674675
// Use ISO 8601 as export format.
675676
$row['datetime'] = $row['datetime']->format(\DateTimeInterface::ATOM);
676677
$row['updatetime'] = $row['updatetime']->format(\DateTimeInterface::ATOM);

src/daos/mysql/Sources.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use daos\DatabaseInterface;
88
use helpers\Configuration;
9+
use function helpers\Functions\map;
910

1011
/**
1112
* Class for accessing persistent saved sources -- mysql
@@ -284,7 +285,7 @@ public function getRaw(): array {
284285
$stmt = static::$stmt;
285286
$sources = $this->database->exec('SELECT * FROM ' . $this->configuration->dbPrefix . 'sources');
286287

287-
return array_map(function($row) {
288+
return map(function($row) {
288289
// Stored as UNIX timestamp, use ISO 8601 as export format.
289290
$row['lastentry'] = (new \DateTime('@' . $row['lastentry']))->format(\DateTimeInterface::ATOM);
290291
$row['lastupdate'] = (new \DateTime('@' . $row['lastupdate']))->format(\DateTimeInterface::ATOM);

src/helpers/Functions.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace helpers\Functions;
6+
7+
use Generator;
8+
9+
/**
10+
* @template T
11+
* @template V
12+
*
13+
* @param callable(T): V $function
14+
* @param iterable<T> $collection
15+
*
16+
* @return Generator<int, V, void, void>
17+
*/
18+
function map(callable $function, iterable $collection): Generator {
19+
foreach ($collection as $item) {
20+
yield $function($item);
21+
}
22+
}

0 commit comments

Comments
 (0)