Commit cf47cd75 authored by Tibo's avatar Tibo

Initial import

parent b88ab264
/vendor/
/nbproject/private/
.phpunit.result.cache
{
"name": "cylab-be/php-spark",
"description": "A wrapper around arrays that mimics the MapReduce methods of Apache Spark",
"type": "library",
"require-dev": {
"phpunit/phpunit": "^8.3",
"squizlabs/php_codesniffer": "^3.4"
},
"license": "MIT",
"authors": [
{
"name": "Thibault Debatty",
"email": "thibault.debatty@gmail.com"
}
],
"require": {},
"autoload": {
"psr-4": {
"Cylab\\Spark\\":"src"
}
}
}
This diff is collapsed.
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_create_2e_tests=false
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_enabled=false
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_path=
auxiliary.org-netbeans-modules-php-phpunit.configuration_2e_enabled=false
auxiliary.org-netbeans-modules-php-phpunit.configuration_2e_path=
auxiliary.org-netbeans-modules-php-phpunit.customSuite_2e_enabled=false
auxiliary.org-netbeans-modules-php-phpunit.customSuite_2e_path=
auxiliary.org-netbeans-modules-php-phpunit.phpUnit_2e_enabled=false
auxiliary.org-netbeans-modules-php-phpunit.phpUnit_2e_path=
auxiliary.org-netbeans-modules-php-phpunit.test_2e_groups_2e_ask=false
auxiliary.org-netbeans-modules-php-phpunit.test_2e_run_2e_all=false
auxiliary.org-netbeans-modules-php-phpunit.test_2e_run_2e_phpunit_2e_only=false
file.reference.php-spark-tests=tests
include.path=${php.global.include.path}
php.version=PHP_72
source.encoding=UTF-8
src.dir=src
tags.asp=false
tags.short=false
test.src.dir=${file.reference.php-spark-tests}
testing.providers=PhpUnit
web.root=.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.php.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/php-project/1">
<name>php-spark</name>
</data>
</configuration>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
</phpunit>
<?php
namespace Cylab\Spark;
/**
* Description of Dataset
*
* @author tibo
*/
class Dataset
{
private $data;
public function __construct(array $data)
{
$this->data = $data;
}
/**
*
* @param callable $function
* @return \Tests\Unit\MRCollection
*/
public function map(callable $function)
{
return new Dataset(array_map($function, $this->data));
}
/**
*
* @param callable $function
* @param type $initial
* @return type
*/
public function reduce(callable $function, $initial = null)
{
return array_reduce($this->data, $function, $initial);
}
/**
*
* @param callable $function
* @param type $initial
* @return \App\MRCollection
*/
public function reduceByKey(callable $function, $initial = null)
{
$groups = $this->groupByKey();
$reduced = [];
foreach ($groups->collect() as $key => $values) {
$reduced[] = new Tuple(
$key,
array_reduce($values, $function, $initial)
);
}
return new Dataset($reduced);
}
/**
*
* @param bool $ascending
* @return \App\MRCollection
*/
public function sortByKey(bool $ascending = true)
{
$copy = $this->data;
if ($ascending) {
usort($copy, function (Tuple $t1, Tuple $t2) {
return $t1->key > $t2->key ? 1 : -1;
});
} else {
usort($copy, function (Tuple $t1, Tuple $t2) {
return $t1->key > $t2->key ? -1 : 1;
});
}
return new Dataset($copy);
}
public function groupByKey()
{
$groups = [];
foreach ($this->data as $tuple) {
$groups[$tuple->key][] = $tuple->value;
}
return new Dataset($groups);
}
public static function fromMongo(\MongoDB\Driver\Cursor $cursor)
{
return new Dataset($cursor->toArray());
}
public function collect() : array
{
return $this->data;
}
public function count() : int
{
return count($this->data);
}
public function first()
{
return $this->data[0];
}
public function take($n = 10) : array
{
return array_slice($this->data, 0, $n);
}
}
<?php
namespace Cylab\Spark;
/**
* Description of Tuple
*
* @author tibo
*/
class Tuple
{
public $key;
public $value;
public function __construct($key, $value)
{
$this->key = $key;
$this->value = $value;
}
}
<?php
namespace Cylab\Spark;
use PHPUnit\Framework\TestCase;
/**
* Description of DatasetTest
*
* @author tibo
*/
class DatasetTest extends TestCase {
public function testMapReduce() {
$data = new Dataset([1, 2, 3, 4]);
$this->assertEquals(20, $data
->map(function ($v) {
return 2 * $v;
})
->reduce(function ($v, $agg) {
return $agg + $v;
}));
}
}
<?php
namespace Cylab\Spark;
use PHPUnit\Framework\TestCase;
/**
* @group mapreduce
*/
class TupleTest extends TestCase
{
public function testMR()
{
$t = new Tuple("key", "value");
$this->assertEquals("key", $t->key);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment