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"
)
type Database struct {
base string
versions []*Version
type database struct {
base string
vers []*version
}
func NewDatabase(dir string) (*Database, error) {
db := &Database{base: dir}
func newDatabase(dir string) (*database, error) {
db := &database{base: dir}
if err := db.load(dir); err != nil {
return nil, err
}
@ -41,7 +41,7 @@ func NewDatabase(dir string) (*Database, error) {
return db, nil
}
func (this *Database) load(dir string) error {
func (this *database) load(dir string) error {
base, err := filepath.Abs(dir)
if err != nil {
return err
@ -52,35 +52,39 @@ func (this *Database) load(dir string) error {
return err
}
versions, err := this.version(dirs)
vers, err := this.version(dirs)
if err != nil {
return err
}
this.base = base
this.versions = versions
this.vers = vers
return nil
}
func (this *Database) version(dirs []string) ([]*Version, error) {
var versions []*Version
var parent *Version
func (this *database) save() error {
return nil
}
func (this *database) version(dirs []string) ([]*version, error) {
var vers []*version
var parent *version
for _, dir := range dirs {
version, err := NewVersion(dir, parent)
ver, err := newVersion(dir, parent)
if err != nil {
return nil, err
}
parent = version
versions = append(versions, version)
vers = append(vers, ver)
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)
if err != nil {
return nil, err

View File

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

12
vfs.go
View File

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