Skip to content

Commit

Permalink
php 7.2 bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nticaric committed Dec 18, 2017
1 parent 525d607 commit 06d68e1
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/Indexer/TNTIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class TNTIndexer
public $inMemory = true;
public $steps = 1000;
public $indexName = "";
public $statementsPrepared = false;

public function __construct()
{
Expand Down Expand Up @@ -147,6 +148,16 @@ public function setFileReader($filereader)
$this->filereader = $filereader;
}

public function prepareStatementsForIndex()
{
if (!$this->statementsPrepared) {
$this->insertWordlistStmt = $this->index->prepare("INSERT INTO wordlist (term, num_hits, num_docs) VALUES (:keyword, :hits, :docs)");
$this->selectWordlistStmt = $this->index->prepare("SELECT * FROM wordlist WHERE term like :keyword LIMIT 1");
$this->updateWordlistStmt = $this->index->prepare("UPDATE wordlist SET num_docs = num_docs + :docs, num_hits = num_hits + :hits WHERE term = :keyword");
$this->statementsPrepared = true;
}
}

/**
* @param string $indexName
*
Expand Down Expand Up @@ -413,6 +424,7 @@ public function decodeHtmlEntities($value = true)

public function saveToIndex($stems, $docId)
{
$this->prepareStatementsForIndex();
$terms = $this->saveWordlist($stems);
$this->saveDoclist($terms, $docId);
$this->saveHitList($stems, $docId, $terms);
Expand Down Expand Up @@ -441,37 +453,33 @@ public function saveWordlist($stems)
}
});

$insertStmt = $this->index->prepare("INSERT INTO wordlist (term, num_hits, num_docs) VALUES (:keyword, :hits, :docs)");
$selectStmt = $this->index->prepare("SELECT * FROM wordlist WHERE term like :keyword LIMIT 1");
$updateStmt = $this->index->prepare("UPDATE wordlist SET num_docs = num_docs + :docs, num_hits = num_hits + :hits WHERE term = :keyword");

foreach ($terms as $key => $term) {
try {
$insertStmt->bindParam(":keyword", $key);
$insertStmt->bindParam(":hits", $term['hits']);
$insertStmt->bindParam(":docs", $term['docs']);
$insertStmt->execute();
$this->insertWordlistStmt->bindParam(":keyword", $key);
$this->insertWordlistStmt->bindParam(":hits", $term['hits']);
$this->insertWordlistStmt->bindParam(":docs", $term['docs']);
$this->insertWordlistStmt->execute();

$terms[$key]['id'] = $this->index->lastInsertId();
if ($this->inMemory) {
$this->inMemoryTerms[$key] = $terms[$key]['id'];
}
} catch (\Exception $e) {
if ($e->getCode() == 23000) {
$updateStmt->bindValue(':docs', $term['docs']);
$updateStmt->bindValue(':hits', $term['hits']);
$updateStmt->bindValue(':keyword', $key);
$updateStmt->execute();
$this->updateWordlistStmt->bindValue(':docs', $term['docs']);
$this->updateWordlistStmt->bindValue(':hits', $term['hits']);
$this->updateWordlistStmt->bindValue(':keyword', $key);
$this->updateWordlistStmt->execute();
if (!$this->inMemory) {
$selectStmt->bindValue(':keyword', $key);
$selectStmt->execute();
$res = $selectStmt->fetch(PDO::FETCH_ASSOC);
$this->selectWordlistStmt->bindValue(':keyword', $key);
$this->selectWordlistStmt->execute();
$res = $this->selectWordlistStmt->fetch(PDO::FETCH_ASSOC);
$terms[$key]['id'] = $res['id'];
} else {
$terms[$key]['id'] = $this->inMemoryTerms[$key];
}
} else {
echo $e->getMessage()."\n";
echo "Error while saving wordlist: ".$e->getMessage()."\n";
}
}
}
Expand Down

0 comments on commit 06d68e1

Please sign in to comment.