private function restoreData(RestoreJob $job, JobStats $jobStats, OutputInterface $output)
 {
     $memoryAtSection = memory_get_usage();
     $timeStartSection = microtime(true);
     $output->writeln('<info>*** Starting with data restoring ***</info>' . PHP_EOL);
     $storedStats = array();
     /** @var Index $index */
     foreach ($job->getMappings()->getIndices() as $index) {
         $toIndex = null;
         /** @var Type $type */
         foreach ($index->getTypes() as $type) {
             /** @var Finder $finder */
             $finder = $this->filesystem->loadDataFiles($job->getPath(), $index->getName(), $type->getName());
             if (null === $finder) {
                 continue;
             }
             if (!isset($storedStats[$index->getName()])) {
                 $storedStats[$index->getName()] = array();
             }
             if (!isset($storedStats[$index->getName()][$type->getName()])) {
                 $storedStats[$index->getName()][$type->getName()] = array('filesRead' => 0, 'dataInType' => 0);
             }
             $mappingAction = $job->getStrategy()->getMapping($index->getName(), $type->getName());
             if (null === $mappingAction) {
                 throw new \Exception('Mapping action missing for "' . $index->getName() . '/' . $type->getName() . '"');
             }
             $docCount = $finder->count();
             $output->writeln('<comment>Store Data for:</comment>');
             $output->writeln('<comment> [from]' . $index->getName() . '/' . $type->getName() . '</comment>');
             $output->writeln('<comment> [to]' . $mappingAction->getTargetIndex() . '/' . $mappingAction->getTargetType() . '</comment>' . PHP_EOL);
             $progress = new ProgressBar($output, $docCount);
             $progress->setFormat('debug');
             $progress->display();
             $progress->start();
             /** @var SplFileInfo $file */
             foreach ($finder as $file) {
                 $data = json_decode($file->getContents(), true);
                 $id = $data['_id'];
                 $this->elastic->createDocument($mappingAction->getTargetIndex(), $mappingAction->getTargetType(), $id, $data['_source'], $job->getHost(), $job->getPort());
                 $progress->advance();
                 $storedStats[$index->getName()][$type->getName()]['filesRead']++;
             }
             $progress->finish();
             $output->writeln(PHP_EOL);
             $toIndex = $mappingAction->getTargetIndex();
         }
         $this->elastic->refreshIndex($toIndex, $job->getHost(), $job->getPort());
     }
     //add aggregated to storedStats for comparing later
     $docStats = $this->elastic->getDocCountByIndexType($job->getHost(), $job->getPort());
     foreach ($storedStats as $indexName => $types) {
         foreach ($types as $typeName => $typeStats) {
             $storedStats[$indexName][$typeName]['dataInType'] = $docStats->getDocCount($indexName, $typeName);
         }
     }
     $jobStats->setRestoreData(microtime(true) - $timeStartSection, memory_get_usage(), memory_get_usage() - $memoryAtSection, array('storedStats' => $storedStats));
     return $storedStats;
 }
 public function testRefreshIndexWithExceptionForVersion()
 {
     $version = '0.90.1';
     $serverInfoData = $this->getServerInfoData($version);
     $index = 'my-index';
     $this->serverInfoResponse->expects($this->exactly(3))->method('getData')->willReturn($serverInfoData);
     $this->requestFactory->expects($this->never())->method('create');
     $this->client->expects($this->once())->method('send')->with($this->isInstanceOf('Elastification\\Client\\Request\\V1x\\NodeInfoRequest'))->willReturn($this->serverInfoResponse);
     try {
         $this->repository->refreshIndex($index, self::HOST, self::PORT);
     } catch (\Exception $exception) {
         $this->assertSame('Elasticsearch version ' . $version . ' is not supported by this tool', $exception->getMessage());
         return;
     }
     $this->fail();
 }
 /**
  * Stores data into the filesystem and returns stored stats
  *
  * @param BackupJob $job
  * @param JobStats $jobStats
  * @param OutputInterface $output
  * @return array
  * @author Daniel Wendlandt
  */
 private function storeData(BackupJob $job, JobStats $jobStats, OutputInterface $output)
 {
     $memoryAtSection = memory_get_usage();
     $timeStartSection = microtime(true);
     $output->writeln('<info>*** Starting with data storing ***</info>' . PHP_EOL);
     $docCount = $this->elastic->getDocCountByIndexType($job->getHost(), $job->getPort());
     $storedStats = array();
     /** @var Index $index */
     foreach ($job->getMappings()->getIndices() as $index) {
         if (0 === $docCount->getDocCount($index->getName())) {
             continue;
         }
         /** @var Type $type */
         foreach ($index->getTypes() as $type) {
             $docsInType = $docCount->getDocCount($index->getName(), $type->getName());
             if (0 === $docsInType) {
                 continue;
             }
             $scrollId = $this->elastic->createScrollSearch($index->getName(), $type->getName(), $job->getHost(), $job->getPort());
             $storedStats[$index->getName()][$type->getName()]['aggregatedNumberOfDocs'] = $docsInType;
             $storedStats[$index->getName()][$type->getName()]['storedNumberOfDocs'] = 0;
             $output->writeln('<comment>Store Data for: ' . $index->getName() . '/' . $type->getName() . '</comment>');
             $progress = new ProgressBar($output, $docsInType);
             $progress->setFormat('debug');
             $progress->display();
             $progress->start();
             while (true) {
                 $data = $this->elastic->getScrollSearchData($scrollId, $job->getHost(), $job->getPort());
                 if (0 === count($data['hits'])) {
                     break;
                 }
                 $scrollId = $data['scrollId'];
                 $storedDocs = $this->filesystem->storeData($job->getPath(), $index->getName(), $type->getName(), $data['hits']);
                 $storedStats[$index->getName()][$type->getName()]['storedNumberOfDocs'] += $storedDocs;
                 $progress->advance($storedDocs);
             }
             $progress->finish();
             $output->writeln(PHP_EOL);
         }
     }
     $jobStats->setStoreData(microtime(true) - $timeStartSection, memory_get_usage(), memory_get_usage() - $memoryAtSection, array('stats' => $storedStats));
     return $storedStats;
 }