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.

96 lines
2.7 KiB

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