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

Add batchSize to saveAll / destroyAll #422

Merged
merged 1 commit into from
Nov 30, 2018
Merged
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
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