yanic/database/all/connection.go

81 lines
1.8 KiB
Go
Raw Permalink Normal View History

2018-01-03 15:41:40 +01:00
package all
import (
"fmt"
"time"
2019-01-17 13:26:16 +01:00
"github.com/bdlm/log"
2018-01-03 15:41:40 +01:00
"github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/runtime"
)
type Connection struct {
database.Connection
list []database.Connection
}
func Connect(allConnection map[string]interface{}) (database.Connection, error) {
var list []database.Connection
for dbType, conn := range database.Adapters {
configForType := allConnection[dbType]
if configForType == nil {
2019-01-17 13:26:16 +01:00
log.WithField("database", dbType).Infof("no configuration found")
2018-01-03 15:41:40 +01:00
continue
}
2018-01-15 01:01:32 +01:00
dbConfigs, ok := configForType.([]interface{})
2018-01-03 15:41:40 +01:00
if !ok {
2018-01-15 01:01:32 +01:00
return nil, fmt.Errorf("the database type '%s' has the wrong format", dbType)
2018-01-03 15:41:40 +01:00
}
2018-01-15 01:01:32 +01:00
for _, dbConfig := range dbConfigs {
config, ok := dbConfig.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("the database type '%s' has the wrong format", dbType)
}
2018-01-03 15:41:40 +01:00
if c, ok := config["enable"].(bool); ok && !c {
continue
}
connected, err := conn(config)
if err != nil {
return nil, err
}
if connected == nil {
continue
}
list = append(list, connected)
}
}
return &Connection{list: list}, nil
}
func (conn *Connection) InsertNode(node *runtime.Node) {
for _, item := range conn.list {
item.InsertNode(node)
}
}
func (conn *Connection) InsertLink(link *runtime.Link, time time.Time) {
for _, item := range conn.list {
item.InsertLink(link, time)
}
}
func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time, site string, domain string) {
2018-01-03 15:41:40 +01:00
for _, item := range conn.list {
item.InsertGlobals(stats, time, site, domain)
2018-01-03 15:41:40 +01:00
}
}
func (conn *Connection) PruneNodes(deleteAfter time.Duration) {
for _, item := range conn.list {
item.PruneNodes(deleteAfter)
}
}
func (conn *Connection) Close() {
for _, item := range conn.list {
item.Close()
}
}