Skip to content

Commit

Permalink
Fix running out of memory
Browse files Browse the repository at this point in the history
  • Loading branch information
jtojnar committed Jun 13, 2023
1 parent 65563be commit cfd7ce9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
}
],
"autoload": {
"files": [
"src/helpers/Functions.php"
],
"psr-4": {
"Commands\\": "src/Commands/",
"controllers\\": "src/controllers/",
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/Database/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'items' => $this->items->getRaw(),
];

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

return self::SUCCESS;
}
Expand Down
5 changes: 3 additions & 2 deletions src/daos/mysql/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use DateTime;
use DateTimeImmutable;
use helpers\Configuration;
use function helpers\functions\map;
use helpers\HtmlString;
use Monolog\Logger;

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

/** @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}> */
$items = array_map(function($row) {
/** @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}> */
$items = map(function($row) {
// Use ISO 8601 as export format.
$row['datetime'] = $row['datetime']->format(\DateTimeInterface::ATOM);
$row['updatetime'] = $row['updatetime']->format(\DateTimeInterface::ATOM);
Expand Down
3 changes: 2 additions & 1 deletion src/daos/mysql/Sources.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use daos\DatabaseInterface;
use Exception;
use helpers\Configuration;
use function helpers\Functions\map;
use function json_encode;
use const JSON_ERROR_NONE;
use function json_last_error;
Expand Down Expand Up @@ -316,7 +317,7 @@ public function getRaw(): array {
$stmt = static::$stmt;
$sources = $this->database->exec('SELECT * FROM ' . $this->configuration->dbPrefix . 'sources');

return array_map(function($row) {
return map(function($row) {
// Stored as UNIX timestamp, use ISO 8601 as export format.
$row['lastentry'] = (new \DateTime('@' . $row['lastentry']))->format(\DateTimeInterface::ATOM);
$row['lastupdate'] = (new \DateTime('@' . $row['lastupdate']))->format(\DateTimeInterface::ATOM);
Expand Down
22 changes: 22 additions & 0 deletions src/helpers/Functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace helpers\Functions;

use Generator;

/**
* @template T
* @template V
*
* @param callable(T): V $function
* @param iterable<T> $collection
*
* @return Generator<int, V, void, void>
*/
function map(callable $function, iterable $collection): Generator {
foreach ($collection as $item) {
yield $function($item);
}
}

0 comments on commit cfd7ce9

Please sign in to comment.