Once I had a task to import a number of data from datafeeds (that are in CSV or XML) into MySQL database. See solution in my another post. Here I want to tell about handling large files.
Datafeeds there are 50-200M each, so reading all file at once was not possible due to memory limitations. So we need to split it into portions. Here is the class, that solves the problem of downloading remote file, saving it locally and then reading by portions. Position is saved in $this->Offset:
-
class WebServiceSource implements GenericSource
-
{
-
private $URI;
-
private $TempFile;
-
private $URIHandler;
-
private $TMPHandler;
-
private $Data;
-
private $Offset = 0;
-
private $EOF;
-
-
/**
-
* Size to read from remote host at once, buffer
-
*/
-
const BUFFER_LENGTH = 102400;//1 MB
-
/**
-
* Number of records to read at once, from the local file
-
*/
-
const RECORDS_NUM = 15;
-
-
public function __construct($uri = NULL)
-
{
-
$this->SetURI($uri);
-
$this->EOF = false;
-
}
-
-
private function OpenURI()
-
{
-
if (!$this->URIHandler) throw new ASException(‘WrongDatafeedURL’, ‘DFImport’);
-
}
-
-
private function OpenTempFile($mode = ‘w+’)
-
{
-
if (!$this->TMPHandler) throw new ASException(‘TempFileNotAccessible’, ‘DFImport’);
-
}
-
-
private function CloseURI()
-
{
-
}
-
-
private function CloseTempFile()
-
{
-
}
-
-
private function IsDownloaded()
-
{
-
return true;
-
}
-
-
private function Download()
-
{
-
if ($this->IsDownloaded()) return;
-
// set_time_limit(0);
-
$this->OpenURI();
-
$this->OpenTempFile();
-
{
-
}
-
$this->CloseURI();
-
$this->CloseTempFile();
-
}
-
-
private function GetFragment()
-
{
-
$this->OpenTempFile(‘r’);
-
$RecordsNum = 1;
-
$this->Data = ”;
-
{
-
$this->Data .= $row;
-
$RecordsNum++;
-
}
-
//if number of records read is less then maximum, then we came to the
-
// end of file
-
if ($RecordsNum < self::RECORDS_NUM) $this->EOF = true;
-
$this->CloseTempFile();
-
return $this->Data;
-
}
-
-
public function GetData()
-
{
-
$this->Download();
-
if (!$this->EOF)
-
return $this->GetFragment();
-
return false;
-
}
-
-
public function SetURI($uri)
-
{
-
$this->URI = $uri;
-
}
-
}
And here is an example of how it works:
-
$source = new WebServiceSource($url);
-
while ($res = $this->Source->GetData())
-
{
-
$this->Parser->SetRawData($res);
-
$this->Parser->Parse();
-
}
Related posts:
- Low Coupling and High Cohesion – GRASP (Design patterns series) Low coupling When designing some architecture, you face with the...
Related posts brought to you by Yet Another Related Posts Plugin.
Share this post with a friend











Petya says:
yayawio@gouhuoj.ru” rel=”nofollow”>1…
no more…
June 13, 2010, 18:02