Repository: hashicorp/nomad
File: nomad/state/state_store.go (Go)

Code before:
		return fmt.Errorf("unable to update job submission: %v", err)
	}

	if err := s.updatePreservedValues(job, existingJob, req); err != nil {
		return fmt.Errorf("unable to update preserved values: %v", err)
	}

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

	return nil
}

// CheckIdempotencyToken finds all children of the parent job ID and checks to
// make sure none of them were dispatched with the idempotency token passed as
// an argument. Returns the child job found, if any.
func (s *StateStore) CheckIdempotencyToken(ns, parentID, idempotencyToken string) (*structs.Job, error) {
	iter, err := s.JobsByIDPrefix(nil, ns, parentID, SortDefault)
	if err != nil {
		const errMsg = "failed to retrieve jobs for idempotency check"
		return nil, fmt.Errorf(errMsg)
	}

	for {
		raw := iter.Next()
		if raw == nil {
			break
		}
		existingDispatch := raw.(*structs.Job)
		if existingDispatch.ParentID != parentID {
			continue
		}
		if existingDispatch.DispatchIdempotencyToken == idempotencyToken {
			return existingDispatch, nil
		}
	}

	return nil, nil
}

// DeleteJob is used to deregister a job
func (s *StateStore) DeleteJob(index uint64, namespace, jobID string) error {
	txn := s.db.WriteTxn(index)
	defer txn.Abort()

	err := s.DeleteJobTxn(index, namespace, jobID, txn)

Review comment (suggestion):
The string isn't being formatted, so we could get away with using a non-formatting function:
```suggestion
		return nil, errors.New(errMsg)
```

Code after:
		return fmt.Errorf("unable to update job submission: %v", err)
	}

	if err := s.updatePreservedValues(job, existingJob, req); err != nil {
		return fmt.Errorf("unable to update preserved values: %v", err)
	}

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

	return nil
}

// CheckIdempotencyToken finds all children of the parent job ID and checks to
// make sure none of them were dispatched with the idempotency token passed as
// an argument. Returns the child job found, if any.
func (s *StateStore) CheckIdempotencyToken(ns, parentID, idempotencyToken string) (*structs.Job, error) {
	iter, err := s.JobsByIDPrefix(nil, ns, parentID, SortDefault)
	if err != nil {
		return nil, errors.New("failed to retrieve jobs for idempotency check")
	}

	for {
		raw := iter.Next()
		if raw == nil {
			break
		}
		existingDispatch := raw.(*structs.Job)
		if existingDispatch.ParentID != parentID {
			continue
		}
		if existingDispatch.DispatchIdempotencyToken == idempotencyToken {
			return existingDispatch, nil
		}
	}

	return nil, nil
}

// DeleteJob is used to deregister a job
func (s *StateStore) DeleteJob(index uint64, namespace, jobID string) error {
	txn := s.db.WriteTxn(index)
	defer txn.Abort()

	err := s.DeleteJobTxn(index, namespace, jobID, txn)
	if err == nil {