From 93edef26817df52574251d6fe769ead6352b049a Mon Sep 17 00:00:00 2001 From: Oliver Tonnhofer Date: Tue, 29 Oct 2013 16:32:16 +0100 Subject: [PATCH] refactored database API --- database/database.go | 49 +++++++++++++++++++++++--------------------- diff/process.go | 8 +++----- imposm3.go | 14 +++---------- writer/writer.go | 2 +- 4 files changed, 33 insertions(+), 40 deletions(-) diff --git a/database/database.go b/database/database.go index 7a1ca61..aa7d242 100644 --- a/database/database.go +++ b/database/database.go @@ -8,7 +8,6 @@ import ( ) type Config struct { - Type string ConnectionParams string Srid int } @@ -27,9 +26,14 @@ type BulkBeginner 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{}) ProbeLineString(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{}) InsertLineString(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) { - newFunc, ok := databases[conf.Type] + parts := strings.SplitN(conf.ConnectionParams, ":", 2) + connectionType := parts[0] + + newFunc, ok := databases[connectionType] if !ok { - return nil, errors.New("unsupported database type: " + conf.Type) + return nil, errors.New("unsupported database type: " + connectionType) } db, err := newFunc(conf, m) @@ -83,29 +90,25 @@ func Open(conf Config, m *mapping.Mapping) (DB, error) { return db, nil } -func ConnectionType(param string) string { - parts := strings.SplitN(param, ":", 2) - return parts[0] -} +// nullDb is a dummy database that imports into /dev/null +type nullDb struct{} -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 (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 NewNullDb(conf Config, m *mapping.Mapping) (DB, error) { - return &NullDb{}, nil +func newNullDb(conf Config, m *mapping.Mapping) (DB, error) { + return &nullDb{}, nil } func init() { - Register("null", NewNullDb) + Register("null", newNullDb) } diff --git a/diff/process.go b/diff/process.go index 975f76e..e58aaa6 100644 --- a/diff/process.go +++ b/diff/process.go @@ -59,9 +59,7 @@ func Update(oscFile string, geometryLimiter *limit.Limiter, force bool) { log.Fatal(err) } - connType := database.ConnectionType(config.BaseOptions.Connection) dbConf := database.Config{ - Type: connType, ConnectionParams: config.BaseOptions.Connection, Srid: config.BaseOptions.Srid, } @@ -258,9 +256,9 @@ For: close(ways) close(nodes) - nodeWriter.Close() - relWriter.Close() - wayWriter.Close() + nodeWriter.Wait() + relWriter.Wait() + wayWriter.Wait() if genDb != nil { genDb.GeneralizeUpdates() diff --git a/imposm3.go b/imposm3.go index 01a0fbb..bd29c71 100644 --- a/imposm3.go +++ b/imposm3.go @@ -153,9 +153,7 @@ func mainimport() { if config.BaseOptions.Connection == "" { log.Fatal("missing connection option") } - connType := database.ConnectionType(config.BaseOptions.Connection) conf := database.Config{ - Type: connType, ConnectionParams: config.BaseOptions.Connection, Srid: config.BaseOptions.Srid, } @@ -256,9 +254,7 @@ func mainimport() { db, progress, config.BaseOptions.Srid) relWriter.SetLimiter(geometryLimiter) relWriter.Start() - - // blocks till the Relations.Iter() finishes - relWriter.Close() + relWriter.Wait() // blocks till the Relations.Iter() finishes osmCache.Relations.Close() ways := osmCache.Ways.Iter() @@ -266,9 +262,7 @@ func mainimport() { progress, config.BaseOptions.Srid) wayWriter.SetLimiter(geometryLimiter) wayWriter.Start() - - // blocks till the Ways.Iter() finishes - wayWriter.Close() + wayWriter.Wait() // blocks till the Ways.Iter() finishes osmCache.Ways.Close() nodes := osmCache.Nodes.Iter() @@ -276,9 +270,7 @@ func mainimport() { progress, config.BaseOptions.Srid) nodeWriter.SetLimiter(geometryLimiter) nodeWriter.Start() - - // blocks till the Nodes.Iter() finishes - nodeWriter.Close() + nodeWriter.Wait() // blocks till the Nodes.Iter() finishes osmCache.Close() err = db.End() diff --git a/writer/writer.go b/writer/writer.go index 4dd9bbb..1f3d38b 100644 --- a/writer/writer.go +++ b/writer/writer.go @@ -45,6 +45,6 @@ func (writer *OsmElemWriter) SetExpireTiles(expireTiles *expire.Tiles) { writer.expireTiles = expireTiles } -func (writer *OsmElemWriter) Close() { +func (writer *OsmElemWriter) Wait() { writer.wg.Wait() }