-
Notifications
You must be signed in to change notification settings - Fork 2
/
bloomdb.go
59 lines (50 loc) · 1.09 KB
/
bloomdb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package bloomdb
import (
"database/sql"
"github.com/spf13/viper"
elastigo "github.com/mattbaird/elastigo/lib"
)
type BloomDatabase struct {
sqlConnStr string
searchHosts []string
sharedSearch *elastigo.Conn
sharedDB *sql.DB
}
func (bdb *BloomDatabase) NewSqlConnection() (*sql.DB, error) {
db, err := sql.Open("postgres", bdb.sqlConnStr)
return db, err
}
func (bdb *BloomDatabase) SqlConnection() (*sql.DB, error) {
if bdb.sharedDB == nil {
db, err := sql.Open("postgres", bdb.sqlConnStr)
if err != nil {
return nil, err
}
bdb.sharedDB = db
}
return bdb.sharedDB, nil
}
func (bdb *BloomDatabase) SearchConnection() (*elastigo.Conn) {
if bdb.sharedSearch == nil {
conn := elastigo.NewConn()
conn.SetHosts(bdb.searchHosts)
bdb.sharedSearch = conn
}
return bdb.sharedSearch
}
func CreateDB() *BloomDatabase {
return &BloomDatabase {
viper.GetString("sqlConnStr"),
viper.GetStringSlice("searchHosts"),
nil,
nil,
}
}
func DBFromConfig(sqlConnStr string, searchHosts []string) *BloomDatabase {
return &BloomDatabase {
sqlConnStr,
searchHosts,
nil,
nil,
}
}