@@ -280,6 +280,14 @@ default List<UUID> findOutputDatasetsFor(UUID jobVersionUuid) {
280280 return findInputOrOutputDatasetsFor (jobVersionUuid , IoType .OUTPUT );
281281 }
282282
283+ /**
284+ * Verifies if a job with a specified job version is present in table.
285+ *
286+ * @param version Version identifier
287+ */
288+ @ SqlQuery ("SELECT EXISTS (SELECT 1 FROM job_versions WHERE version = :version)" )
289+ boolean versionExists (UUID version );
290+
283291 /**
284292 * Returns the input or output datasets for a given job version.
285293 *
@@ -447,98 +455,73 @@ private static ExtendedDatasetVersionRow toExtendedDatasetVersionRow(DatasetReco
447455 * and context. A version for a given job is created <i>only</i> when a {@link Run} transitions
448456 * into a {@code COMPLETED}, {@code ABORTED}, or {@code FAILED} state.
449457 *
450- * @param jobRow The job.
451- * @param runUuid The unique ID of the run associated with the job version.
458+ * @param jobRowRunDetails The job row with run details.
452459 * @param runState The current run state.
453460 * @param transitionedAt The timestamp of the run state transition.
454461 * @return A {@link BagOfJobVersionInfo} object.
455462 */
456463 default BagOfJobVersionInfo upsertJobVersionOnRunTransition (
457- @ NonNull JobRow jobRow ,
458- @ NonNull UUID runUuid ,
464+ @ NonNull JobRowRunDetails jobRowRunDetails ,
459465 @ NonNull RunState runState ,
460- @ NonNull Instant transitionedAt ) {
466+ @ NonNull Instant transitionedAt ,
467+ boolean linkJobToJobVersion ) {
461468 // Get the job.
462469 final JobDao jobDao = createJobDao ();
463470
464- // Get the inputs and outputs dataset versions for the run associated with the job version.
465- final DatasetVersionDao datasetVersionDao = createDatasetVersionDao ();
466- final List <ExtendedDatasetVersionRow > jobVersionInputs =
467- datasetVersionDao .findInputDatasetVersionsFor (runUuid );
468- final List <ExtendedDatasetVersionRow > jobVersionOutputs =
469- datasetVersionDao .findOutputDatasetVersionsFor (runUuid );
470-
471- // Get the namespace for the job.
472- final NamespaceRow namespaceRow =
473- createNamespaceDao ().findNamespaceByName (jobRow .getNamespaceName ()).get ();
474-
475- // Generate the version for the job; the version may already exist.
476- final Version jobVersion =
477- Utils .newJobVersionFor (
478- NamespaceName .of (jobRow .getNamespaceName ()),
479- JobName .of (
480- Optional .ofNullable (jobRow .getParentJobName ())
481- .map (pn -> pn + "." + jobRow .getSimpleName ())
482- .orElse (jobRow .getName ())),
483- toDatasetIds (
484- jobVersionInputs .stream ()
485- .map (i -> (DatasetVersionRow ) i )
486- .collect (Collectors .toList ())),
487- toDatasetIds (
488- jobVersionOutputs .stream ()
489- .map (o -> (DatasetVersionRow ) o )
490- .collect (Collectors .toList ())),
491- jobRow .getLocation ());
492-
493471 // Add the job version.
494472 final JobVersionDao jobVersionDao = createJobVersionDao ();
473+
495474 final JobVersionRow jobVersionRow =
496475 jobVersionDao .upsertJobVersion (
497476 UUID .randomUUID (),
498477 transitionedAt , // Use the timestamp of when the run state transitioned.
499- jobRow .getUuid (),
500- jobRow .getLocation (),
501- jobVersion .getValue (),
502- jobRow .getName (),
503- namespaceRow .getUuid (),
504- jobRow .getNamespaceName ());
478+ jobRowRunDetails . jobRow .getUuid (),
479+ jobRowRunDetails . jobRow .getLocation (),
480+ jobRowRunDetails . jobVersion .getValue (),
481+ jobRowRunDetails . jobRow .getName (),
482+ jobRowRunDetails . namespaceRow .getUuid (),
483+ jobRowRunDetails . jobRow .getNamespaceName ());
505484
506485 // Link the input datasets to the job version.
507- jobVersionInputs .forEach (
486+ jobRowRunDetails . jobVersionInputs .forEach (
508487 jobVersionInput -> {
509488 jobVersionDao .upsertInputDatasetFor (
510489 jobVersionRow .getUuid (),
511490 jobVersionInput .getDatasetUuid (),
512491 jobVersionRow .getJobUuid (),
513- jobRow .getSymlinkTargetId ());
492+ jobRowRunDetails . jobRow .getSymlinkTargetId ());
514493 });
515494
516495 // Link the output datasets to the job version.
517- jobVersionOutputs .forEach (
496+ jobRowRunDetails . jobVersionOutputs .forEach (
518497 jobVersionOutput -> {
519498 jobVersionDao .upsertOutputDatasetFor (
520499 jobVersionRow .getUuid (),
521500 jobVersionOutput .getDatasetUuid (),
522501 jobVersionRow .getJobUuid (),
523- jobRow .getSymlinkTargetId ());
502+ jobRowRunDetails . jobRow .getSymlinkTargetId ());
524503 });
525504
526505 // Link the job version to the run.
527- createRunDao ().updateJobVersion (runUuid , jobVersionRow .getUuid ());
506+ createRunDao ().updateJobVersion (jobRowRunDetails . runUuid , jobVersionRow .getUuid ());
528507
529508 // Link the run to the job version; multiple run instances may be linked to a job version.
530- jobVersionDao .updateLatestRunFor (jobVersionRow .getUuid (), transitionedAt , runUuid );
509+ jobVersionDao .updateLatestRunFor (
510+ jobVersionRow .getUuid (), transitionedAt , jobRowRunDetails .runUuid );
531511
532512 // Link the job facets to this job version
533- jobVersionDao .linkJobFacetsToJobVersion (runUuid , jobVersionRow .getUuid ());
513+ jobVersionDao .linkJobFacetsToJobVersion (jobRowRunDetails . runUuid , jobVersionRow .getUuid ());
534514
535- // Link the job version to the job only if the run is marked done and has transitioned into one
536- // of the following states: COMPLETED, ABORTED, or FAILED.
537- if (runState .isDone ()) {
538- jobDao .updateVersionFor (jobRow .getUuid (), transitionedAt , jobVersionRow .getUuid ());
515+ if (linkJobToJobVersion ) {
516+ jobDao .updateVersionFor (
517+ jobRowRunDetails .jobRow .getUuid (), transitionedAt , jobVersionRow .getUuid ());
539518 }
540519
541- return new BagOfJobVersionInfo (jobRow , jobVersionRow , jobVersionInputs , jobVersionOutputs );
520+ return new BagOfJobVersionInfo (
521+ jobRowRunDetails .jobRow ,
522+ jobVersionRow ,
523+ jobRowRunDetails .jobVersionInputs ,
524+ jobRowRunDetails .jobVersionOutputs );
542525 }
543526
544527 /** Returns the specified {@link ExtendedDatasetVersionRow}s as {@link DatasetId}s. */
@@ -556,6 +539,40 @@ private DatasetId toDatasetId(DatasetVersionRow dataset) {
556539 NamespaceName .of (dataset .getNamespaceName ()), DatasetName .of (dataset .getDatasetName ()));
557540 }
558541
542+ default JobRowRunDetails loadJobRowRunDetails (JobRow jobRow , UUID runUuid ) {
543+ // Get the inputs and outputs dataset versions for the run associated with the job version.
544+ final DatasetVersionDao datasetVersionDao = createDatasetVersionDao ();
545+ final List <ExtendedDatasetVersionRow > jobVersionInputs =
546+ datasetVersionDao .findInputDatasetVersionsFor (runUuid );
547+ final List <ExtendedDatasetVersionRow > jobVersionOutputs =
548+ datasetVersionDao .findOutputDatasetVersionsFor (runUuid );
549+
550+ // Get the namespace for the job.
551+ final NamespaceRow namespaceRow =
552+ createNamespaceDao ().findNamespaceByName (jobRow .getNamespaceName ()).get ();
553+
554+ // Generate the version for the job; the version may already exist.
555+ final Version jobVersion =
556+ Utils .newJobVersionFor (
557+ NamespaceName .of (jobRow .getNamespaceName ()),
558+ JobName .of (
559+ Optional .ofNullable (jobRow .getParentJobName ())
560+ .map (pn -> pn + "." + jobRow .getSimpleName ())
561+ .orElse (jobRow .getName ())),
562+ toDatasetIds (
563+ jobVersionInputs .stream ()
564+ .map (i -> (DatasetVersionRow ) i )
565+ .collect (Collectors .toList ())),
566+ toDatasetIds (
567+ jobVersionOutputs .stream ()
568+ .map (o -> (DatasetVersionRow ) o )
569+ .collect (Collectors .toList ())),
570+ jobRow .getLocation ());
571+
572+ return new JobRowRunDetails (
573+ jobRow , runUuid , namespaceRow , jobVersionInputs , jobVersionOutputs , jobVersion );
574+ }
575+
559576 /** A container class for job version info. */
560577 @ Value
561578 class BagOfJobVersionInfo {
@@ -567,6 +584,14 @@ class BagOfJobVersionInfo {
567584
568585 record JobDataset (String namespace , String name , IoType ioType ) {}
569586
587+ record JobRowRunDetails (
588+ JobRow jobRow ,
589+ UUID runUuid ,
590+ NamespaceRow namespaceRow ,
591+ List <ExtendedDatasetVersionRow > jobVersionInputs ,
592+ List <ExtendedDatasetVersionRow > jobVersionOutputs ,
593+ Version jobVersion ) {}
594+
570595 class JobDatasetMapper implements RowMapper <JobDataset > {
571596 @ Override
572597 public JobDataset map (ResultSet rs , StatementContext ctx ) throws SQLException {
0 commit comments