This commit is contained in:
Alex Yatskov 2015-05-10 16:15:17 +09:00
parent 132179b904
commit b328e58e6f
3 changed files with 41 additions and 29 deletions

View File

@ -27,13 +27,13 @@ import (
"path/filepath" "path/filepath"
) )
type Database struct { type database struct {
base string base string
versions []*Version vers []*version
} }
func NewDatabase(dir string) (*Database, error) { func newDatabase(dir string) (*database, error) {
db := &Database{base: dir} db := &database{base: dir}
if err := db.load(dir); err != nil { if err := db.load(dir); err != nil {
return nil, err return nil, err
} }
@ -41,7 +41,7 @@ func NewDatabase(dir string) (*Database, error) {
return db, nil return db, nil
} }
func (this *Database) load(dir string) error { func (this *database) load(dir string) error {
base, err := filepath.Abs(dir) base, err := filepath.Abs(dir)
if err != nil { if err != nil {
return err return err
@ -52,35 +52,39 @@ func (this *Database) load(dir string) error {
return err return err
} }
versions, err := this.version(dirs) vers, err := this.version(dirs)
if err != nil { if err != nil {
return err return err
} }
this.base = base this.base = base
this.versions = versions this.vers = vers
return nil return nil
} }
func (this *Database) version(dirs []string) ([]*Version, error) { func (this *database) save() error {
var versions []*Version return nil
var parent *Version }
func (this *database) version(dirs []string) ([]*version, error) {
var vers []*version
var parent *version
for _, dir := range dirs { for _, dir := range dirs {
version, err := NewVersion(dir, parent) ver, err := newVersion(dir, parent)
if err != nil { if err != nil {
return nil, err return nil, err
} }
parent = version vers = append(vers, ver)
versions = append(versions, version) parent = ver
} }
return versions, nil return vers, nil
} }
func (this *Database) scan(dir string) ([]string, error) { func (this *database) scan(dir string) ([]string, error) {
nodes, err := ioutil.ReadDir(dir) nodes, err := ioutil.ReadDir(dir)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -33,18 +33,18 @@ import (
"time" "time"
) )
type Metadata struct { type metadata struct {
Deleted []string Deleted []string
} }
type Version struct { type version struct {
base string base string
parent *Version parent *version
timestamp time.Time timestamp time.Time
metadata Metadata meta metadata
} }
func NewVersion(base string, parent *Version) (*Version, error) { func newVersion(base string, parent *version) (*version, error) {
re, err := regexp.Compile(`/vfs_([0-9a-f])$`) re, err := regexp.Compile(`/vfs_([0-9a-f])$`)
if err != nil { if err != nil {
return nil, err return nil, err
@ -60,7 +60,7 @@ func NewVersion(base string, parent *Version) (*Version, error) {
return nil, err return nil, err
} }
version := &Version{ version := &version{
base: base, base: base,
parent: parent, parent: parent,
timestamp: time.Unix(timeval, 0)} timestamp: time.Unix(timeval, 0)}
@ -68,7 +68,7 @@ func NewVersion(base string, parent *Version) (*Version, error) {
return version, nil return version, nil
} }
func (this *Version) loadMetadata() error { func (this *version) loadMetadata() error {
path := this.metadataPath() path := this.metadataPath()
if _, err := os.Stat(path); os.IsNotExist(err) { if _, err := os.Stat(path); os.IsNotExist(err) {
return nil return nil
@ -79,15 +79,15 @@ func (this *Version) loadMetadata() error {
return err return err
} }
if err := json.Unmarshal(bytes, &this.metadata); err != nil { if err := json.Unmarshal(bytes, &this.meta); err != nil {
return err return err
} }
return nil return nil
} }
func (this *Version) saveMetadata() error { func (this *version) saveMetadata() error {
js, err := json.Marshal(this.metadata) js, err := json.Marshal(this.meta)
if err != nil { if err != nil {
return err return err
} }
@ -95,6 +95,6 @@ func (this *Version) saveMetadata() error {
return ioutil.WriteFile(this.metadataPath(), js, 0644) return ioutil.WriteFile(this.metadataPath(), js, 0644)
} }
func (this *Version) metadataPath() string { func (this *version) metadataPath() string {
return filepath.Join(this.base, "meta.json") return filepath.Join(this.base, "meta.json")
} }

12
vfs.go
View File

@ -22,7 +22,15 @@
package main package main
import (
"log"
)
func main() { func main() {
var db Database db, err := newDatabase("fs")
db.load("fs") if err != nil {
log.Fatal(err)
}
db.save()
} }