refactored database API

master
Oliver Tonnhofer 2013-10-29 16:32:16 +01:00
parent abb7734f8c
commit 93edef2681
4 changed files with 33 additions and 40 deletions

View File

@ -8,7 +8,6 @@ import (
) )
type Config struct { type Config struct {
Type string
ConnectionParams string ConnectionParams string
Srid int Srid int
} }
@ -27,9 +26,14 @@ type BulkBeginner interface {
} }
type Inserter interface { type Inserter interface {
// ProbeXxx returns true if the element should be inserted.
// The interface{} value is passed to InsertXxx when that element
// gets inserted (can be used to pass a match object to the insert call).
ProbePoint(element.OSMElem) (bool, interface{}) ProbePoint(element.OSMElem) (bool, interface{})
ProbeLineString(element.OSMElem) (bool, interface{}) ProbeLineString(element.OSMElem) (bool, interface{})
ProbePolygon(element.OSMElem) (bool, interface{}) ProbePolygon(element.OSMElem) (bool, interface{})
// InsertXxx inserts element of that type into the database.
// element.Geom is set to that type.
InsertPoint(element.OSMElem, interface{}) InsertPoint(element.OSMElem, interface{})
InsertLineString(element.OSMElem, interface{}) InsertLineString(element.OSMElem, interface{})
InsertPolygon(element.OSMElem, interface{}) InsertPolygon(element.OSMElem, interface{})
@ -71,9 +75,12 @@ func Register(name string, f func(Config, *mapping.Mapping) (DB, error)) {
} }
func Open(conf Config, m *mapping.Mapping) (DB, error) { func Open(conf Config, m *mapping.Mapping) (DB, error) {
newFunc, ok := databases[conf.Type] parts := strings.SplitN(conf.ConnectionParams, ":", 2)
connectionType := parts[0]
newFunc, ok := databases[connectionType]
if !ok { if !ok {
return nil, errors.New("unsupported database type: " + conf.Type) return nil, errors.New("unsupported database type: " + connectionType)
} }
db, err := newFunc(conf, m) db, err := newFunc(conf, m)
@ -83,29 +90,25 @@ func Open(conf Config, m *mapping.Mapping) (DB, error) {
return db, nil return db, nil
} }
func ConnectionType(param string) string { // nullDb is a dummy database that imports into /dev/null
parts := strings.SplitN(param, ":", 2) type nullDb struct{}
return parts[0]
}
type NullDb struct{} func (n *nullDb) Init() error { return nil }
func (n *nullDb) Begin() error { return nil }
func (n *nullDb) End() error { return nil }
func (n *nullDb) Close() error { return nil }
func (n *nullDb) Abort() error { return nil }
func (n *nullDb) InsertPoint(element.OSMElem, interface{}) {}
func (n *nullDb) InsertLineString(element.OSMElem, interface{}) {}
func (n *nullDb) InsertPolygon(element.OSMElem, interface{}) {}
func (n *nullDb) ProbePoint(element.OSMElem) (bool, interface{}) { return true, nil }
func (n *nullDb) ProbeLineString(element.OSMElem) (bool, interface{}) { return true, nil }
func (n *nullDb) ProbePolygon(element.OSMElem) (bool, interface{}) { return true, nil }
func (n *NullDb) Init() error { return nil } func newNullDb(conf Config, m *mapping.Mapping) (DB, error) {
func (n *NullDb) Begin() error { return nil } return &nullDb{}, nil
func (n *NullDb) End() error { return nil }
func (n *NullDb) Close() error { return nil }
func (n *NullDb) Abort() error { return nil }
func (n *NullDb) InsertPoint(element.OSMElem, interface{}) {}
func (n *NullDb) InsertLineString(element.OSMElem, interface{}) {}
func (n *NullDb) InsertPolygon(element.OSMElem, interface{}) {}
func (n *NullDb) ProbePoint(element.OSMElem) (bool, interface{}) { return true, nil }
func (n *NullDb) ProbeLineString(element.OSMElem) (bool, interface{}) { return true, nil }
func (n *NullDb) ProbePolygon(element.OSMElem) (bool, interface{}) { return true, nil }
func NewNullDb(conf Config, m *mapping.Mapping) (DB, error) {
return &NullDb{}, nil
} }
func init() { func init() {
Register("null", NewNullDb) Register("null", newNullDb)
} }

View File

@ -59,9 +59,7 @@ func Update(oscFile string, geometryLimiter *limit.Limiter, force bool) {
log.Fatal(err) log.Fatal(err)
} }
connType := database.ConnectionType(config.BaseOptions.Connection)
dbConf := database.Config{ dbConf := database.Config{
Type: connType,
ConnectionParams: config.BaseOptions.Connection, ConnectionParams: config.BaseOptions.Connection,
Srid: config.BaseOptions.Srid, Srid: config.BaseOptions.Srid,
} }
@ -258,9 +256,9 @@ For:
close(ways) close(ways)
close(nodes) close(nodes)
nodeWriter.Close() nodeWriter.Wait()
relWriter.Close() relWriter.Wait()
wayWriter.Close() wayWriter.Wait()
if genDb != nil { if genDb != nil {
genDb.GeneralizeUpdates() genDb.GeneralizeUpdates()

View File

@ -153,9 +153,7 @@ func mainimport() {
if config.BaseOptions.Connection == "" { if config.BaseOptions.Connection == "" {
log.Fatal("missing connection option") log.Fatal("missing connection option")
} }
connType := database.ConnectionType(config.BaseOptions.Connection)
conf := database.Config{ conf := database.Config{
Type: connType,
ConnectionParams: config.BaseOptions.Connection, ConnectionParams: config.BaseOptions.Connection,
Srid: config.BaseOptions.Srid, Srid: config.BaseOptions.Srid,
} }
@ -256,9 +254,7 @@ func mainimport() {
db, progress, config.BaseOptions.Srid) db, progress, config.BaseOptions.Srid)
relWriter.SetLimiter(geometryLimiter) relWriter.SetLimiter(geometryLimiter)
relWriter.Start() relWriter.Start()
relWriter.Wait() // blocks till the Relations.Iter() finishes
// blocks till the Relations.Iter() finishes
relWriter.Close()
osmCache.Relations.Close() osmCache.Relations.Close()
ways := osmCache.Ways.Iter() ways := osmCache.Ways.Iter()
@ -266,9 +262,7 @@ func mainimport() {
progress, config.BaseOptions.Srid) progress, config.BaseOptions.Srid)
wayWriter.SetLimiter(geometryLimiter) wayWriter.SetLimiter(geometryLimiter)
wayWriter.Start() wayWriter.Start()
wayWriter.Wait() // blocks till the Ways.Iter() finishes
// blocks till the Ways.Iter() finishes
wayWriter.Close()
osmCache.Ways.Close() osmCache.Ways.Close()
nodes := osmCache.Nodes.Iter() nodes := osmCache.Nodes.Iter()
@ -276,9 +270,7 @@ func mainimport() {
progress, config.BaseOptions.Srid) progress, config.BaseOptions.Srid)
nodeWriter.SetLimiter(geometryLimiter) nodeWriter.SetLimiter(geometryLimiter)
nodeWriter.Start() nodeWriter.Start()
nodeWriter.Wait() // blocks till the Nodes.Iter() finishes
// blocks till the Nodes.Iter() finishes
nodeWriter.Close()
osmCache.Close() osmCache.Close()
err = db.End() err = db.End()

View File

@ -45,6 +45,6 @@ func (writer *OsmElemWriter) SetExpireTiles(expireTiles *expire.Tiles) {
writer.expireTiles = expireTiles writer.expireTiles = expireTiles
} }
func (writer *OsmElemWriter) Close() { func (writer *OsmElemWriter) Wait() {
writer.wg.Wait() writer.wg.Wait()
} }