Skip to content

Commit

Permalink
Add batchSize to saveAll / destroyAll (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
dplewis authored Nov 30, 2018
1 parent 1c73a1c commit 9fb3ce5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Parse/ParseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -797,16 +797,16 @@ public function destroy($useMasterKey = false)
*
* @param array $objects Objects to destroy.
* @param bool $useMasterKey Whether to use the master key or not.
* @param int $batchSize Number of objects to process per request
*
* @throws ParseAggregateException
*/
public static function destroyAll(array $objects, $useMasterKey = false)
public static function destroyAll(array $objects, $useMasterKey = false, $batchSize = 40)
{
$errors = [];
$objects = array_values($objects); // To support non-ordered arrays
$count = count($objects);
if ($count) {
$batchSize = 40;
$processed = 0;
$currentBatch = [];
$currentcount = 0;
Expand Down Expand Up @@ -1153,23 +1153,25 @@ public function save($useMasterKey = false)
*
* @param array $list
* @param bool $useMasterKey Whether to use the Master Key.
* @param int $batchSize Number of objects to process per request
*/
public static function saveAll($list, $useMasterKey = false)
public static function saveAll($list, $useMasterKey = false, $batchSize = 40)
{
static::deepSave($list, $useMasterKey);
static::deepSave($list, $useMasterKey, $batchSize);
}

/**
* Save object and unsaved children within.
*
* @param ParseObject|array $target
* @param bool $useMasterKey Whether to use the Master Key.
* @param int $batchSize Number of objects to process per request
*
* @throws Exception
* @throws ParseAggregateException
* @throws ParseException
*/
private static function deepSave($target, $useMasterKey = false)
private static function deepSave($target, $useMasterKey = false, $batchSize = 40)
{
$unsavedChildren = [];
$unsavedFiles = [];
Expand Down Expand Up @@ -1197,7 +1199,7 @@ private static function deepSave($target, $useMasterKey = false)
$newRemaining = [];

foreach ($remaining as $key => &$object) {
if (count($batch) > 40) {
if (count($batch) > $batchSize) {
$newRemaining[] = $object;
continue;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/Parse/ParseObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,30 @@ public function testDestroyAll()
$user->destroy(true);
}

public function testBatchSize()
{
$batchSize = 1;
Helper::clearClass('TestObject');

// log in
$user = new ParseUser();
$user->setUsername('username123');
$user->setPassword('password123');
$user->signUp();

$o1 = ParseObject::create('TestObject');
$o2 = ParseObject::create('TestObject');
$o3 = ParseObject::create('TestObject');
ParseObject::saveAll([$o1, $o2, $o3], true, $batchSize);
ParseObject::destroyAll([$o1, $o2, $o3], true, $batchSize);
$query = new ParseQuery('TestObject');
$results = $query->find();
$this->assertEquals(0, count($results));

ParseUser::logOut();
$user->destroy(true);
}

public function testEmptyArray()
{
$obj = ParseObject::create('TestObject');
Expand Down

0 comments on commit 9fb3ce5

Please sign in to comment.