Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
12116dd6
Commit
12116dd6
authored
Jan 14, 2015
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
style(fsrepo): rename to counter.Openers
parent
b666163e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
19 deletions
+19
-19
repo/fsrepo/counter/openers.go
repo/fsrepo/counter/openers.go
+7
-7
repo/fsrepo/fsrepo.go
repo/fsrepo/fsrepo.go
+12
-12
No files found.
repo/fsrepo/
opener/count
er.go
→
repo/fsrepo/
counter/open
er
s
.go
View file @
12116dd6
package
fsrepo
package
counter
import
"path"
// TODO this could be made into something more generic.
type
Count
er
struct
{
type
Open
er
s
struct
{
// repos maps repo paths to the number of openers holding an FSRepo handle
// to it
repos
map
[
string
]
int
}
func
NewCounter
()
*
Count
er
{
return
&
Count
er
{
func
New
Openers
Counter
()
*
Open
er
s
{
return
&
Open
er
s
{
repos
:
make
(
map
[
string
]
int
),
}
}
...
...
@@ -19,13 +19,13 @@ func NewCounter() *Counter {
// 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.
func
(
l
*
Count
er
)
NumOpeners
(
repoPath
string
)
int
{
func
(
l
*
Open
er
s
)
NumOpeners
(
repoPath
string
)
int
{
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.
func
(
l
*
Count
er
)
AddOpener
(
repoPath
string
)
error
{
func
(
l
*
Open
er
s
)
AddOpener
(
repoPath
string
)
error
{
l
.
repos
[
key
(
repoPath
)]
++
return
nil
}
...
...
@@ -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
// this path. This method is not thread-safe. The caller must have this object
// locked.
func
(
l
*
Count
er
)
RemoveOpener
(
repoPath
string
)
error
{
func
(
l
*
Open
er
s
)
RemoveOpener
(
repoPath
string
)
error
{
l
.
repos
[
key
(
repoPath
)]
--
return
nil
}
...
...
repo/fsrepo/fsrepo.go
View file @
12116dd6
...
...
@@ -12,9 +12,9 @@ import (
repo
"github.com/jbenet/go-ipfs/repo"
config
"github.com/jbenet/go-ipfs/repo/config"
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"
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"
debugerror
"github.com/jbenet/go-ipfs/util/debugerror"
)
...
...
@@ -23,22 +23,22 @@ var (
// packageLock must be held to while performing any operation that modifies an
// FSRepo's state field. This includes Init, Open, Close, and Remove.
packageLock
sync
.
Mutex
// protects openerCounter and lockfiles
packageLock
sync
.
Mutex
// protects opener
s
Counter and lockfiles
// lockfiles holds references to the Closers that ensure that repos are
// only accessed by one process at a time.
lockfiles
map
[
string
]
io
.
Closer
// openerCounter prevents the fsrepo from being removed while there exist open
// opener
s
Counter prevents the fsrepo from being removed while there exist open
// FSRepo handles. It also ensures that the Init is atomic.
//
// packageLock also protects numOpenedRepos
//
// 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.
openerCounter
*
opener
.
Count
er
opener
s
Counter
*
counter
.
Open
er
s
)
func
init
()
{
openerCounter
=
open
er
.
NewCounter
()
opener
s
Counter
=
count
er
.
New
Openers
Counter
()
lockfiles
=
make
(
map
[
string
]
io
.
Closer
)
}
...
...
@@ -113,7 +113,7 @@ func Remove(repoPath string) error {
packageLock
.
Lock
()
defer
packageLock
.
Unlock
()
if
openerCounter
.
NumOpeners
(
repoPath
)
!=
0
{
if
opener
s
Counter
.
NumOpeners
(
repoPath
)
!=
0
{
return
errors
.
New
(
"repo in use"
)
}
return
os
.
RemoveAll
(
repoPath
)
...
...
@@ -129,7 +129,7 @@ func LockedByOtherProcess(repoPath string) bool {
defer
packageLock
.
Unlock
()
// NB: the lock is only held when repos are Open
return
lockfile
.
Locked
(
repoPath
)
&&
openerCounter
.
NumOpeners
(
repoPath
)
==
0
return
lockfile
.
Locked
(
repoPath
)
&&
opener
s
Counter
.
NumOpeners
(
repoPath
)
==
0
}
// Open returns an error if the repo is not initialized.
...
...
@@ -265,7 +265,7 @@ func IsInitialized(path string) bool {
// private methods below this point. NB: packageLock must held by caller.
// isInitializedUnsynced reports whether the repo is initialized. Caller must
// hold
openerCounter l
ock.
// hold
the packageL
ock.
func
isInitializedUnsynced
(
path
string
)
bool
{
for
_
,
b
:=
range
componentBuilders
()
{
if
!
b
.
IsInitialized
(
path
)
{
...
...
@@ -279,24 +279,24 @@ func isInitializedUnsynced(path string) bool {
// the package mutex.
func
(
r
*
FSRepo
)
transitionToOpened
()
error
{
r
.
state
=
opened
if
countBefore
:=
openerCounter
.
NumOpeners
(
r
.
path
);
countBefore
==
0
{
// #first
if
countBefore
:=
opener
s
Counter
.
NumOpeners
(
r
.
path
);
countBefore
==
0
{
// #first
closer
,
err
:=
lockfile
.
Lock
(
r
.
path
)
if
err
!=
nil
{
return
err
}
lockfiles
[
r
.
path
]
=
closer
}
return
openerCounter
.
AddOpener
(
r
.
path
)
return
opener
s
Counter
.
AddOpener
(
r
.
path
)
}
// transitionToClosed manages the state transition to |closed|. Caller must
// hold the package mutex.
func
(
r
*
FSRepo
)
transitionToClosed
()
error
{
r
.
state
=
closed
if
err
:=
openerCounter
.
RemoveOpener
(
r
.
path
);
err
!=
nil
{
if
err
:=
opener
s
Counter
.
RemoveOpener
(
r
.
path
);
err
!=
nil
{
return
err
}
if
countAfter
:=
openerCounter
.
NumOpeners
(
r
.
path
);
countAfter
==
0
{
if
countAfter
:=
opener
s
Counter
.
NumOpeners
(
r
.
path
);
countAfter
==
0
{
closer
,
ok
:=
lockfiles
[
r
.
path
]
if
!
ok
{
return
errors
.
New
(
"package error: lockfile is not held"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment