diff --git a/cache/db.go b/cache/db.go index 80765b6..f7c0767 100644 --- a/cache/db.go +++ b/cache/db.go @@ -5,6 +5,7 @@ import ( "github.com/jmhodges/levigo" "goposm/binary" "goposm/element" + "os" "path/filepath" ) @@ -14,6 +15,7 @@ type OSMCache struct { Ways *WaysCache Nodes *NodesCache Relations *RelationsCache + opened bool } func (c *OSMCache) Close() { @@ -35,29 +37,72 @@ func (c *OSMCache) Close() { } } -func NewOSMCache(dir string) (*OSMCache, error) { +func NewOSMCache(dir string) *OSMCache { cache := &OSMCache{Dir: dir} + return cache +} + +func (c *OSMCache) Open() error { var err error - cache.Coords, err = NewDeltaCoordsCache(filepath.Join(dir, "coords")) + c.Coords, err = NewDeltaCoordsCache(filepath.Join(c.Dir, "coords")) if err != nil { - return nil, err + return err } - cache.Nodes, err = NewNodesCache(filepath.Join(dir, "nodes")) + c.Nodes, err = NewNodesCache(filepath.Join(c.Dir, "nodes")) if err != nil { - cache.Close() - return nil, err + c.Close() + return err } - cache.Ways, err = NewWaysCache(filepath.Join(dir, "ways")) + c.Ways, err = NewWaysCache(filepath.Join(c.Dir, "ways")) if err != nil { - cache.Close() - return nil, err + c.Close() + return err } - cache.Relations, err = NewRelationsCache(filepath.Join(dir, "relations")) + c.Relations, err = NewRelationsCache(filepath.Join(c.Dir, "relations")) if err != nil { - cache.Close() - return nil, err + c.Close() + return err } - return cache, nil + c.opened = true + return nil +} + +func (c *OSMCache) Exists() bool { + if c.opened { + return true + } + if _, err := os.Stat(filepath.Join(c.Dir, "coords")); !os.IsNotExist(err) { + return true + } + if _, err := os.Stat(filepath.Join(c.Dir, "nodes")); !os.IsNotExist(err) { + return true + } + if _, err := os.Stat(filepath.Join(c.Dir, "ways")); !os.IsNotExist(err) { + return true + } + if _, err := os.Stat(filepath.Join(c.Dir, "relations")); !os.IsNotExist(err) { + return true + } + return false +} + +func (c *OSMCache) Remove() error { + if c.opened { + c.Close() + } + if err := os.RemoveAll(filepath.Join(c.Dir, "coords")); err != nil { + return err + } + if err := os.RemoveAll(filepath.Join(c.Dir, "nodes")); err != nil { + return err + } + if err := os.RemoveAll(filepath.Join(c.Dir, "ways")); err != nil { + return err + } + if err := os.RemoveAll(filepath.Join(c.Dir, "relations")); err != nil { + return err + } + return nil } type Cache struct { diff --git a/goposm.go b/goposm.go index 4b16974..aaa29e8 100644 --- a/goposm.go +++ b/goposm.go @@ -92,8 +92,12 @@ func parse(cache *cache.OSMCache, progress *stats.Statistics, filename string) { } var ( - cpuprofile = flag.String("cpuprofile", "", "filename of cpu profile output") - cachedir = flag.String("cachedir", "/tmp/goposm", "cache directory") + cpuprofile = flag.String("cpuprofile", "", "filename of cpu profile output") + cachedir = flag.String("cachedir", "/tmp/goposm", "cache directory") + overwritecache = flag.Bool("overwritecache", false, "overwritecache") + appendcache = flag.Bool("appendcache", false, "append cache") + read = flag.String("read", "", "read") + write = flag.Bool("write", false, "write") ) func main() { @@ -110,7 +114,21 @@ func main() { defer pprof.StopCPUProfile() } - osmCache, err := cache.NewOSMCache(*cachedir) + osmCache := cache.NewOSMCache(*cachedir) + + if *read != "" && osmCache.Exists() { + if *overwritecache { + log.Println("removing existing cache", *cachedir) + err := osmCache.Remove() + if err != nil { + log.Fatal("unable to remove cache:", err) + } + } else if !*appendcache { + log.Fatal("cache already exists use -appendcache or -overwritecache") + } + } + + err := osmCache.Open() if err != nil { log.Fatal(err) } @@ -119,40 +137,45 @@ func main() { fmt.Println("start") progress := stats.StatsReporter() - // parse(osmCache, progress, flag.Arg(0)) - - //rel := osmCache.Relations.Iter() - //for r := range rel { - //fmt.Println(r) - //} - - way := osmCache.Ways.Iter() - refCache, err := cache.NewRefIndex("/tmp/refindex") - if err != nil { - log.Fatal(err) + if *read != "" { + parse(osmCache, progress, *read) } - waitFill := sync.WaitGroup{} - for i := 0; i < runtime.NumCPU(); i++ { - waitFill.Add(1) + if *write { + rel := osmCache.Relations.Iter() + for r := range rel { + fmt.Println(r) + } - go func() { - for w := range way { - progress.AddWays(-1) - ok := osmCache.Coords.FillWay(w) - if !ok { - continue - } - if true { - for _, node := range w.Nodes { - refCache.Add(node.Id, w.Id) + way := osmCache.Ways.Iter() + refCache, err := cache.NewRefIndex("/tmp/refindex") + if err != nil { + log.Fatal(err) + } + + waitFill := sync.WaitGroup{} + for i := 0; i < runtime.NumCPU(); i++ { + waitFill.Add(1) + + go func() { + for w := range way { + progress.AddWays(-1) + ok := osmCache.Coords.FillWay(w) + if !ok { + continue + } + if true { + for _, node := range w.Nodes { + refCache.Add(node.Id, w.Id) + } } } - } - waitFill.Done() - }() + waitFill.Done() + }() + } + waitFill.Wait() } - waitFill.Wait() + //parser.PBFStats(os.Args[1]) fmt.Println("done") } diff --git a/stats/stats.go b/stats/stats.go index 0b6b14a..35598fc 100644 --- a/stats/stats.go +++ b/stats/stats.go @@ -64,7 +64,7 @@ func (c *counter) Print() { waysPS := float64(c.ways-c.lastWays) / dur.Seconds() relationsPS := float64(c.relations-c.lastRelations) / dur.Seconds() - fmt.Printf("Coords: %8.1f (%10d) Nodes: %6.1f (%9d) Ways: %6.1f (%8d) Relations: %6.1f (%7d)\n", + fmt.Printf("Coords: %8.1f (%10d) Nodes: %6.1f (%9d) Ways: %6.1f (%8d) Relations: %6.1f (%7d)\r\b", coordsPS, c.coords, nodesPS,