create OSMCache

master
Oliver Tonnhofer 2013-05-04 16:27:05 +02:00
parent e543b05a16
commit 6ab604da0e
2 changed files with 70 additions and 44 deletions

57
cache/db.go vendored
View File

@ -5,8 +5,61 @@ import (
"github.com/jmhodges/levigo" "github.com/jmhodges/levigo"
"goposm/binary" "goposm/binary"
"goposm/element" "goposm/element"
"path/filepath"
) )
type OSMCache struct {
Dir string
Coords *DeltaCoordsCache
Ways *WaysCache
Nodes *NodesCache
Relations *RelationsCache
}
func (c *OSMCache) Close() {
if c.Coords != nil {
c.Coords.close()
c.Coords = nil
}
if c.Nodes != nil {
c.Nodes.close()
c.Nodes = nil
}
if c.Ways != nil {
c.Ways.close()
c.Ways = nil
}
if c.Relations != nil {
c.Relations.close()
c.Relations = nil
}
}
func NewOSMCache(dir string) (*OSMCache, error) {
cache := &OSMCache{Dir: dir}
var err error
cache.Coords, err = NewDeltaCoordsCache(filepath.Join(dir, "coords"))
if err != nil {
return nil, err
}
cache.Nodes, err = NewNodesCache(filepath.Join(dir, "nodes"))
if err != nil {
cache.Close()
return nil, err
}
cache.Ways, err = NewWaysCache(filepath.Join(dir, "ways"))
if err != nil {
cache.Close()
return nil, err
}
cache.Relations, err = NewRelationsCache(filepath.Join(dir, "relations"))
if err != nil {
cache.Close()
return nil, err
}
return cache, nil
}
type Cache struct { type Cache struct {
db *levigo.DB db *levigo.DB
wo *levigo.WriteOptions wo *levigo.WriteOptions
@ -27,6 +80,10 @@ func (c *Cache) open(path string) error {
return nil return nil
} }
func (c *Cache) close() {
c.db.Close()
}
type NodesCache struct { type NodesCache struct {
Cache Cache
} }

View File

@ -13,7 +13,7 @@ import (
"sync" "sync"
) )
func parse(filename string) { func parse(cache *cache.OSMCache, filename string) {
nodes := make(chan []element.Node) nodes := make(chan []element.Node)
coords := make(chan []element.Node) coords := make(chan []element.Node)
ways := make(chan []element.Way) ways := make(chan []element.Way)
@ -33,68 +33,49 @@ func parse(filename string) {
} }
waitCounter := sync.WaitGroup{} waitCounter := sync.WaitGroup{}
wayCache, err := cache.NewWaysCache("/tmp/goposm/way.cache")
if err != nil {
log.Fatal(err)
}
defer wayCache.Close()
for i := 0; i < runtime.NumCPU(); i++ { for i := 0; i < runtime.NumCPU(); i++ {
waitCounter.Add(1) waitCounter.Add(1)
go func() { go func() {
wayCounter := 0 wayCounter := 0
for ws := range ways { for ws := range ways {
wayCache.PutWays(ws) cache.Ways.PutWays(ws)
wayCounter += len(ws) wayCounter += len(ws)
} }
fmt.Println("ways", wayCounter) fmt.Println("ways", wayCounter)
waitCounter.Done() waitCounter.Done()
}() }()
} }
relCache, err := cache.NewRelationsCache("/tmp/goposm/relation.cache")
if err != nil {
log.Fatal(err)
}
defer relCache.Close()
for i := 0; i < runtime.NumCPU(); i++ { for i := 0; i < runtime.NumCPU(); i++ {
waitCounter.Add(1) waitCounter.Add(1)
go func() { go func() {
relationCounter := 0 relationCounter := 0
for rels := range relations { for rels := range relations {
relCache.PutRelations(rels) cache.Relations.PutRelations(rels)
relationCounter += len(rels) relationCounter += len(rels)
} }
fmt.Println("relations", relationCounter) fmt.Println("relations", relationCounter)
waitCounter.Done() waitCounter.Done()
}() }()
} }
coordCache, err := cache.NewDeltaCoordsCache("/tmp/goposm/coords.cache")
if err != nil {
log.Fatal(err)
}
defer coordCache.Close()
for i := 0; i < runtime.NumCPU(); i++ { for i := 0; i < runtime.NumCPU(); i++ {
waitCounter.Add(1) waitCounter.Add(1)
go func() { go func() {
nodeCounter := 0 nodeCounter := 0
for nds := range coords { for nds := range coords {
coordCache.PutCoords(nds) cache.Coords.PutCoords(nds)
nodeCounter += len(nds) nodeCounter += len(nds)
} }
fmt.Println("coords", nodeCounter) fmt.Println("coords", nodeCounter)
waitCounter.Done() waitCounter.Done()
}() }()
} }
nodeCache, err := cache.NewNodesCache("/tmp/goposm/node.cache")
if err != nil {
log.Fatal(err)
}
defer nodeCache.Close()
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
waitCounter.Add(1) waitCounter.Add(1)
go func() { go func() {
nodeCounter := 0 nodeCounter := 0
for nds := range nodes { for nds := range nodes {
n, _ := nodeCache.PutNodes(nds) n, _ := cache.Nodes.PutNodes(nds)
nodeCounter += n nodeCounter += n
} }
fmt.Println("nodes", nodeCounter) fmt.Println("nodes", nodeCounter)
@ -120,36 +101,24 @@ func main() {
log.SetFlags(log.LstdFlags | log.Llongfile) log.SetFlags(log.LstdFlags | log.Llongfile)
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse() flag.Parse()
//parse(flag.Arg(0)) cache, err := cache.NewOSMCache("/tmp/goposm")
relCache, err := cache.NewRelationsCache("/tmp/goposm/relation.cache")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer relCache.Close() defer cache.Close()
rel := relCache.Iter() parse(cache, flag.Arg(0))
rel := cache.Relations.Iter()
for r := range rel { for r := range rel {
fmt.Println(r) fmt.Println(r)
} }
wayCache, err := cache.NewWaysCache("/tmp/goposm/way.cache") way := cache.Ways.Iter()
if err != nil {
log.Fatal(err)
}
defer wayCache.Close()
coordCache, err := cache.NewDeltaCoordsCache("/tmp/goposm/coords.cache")
if err != nil {
log.Fatal(err)
}
defer coordCache.Close()
way := wayCache.Iter()
i := 0 i := 0
for w := range way { for w := range way {
i += 1 i += 1
coordCache.FillWay(w) cache.Coords.FillWay(w)
//fmt.Println(i) //fmt.Println(i)
} }
fmt.Println(i) fmt.Println(i)