Work in progress

This commit is contained in:
Alex Yatskov 2015-05-10 14:14:15 +09:00
parent 0f0a8e29f9
commit c35a348bf9
2 changed files with 48 additions and 54 deletions

View File

@ -23,18 +23,13 @@
package main package main
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"regexp"
"strconv"
"strings"
"time"
) )
type Database struct { type Database struct {
base string base string
versions []Version versions []*Version
} }
func NewDatabase(dir string) (*Database, error) { func NewDatabase(dir string) (*Database, error) {
@ -52,65 +47,51 @@ func (this *Database) load(dir string) error {
return err return err
} }
this.base = base dirs, err := this.scan(base)
dirs, err := this.scan(dir)
if err != nil { if err != nil {
return err return err
} }
this.version(dirs) versions, err := this.version(dirs)
if err != nil {
return err
}
this.base = base
this.versions = versions
return nil return nil
} }
func (this *Database) version(dirs []string) []Version { func (this *Database) version(dirs []string) ([]*Version, error) {
versions := make([]Version, 0, len(dirs)) var versions []*Version
var parent *Version
// var parent *Version for _, dir := range dirs {
// for _, dir := range dirs { version, err := NewVersion(dir, parent)
// base := filepath.Join(this.base, dir) if err != nil {
// // timestamp := this.timestamp(dir) return nil, err
// // version := NewVersion(base, timestamp, parent) }
// // parent = version
// }
return versions parent = version
versions = append(versions, version)
}
return versions, nil
} }
func (this *Database) scan(dir string) ([]VersionMeta, 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
} }
re, err := regexp.Compile(`^vfs_([0-9a-f])$`) var dirs []string
var meta []VersionMeta
for _, node := range nodes { for _, node := range nodes {
if !node.IsDir() { if node.IsDir() {
continue dirs = append(dirs, node.Name())
} }
matches := re.FindStringSubmatch(node.Name())
if len(matches) < 2 {
continue
}
timestamp, err := strconv.ParseInt(matches[1], 16, 64)
if err != nil {
continue
}
item := VersionMeta{
path: filepath.Join(dir, matches[0]),
timestamp: time.Unix(timestamp, 0)}
meta = append(meta, item)
} }
return meta, nil return dirs, nil
}
func (this *Database) scanProps(path string) error {
} }

View File

@ -23,20 +23,33 @@
package main package main
import ( import (
"fmt"
"regexp"
"strconv"
"time" "time"
) )
type VersionMeta struct {
path string
timestamp time.Time
}
type Version struct { type Version struct {
base string base string
parent *Version parent *Version
timestamp time.Time timestamp time.Time
} }
func NewVersion(base string, timestamp time.Time, parent *Version) *Version { func NewVersion(base string, parent *Version) (*Version, error) {
return &Version{base, parent, timestamp} re, err := regexp.Compile(`/vfs_([0-9a-f])$`)
if err != nil {
return nil, err
}
matches := re.FindStringSubmatch(base)
if len(matches) < 2 {
return nil, fmt.Errorf("invalid version identifier for %s", base)
}
timeval, err := strconv.ParseInt(matches[1], 16, 64)
if err != nil {
return nil, err
}
return &Version{base: base, parent: parent, timestamp: time.Unix(timeval, 0)}, nil
} }