<commit_before><?php

namespace JPC\MongoDB\ODM\Query;

use JPC\MongoDB\ODM\DocumentManager;
use JPC\MongoDB\ODM\Query\Query;
use JPC\MongoDB\ODM\Repository;

class BulkWrite extends Query
{

    protected $queries;

    public function __construct(DocumentManager $dm, Repository $repository, array $queries = [])
    {
        parent::__construct($dm, $repository, null);
        $this->queries = $queries;
    }

    public function addQuery(Query $query)
    {
        if (defined('BLA')) {
            dump($this->repository->getCollection()->getCollectionName());
            dump(get_class($query));
            dump($query->getDocument()['_id']);
        }
        $this->queries[] = $query;
    }

    public function getType()
    {
        return self::TYPE_BULK_WRITE;
    }

    public function beforeQuery()
    {
        foreach ($this->queries as $query) {
            $query->beforeQuery();
        }
    }

    public function perfomQuery(&$result)
    {
        $operations = [];
        foreach ($this->queries as $query) {
            switch ($query->getType()) {
                case self::TYPE_INSERT_ONE:
                    $operations[] = [$query->getType() => [$query->getDocument()]];
                    break;
                case self::TYPE_UPDATE_ONE:
                    $operations[] = [$query->getType() => [$query->getFilters(), $query->getUpdate(), $query->getOptions()]];
                    break;
                case self::TYPE_DELETE_ONE:
                    $operations[] = [$query->getType() => [$query->getFilters(), $query->getOptions()]];
                    break;
                default:
                    throw new \Exception('Not supported operation type \'' . $query->getType() . '\'');
            }
        }

        $result = $this->repository->getCollection()->bulkWrite($operations);
        return $result->isAcknowledged() || $this->repository->getCollection()->getWriteConcern()->getW() === 0;<commit_msg>Fix bug when bulk write operations is empty<commit_after><?php

namespace JPC\MongoDB\ODM\Query;

use JPC\MongoDB\ODM\DocumentManager;
use JPC\MongoDB\ODM\Query\Query;
use JPC\MongoDB\ODM\Repository;

class BulkWrite extends Query
{

    protected $queries;

    public function __construct(DocumentManager $dm, Repository $repository, array $queries = [])
    {
        parent::__construct($dm, $repository, null);
        $this->queries = $queries;
    }

    public function addQuery(Query $query)
    {
        $this->queries[] = $query;
    }

    public function getType()
    {
        return self::TYPE_BULK_WRITE;
    }

    public function beforeQuery()
    {
        foreach ($this->queries as $query) {
            $query->beforeQuery();
        }
    }

    public function perfomQuery(&$result)
    {
        $operations = [];
        foreach ($this->queries as $query) {
            switch ($query->getType()) {
                case self::TYPE_INSERT_ONE:
                    $operations[] = [$query->getType() => [$query->getDocument()]];
                    break;
                case self::TYPE_UPDATE_ONE:
                    $operations[] = [$query->getType() => [$query->getFilters(), $query->getUpdate(), $query->getOptions()]];
                    break;
                case self::TYPE_DELETE_ONE:
                    $operations[] = [$query->getType() => [$query->getFilters(), $query->getOptions()]];
                    break;
                default:
                    throw new \Exception('Not supported operation type \'' . $query->getType() . '\'');
            }
        }

        if (empty($operations)) {
            return true;
        }

        $result = $this->repository->getCollection()->bulkWrite($operations);
        return $result->isAcknowledged() || $this->repository->getCollection()->getWriteConcern()->getW() === 0;
{'fraction_non_alphanumeric': 0.15372589890568003, 'fraction_numerical': 0.0005211047420531526, 'mean_word_length': 2.1009693053311795, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1968026', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1048, 'n_words': 199}