func (s *StateStore) upsertJobVersion(index uint64, job *structs.Job, txn *memdb.Txn) error {
	// Insert the job
	if err := txn.Insert("job_version", job); err != nil {
		return fmt.Errorf("failed to insert job into job_version table: %v", err)
	}

	if err := txn.Insert("index", &IndexEntry{"job_version", index}); err != nil {
		return fmt.Errorf("index update failed: %v", err)
	}

	// Get all the historic jobs for this ID
	all, err := s.jobVersionByID(txn, nil, job.ID)
	if err != nil {
		return fmt.Errorf("failed to look up job versions for %q: %v", job.ID, err)
	}

	// If we are below the limit there is no GCing to be done
	if len(all) <= structs.JobTrackedVersions {
		return nil
	}

	// We have to delete a historic job to make room.
	// Find index of the highest versioned stable job
	stableIdx := -1
	for i, j := range all {
		if j.Stable {
			stableIdx = i
			break
		}
	}

	// If the stable job is the oldest version, do a swap to bring it into the
	// keep set.
	max := structs.JobTrackedVersions
	if stableIdx == max {
		all[max-1], all[max] = all[max], all[max-1]
	}

	// Delete the job outside of the set that are being kept.
	d := all[max]
	if err := txn.Delete("job_version", d); err != nil {
		return fmt.Errorf("failed to delete job %v (%d) from job_version", d.ID, d.Version)
	}

	return nil
}
// upsertJobVersion inserts a job into its historic version table and limits the
// number of job versions that are tracked.
upsertJobVersion inserts a job into its historic version table and limits the
number of job versions that are tracked.
// upsertJobVersion inserts a job into its historic version table and limits the // number of job versions that are tracked .
func (s *StateStore) upsertJobVersion(index uint64, job *structs.Job, txn *memdb.Txn) error {
	if err := txn.Insert("job_version", job); err != nil {
		return fmt.Errorf("failed to insert job into job_version table: %v", err)
	}
	if err := txn.Insert("index", &IndexEntry{"job_version", index}); err != nil {
		return fmt.Errorf("index update failed: %v", err)
	}
	all, err := s.jobVersionByID(txn, nil, job.ID)
	if err != nil {
		return fmt.Errorf("failed to look up job versions for %q: %v", job.ID, err)
	}
	if len(all) <= structs.JobTrackedVersions {
		return nil
	}
	stableIdx := -1
	for i, j := range all {
		if j.Stable {
			stableIdx = i
			break
		}
	}
	max := structs.JobTrackedVersions
	if stableIdx == max {
		all[max-1], all[max] = all[max], all[max-1]
	}
	d := all[max]
	if err := txn.Delete("job_version", d); err != nil {
		return fmt.Errorf("failed to delete job %v (%d) from job_version", d.ID, d.Version)
	}
	return nil
}
func ( s * StateStore ) upsertJobVersion ( index uint64 , job * structs . Job , txn * memdb . Txn ) error { if err := txn . Insert ( " " , job ) ; err != nil { return fmt . Errorf ( " " , err ) } if err := txn . Insert ( " " , & IndexEntry { " " , index } ) ; err != nil { return fmt . Errorf ( " " , err ) } all , err := s . jobVersionByID ( txn , nil , job . ID ) if err != nil { return fmt . Errorf ( " " , job . ID , err ) } if len ( all ) <= structs . JobTrackedVersions { return nil } stableIdx := - 1 for i , j := range all { if j . Stable { stableIdx = i break } } max := structs . JobTrackedVersions if stableIdx == max { all [ max - 1 ] , all [ max ] = all [ max ] , all [ max - 1 ] } d := all [ max ] if err := txn . Delete ( " " , d ) ; err != nil { return fmt . Errorf ( " " , d . ID , d . Version ) } return nil }
upsertJobVersion inserts a job into its historic version table and limits the
number of job versions that are tracked.
upsertJobVersion inserts a job into its historic version table and limits the number of job versions that are tracked .
// Insert the job // Get all the historic jobs for this ID // If we are below the limit there is no GCing to be done // We have to delete a historic job to make room. // Find index of the highest versioned stable job // If the stable job is the oldest version, do a swap to bring it into the // keep set. // Delete the job outside of the set that are being kept.