func (store *store) References(id image.ID) []Named {
	store.mu.RLock()
	defer store.mu.RUnlock()
	var references []Named
	for _, ref := range store.referencesByIDCache[id] {
		references = append(references, ref)
	}
	sort.Sort(lexicalRefs(references))
	return references
}
References returns a slice of references to the given image ID. The slice
will be nil if there are no references to this image ID.
package reference
import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"sync"
	"github.com/docker/distribution/digest"
	"github.com/docker/docker/image"
)
var (
	ErrDoesNotExist = errors.New("reference does not exist")
)
type Association struct {
	Ref     Named
	ImageID image.ID
}
type Store interface {
	References(id image.ID) []Named
	ReferencesByName(ref Named) []Association
	AddTag(ref Named, id image.ID, force bool) error
	AddDigest(ref Canonical, id image.ID, force bool) error
	Delete(ref Named) (bool, error)
	Get(ref Named) (image.ID, error)
}
type store struct {
	mu sync.RWMutex
	jsonPath string
	Repositories map[string]repository
	referencesByIDCache map[image.ID]map[string]Named
}
type repository map[string]image.ID
type lexicalRefs []Named
func (a lexicalRefs) Len() int           { return len(a) }
func (a lexicalRefs) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a lexicalRefs) Less(i, j int) bool { return a[i].String() < a[j].String() }
type lexicalAssociations []Association
func (a lexicalAssociations) Len() int           { return len(a) }
func (a lexicalAssociations) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a lexicalAssociations) Less(i, j int) bool { return a[i].Ref.String() < a[j].Ref.String() }
func NewReferenceStore(jsonPath string) (Store, error) {
	abspath, err := filepath.Abs(jsonPath)
	if err != nil {
		return nil, err
	}
	store := &store{
		jsonPath:            abspath,
		Repositories:        make(map[string]repository),
		referencesByIDCache: make(map[image.ID]map[string]Named),
	}
	if err := store.reload(); os.IsNotExist(err) {
		if err := store.save(); err != nil {
			return nil, err
		}
	} else if err != nil {
		return nil, err
	}
	return store, nil
}
func (store *store) AddTag(ref Named, id image.ID, force bool) error {
	if _, isCanonical := ref.(Canonical); isCanonical {
		return errors.New("refusing to create a tag with a digest reference")
	}
	return store.addReference(WithDefaultTag(ref), id, force)
}
func (store *store) AddDigest(ref Canonical, id image.ID, force bool) error {
	return store.addReference(ref, id, force)
}
func (store *store) addReference(ref Named, id image.ID, force bool) error {
	if ref.Name() == string(digest.Canonical) {
		return errors.New("refusing to create an ambiguous tag using digest algorithm as name")
	}
	store.mu.Lock()
	defer store.mu.Unlock()
	repository, exists := store.Repositories[ref.Name()]
	if !exists || repository == nil {
		repository = make(map[string]image.ID)
		store.Repositories[ref.Name()] = repository
	}
	refStr := ref.String()
	oldID, exists := repository[refStr]
	if exists {
		if digested, isDigest := ref.(Canonical); isDigest {
			return fmt.Errorf("Cannot overwrite digest %s", digested.Digest().String())
		}
		if !force {
			return fmt.Errorf("Conflict: Tag %s is already set to image %s, if you want to replace it, please use -f option", ref.String(), oldID.String())
		}
		if store.referencesByIDCache[oldID] != nil {
			delete(store.referencesByIDCache[oldID], refStr)
			if len(store.referencesByIDCache[oldID]) == 0 {
				delete(store.referencesByIDCache, oldID)
			}
		}
	}
	repository[refStr] = id
	if store.referencesByIDCache[id] == nil {
		store.referencesByIDCache[id] = make(map[string]Named)
	}
	store.referencesByIDCache[id][refStr] = ref
	return store.save()
}
func (store *store) Delete(ref Named) (bool, error) {
	ref = WithDefaultTag(ref)
	store.mu.Lock()
	defer store.mu.Unlock()
	repoName := ref.Name()
	repository, exists := store.Repositories[repoName]
	if !exists {
		return false, ErrDoesNotExist
	}
	refStr := ref.String()
	if id, exists := repository[refStr]; exists {
		delete(repository, refStr)
		if len(repository) == 0 {
			delete(store.Repositories, repoName)
		}
		if store.referencesByIDCache[id] != nil {
			delete(store.referencesByIDCache[id], refStr)
			if len(store.referencesByIDCache[id]) == 0 {
				delete(store.referencesByIDCache, id)
			}
		}
		return true, store.save()
	}
	return false, ErrDoesNotExist
}
func (store *store) Get(ref Named) (image.ID, error) {
	ref = WithDefaultTag(ref)
	store.mu.RLock()
	defer store.mu.RUnlock()
	repository, exists := store.Repositories[ref.Name()]
	if !exists || repository == nil {
		return "", ErrDoesNotExist
	}
	id, exists := repository[ref.String()]
	if !exists {
		return "", ErrDoesNotExist
	}
	return id, nil
}