Commit 12116dd6 authored by Brian Tiger Chow's avatar Brian Tiger Chow

style(fsrepo): rename to counter.Openers

parent b666163e
package fsrepo package counter
import "path" import "path"
// TODO this could be made into something more generic. // TODO this could be made into something more generic.
type Counter struct { type Openers struct {
// repos maps repo paths to the number of openers holding an FSRepo handle // repos maps repo paths to the number of openers holding an FSRepo handle
// to it // to it
repos map[string]int repos map[string]int
} }
func NewCounter() *Counter { func NewOpenersCounter() *Openers {
return &Counter{ return &Openers{
repos: make(map[string]int), repos: make(map[string]int),
} }
} }
...@@ -19,13 +19,13 @@ func NewCounter() *Counter { ...@@ -19,13 +19,13 @@ func NewCounter() *Counter {
// NumOpeners returns the number of FSRepos holding a handle to the repo at // 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 // this path. This method is not thread-safe. The caller must have this object
// locked. // locked.
func (l *Counter) NumOpeners(repoPath string) int { func (l *Openers) NumOpeners(repoPath string) int {
return l.repos[key(repoPath)] return l.repos[key(repoPath)]
} }
// AddOpener messages that an FSRepo holds a handle to the repo at this path. // 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. // This method is not thread-safe. The caller must have this object locked.
func (l *Counter) AddOpener(repoPath string) error { func (l *Openers) AddOpener(repoPath string) error {
l.repos[key(repoPath)]++ l.repos[key(repoPath)]++
return nil return nil
} }
...@@ -33,7 +33,7 @@ func (l *Counter) AddOpener(repoPath string) error { ...@@ -33,7 +33,7 @@ func (l *Counter) AddOpener(repoPath string) error {
// RemoveOpener messgaes that an FSRepo no longer holds a handle to the repo at // 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 // this path. This method is not thread-safe. The caller must have this object
// locked. // locked.
func (l *Counter) RemoveOpener(repoPath string) error { func (l *Openers) RemoveOpener(repoPath string) error {
l.repos[key(repoPath)]-- l.repos[key(repoPath)]--
return nil return nil
} }
......
...@@ -12,9 +12,9 @@ import ( ...@@ -12,9 +12,9 @@ import (
repo "github.com/jbenet/go-ipfs/repo" repo "github.com/jbenet/go-ipfs/repo"
config "github.com/jbenet/go-ipfs/repo/config" config "github.com/jbenet/go-ipfs/repo/config"
component "github.com/jbenet/go-ipfs/repo/fsrepo/component" component "github.com/jbenet/go-ipfs/repo/fsrepo/component"
counter "github.com/jbenet/go-ipfs/repo/fsrepo/counter"
dir "github.com/jbenet/go-ipfs/repo/fsrepo/dir" dir "github.com/jbenet/go-ipfs/repo/fsrepo/dir"
lockfile "github.com/jbenet/go-ipfs/repo/fsrepo/lock" lockfile "github.com/jbenet/go-ipfs/repo/fsrepo/lock"
opener "github.com/jbenet/go-ipfs/repo/fsrepo/opener"
serialize "github.com/jbenet/go-ipfs/repo/fsrepo/serialize" serialize "github.com/jbenet/go-ipfs/repo/fsrepo/serialize"
debugerror "github.com/jbenet/go-ipfs/util/debugerror" debugerror "github.com/jbenet/go-ipfs/util/debugerror"
) )
...@@ -23,22 +23,22 @@ var ( ...@@ -23,22 +23,22 @@ var (
// packageLock must be held to while performing any operation that modifies an // packageLock must be held to while performing any operation that modifies an
// FSRepo's state field. This includes Init, Open, Close, and Remove. // FSRepo's state field. This includes Init, Open, Close, and Remove.
packageLock sync.Mutex // protects openerCounter and lockfiles packageLock sync.Mutex // protects openersCounter and lockfiles
// lockfiles holds references to the Closers that ensure that repos are // lockfiles holds references to the Closers that ensure that repos are
// only accessed by one process at a time. // only accessed by one process at a time.
lockfiles map[string]io.Closer lockfiles map[string]io.Closer
// openerCounter prevents the fsrepo from being removed while there exist open // openersCounter prevents the fsrepo from being removed while there exist open
// FSRepo handles. It also ensures that the Init is atomic. // FSRepo handles. It also ensures that the Init is atomic.
// //
// packageLock also protects numOpenedRepos // packageLock also protects numOpenedRepos
// //
// If an operation is used when repo is Open and the operation does not // If an operation is used when repo is Open and the operation does not
// change the repo's state, the package lock does not need to be acquired. // change the repo's state, the package lock does not need to be acquired.
openerCounter *opener.Counter openersCounter *counter.Openers
) )
func init() { func init() {
openerCounter = opener.NewCounter() openersCounter = counter.NewOpenersCounter()
lockfiles = make(map[string]io.Closer) lockfiles = make(map[string]io.Closer)
} }
...@@ -113,7 +113,7 @@ func Remove(repoPath string) error { ...@@ -113,7 +113,7 @@ func Remove(repoPath string) error {
packageLock.Lock() packageLock.Lock()
defer packageLock.Unlock() defer packageLock.Unlock()
if openerCounter.NumOpeners(repoPath) != 0 { if openersCounter.NumOpeners(repoPath) != 0 {
return errors.New("repo in use") return errors.New("repo in use")
} }
return os.RemoveAll(repoPath) return os.RemoveAll(repoPath)
...@@ -129,7 +129,7 @@ func LockedByOtherProcess(repoPath string) bool { ...@@ -129,7 +129,7 @@ func LockedByOtherProcess(repoPath string) bool {
defer packageLock.Unlock() defer packageLock.Unlock()
// NB: the lock is only held when repos are Open // NB: the lock is only held when repos are Open
return lockfile.Locked(repoPath) && openerCounter.NumOpeners(repoPath) == 0 return lockfile.Locked(repoPath) && openersCounter.NumOpeners(repoPath) == 0
} }
// Open returns an error if the repo is not initialized. // Open returns an error if the repo is not initialized.
...@@ -265,7 +265,7 @@ func IsInitialized(path string) bool { ...@@ -265,7 +265,7 @@ func IsInitialized(path string) bool {
// private methods below this point. NB: packageLock must held by caller. // private methods below this point. NB: packageLock must held by caller.
// isInitializedUnsynced reports whether the repo is initialized. Caller must // isInitializedUnsynced reports whether the repo is initialized. Caller must
// hold openerCounter lock. // hold the packageLock.
func isInitializedUnsynced(path string) bool { func isInitializedUnsynced(path string) bool {
for _, b := range componentBuilders() { for _, b := range componentBuilders() {
if !b.IsInitialized(path) { if !b.IsInitialized(path) {
...@@ -279,24 +279,24 @@ func isInitializedUnsynced(path string) bool { ...@@ -279,24 +279,24 @@ func isInitializedUnsynced(path string) bool {
// the package mutex. // the package mutex.
func (r *FSRepo) transitionToOpened() error { func (r *FSRepo) transitionToOpened() error {
r.state = opened r.state = opened
if countBefore := openerCounter.NumOpeners(r.path); countBefore == 0 { // #first if countBefore := openersCounter.NumOpeners(r.path); countBefore == 0 { // #first
closer, err := lockfile.Lock(r.path) closer, err := lockfile.Lock(r.path)
if err != nil { if err != nil {
return err return err
} }
lockfiles[r.path] = closer lockfiles[r.path] = closer
} }
return openerCounter.AddOpener(r.path) return openersCounter.AddOpener(r.path)
} }
// transitionToClosed manages the state transition to |closed|. Caller must // transitionToClosed manages the state transition to |closed|. Caller must
// hold the package mutex. // hold the package mutex.
func (r *FSRepo) transitionToClosed() error { func (r *FSRepo) transitionToClosed() error {
r.state = closed r.state = closed
if err := openerCounter.RemoveOpener(r.path); err != nil { if err := openersCounter.RemoveOpener(r.path); err != nil {
return err return err
} }
if countAfter := openerCounter.NumOpeners(r.path); countAfter == 0 { if countAfter := openersCounter.NumOpeners(r.path); countAfter == 0 {
closer, ok := lockfiles[r.path] closer, ok := lockfiles[r.path]
if !ok { if !ok {
return errors.New("package error: lockfile is not held") return errors.New("package error: lockfile is not held")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment