Browse code

Merge pull request #3417 from Sjord/use-most-recent-cache-image

Fix issue #3375 - Return most recent image from the cache

Guillaume J. Charmes authored on 2014/01/09 08:30:36
Showing 1 changed files
... ...
@@ -22,7 +22,6 @@ import (
22 22
 	"path"
23 23
 	"path/filepath"
24 24
 	"runtime"
25
-	"sort"
26 25
 	"strconv"
27 26
 	"strings"
28 27
 	"sync"
... ...
@@ -1697,22 +1696,28 @@ func (srv *Server) ImageGetCached(imgID string, config *Config) (*Image, error)
1697 1697
 	}
1698 1698
 
1699 1699
 	// Store the tree in a map of map (map[parentId][childId])
1700
-	imageMap := make(map[string][]string)
1700
+	imageMap := make(map[string]map[string]struct{})
1701 1701
 	for _, img := range images {
1702
-		imageMap[img.Parent] = append(imageMap[img.Parent], img.ID)
1702
+		if _, exists := imageMap[img.Parent]; !exists {
1703
+			imageMap[img.Parent] = make(map[string]struct{})
1704
+		}
1705
+		imageMap[img.Parent][img.ID] = struct{}{}
1703 1706
 	}
1704
-	sort.Strings(imageMap[imgID])
1707
+
1705 1708
 	// Loop on the children of the given image and check the config
1706
-	for _, elem := range imageMap[imgID] {
1709
+	var match *Image
1710
+	for elem := range imageMap[imgID] {
1707 1711
 		img, err := srv.runtime.graph.Get(elem)
1708 1712
 		if err != nil {
1709 1713
 			return nil, err
1710 1714
 		}
1711 1715
 		if CompareConfig(&img.ContainerConfig, config) {
1712
-			return img, nil
1716
+			if match == nil || match.Created.Before(img.Created) {
1717
+				match = img
1718
+			}
1713 1719
 		}
1714 1720
 	}
1715
-	return nil, nil
1721
+	return match, nil
1716 1722
 }
1717 1723
 
1718 1724
 func (srv *Server) RegisterLinks(container *Container, hostConfig *HostConfig) error {