Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add simdjson handler 🚀 #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
76 changes: 76 additions & 0 deletions src/Statement/SimdjsonHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Sandro Keil (https://sandro-keil.de)
*
* @link http://github.com/sandrokeil/arangodb-php-client for the canonical source repository
* @copyright Copyright (c) 2018-2020 Sandro Keil
* @license http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace ArangoDb\Statement;

use Psr\Http\Message\StreamInterface;

final class SimdjsonHandler implements StreamHandler
{
use SimdjsonStreamHandlerTrait;

/**
* @var mixed
*/
private $data = [];

public function __construct(StreamInterface $stream)
{
$this->data[$this->fetches] = $stream->getContents();
$this->length = \simdjson_key_count($this->data[$this->fetches], 'result');
$this->batchSize = $this->length;
}

/**
* Return the current result row
*
* @return array
*/
public function current(): array
{
return \simdjson_key_value(
$this->data[$this->fetches],
"result/" . ($this->position - ($this->batchSize * $this->fetches)),
true
);
}

public function result(): array
{
return \simdjson_key_value(
$this->data[$this->fetches],
"result",
true
);
}

public function raw(): array
{
return $this->data[$this->fetches];
}

public function completeResult()
{
$completeResult = [[]];

foreach ($this->data as $result) {
$completeResult[] = \simdjson_key_value($result, 'result', true);
}
return array_merge(...$completeResult);
}

public function appendStream(StreamInterface $stream): void
{
$this->data[++$this->fetches] = $stream->getContents();
$this->length += \simdjson_key_count($this->data[$this->fetches], 'result', true);
}

}
22 changes: 22 additions & 0 deletions src/Statement/SimdjsonStreamHandlerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Sandro Keil (https://sandro-keil.de)
*
* @link http://github.com/sandrokeil/arangodb-php-client for the canonical source repository
* @copyright Copyright (c) 2018-2020 Sandro Keil
* @license http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace ArangoDb\Statement;

use Psr\Http\Message\StreamInterface;

class SimdjsonStreamHandlerFactory implements StreamHandlerFactoryInterface
{
public function createStreamHandler(StreamInterface $body): StreamHandler
{
return new SimdjsonHandler($body);
}
}
129 changes: 129 additions & 0 deletions src/Statement/SimdjsonStreamHandlerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
/**
* Sandro Keil (https://sandro-keil.de)
*
* @link http://github.com/sandrokeil/arangodb-php-client for the canonical source repository
* @copyright Copyright (c) 2018-2020 Sandro Keil
* @license http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace ArangoDb\Statement;

trait SimdjsonStreamHandlerTrait
{
/**
* Current position in result set iteration (zero-based)
*
* @var int
*/
private $position = 0;

/**
* Number of HTTP calls that were made to build the cursor result
*
* @var int
*/
private $fetches = 0;

/**
* Total length of result set (in number of documents)
*
* @var int
*/
private $length;

/**
* @var int
*/
private $batchSize;

public function cursorId(): ?string
{
return \simdjson_key_value($this->data[$this->fetches], 'id', true);
}

public function hasMore(): bool
{
return \simdjson_key_value($this->data[$this->fetches], 'hasMore', true);
}

public function resultCount(): ?int
{
return \simdjson_key_value($this->data[$this->fetches], 'count', true);
}

/**
* Get the total number of current loaded results.
*
* @return int Total number of laoded results
*/
public function count()
{
return $this->length;
}

public function rewind(): void
{
$this->position = 0;
}

public function key(): int
{
return $this->position;
}

public function next(): void
{
$this->position++;
}

/**
* @return bool
*/
public function valid(): bool
{
return $this->position <= $this->length - 1;
}

public function writesExecuted(): ?int
{
return \simdjson_key_value($this->data[$this->fetches], "extra/stats/writesExecuted", true);
}

public function writesIgnored(): ?int
{
return \simdjson_key_value($this->data[$this->fetches], "extra/stats/writesIgnored", true);
}

public function scannedFull(): ?int
{
return \simdjson_key_value($this->data[$this->fetches], "extra/stats/scannedFull", true);
}

public function scannedIndex(): ?int
{
return \simdjson_key_value($this->data[$this->fetches], "extra/stats/scannedIndex", true);
}

public function filtered(): ?int
{
return \simdjson_key_value($this->data[$this->fetches], "extra/stats/filtered", true);
}

public function fullCount(): ?int
{
return \simdjson_key_value($this->data[$this->fetches], "extra/stats/fullCount", true);
}

public function warnings(): array
{
return \simdjson_key_value($this->data[$this->fetches], "extra/warnings", true);
}

public function isCached(): bool
{
return \simdjson_key_value($this->data[$this->fetches], "cached", true);
}
}
4 changes: 3 additions & 1 deletion test/TestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use ArangoDb\Http\Client;
use ArangoDb\Exception\ArangoDbException;
use ArangoDb\Statement\ArrayStreamHandlerFactory;
use ArangoDb\Statement\SimdjsonStreamHandlerFactory;
use ArangoDb\Statement\StreamHandlerFactoryInterface;
use ArangoDb\Type\Database;
use ArangoDb\Http\ClientOptions;
Expand Down Expand Up @@ -84,7 +85,8 @@ public static function getStreamFactory(): StreamFactoryInterface

public static function getStreamHandlerFactory(): StreamHandlerFactoryInterface
{
return new ArrayStreamHandlerFactory();
return new SimdjsonStreamHandlerFactory();
// return new ArrayStreamHandlerFactory();
}

public static function createDatabase(): void
Expand Down