imposm3/cache/osm.go

223 lines
4.5 KiB
Go
Raw Permalink Normal View History

2013-05-17 17:30:22 +04:00
package cache
import (
bin "encoding/binary"
"errors"
"os"
"path/filepath"
"github.com/jmhodges/levigo"
"github.com/omniscale/imposm3/element"
2013-05-17 17:30:22 +04:00
)
var (
NotFound = errors.New("not found")
)
2013-11-04 11:51:21 +04:00
const SKIP int64 = -1
2013-05-17 17:30:22 +04:00
type OSMCache struct {
dir string
Coords *DeltaCoordsCache
Ways *WaysCache
Nodes *NodesCache
Relations *RelationsCache
opened bool
2013-05-17 17:30:22 +04:00
}
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 {
2013-07-04 19:27:22 +04:00
cache := &OSMCache{dir: dir}
2013-05-17 17:30:22 +04:00
return cache
}
func (c *OSMCache) Open() error {
2013-07-04 19:27:22 +04:00
err := os.MkdirAll(c.dir, 0755)
2013-05-17 17:30:22 +04:00
if err != nil {
return err
}
2013-07-04 19:27:22 +04:00
c.Coords, err = newDeltaCoordsCache(filepath.Join(c.dir, "coords"))
2013-05-17 17:30:22 +04:00
if err != nil {
return err
}
2013-07-04 19:27:22 +04:00
c.Nodes, err = newNodesCache(filepath.Join(c.dir, "nodes"))
2013-05-17 17:30:22 +04:00
if err != nil {
c.Close()
return err
}
2013-07-04 19:27:22 +04:00
c.Ways, err = newWaysCache(filepath.Join(c.dir, "ways"))
2013-05-17 17:30:22 +04:00
if err != nil {
c.Close()
return err
}
2013-07-04 19:27:22 +04:00
c.Relations, err = newRelationsCache(filepath.Join(c.dir, "relations"))
2013-05-17 17:30:22 +04:00
if err != nil {
c.Close()
return err
}
c.opened = true
return nil
}
func (c *OSMCache) Exists() bool {
if c.opened {
return true
}
2013-07-04 19:27:22 +04:00
if _, err := os.Stat(filepath.Join(c.dir, "coords")); !os.IsNotExist(err) {
2013-05-17 17:30:22 +04:00
return true
}
2013-07-04 19:27:22 +04:00
if _, err := os.Stat(filepath.Join(c.dir, "nodes")); !os.IsNotExist(err) {
2013-05-17 17:30:22 +04:00
return true
}
2013-07-04 19:27:22 +04:00
if _, err := os.Stat(filepath.Join(c.dir, "ways")); !os.IsNotExist(err) {
2013-05-17 17:30:22 +04:00
return true
}
2013-07-04 19:27:22 +04:00
if _, err := os.Stat(filepath.Join(c.dir, "relations")); !os.IsNotExist(err) {
2013-05-17 17:30:22 +04:00
return true
}
2013-07-04 19:27:22 +04:00
if _, err := os.Stat(filepath.Join(c.dir, "inserted_ways")); !os.IsNotExist(err) {
2013-05-17 17:30:22 +04:00
return true
}
return false
}
func (c *OSMCache) Remove() error {
if c.opened {
c.Close()
}
2013-07-04 19:27:22 +04:00
if err := os.RemoveAll(filepath.Join(c.dir, "coords")); err != nil {
2013-05-17 17:30:22 +04:00
return err
}
2013-07-04 19:27:22 +04:00
if err := os.RemoveAll(filepath.Join(c.dir, "nodes")); err != nil {
2013-05-17 17:30:22 +04:00
return err
}
2013-07-04 19:27:22 +04:00
if err := os.RemoveAll(filepath.Join(c.dir, "ways")); err != nil {
2013-05-17 17:30:22 +04:00
return err
}
2013-07-04 19:27:22 +04:00
if err := os.RemoveAll(filepath.Join(c.dir, "relations")); err != nil {
2013-05-17 17:30:22 +04:00
return err
}
2013-07-04 19:27:22 +04:00
if err := os.RemoveAll(filepath.Join(c.dir, "inserted_ways")); err != nil {
2013-05-17 17:30:22 +04:00
return err
}
return nil
}
// FirstMemberIsCached checks whether the first way or node member is cached.
// Also returns true if there are no members of type WAY or NODE.
func (c *OSMCache) FirstMemberIsCached(members []element.Member) (bool, error) {
for _, m := range members {
if m.Type == element.WAY {
_, err := c.Ways.GetWay(m.Id)
if err == NotFound {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
} else if m.Type == element.NODE {
_, err := c.Coords.GetCoord(m.Id)
if err == NotFound {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
}
return true, nil
}
2013-07-04 19:27:22 +04:00
type cache struct {
2013-06-18 17:53:04 +04:00
db *levigo.DB
2013-07-04 19:27:22 +04:00
options *cacheOptions
2013-06-18 17:53:04 +04:00
cache *levigo.Cache
wo *levigo.WriteOptions
ro *levigo.ReadOptions
2013-05-17 17:30:22 +04:00
}
2013-07-04 19:27:22 +04:00
func (c *cache) open(path string) error {
2013-05-17 17:30:22 +04:00
opts := levigo.NewOptions()
opts.SetCreateIfMissing(true)
2013-06-18 17:53:04 +04:00
if c.options.CacheSizeM > 0 {
c.cache = levigo.NewLRUCache(c.options.CacheSizeM * 1024 * 1024)
opts.SetCache(c.cache)
2013-05-17 17:30:22 +04:00
}
2013-06-18 17:53:04 +04:00
if c.options.MaxOpenFiles > 0 {
opts.SetMaxOpenFiles(c.options.MaxOpenFiles)
2013-05-17 17:30:22 +04:00
}
2013-06-18 17:53:04 +04:00
if c.options.BlockRestartInterval > 0 {
opts.SetBlockRestartInterval(c.options.BlockRestartInterval)
}
if c.options.WriteBufferSizeM > 0 {
opts.SetWriteBufferSize(c.options.WriteBufferSizeM * 1024 * 1024)
}
if c.options.BlockSizeK > 0 {
2013-11-13 16:20:06 +04:00
opts.SetBlockSize(c.options.BlockSizeK * 1024)
2013-06-18 17:53:04 +04:00
}
if c.options.MaxFileSizeM > 0 {
// max file size option is only available with LevelDB 1.21 and higher
// build with -tags="ldppost121" to enable this option.
setMaxFileSize(opts, c.options.MaxFileSizeM*1024*1024)
}
2013-06-18 17:53:04 +04:00
2013-05-17 17:30:22 +04:00
db, err := levigo.Open(path, opts)
if err != nil {
return err
}
c.db = db
c.wo = levigo.NewWriteOptions()
c.ro = levigo.NewReadOptions()
2013-05-17 17:30:22 +04:00
return nil
}
func idToKeyBuf(id int64) []byte {
2013-06-04 11:34:24 +04:00
b := make([]byte, 8)
bin.BigEndian.PutUint64(b, uint64(id))
2013-06-03 15:12:20 +04:00
return b[:8]
2013-05-17 17:30:22 +04:00
}
func idFromKeyBuf(buf []byte) int64 {
return int64(bin.BigEndian.Uint64(buf))
}
2013-07-04 19:27:22 +04:00
func (c *cache) Close() {
2013-11-19 18:07:26 +04:00
if c.ro != nil {
c.ro.Close()
c.ro = nil
}
if c.wo != nil {
c.wo.Close()
c.wo = nil
}
2013-06-20 12:34:22 +04:00
if c.db != nil {
c.db.Close()
c.db = nil
}
2013-06-18 17:53:04 +04:00
if c.cache != nil {
c.cache.Close()
2013-06-20 12:34:22 +04:00
c.cache = nil
2013-06-18 17:53:04 +04:00
}
2013-05-17 17:30:22 +04:00
}