style(fsrepo): rename to counter.Openers
Brian Tiger Chow committed
Jan 14, 2015 at 09:18 UTC
12116dd6e4af4bb06c969f42e9f8e86973ae0d4f
2 files changed
+19
-19
repo/fsrepo/counter/openers.go
renamed
+7
-7
@@ -1,17 +1,17 @@
1
-package fsrepo
1
+package counter
2
3
import "path"
4
5
// TODO this could be made into something more generic.
6
7
-type Counter struct {
7
+type Openers struct {
8
// repos maps repo paths to the number of openers holding an FSRepo handle
9
// to it
10
repos map[string]int
11
}
12
13
-func NewCounter() *Counter {
14
- return &Counter{
13
+func NewOpenersCounter() *Openers {
14
+ return &Openers{
15
repos: make(map[string]int),
16
}
17
}
@@ -19,13 +19,13 @@ func NewCounter() *Counter {
19
// NumOpeners returns the number of FSRepos holding a handle to the repo at
20
// this path. This method is not thread-safe. The caller must have this object
21
// locked.
22
-func (l *Counter) NumOpeners(repoPath string) int {
22
+func (l *Openers) NumOpeners(repoPath string) int {
23
return l.repos[key(repoPath)]
24
}
25
26
// AddOpener messages that an FSRepo holds a handle to the repo at this path.
27
// This method is not thread-safe. The caller must have this object locked.
28
-func (l *Counter) AddOpener(repoPath string) error {
28
+func (l *Openers) AddOpener(repoPath string) error {
29
l.repos[key(repoPath)]++
30
return nil
31
}
@@ -33,7 +33,7 @@ func (l *Counter) AddOpener(repoPath string) error {
33
// RemoveOpener messgaes that an FSRepo no longer holds a handle to the repo at
34
// this path. This method is not thread-safe. The caller must have this object
35
// locked.
36
-func (l *Counter) RemoveOpener(repoPath string) error {
36
+func (l *Openers) RemoveOpener(repoPath string) error {
37
l.repos[key(repoPath)]--
38
return nil
39
}
repo/fsrepo/fsrepo.go
+12
-12
@@ -12,9 +12,9 @@ import (
12
repo "github.com/jbenet/go-ipfs/repo"
13
config "github.com/jbenet/go-ipfs/repo/config"
14
component "github.com/jbenet/go-ipfs/repo/fsrepo/component"
15
+ counter "github.com/jbenet/go-ipfs/repo/fsrepo/counter"
16
dir "github.com/jbenet/go-ipfs/repo/fsrepo/dir"
17
lockfile "github.com/jbenet/go-ipfs/repo/fsrepo/lock"
17
- opener "github.com/jbenet/go-ipfs/repo/fsrepo/opener"
18
serialize "github.com/jbenet/go-ipfs/repo/fsrepo/serialize"
19
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
20
)
@@ -23,22 +23,22 @@ var (
23
24
// packageLock must be held to while performing any operation that modifies an
25
// FSRepo's state field. This includes Init, Open, Close, and Remove.
26
- packageLock sync.Mutex // protects openerCounter and lockfiles
26
+ packageLock sync.Mutex // protects openersCounter and lockfiles
27
// lockfiles holds references to the Closers that ensure that repos are
28
// only accessed by one process at a time.
29
lockfiles map[string]io.Closer
30
- // openerCounter prevents the fsrepo from being removed while there exist open
30
+ // openersCounter prevents the fsrepo from being removed while there exist open
31
// FSRepo handles. It also ensures that the Init is atomic.
32
//
33
// packageLock also protects numOpenedRepos
34
//
35
// If an operation is used when repo is Open and the operation does not
36
// change the repo's state, the package lock does not need to be acquired.
37
- openerCounter *opener.Counter
37
+ openersCounter *counter.Openers
38
)
39
40
func init() {
41
- openerCounter = opener.NewCounter()
41
+ openersCounter = counter.NewOpenersCounter()
42
lockfiles = make(map[string]io.Closer)
43
}
44
@@ -113,7 +113,7 @@ func Remove(repoPath string) error {
113
packageLock.Lock()
114
defer packageLock.Unlock()
115
116
- if openerCounter.NumOpeners(repoPath) != 0 {
116
+ if openersCounter.NumOpeners(repoPath) != 0 {
117
return errors.New("repo in use")
118
}
119
return os.RemoveAll(repoPath)
@@ -129,7 +129,7 @@ func LockedByOtherProcess(repoPath string) bool {
129
defer packageLock.Unlock()
130
131
// NB: the lock is only held when repos are Open
132
- return lockfile.Locked(repoPath) && openerCounter.NumOpeners(repoPath) == 0
132
+ return lockfile.Locked(repoPath) && openersCounter.NumOpeners(repoPath) == 0
133
}
134
135
// Open returns an error if the repo is not initialized.
@@ -265,7 +265,7 @@ func IsInitialized(path string) bool {
265
// private methods below this point. NB: packageLock must held by caller.
266
267
// isInitializedUnsynced reports whether the repo is initialized. Caller must
268
-// hold openerCounter lock.
268
+// hold the packageLock.
269
func isInitializedUnsynced(path string) bool {
270
for _, b := range componentBuilders() {
271
if !b.IsInitialized(path) {
@@ -279,24 +279,24 @@ func isInitializedUnsynced(path string) bool {
279
// the package mutex.
280
func (r *FSRepo) transitionToOpened() error {
281
r.state = opened
282
- if countBefore := openerCounter.NumOpeners(r.path); countBefore == 0 { // #first
282
+ if countBefore := openersCounter.NumOpeners(r.path); countBefore == 0 { // #first
283
closer, err := lockfile.Lock(r.path)
284
if err != nil {
285
return err
286
}
287
lockfiles[r.path] = closer
288
}
289
- return openerCounter.AddOpener(r.path)
289
+ return openersCounter.AddOpener(r.path)
290
}
291
292
// transitionToClosed manages the state transition to |closed|. Caller must
293
// hold the package mutex.
294
func (r *FSRepo) transitionToClosed() error {
295
r.state = closed
296
- if err := openerCounter.RemoveOpener(r.path); err != nil {
296
+ if err := openersCounter.RemoveOpener(r.path); err != nil {
297
return err
298
}
299
- if countAfter := openerCounter.NumOpeners(r.path); countAfter == 0 {
299
+ if countAfter := openersCounter.NumOpeners(r.path); countAfter == 0 {
300
closer, ok := lockfiles[r.path]
301
if !ok {
302
return errors.New("package error: lockfile is not held")