github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/container_pool/fake_graph/fake_graph.go (about) 1 package fake_graph 2 3 import ( 4 "errors" 5 "sync" 6 7 "github.com/docker/docker/image" 8 "github.com/docker/docker/pkg/archive" 9 ) 10 11 type FakeGraph struct { 12 exists map[string]*image.Image 13 14 WhenRegistering func(image *image.Image, layer archive.ArchiveReader) error 15 16 mutex *sync.RWMutex 17 } 18 19 func New() *FakeGraph { 20 return &FakeGraph{ 21 exists: make(map[string]*image.Image), 22 23 mutex: &sync.RWMutex{}, 24 } 25 } 26 27 func (graph *FakeGraph) Exists(imageID string) bool { 28 graph.mutex.RLock() 29 defer graph.mutex.RUnlock() 30 31 _, exists := graph.exists[imageID] 32 return exists 33 } 34 35 func (graph *FakeGraph) SetExists(imageID string, imgJSON []byte) { 36 graph.mutex.Lock() 37 defer graph.mutex.Unlock() 38 img, err := image.NewImgJSON(imgJSON) 39 if err != nil { 40 panic("bad imgJSON for imageID: " + imageID) 41 } 42 graph.exists[imageID] = img 43 } 44 45 func (graph *FakeGraph) Get(imageID string) (*image.Image, error) { 46 graph.mutex.RLock() 47 defer graph.mutex.RUnlock() 48 49 img, exists := graph.exists[imageID] 50 if exists { 51 return img, nil 52 } else { 53 return nil, errors.New("image not found in graph: " + imageID) 54 } 55 } 56 57 func (graph *FakeGraph) Register(image *image.Image, layer archive.ArchiveReader) error { 58 if graph.WhenRegistering != nil { 59 return graph.WhenRegistering(image, layer) 60 } 61 62 return nil 63 }