Browse code

Reduce duplication in graphdriver

Removes some duplication in counter.go and proxy.go

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2017/02/28 19:12:02
Showing 2 changed files
... ...
@@ -24,29 +24,19 @@ func NewRefCounter(c Checker) *RefCounter {
24 24
 
25 25
 // Increment increaes the ref count for the given id and returns the current count
26 26
 func (c *RefCounter) Increment(path string) int {
27
-	c.mu.Lock()
28
-	m := c.counts[path]
29
-	if m == nil {
30
-		m = &minfo{}
31
-		c.counts[path] = m
32
-	}
33
-	// if we are checking this path for the first time check to make sure
34
-	// if it was already mounted on the system and make sure we have a correct ref
35
-	// count if it is mounted as it is in use.
36
-	if !m.check {
37
-		m.check = true
38
-		if c.checker.IsMounted(path) {
39
-			m.count++
40
-		}
41
-	}
42
-	m.count++
43
-	count := m.count
44
-	c.mu.Unlock()
45
-	return count
27
+	return c.incdec(path, func(minfo *minfo) {
28
+		minfo.count++
29
+	})
46 30
 }
47 31
 
48 32
 // Decrement decreases the ref count for the given id and returns the current count
49 33
 func (c *RefCounter) Decrement(path string) int {
34
+	return c.incdec(path, func(minfo *minfo) {
35
+		minfo.count--
36
+	})
37
+}
38
+
39
+func (c *RefCounter) incdec(path string, infoOp func(minfo *minfo)) int {
50 40
 	c.mu.Lock()
51 41
 	m := c.counts[path]
52 42
 	if m == nil {
... ...
@@ -62,8 +52,8 @@ func (c *RefCounter) Decrement(path string) int {
62 62
 			m.count++
63 63
 		}
64 64
 	}
65
-	m.count--
65
+	infoOp(m)
66 66
 	count := m.count
67 67
 	c.mu.Unlock()
68 68
 	return count
69
-}
69
+}
70 70
\ No newline at end of file
... ...
@@ -68,26 +68,14 @@ func (d *graphDriverProxy) String() string {
68 68
 }
69 69
 
70 70
 func (d *graphDriverProxy) CreateReadWrite(id, parent string, opts *CreateOpts) error {
71
-	args := &graphDriverRequest{
72
-		ID:     id,
73
-		Parent: parent,
74
-	}
75
-	if opts != nil {
76
-		args.MountLabel = opts.MountLabel
77
-		args.StorageOpt = opts.StorageOpt
78
-	}
79
-
80
-	var ret graphDriverResponse
81
-	if err := d.p.Client().Call("GraphDriver.CreateReadWrite", args, &ret); err != nil {
82
-		return err
83
-	}
84
-	if ret.Err != "" {
85
-		return errors.New(ret.Err)
86
-	}
87
-	return nil
71
+	return d.create("GraphDriver.CreateReadWrite", id, parent, opts)
88 72
 }
89 73
 
90 74
 func (d *graphDriverProxy) Create(id, parent string, opts *CreateOpts) error {
75
+	return d.create("GraphDriver.Create", id, parent, opts)
76
+}
77
+
78
+func (d *graphDriverProxy) create(method, id, parent string, opts *CreateOpts) error {
91 79
 	args := &graphDriverRequest{
92 80
 		ID:     id,
93 81
 		Parent: parent,
... ...
@@ -97,7 +85,7 @@ func (d *graphDriverProxy) Create(id, parent string, opts *CreateOpts) error {
97 97
 		args.StorageOpt = opts.StorageOpt
98 98
 	}
99 99
 	var ret graphDriverResponse
100
-	if err := d.p.Client().Call("GraphDriver.Create", args, &ret); err != nil {
100
+	if err := d.p.Client().Call(method, args, &ret); err != nil {
101 101
 		return err
102 102
 	}
103 103
 	if ret.Err != "" {