Browse code

Merge pull request #1759 from bdon/graph-map

Minor refactor of Graph; replace uses of Graph.All (slice) with Graph.Map (map)

Michael Crosby authored on 2013/09/11 00:49:11
Showing 5 changed files
... ...
@@ -68,7 +68,7 @@ func TestGetInfo(t *testing.T) {
68 68
 
69 69
 	srv := &Server{runtime: runtime}
70 70
 
71
-	initialImages, err := srv.runtime.graph.All()
71
+	initialImages, err := srv.runtime.graph.Map()
72 72
 	if err != nil {
73 73
 		t.Fatal(err)
74 74
 	}
... ...
@@ -274,30 +274,19 @@ func (graph *Graph) Delete(name string) error {
274 274
 
275 275
 // Map returns a list of all images in the graph, addressable by ID.
276 276
 func (graph *Graph) Map() (map[string]*Image, error) {
277
-	// FIXME: this should replace All()
278
-	all, err := graph.All()
277
+	images := make(map[string]*Image)
278
+	err := graph.walkAll(func(image *Image) {
279
+		images[image.ID] = image
280
+	})
279 281
 	if err != nil {
280 282
 		return nil, err
281 283
 	}
282
-	images := make(map[string]*Image, len(all))
283
-	for _, image := range all {
284
-		images[image.ID] = image
285
-	}
286 284
 	return images, nil
287 285
 }
288 286
 
289
-// All returns a list of all images in the graph.
290
-func (graph *Graph) All() ([]*Image, error) {
291
-	var images []*Image
292
-	err := graph.WalkAll(func(image *Image) {
293
-		images = append(images, image)
294
-	})
295
-	return images, err
296
-}
297
-
298
-// WalkAll iterates over each image in the graph, and passes it to a handler.
287
+// walkAll iterates over each image in the graph, and passes it to a handler.
299 288
 // The walking order is undetermined.
300
-func (graph *Graph) WalkAll(handler func(*Image)) error {
289
+func (graph *Graph) walkAll(handler func(*Image)) error {
301 290
 	files, err := ioutil.ReadDir(graph.Root)
302 291
 	if err != nil {
303 292
 		return err
... ...
@@ -319,7 +308,7 @@ func (graph *Graph) WalkAll(handler func(*Image)) error {
319 319
 // If an image has no children, it will not have an entry in the table.
320 320
 func (graph *Graph) ByParent() (map[string][]*Image, error) {
321 321
 	byParent := make(map[string][]*Image)
322
-	err := graph.WalkAll(func(image *Image) {
322
+	err := graph.walkAll(func(image *Image) {
323 323
 		parent, err := graph.Get(image.Parent)
324 324
 		if err != nil {
325 325
 			return
... ...
@@ -341,7 +330,7 @@ func (graph *Graph) Heads() (map[string]*Image, error) {
341 341
 	if err != nil {
342 342
 		return nil, err
343 343
 	}
344
-	err = graph.WalkAll(func(image *Image) {
344
+	err = graph.walkAll(func(image *Image) {
345 345
 		// If it's not in the byParent lookup table, then
346 346
 		// it's not a parent -> so it's a head!
347 347
 		if _, exists := byParent[image.ID]; !exists {
... ...
@@ -20,11 +20,11 @@ func TestInit(t *testing.T) {
20 20
 	if _, err := os.Stat(graph.Root); err != nil {
21 21
 		t.Fatal(err)
22 22
 	}
23
-	// All() should be empty
24
-	if l, err := graph.All(); err != nil {
23
+	// Map() should be empty
24
+	if l, err := graph.Map(); err != nil {
25 25
 		t.Fatal(err)
26 26
 	} else if len(l) != 0 {
27
-		t.Fatalf("List() should return %d, not %d", 0, len(l))
27
+		t.Fatalf("len(Map()) should return %d, not %d", 0, len(l))
28 28
 	}
29 29
 }
30 30
 
... ...
@@ -76,11 +76,15 @@ func TestGraphCreate(t *testing.T) {
76 76
 	if image.DockerVersion != VERSION {
77 77
 		t.Fatalf("Wrong docker_version: should be '%s', not '%s'", VERSION, image.DockerVersion)
78 78
 	}
79
-	if images, err := graph.All(); err != nil {
79
+	images, err := graph.Map()
80
+	if err != nil {
80 81
 		t.Fatal(err)
81 82
 	} else if l := len(images); l != 1 {
82 83
 		t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
83 84
 	}
85
+	if images[image.ID] == nil {
86
+		t.Fatalf("Could not find image with id %s", image.ID)
87
+	}
84 88
 }
85 89
 
86 90
 func TestRegister(t *testing.T) {
... ...
@@ -99,7 +103,7 @@ func TestRegister(t *testing.T) {
99 99
 	if err != nil {
100 100
 		t.Fatal(err)
101 101
 	}
102
-	if images, err := graph.All(); err != nil {
102
+	if images, err := graph.Map(); err != nil {
103 103
 		t.Fatal(err)
104 104
 	} else if l := len(images); l != 1 {
105 105
 		t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
... ...
@@ -274,7 +278,7 @@ func TestByParent(t *testing.T) {
274 274
 }
275 275
 
276 276
 func assertNImages(graph *Graph, t *testing.T, n int) {
277
-	if images, err := graph.All(); err != nil {
277
+	if images, err := graph.Map(); err != nil {
278 278
 		t.Fatal(err)
279 279
 	} else if actualN := len(images); actualN != n {
280 280
 		t.Fatalf("Expected %d images, found %d", n, actualN)
... ...
@@ -50,7 +50,7 @@ func cleanup(runtime *Runtime) error {
50 50
 		container.Kill()
51 51
 		runtime.Destroy(container)
52 52
 	}
53
-	images, err := runtime.graph.All()
53
+	images, err := runtime.graph.Map()
54 54
 	if err != nil {
55 55
 		return err
56 56
 	}
... ...
@@ -123,13 +123,13 @@ func init() {
123 123
 // FIXME: test that ImagePull(json=true) send correct json output
124 124
 
125 125
 func GetTestImage(runtime *Runtime) *Image {
126
-	imgs, err := runtime.graph.All()
126
+	imgs, err := runtime.graph.Map()
127 127
 	if err != nil {
128 128
 		panic(err)
129 129
 	}
130
-	for i := range imgs {
131
-		if imgs[i].ID == unitTestImageID {
132
-			return imgs[i]
130
+	for _, image := range imgs {
131
+		if image.ID == unitTestImageID {
132
+			return image
133 133
 		}
134 134
 	}
135 135
 	panic(fmt.Errorf("Test image %v not found", unitTestImageID))
... ...
@@ -157,7 +157,7 @@ func (srv *Server) ImageInsert(name, url, path string, out io.Writer, sf *utils.
157 157
 }
158 158
 
159 159
 func (srv *Server) ImagesViz(out io.Writer) error {
160
-	images, _ := srv.runtime.graph.All()
160
+	images, _ := srv.runtime.graph.Map()
161 161
 	if images == nil {
162 162
 		return nil
163 163
 	}
... ...
@@ -248,7 +248,7 @@ func (srv *Server) Images(all bool, filter string) ([]APIImages, error) {
248 248
 }
249 249
 
250 250
 func (srv *Server) DockerInfo() *APIInfo {
251
-	images, _ := srv.runtime.graph.All()
251
+	images, _ := srv.runtime.graph.Map()
252 252
 	var imgcount int
253 253
 	if images == nil {
254 254
 		imgcount = 0
... ...
@@ -1110,7 +1110,7 @@ func (srv *Server) ImageDelete(name string, autoPrune bool) ([]APIRmi, error) {
1110 1110
 func (srv *Server) ImageGetCached(imgID string, config *Config) (*Image, error) {
1111 1111
 
1112 1112
 	// Retrieve all images
1113
-	images, err := srv.runtime.graph.All()
1113
+	images, err := srv.runtime.graph.Map()
1114 1114
 	if err != nil {
1115 1115
 		return nil, err
1116 1116
 	}