create OSMCache
parent
e543b05a16
commit
6ab604da0e
|
@ -5,8 +5,61 @@ import (
|
|||
"github.com/jmhodges/levigo"
|
||||
"goposm/binary"
|
||||
"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 {
|
||||
db *levigo.DB
|
||||
wo *levigo.WriteOptions
|
||||
|
@ -27,6 +80,10 @@ func (c *Cache) open(path string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) close() {
|
||||
c.db.Close()
|
||||
}
|
||||
|
||||
type NodesCache struct {
|
||||
Cache
|
||||
}
|
||||
|
|
57
parser.go
57
parser.go
|
@ -13,7 +13,7 @@ import (
|
|||
"sync"
|
||||
)
|
||||
|
||||
func parse(filename string) {
|
||||
func parse(cache *cache.OSMCache, filename string) {
|
||||
nodes := make(chan []element.Node)
|
||||
coords := make(chan []element.Node)
|
||||
ways := make(chan []element.Way)
|
||||
|
@ -33,68 +33,49 @@ func parse(filename string) {
|
|||
}
|
||||
|
||||
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++ {
|
||||
waitCounter.Add(1)
|
||||
go func() {
|
||||
wayCounter := 0
|
||||
for ws := range ways {
|
||||
wayCache.PutWays(ws)
|
||||
cache.Ways.PutWays(ws)
|
||||
wayCounter += len(ws)
|
||||
}
|
||||
fmt.Println("ways", wayCounter)
|
||||
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++ {
|
||||
waitCounter.Add(1)
|
||||
go func() {
|
||||
relationCounter := 0
|
||||
for rels := range relations {
|
||||
relCache.PutRelations(rels)
|
||||
cache.Relations.PutRelations(rels)
|
||||
relationCounter += len(rels)
|
||||
}
|
||||
fmt.Println("relations", relationCounter)
|
||||
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++ {
|
||||
waitCounter.Add(1)
|
||||
go func() {
|
||||
nodeCounter := 0
|
||||
for nds := range coords {
|
||||
coordCache.PutCoords(nds)
|
||||
cache.Coords.PutCoords(nds)
|
||||
nodeCounter += len(nds)
|
||||
}
|
||||
fmt.Println("coords", nodeCounter)
|
||||
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++ {
|
||||
waitCounter.Add(1)
|
||||
go func() {
|
||||
nodeCounter := 0
|
||||
for nds := range nodes {
|
||||
n, _ := nodeCache.PutNodes(nds)
|
||||
n, _ := cache.Nodes.PutNodes(nds)
|
||||
nodeCounter += n
|
||||
}
|
||||
fmt.Println("nodes", nodeCounter)
|
||||
|
@ -120,36 +101,24 @@ func main() {
|
|||
log.SetFlags(log.LstdFlags | log.Llongfile)
|
||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||
flag.Parse()
|
||||
//parse(flag.Arg(0))
|
||||
|
||||
relCache, err := cache.NewRelationsCache("/tmp/goposm/relation.cache")
|
||||
cache, err := cache.NewOSMCache("/tmp/goposm")
|
||||
if err != nil {
|
||||
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 {
|
||||
fmt.Println(r)
|
||||
}
|
||||
|
||||
wayCache, err := cache.NewWaysCache("/tmp/goposm/way.cache")
|
||||
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()
|
||||
way := cache.Ways.Iter()
|
||||
i := 0
|
||||
for w := range way {
|
||||
i += 1
|
||||
coordCache.FillWay(w)
|
||||
cache.Coords.FillWay(w)
|
||||
//fmt.Println(i)
|
||||
}
|
||||
fmt.Println(i)
|
||||
|
|
Loading…
Reference in New Issue