You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: sequence.go
    path of file: ./repos/juju/state
    
    the code of the file until where you have to start completion: // Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package state

import (
	"fmt"

	"github.com/juju/errors"
	"github.com/juju/mgo/v3"
	"github.com/juju/mgo/v3/bson"
)

type sequenceDoc struct {
	DocID     string `bson:"_id"`
	Name      string `bson:"name"`
	ModelUUID string `bson:"model-uuid"`
	Counter   int    // This stores the *next* value to return.
}

// sequence safely increments 
    
func updateSeqWithMin(sequence seqUpdater, minVal int) (int, error) {
	for try := 0; try < maxSeqRetries; try++ {
		curVal, err := sequence.read()
		if err != nil {
			return -1, errors.Annotate(err, "could not read sequence")
		}
		if curVal == 0 {
			// No sequence document exists, create one.
			ok, err := sequence.create(minVal + 1)
			if err != nil {
				return -1, errors.Annotate(err, "could not create sequence")
			}
			if ok {
				return minVal, nil
			}
			// Someone else created the sequence document at the same
			// time, try again.
		} else {
			// Increment an existing sequence document, respecting the
			// minimum value provided.
			nextVal := curVal + 1
			if nextVal <= minVal {
				nextVal = minVal + 1
			}
			ok, err := sequence.set(curVal, nextVal)
			if err != nil {
				return -1, errors.Annotate(err, "could not set sequence")
			}
			if ok {
				return nextVal - 1, nil
			}
			// Someone else incremented the sequence at the same time,
			// try again.
		}
	}
	return -1, errors.New("too much contention while updating sequence")
}