/** * For the file of March 8th 2010 (blz_20100308.txt) * Bundesbank appended new banks at the end of the file. * That broked binary search. This method sorts the lines so * that binary search is working again. * * Be aware that this needs some amount of memory. * * @param String $file * @throws DataBackendIOException * @throws FileException */ private function sortFile($file) { //read the unordered bank file $lines = file($file); if (!is_array($lines) || empty($lines)) { throw new DataBackendIOException("Could not read lines in '{$file}'."); } //build a sorted index for the bankIDs $index = array(); foreach ($lines as $line => $data) { $bankID = substr($data, FileParser::BANKID_OFFSET, FileParser::BANKID_LENGTH); $index[$line] = $bankID; } asort($index); //write a sorted bank file atomically $temp = tempnam($this->fileUtil->getTempDirectory(), ""); $tempH = fopen($temp, 'w'); if (!($temp && $tempH)) { throw new DataBackendIOException("Could not open a temporary file."); } foreach (array_keys($index) as $line) { $data = $lines[$line]; $writtenBytes = fputs($tempH, $data); if ($writtenBytes != strlen($data)) { throw new DataBackendIOException("Could not write sorted data: '{$data}' into {$temp}."); } } fclose($tempH); $this->fileUtil->safeRename($temp, $file); }
public function update() { $this->em->transactional(function (EntityManager $em) { // Download data $fileUtil = new FileUtil(); $fileBackend = new FileDataBackend(tempnam($fileUtil->getTempDirectory(), 'bav')); $fileBackend->install(); // Delete all $em->createQuery("DELETE FROM malkusch\\bav\\Agency")->execute(); $em->createQuery("DELETE FROM malkusch\\bav\\Bank")->execute(); // Inserting data foreach ($fileBackend->getAllBanks() as $bank) { try { $em->persist($bank); $agencies = $bank->getAgencies(); $agencies[] = $bank->getMainAgency(); foreach ($agencies as $agency) { $em->persist($agency); } } catch (NoMainAgencyException $e) { trigger_error("Skipping bank {$e->getBank()->getBankID()} without any main agency."); } } // last modified $lastModified = $em->find("malkusch\\bav\\MetaData", MetaData::LASTMODIFIED); if ($lastModified == null) { $lastModified = new MetaData(); } $lastModified->setName(MetaData::LASTMODIFIED); $lastModified->setValue(time()); $em->persist($lastModified); }); }