public function finalize()
{
self::saveTokenCounts();
// Load Nodes
$files = glob($this->config->projects_root . '/projects/.exakat/nodes.g3.*.csv');
foreach ($files as $file) {
preg_match('/nodes\\.g3\\.(.*)\\.csv$/', $file, $r);
$atom = $r[1];
$queryTemplate = 'CREATE INDEX ON :' . $atom . '(eid)';
$this->cypher->query($queryTemplate);
$b = microtime(true);
$extra = array();
foreach (Load::$PROP_OPTIONS as $title => $atoms) {
if (in_array($atom, $atoms)) {
if (in_array($title, array('delimiter', 'noDelimiter', 'fullnspath', 'alias', 'origin', 'encoding', 'strval'))) {
// Raw string
$extra[] = "{$title}: csvLine.{$title}";
} elseif (in_array($title, array('alternative', 'heredoc', 'reference', 'variadic', 'absolute', 'enclosing', 'bracket', 'close_tag'))) {
// Boolean
$extra[] = "{$title}: (csvLine.{$title} <> \"\")";
} elseif (in_array($title, array('count', 'intval', 'args_max', 'args_min'))) {
// Integer
$extra[] = "{$title}: toInt(csvLine.{$title})";
} else {
die('Unexpected option in ' . __CLASS__ . ' : "' . $title . '"');
}
}
}
$extra = implode(', ', $extra);
if (!empty($extra)) {
$extra = ',' . $extra;
}
$queryTemplate = <<<CYPHER
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:{$this->config->projects_root}/projects/.exakat/nodes.g3.{$atom}.csv" AS csvLine
CREATE (token:{$atom} {
eid: toInt(csvLine.id),
code: csvLine.code,
fullcode: csvLine.fullcode,
line: toInt(csvLine.line),
token: csvLine.token,
rank: toInt(csvLine.rank)
{$extra}})
CYPHER;
try {
$res = $this->cypher->query($queryTemplate);
if (isset($res->message)) {
print $queryTemplate . "\n";
print_r($res);
die;
}
$this->unlink[] = $file;
$e = microtime(true);
} catch (Exception $e) {
$this->cleanCsv();
die("Couldn't load nodes in the database\n" . $e->getMessage());
}
}
display('Loaded nodes');
// Load relations
$files = glob($this->config->projects_root . '/projects/.exakat/rels.g3.*.csv');
foreach ($files as $file) {
preg_match('/rels\\.g3\\.(.*)\\.(.*)\\.(.*)\\.csv$/', $file, $r);
$edge = $r[1];
$origin = $r[2];
$destination = $r[3];
$b = microtime(true);
$queryTemplate = <<<CYPHER
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:{$this->config->projects_root}/projects/.exakat/rels.g3.{$edge}.{$origin}.{$destination}.csv" AS csvLine
MATCH (token:{$origin} { eid: toInt(csvLine.start)}),(token2:{$destination} { eid: toInt(csvLine.end)})
CREATE (token)-[:{$edge}]->(token2)
CYPHER;
try {
$res = $this->cypher->query($queryTemplate);
if (isset($res->message)) {
print $queryTemplate . "\n";
print_r($res);
die;
}
$this->unlink[] = $file;
$e = microtime(true);
} catch (Exception $e) {
$this->cleanCsv();
die("Couldn't load relations for " . $edge . " in the database\n" . $e->getMessage());
}
}
display('Loaded links');
$this->cleanCsv();
display('Cleaning CSV');
return true;
}