Skip to content
Open

roeyc #280

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions app/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,48 @@

namespace App;

use Exception;

final class Parser
{
public function parse(string $inputPath, string $outputPath): void
{
throw new Exception('TODO');
}
}
public function parse(string $inputPath, string $outputPath): void
{
gc_disable();
$posts = [];
$chunk_size = 16 * 1024;

$handle = fopen($inputPath, "r");
stream_set_read_buffer($handle, 0);
$last_line = '';
while (!feof($handle))
{
$chunk = fread($handle, $chunk_size);
$lines = explode("\n", $chunk);
$count = count($lines);
for ($i = 0; $i < $count; $i++)
{
if ($i === $count - 1)
{
$last_line = $lines[$i];
}
else
{
if ($i === 0)
$line = $last_line . $lines[$i];
else
$line = $lines[$i];

$path = substr($line, 19, -26);
$date = substr($line, -25, 10);
$posts[$path][] = $date;
}
}
}
fclose($handle);
foreach ($posts as &$dates)
{
$dates = array_count_values($dates);
ksort($dates);
}

file_put_contents($outputPath, json_encode($posts, JSON_PRETTY_PRINT));
}
}
16 changes: 16 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

require_once 'app/Parser.php';

$script_start = microtime(true);

//file_put_contents('data/numbers.csv', implode("\n", range(100, 199)));
$p = new app\Parser();
//$p->parse('data/numbers.csv', 'data/data.json');
//$p->parse('data/test-data.csv', 'data/test-data-actual.json');
//$p->parse('/home/roey/data.csv', 'data/data.json');
//$p->parse('data/data.csv', 'data/data.json');
$p->parse('data/data-10M.csv', 'data/data.json');
//$p->parse('data/data-100M.csv', 'data/data.json');

echo "Total time: " . (microtime(true) - $script_start) . "\n";