Skip to content

Commit cfd7ce9

Browse files
committed
Fix running out of memory
1 parent 65563be commit cfd7ce9

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

composer.json

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

src/Commands/Database/ExportCommand.php

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

60-
file_put_contents($input->getArgument('path'), json_encode($data));
60+
$encoder = new \Violet\StreamingJsonEncoder\BufferJsonEncoder($data);
61+
$stream = new \Violet\StreamingJsonEncoder\JsonStream($encoder);
62+
file_put_contents($input->getArgument('path'), $stream);
6163

6264
return self::SUCCESS;
6365
}

src/daos/mysql/Items.php

Lines changed: 3 additions & 2 deletions
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 helpers\HtmlString;
1314
use Monolog\Logger;
1415

@@ -690,8 +691,8 @@ public function getRaw(): array {
690691
$stmt = static::$stmt;
691692
$items = $this->database->exec('SELECT * FROM ' . $this->configuration->dbPrefix . 'items');
692693

693-
/** @var array<array{author: string, content: string, datetime: string, icon: string, id: int, lastseen: string, link: string, shared: int, source: int, starred: bool, thumbnail: ?string, title: string, uid: string, unread: bool, updatetime: string}> */
694-
$items = array_map(function($row) {
694+
/** @var iterable<array{author: string, content: string, datetime: string, icon: string, id: int, lastseen: string, link: string, shared: int, source: int, starred: bool, thumbnail: ?string, title: string, uid: string, unread: bool, updatetime: string}> */
695+
$items = map(function($row) {
695696
// Use ISO 8601 as export format.
696697
$row['datetime'] = $row['datetime']->format(\DateTimeInterface::ATOM);
697698
$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
@@ -7,6 +7,7 @@
77
use daos\DatabaseInterface;
88
use Exception;
99
use helpers\Configuration;
10+
use function helpers\Functions\map;
1011
use function json_encode;
1112
use const JSON_ERROR_NONE;
1213
use function json_last_error;
@@ -316,7 +317,7 @@ public function getRaw(): array {
316317
$stmt = static::$stmt;
317318
$sources = $this->database->exec('SELECT * FROM ' . $this->configuration->dbPrefix . 'sources');
318319

319-
return array_map(function($row) {
320+
return map(function($row) {
320321
// Stored as UNIX timestamp, use ISO 8601 as export format.
321322
$row['lastentry'] = (new \DateTime('@' . $row['lastentry']))->format(\DateTimeInterface::ATOM);
322323
$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)