• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/location/import/import.php
  • Класс: BitrixSaleLocationImportImportProcess
  • Вызов: ImportProcess::stageRebalanceWalkTree
protected function stageRebalanceWalkTree()
{
	if(!isset($this->data['rebalance']['queue']))
	{
		$this->restoreIndexes('IX_B_SALE_LOC_PARENT');

		$this->logMessage('initialize Queue');

		$this->data['rebalance']['margin'] = -1;
		$this->data['processed'] = 0;
		$this->data['rebalance']['queue'] = array(array('I' => 'root', 'D' => 0));

		$tableName = LocationLocationTable::getTableName();
		$res = MainHttpApplication::getConnection()->query("select count(*) as CNT from {$tableName}")->fetch();

		$this->data['rebalance']['cnt'] = intval($res['CNT']);
	}

	$i = -1;
	while(!empty($this->data['rebalance']['queue']) && $this->checkQuota())
	{
		$i++;

		$node =& $this->data['rebalance']['queue'][0];

		if(isset($node['L']))
		{
			// we have already been here
			array_shift($this->data['rebalance']['queue']);
			if($node['I'] != 'root') // we dont need for ROOT item in outgoing
			{
				$this->acceptRebalancedNode(array(
					'I' => $node['I'],
					'D' => $node['D'],
					'L' => $node['L'],
					'R' => ++$this->data['rebalance']['margin']
				));
			}
			else
				$this->data['rebalance']['margin']++;
		}
		else
		{
			$a = $this->getCachedBundle($node['I']);

			if(!empty($a))
			{
				// go deeper
				$node['L'] = ++$this->data['rebalance']['margin'];

				foreach($a as $id)
				{
					if($this->checkNodeIsParent($id))
					{
						array_unshift($this->data['rebalance']['queue'], array('I' => $id, 'D' => $node['D'] + 1));
					}
					else // we dont need to put it to the query
					{
						$this->acceptRebalancedNode(array(
							'I' => $id,
							'D' => $node['D'] + 1,
							'L' => ++$this->data['rebalance']['margin'],
							'R' => ++$this->data['rebalance']['margin']
						));
					}
				}
			}
			else
			{
				array_shift($this->data['rebalance']['queue']);
				$this->acceptRebalancedNode(array(
					'I' => $node['I'],
					'D' => $node['D'],
					'L' => ++$this->data['rebalance']['margin'],
					'R' => ++$this->data['rebalance']['margin']
				));
			}
		}
	}

	$this->logMessage('Q size is '.count($this->data['rebalance']['queue']).' already processed: '.$this->data['processed'].'/'.$this->data['rebalance']['cnt']);
	$this->logMemoryUsage();

	if(empty($this->data['rebalance']['queue']))
	{
		// last flush & then merge
		$this->mergeRebalancedNodes();

		$this->nextStage();
		return;
	}

	$this->rebalanceInserter->flush();

	$this->nextStep();
}