Browse code

Add comments in graph.go

Solomon Hykes authored on 2013/03/30 13:13:59
Showing 1 changed files
... ...
@@ -10,10 +10,13 @@ import (
10 10
 	"time"
11 11
 )
12 12
 
13
+// A Graph is a store for versioned filesystem images, and the relationship between them.
13 14
 type Graph struct {
14 15
 	Root string
15 16
 }
16 17
 
18
+// NewGraph instanciates a new graph at the given root path in the filesystem.
19
+// `root` will be created if it doesn't exist.
17 20
 func NewGraph(root string) (*Graph, error) {
18 21
 	abspath, err := filepath.Abs(root)
19 22
 	if err != nil {
... ...
@@ -34,6 +37,8 @@ func (graph *Graph) IsNotExist(err error) bool {
34 34
 	return err != nil && strings.Contains(err.Error(), "does not exist")
35 35
 }
36 36
 
37
+// Exists returns true if an image is registered at the given id.
38
+// If the image doesn't exist or if an error is encountered, false is returned.
37 39
 func (graph *Graph) Exists(id string) bool {
38 40
 	if _, err := graph.Get(id); err != nil {
39 41
 		return false
... ...
@@ -41,6 +46,7 @@ func (graph *Graph) Exists(id string) bool {
41 41
 	return true
42 42
 }
43 43
 
44
+// Get returns the image with the given id, or an error if the image doesn't exist.
44 45
 func (graph *Graph) Get(id string) (*Image, error) {
45 46
 	// FIXME: return nil when the image doesn't exist, instead of an error
46 47
 	img, err := LoadImage(graph.imageRoot(id))
... ...
@@ -54,6 +60,7 @@ func (graph *Graph) Get(id string) (*Image, error) {
54 54
 	return img, nil
55 55
 }
56 56
 
57
+// Create creates a new image and registers it in the graph.
57 58
 func (graph *Graph) Create(layerData Archive, container *Container, comment string) (*Image, error) {
58 59
 	img := &Image{
59 60
 		Id:      GenerateId(),
... ...
@@ -71,6 +78,8 @@ func (graph *Graph) Create(layerData Archive, container *Container, comment stri
71 71
 	return img, nil
72 72
 }
73 73
 
74
+// Register imports a pre-existing image into the graph.
75
+// FIXME: pass img as first argument
74 76
 func (graph *Graph) Register(layerData Archive, img *Image) error {
75 77
 	if err := ValidateId(img.Id); err != nil {
76 78
 		return err
... ...
@@ -95,6 +104,7 @@ func (graph *Graph) Register(layerData Archive, img *Image) error {
95 95
 	return nil
96 96
 }
97 97
 
98
+// Mktemp creates a temporary sub-directory inside the graph's filesystem.
98 99
 func (graph *Graph) Mktemp(id string) (string, error) {
99 100
 	tmp, err := NewGraph(path.Join(graph.Root, ":tmp:"))
100 101
 	if err != nil {
... ...
@@ -106,6 +116,9 @@ func (graph *Graph) Mktemp(id string) (string, error) {
106 106
 	return tmp.imageRoot(id), nil
107 107
 }
108 108
 
109
+// Garbage returns the "garbage", a staging area for deleted images.
110
+// This allows images ot be deleted atomically by os.Rename(), instead of
111
+// os.RemoveAll() which is prone to race conditions
109 112
 func (graph *Graph) Garbage() (*Graph, error) {
110 113
 	return NewGraph(path.Join(graph.Root, ":garbage:"))
111 114
 }
... ...
@@ -124,6 +137,7 @@ func isNotEmpty(err error) bool {
124 124
 	return strings.Contains(err.Error(), " not empty")
125 125
 }
126 126
 
127
+// Delete atomically removes an image from the graph.
127 128
 func (graph *Graph) Delete(id string) error {
128 129
 	garbage, err := graph.Garbage()
129 130
 	if err != nil {
... ...
@@ -150,6 +164,7 @@ func (graph *Graph) Delete(id string) error {
150 150
 	return nil
151 151
 }
152 152
 
153
+// Undelete moves an image back from the garbage to the main graph
153 154
 func (graph *Graph) Undelete(id string) error {
154 155
 	garbage, err := graph.Garbage()
155 156
 	if err != nil {
... ...
@@ -158,6 +173,7 @@ func (graph *Graph) Undelete(id string) error {
158 158
 	return os.Rename(garbage.imageRoot(id), graph.imageRoot(id))
159 159
 }
160 160
 
161
+// GarbageCollect definitely deletes all images moved to the garbage
161 162
 func (graph *Graph) GarbageCollect() error {
162 163
 	garbage, err := graph.Garbage()
163 164
 	if err != nil {
... ...
@@ -166,6 +182,7 @@ func (graph *Graph) GarbageCollect() error {
166 166
 	return os.RemoveAll(garbage.Root)
167 167
 }
168 168
 
169
+// Map returns a list of all images in the graph, addressable by ID
169 170
 func (graph *Graph) Map() (map[string]*Image, error) {
170 171
 	// FIXME: this should replace All()
171 172
 	all, err := graph.All()
... ...
@@ -179,6 +196,7 @@ func (graph *Graph) Map() (map[string]*Image, error) {
179 179
 	return images, nil
180 180
 }
181 181
 
182
+// All returns a list of all images in the graph
182 183
 func (graph *Graph) All() ([]*Image, error) {
183 184
 	var images []*Image
184 185
 	err := graph.WalkAll(func(image *Image) {
... ...
@@ -187,6 +205,8 @@ func (graph *Graph) All() ([]*Image, error) {
187 187
 	return images, err
188 188
 }
189 189
 
190
+// WalkAll iterates over each image in the graph, and passes it to a handler.
191
+// The walking order is undetermined.
190 192
 func (graph *Graph) WalkAll(handler func(*Image)) error {
191 193
 	files, err := ioutil.ReadDir(graph.Root)
192 194
 	if err != nil {
... ...
@@ -203,6 +223,10 @@ func (graph *Graph) WalkAll(handler func(*Image)) error {
203 203
 	return nil
204 204
 }
205 205
 
206
+// ByParent returns a lookup table of images by their parent.
207
+// If an image of id ID has 3 children images, then the value for key ID
208
+// will be a list of 3 images.
209
+// If an image has no children, it will not have an entry in the table.
206 210
 func (graph *Graph) ByParent() (map[string][]*Image, error) {
207 211
 	byParent := make(map[string][]*Image)
208 212
 	err := graph.WalkAll(func(image *Image) {
... ...
@@ -219,6 +243,8 @@ func (graph *Graph) ByParent() (map[string][]*Image, error) {
219 219
 	return byParent, err
220 220
 }
221 221
 
222
+// Heads returns all heads in the graph, keyed by id.
223
+// A head is an image which is not the parent of another image in the graph.
222 224
 func (graph *Graph) Heads() (map[string]*Image, error) {
223 225
 	heads := make(map[string]*Image)
224 226
 	byParent, err := graph.ByParent()