Skip to content

Commit 8fde0f5

Browse files
stefan1144TheCelavi
authored andcommitted
Updated dataset author list
Proposal for buffer operators Fixing copilot suggestions Increase number of mysql connection attempts
1 parent b959580 commit 8fde0f5

20 files changed

Lines changed: 870 additions & 427 deletions

src/RunOpenCode/Component/Dataset/composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
{
77
"name": "Nikola Svitlica a.k.a TheCelavi",
88
"email": "thecelavi@runopencode.com"
9+
},
10+
{
11+
"name": "Stefan Veljancic",
12+
"email": "veljancicstefan@gmail.com"
913
}
1014
],
1115
"require": {

src/RunOpenCode/Component/Dataset/src/Collector/IterableCollector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class IterableCollector implements \IteratorAggregate, CollectorInterface
2828
return $this->getIterator();
2929
}
3030
}
31-
31+
3232
/**
3333
* {@inheritdoc}
3434
*/
@@ -45,7 +45,7 @@ final class IterableCollector implements \IteratorAggregate, CollectorInterface
4545

4646
/**
4747
* Provides you with total number of iterated elements.
48-
*
48+
*
4949
* @var non-negative-int
5050
*/
5151
public private(set) int $count = 0;
@@ -73,4 +73,4 @@ public function getIterator(): \Traversable
7373
$this->count++;
7474
}
7575
}
76-
}
76+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RunOpenCode\Component\Dataset\Model;
6+
7+
use RunOpenCode\Component\Dataset\Exception\LogicException;
8+
9+
/**
10+
* Buffer of iterated items from collection.
11+
*
12+
* @template-covariant TKey
13+
* @template-covariant TValue
14+
*
15+
* @phpstan-type ItemTuple = array{TKey, TValue}
16+
*
17+
* @implements \IteratorAggregate<TKey, TValue>
18+
*/
19+
final readonly class Buffer implements \IteratorAggregate, \Countable
20+
{
21+
/**
22+
* Create new buffer.
23+
*
24+
* @param \ArrayObject<int, ItemTuple> $items Items within buffer.
25+
*
26+
* @internal
27+
*/
28+
public function __construct(private \ArrayObject $items)
29+
{
30+
// noop.
31+
}
32+
33+
/**
34+
* Get first item in buffer.
35+
*
36+
* @return Item<TKey, TValue>
37+
*
38+
* @phpstan-ignore-next-line generics.variance
39+
*/
40+
public function first(): Item
41+
{
42+
$first = $this->items[0] ?? throw new LogicException('Buffer is empty.');
43+
44+
return new Item($first[0], $first[1]);
45+
}
46+
47+
/**
48+
* Get last item in buffer.
49+
*
50+
* @return Item<TKey, TValue>
51+
*
52+
* @phpstan-ignore-next-line generics.variance
53+
*/
54+
public function last(): Item
55+
{
56+
$last = $this->items[\count($this->items) - 1] ?? throw new LogicException('Buffer is empty.');
57+
58+
return new Item($last[0], $last[1]);
59+
}
60+
61+
/**
62+
* Get all keys.
63+
*
64+
* @return list<TKey>
65+
*/
66+
public function keys(): array
67+
{
68+
$keys = [];
69+
70+
foreach ($this->items as [$key]) {
71+
$keys[] = $key;
72+
}
73+
74+
return $keys;
75+
}
76+
77+
/**
78+
* Get all values.
79+
*
80+
* @return list<TValue>
81+
*/
82+
public function values(): array
83+
{
84+
$values = [];
85+
86+
foreach ($this->items as [, $value]) {
87+
$values[] = $value;
88+
}
89+
90+
return $values;
91+
}
92+
93+
/**
94+
* {@inheritdoc}
95+
*/
96+
public function getIterator(): \Traversable
97+
{
98+
foreach ($this->items as [$key, $value]) {
99+
yield $key => $value;
100+
}
101+
}
102+
103+
/**
104+
* {@inheritdoc}
105+
*/
106+
public function count(): int
107+
{
108+
return $this->items->count();
109+
}
110+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RunOpenCode\Component\Dataset\Model;
6+
7+
use RunOpenCode\Component\Dataset\Exception\OutOfBoundsException;
8+
9+
/**
10+
* @template TKey
11+
* @template TValue
12+
*
13+
* @implements \ArrayAccess<int, TKey|TValue>
14+
*/
15+
final readonly class Item implements \ArrayAccess
16+
{
17+
/**
18+
* @param TKey $key
19+
* @param TValue $value
20+
*/
21+
public function __construct(
22+
private mixed $key,
23+
private mixed $value
24+
) {
25+
// noop.
26+
}
27+
28+
/**
29+
* Get key.
30+
*
31+
* @return TKey
32+
*/
33+
public function key(): mixed
34+
{
35+
return $this->key;
36+
}
37+
38+
/**
39+
* Get value.
40+
*
41+
* @return TValue
42+
*/
43+
public function value(): mixed
44+
{
45+
return $this->value;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function offsetExists(mixed $offset): bool
52+
{
53+
return $offset === 0 || $offset === 1;
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*
59+
* @return ($offset is 0 ? TKey : TValue)
60+
*/
61+
public function offsetGet(mixed $offset): mixed
62+
{
63+
return match ($offset) {
64+
0 => $this->key,
65+
1 => $this->value,
66+
default => throw new OutOfBoundsException($offset, $this, \sprintf(
67+
'Item tuple does not have offset "%s".',
68+
$offset
69+
)),
70+
};
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function offsetSet(mixed $offset, mixed $value): void
77+
{
78+
throw new \BadMethodCallException('Item is immutable.');
79+
}
80+
81+
/**
82+
* {@inheritdoc}
83+
*/
84+
public function offsetUnset(mixed $offset): void
85+
{
86+
throw new \BadMethodCallException('Item is immutable.');
87+
}
88+
}

src/RunOpenCode/Component/Dataset/src/Operator/Batch.php

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RunOpenCode\Component\Dataset\Operator;
6+
7+
use RunOpenCode\Component\Dataset\AbstractStream;
8+
use RunOpenCode\Component\Dataset\Contract\OperatorInterface;
9+
use RunOpenCode\Component\Dataset\Model\Buffer;
10+
11+
/**
12+
* Buffer count operator.
13+
*
14+
* Iterates over given collection and creates a buffer of items with number of items
15+
* up to given capacity.
16+
*
17+
* Yields created instances of {@see Buffer} for batch processing.
18+
*
19+
* @template TKey
20+
* @template TValue
21+
*
22+
* @extends AbstractStream<int, Buffer<TKey, TValue>>
23+
* @implements OperatorInterface<int, Buffer<TKey, TValue>>
24+
*/
25+
final class BufferCount extends AbstractStream implements OperatorInterface
26+
{
27+
/**
28+
* @param iterable<TKey, TValue> $collection Collection to iterate over.
29+
* @param positive-int $count How many items to buffer.
30+
*/
31+
public function __construct(
32+
private readonly iterable $collection,
33+
private readonly int $count = 1000,
34+
) {
35+
parent::__construct($collection);
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
protected function iterate(): \Traversable
42+
{
43+
/** @var \ArrayObject<int, array{TKey, TValue}> $items */
44+
$items = new \ArrayObject();
45+
46+
foreach ($this->collection as $key => $value) {
47+
$items[] = [$key, $value];
48+
49+
if (\count($items) === $this->count) {
50+
yield new Buffer($items);
51+
$items = new \ArrayObject();
52+
}
53+
}
54+
55+
if (\count($items) !== 0) {
56+
yield new Buffer($items);
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)