This commit is contained in:
Alex Yatskov 2015-05-28 14:47:24 +09:00
parent ed1c8bc1e6
commit 0613779a78
4 changed files with 13 additions and 18 deletions

View File

@ -36,10 +36,11 @@ import (
"time" "time"
) )
var inodeCnt uint64
type database struct { type database struct {
base string base string
vers []*version vers []*version
inodeCnt uint64
} }
func newDatabase(dir string) (*database, error) { func newDatabase(dir string) (*database, error) {
@ -94,7 +95,7 @@ func (this *database) buildVersions(base string, names []string) ([]*version, er
return nil, err return nil, err
} }
ver, err := newVersion(path.Join(base, name), timestamp, this, parent) ver, err := newVersion(path.Join(base, name), timestamp, parent)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -152,10 +153,6 @@ func (this *database) Root() (fs.Node, error) {
return this.lastVersion().root, nil return this.lastVersion().root, nil
} }
func (this *database) AllocInode() uint64 {
return atomic.AddUint64(&this.inodeCnt, 1)
}
func (this *database) buildVerName(timestamp time.Time) string { func (this *database) buildVerName(timestamp time.Time) string {
return fmt.Sprintf("ver_%.16x", timestamp.Unix()) return fmt.Sprintf("ver_%.16x", timestamp.Unix())
} }
@ -178,3 +175,7 @@ func (this *database) parseVerName(name string) (time.Time, error) {
return time.Unix(timestamp, 0), nil return time.Unix(timestamp, 0), nil
} }
func allocInode() uint64 {
return atomic.AddUint64(&inodeCnt, 1)
}

2
dir.go
View File

@ -43,7 +43,7 @@ func newVersionedDir(node *versionedNode, parent *versionedDir) *versionedDir {
dirs: make(map[string]*versionedDir), dirs: make(map[string]*versionedDir),
files: make(map[string]*versionedFile), files: make(map[string]*versionedFile),
node: node, node: node,
inode: node.ver.inodeAloc.AllocInode(), inode: allocInode(),
parent: parent} parent: parent}
} }

View File

@ -40,7 +40,7 @@ type versionedFile struct {
func newVersionedFile(node *versionedNode, parent *versionedDir) *versionedFile { func newVersionedFile(node *versionedNode, parent *versionedDir) *versionedFile {
return &versionedFile{ return &versionedFile{
node: node, node: node,
inode: node.ver.inodeAloc.AllocInode(), inode: allocInode(),
parent: parent} parent: parent}
} }

View File

@ -39,14 +39,9 @@ type version struct {
timestamp time.Time timestamp time.Time
meta *versionMetadata meta *versionMetadata
root *versionedDir root *versionedDir
inodeAloc InodeAllocator
} }
type InodeAllocator interface { func newVersion(base string, timestamp time.Time, parent *version) (*version, error) {
AllocInode() uint64
}
func newVersion(base string, timestamp time.Time, allocator InodeAllocator, parent *version) (*version, error) {
meta, err := newVersionMetadata(filepath.Join(base, "meta.json")) meta, err := newVersionMetadata(filepath.Join(base, "meta.json"))
if err != nil { if err != nil {
return nil, err return nil, err
@ -56,8 +51,7 @@ func newVersion(base string, timestamp time.Time, allocator InodeAllocator, pare
base: base, base: base,
parent: parent, parent: parent,
timestamp: timestamp, timestamp: timestamp,
meta: meta, meta: meta}
inodeAloc: allocator}
return ver, nil return ver, nil
} }