-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferedWriter.php
More file actions
213 lines (180 loc) · 4.67 KB
/
BufferedWriter.php
File metadata and controls
213 lines (180 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace Bdf\Prime\Repository\Write;
use Bdf\Prime\Events;
use Bdf\Prime\Exception\PrimeException;
use Bdf\Prime\Query\Contract\WriteOperation;
use Bdf\Prime\Repository\EntityRepository;
use Bdf\Prime\Repository\RepositoryEventsSubscriberInterface;
use Bdf\Prime\Repository\RepositoryInterface;
/**
* Store write operations to be executed at once on flush
* It act as transaction for single repository at domain level
*
* <code>
* $writer = new BufferedWriter($repository, $repository->writer());
*
* // Append operations
* $writer->insert(...);
* $writer->update(...);
* // ...
*
* // Execute all operations
* $writer->flush();
*
* // Cancel all operations
* $writer->clear();
* </code>
*
* Note 1: If there is pending operations, there were flushed by the object destructor
* Note 2: Because operations will be performed later, the WriterInterface methods will always returns 1
*
* @template E as object
* @implements BufferedWriterInterface<E>
*/
class BufferedWriter implements BufferedWriterInterface
{
/**
* @var RepositoryInterface&RepositoryEventsSubscriberInterface
*/
private $repository;
/**
* @var WriterInterface
*/
private $writer;
/**
* @var array
*/
private $insert = [];
/**
* @var array
*/
private $update = [];
/**
* @var array
*/
private $delete = [];
/**
* BufferedWriter constructor.
*
* @param RepositoryEventsSubscriberInterface&RepositoryInterface $repository The owner repository where operation should be performed
* @param WriterInterface|null $writer The base writer. If not provided, will use the repository writer
*/
public function __construct(RepositoryInterface $repository, ?WriterInterface $writer = null)
{
$this->repository = $repository;
$this->writer = $writer ?: $repository->writer();
}
/**
* {@inheritdoc}
*/
public function insert($entity, array $options = []): int
{
$this->insert[] = [$entity, $options];
return 1;
}
/**
* {@inheritdoc}
*/
public function update($entity, array $options = []): int
{
$this->update[] = [$entity, $options];
return 1;
}
/**
* {@inheritdoc}
*/
public function delete($entity, array $options = []): int
{
$this->delete[] = [$entity, $options];
return 1;
}
/**
* {@inheritdoc}
*/
public function pending(): int
{
return count($this->insert) + count($this->update) + count($this->delete);
}
/**
* {@inheritdoc}
*/
#[WriteOperation]
public function flush(): int
{
try {
return $this->flushInsert() + $this->flushUpdate() + $this->flushDelete();
} finally {
$this->clear();
}
}
/**
* {@inheritdoc}
*/
public function clear(): void
{
$this->insert = [];
$this->update = [];
$this->delete = [];
}
/**
* @return int
* @throws PrimeException
*/
private function flushInsert()
{
$count = 0;
foreach ($this->insert as list($entity, $options)) {
$count += $this->writer->insert($entity, $options);
}
return $count;
}
/**
* @return int
* @throws PrimeException
*/
private function flushUpdate()
{
$count = 0;
foreach ($this->update as list($entity, $options)) {
$count += $this->writer->update($entity, $options);
}
return $count;
}
/**
* @return int
* @throws PrimeException
*/
private function flushDelete()
{
/** @var EntityRepository $this->repository */
if (empty($this->delete)) {
return 0;
}
// Do not perform bulk delete for one operation
if (count($this->delete) === 1) {
return $this->writer->delete($this->delete[0][0], $this->delete[0][1]);
}
$toDelete = [];
foreach ($this->delete as list($entity, $options)) {
if ($this->repository->notify(Events::PRE_DELETE, [$entity, $this->repository]) !== false) {
$toDelete[] = $entity;
}
}
if (empty($toDelete)) {
return 0;
}
$count = $this->repository->queries()->entities($toDelete)->delete();
foreach ($toDelete as $entity) {
$this->repository->notify(Events::POST_DELETE, [$entity, $this->repository, $count]);
}
return $count;
}
/**
* Flush on destruct
* @throws PrimeException
*/
public function __destruct()
{
$this->flush();
}
}