openers.go 1.12 KB
Newer Older
1
package counter
2

3 4 5
import "path"

// TODO this could be made into something more generic.
6

7
type Openers struct {
8 9 10 11 12
	// repos maps repo paths to the number of openers holding an FSRepo handle
	// to it
	repos map[string]int
}

13 14
func NewOpenersCounter() *Openers {
	return &Openers{
15 16 17 18 19 20 21
		repos: make(map[string]int),
	}
}

// NumOpeners returns the number of FSRepos holding a handle to the repo at
// this path. This method is not thread-safe. The caller must have this object
// locked.
22
func (l *Openers) NumOpeners(repoPath string) int {
23 24 25 26 27
	return l.repos[key(repoPath)]
}

// AddOpener messages that an FSRepo holds a handle to the repo at this path.
// This method is not thread-safe. The caller must have this object locked.
28
func (l *Openers) AddOpener(repoPath string) error {
29
	l.repos[key(repoPath)]++
30
	return nil
31 32 33 34 35
}

// RemoveOpener messgaes that an FSRepo no longer holds a handle to the repo at
// this path. This method is not thread-safe. The caller must have this object
// locked.
36
func (l *Openers) RemoveOpener(repoPath string) error {
37
	l.repos[key(repoPath)]--
38
	return nil
39 40 41 42 43
}

func key(repoPath string) string {
	return path.Clean(repoPath)
}