Skip to content

Commit b22d142

Browse files
committed
Add autocommit after N operations when using Bulk
1 parent f15fad7 commit b22d142

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/Classes/Bulk.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,24 @@ class Bulk
4747
*/
4848
public $operationCount = 0;
4949

50+
/**
51+
* Operation count which will trigger autocommit
52+
* @var int
53+
*/
54+
public $autocommitAfter = 0;
55+
5056

5157
/**
5258
* Bulk constructor.
5359
* @param Query $query
60+
* @param int $autocommitAfter
5461
*/
55-
public function __construct(Query $query)
62+
public function __construct(Query $query, $autocommitAfter = 0)
5663
{
64+
5765
$this->query = $query;
66+
$this->autocommitAfter = intval($autocommitAfter);
67+
5868
}
5969

6070
/**
@@ -186,6 +196,10 @@ public function action($actionType, $data = [])
186196
$this->operationCount++;
187197

188198
$this->reset();
199+
200+
if ($this->autocommitAfter > 0 && $this->operationCount >= $this->autocommitAfter) {
201+
$this->commit();
202+
}
189203
}
190204

191205
/**
@@ -217,9 +231,10 @@ public function reset()
217231
public function commit()
218232
{
219233

220-
$this->query->connection->bulk($this->body);
234+
$result = $this->query->connection->bulk($this->body);
221235
$this->operationCount = 0;
222236
$this->body = [];
223237

238+
return $result;
224239
}
225240
}

0 commit comments

Comments
 (0)