add activate document versions
This commit is contained in:
@@ -5,12 +5,15 @@ namespace App\Service;
|
||||
use App\Entity\Document;
|
||||
use App\Entity\DocumentVersion;
|
||||
use App\Entity\User;
|
||||
use App\Ingest\IngestFlow;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class DocumentService
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em
|
||||
private EntityManagerInterface $em,
|
||||
private LockService $lockService,
|
||||
private IngestFlow $ingestFlow,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -70,20 +73,49 @@ class DocumentService
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktiviert eine Version (setzt andere inaktiv)
|
||||
* Aktiviert eine Version (setzt andere inaktiv) und aktualisiert den Index.
|
||||
*
|
||||
* Beim Aktivieren wird deterministisch sichergestellt, dass nur diese
|
||||
* Version im Index vorhanden ist:
|
||||
* - alle Chunks des Dokuments werden aus index.ndjson entfernt (streaming compaction)
|
||||
* - die aktive Version wird neu ge-chunkt und appended
|
||||
* - FAISS wird vollständig aus index.ndjson neu gebaut
|
||||
*/
|
||||
public function activateVersion(DocumentVersion $version): void
|
||||
{
|
||||
$document = $version->getDocument();
|
||||
|
||||
foreach ($document->getVersions() as $existingVersion) {
|
||||
$existingVersion->setActive(false);
|
||||
if (!$this->lockService->acquire()) {
|
||||
throw new \RuntimeException('Another ingest job is already running.');
|
||||
}
|
||||
|
||||
$version->setActive(true);
|
||||
$document->setCurrentVersion($version);
|
||||
try {
|
||||
$document = $version->getDocument();
|
||||
|
||||
$this->em->flush();
|
||||
// 1) Aktiv-Status in DB konsistent setzen (genau 1 aktive Version)
|
||||
foreach ($document->getVersions() as $existingVersion) {
|
||||
$existingVersion->setActive(false);
|
||||
}
|
||||
|
||||
$version->setActive(true);
|
||||
$document->setCurrentVersion($version);
|
||||
|
||||
// 2) Ingest-Status (UI) – wird im Fehlerfall auf FAILED gesetzt
|
||||
$version->setIngestStatus(DocumentVersion::INGEST_RUNNING);
|
||||
$this->em->flush();
|
||||
|
||||
// 3) Deterministischer Re-Ingest: alte Chunks raus, neue rein, FAISS rebuild
|
||||
$this->ingestFlow->ingestDocumentVersion($version);
|
||||
|
||||
$version->setIngestStatus(DocumentVersion::INGEST_INDEXED);
|
||||
$this->em->flush();
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
// Aktivierung bleibt in DB bestehen, aber Index ist ggf. nicht aktuell → Status markieren
|
||||
$version->setIngestStatus(DocumentVersion::INGEST_FAILED);
|
||||
$this->em->flush();
|
||||
throw $e;
|
||||
} finally {
|
||||
$this->lockService->release();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,4 +152,4 @@ class DocumentService
|
||||
|
||||
return $max + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user