You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.5 KiB

7 years ago
6 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
  1. package postgres
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/filer/abstract_sql"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. _ "github.com/lib/pq"
  10. )
  11. const (
  12. CONNECTION_URL_PATTERN = "host=%s port=%d sslmode=%s connect_timeout=30"
  13. )
  14. func init() {
  15. filer.Stores = append(filer.Stores, &PostgresStore{})
  16. }
  17. type PostgresStore struct {
  18. abstract_sql.AbstractSqlStore
  19. }
  20. func (store *PostgresStore) GetName() string {
  21. return "postgres"
  22. }
  23. func (store *PostgresStore) Initialize(configuration util.Configuration, prefix string) (err error) {
  24. return store.initialize(
  25. configuration.GetString(prefix+"upsertQuery"),
  26. configuration.GetBool(prefix+"enableUpsert"),
  27. configuration.GetString(prefix+"username"),
  28. configuration.GetString(prefix+"password"),
  29. configuration.GetString(prefix+"hostname"),
  30. configuration.GetInt(prefix+"port"),
  31. configuration.GetString(prefix+"database"),
  32. configuration.GetString(prefix+"schema"),
  33. configuration.GetString(prefix+"sslmode"),
  34. configuration.GetInt(prefix+"connection_max_idle"),
  35. configuration.GetInt(prefix+"connection_max_open"),
  36. configuration.GetInt(prefix+"connection_max_lifetime_seconds"),
  37. )
  38. }
  39. func (store *PostgresStore) initialize(upsertQuery string, enableUpsert bool, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) {
  40. store.SupportBucketTable = false
  41. if !enableUpsert {
  42. upsertQuery = ""
  43. }
  44. store.SqlGenerator = &SqlGenPostgres{
  45. CreateTableSqlTemplate: "",
  46. DropTableSqlTemplate: `drop table "%s"`,
  47. UpsertQueryTemplate: upsertQuery,
  48. }
  49. sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode)
  50. if user != "" {
  51. sqlUrl += " user=" + user
  52. }
  53. adaptedSqlUrl := sqlUrl
  54. if password != "" {
  55. sqlUrl += " password=" + password
  56. adaptedSqlUrl += " password=ADAPTED"
  57. }
  58. if database != "" {
  59. sqlUrl += " dbname=" + database
  60. adaptedSqlUrl += " dbname=" + database
  61. }
  62. if schema != "" {
  63. sqlUrl += " search_path=" + schema
  64. adaptedSqlUrl += " search_path=" + schema
  65. }
  66. var dbErr error
  67. store.DB, dbErr = sql.Open("postgres", sqlUrl)
  68. if dbErr != nil {
  69. store.DB.Close()
  70. store.DB = nil
  71. return fmt.Errorf("can not connect to %s error:%v", adaptedSqlUrl, err)
  72. }
  73. store.DB.SetMaxIdleConns(maxIdle)
  74. store.DB.SetMaxOpenConns(maxOpen)
  75. store.DB.SetConnMaxLifetime(time.Duration(maxLifetimeSeconds) * time.Second)
  76. if err = store.DB.Ping(); err != nil {
  77. return fmt.Errorf("connect to %s error:%v", sqlUrl, err)
  78. }
  79. return nil
  80. }