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.

102 lines
2.9 KiB

4 years ago
4 years ago
4 years ago
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. var _ filer.BucketAware = (*PostgresStore2)(nil)
  17. func init() {
  18. filer.Stores = append(filer.Stores, &PostgresStore2{})
  19. }
  20. type PostgresStore2 struct {
  21. abstract_sql.AbstractSqlStore
  22. }
  23. func (store *PostgresStore2) GetName() string {
  24. return "postgres2"
  25. }
  26. func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix string) (err error) {
  27. return store.initialize(
  28. configuration.GetString(prefix+"createTable"),
  29. configuration.GetString(prefix+"upsertQuery"),
  30. configuration.GetBool(prefix+"enableUpsert"),
  31. configuration.GetString(prefix+"username"),
  32. configuration.GetString(prefix+"password"),
  33. configuration.GetString(prefix+"hostname"),
  34. configuration.GetInt(prefix+"port"),
  35. configuration.GetString(prefix+"database"),
  36. configuration.GetString(prefix+"schema"),
  37. configuration.GetString(prefix+"sslmode"),
  38. configuration.GetInt(prefix+"connection_max_idle"),
  39. configuration.GetInt(prefix+"connection_max_open"),
  40. configuration.GetInt(prefix+"connection_max_lifetime_seconds"),
  41. )
  42. }
  43. func (store *PostgresStore2) initialize(createTable, upsertQuery string, enableUpsert bool, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) {
  44. store.SupportBucketTable = true
  45. if !enableUpsert {
  46. upsertQuery = ""
  47. }
  48. store.SqlGenerator = &postgres.SqlGenPostgres{
  49. CreateTableSqlTemplate: createTable,
  50. DropTableSqlTemplate: `drop table "%s"`,
  51. UpsertQueryTemplate: upsertQuery,
  52. }
  53. sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode)
  54. if user != "" {
  55. sqlUrl += " user=" + user
  56. }
  57. adaptedSqlUrl := sqlUrl
  58. if password != "" {
  59. sqlUrl += " password=" + password
  60. adaptedSqlUrl += " password=ADAPTED"
  61. }
  62. if database != "" {
  63. sqlUrl += " dbname=" + database
  64. adaptedSqlUrl += " dbname=" + database
  65. }
  66. if schema != "" {
  67. sqlUrl += " search_path=" + schema
  68. adaptedSqlUrl += " search_path=" + schema
  69. }
  70. var dbErr error
  71. store.DB, dbErr = sql.Open("postgres", sqlUrl)
  72. if dbErr != nil {
  73. store.DB.Close()
  74. store.DB = nil
  75. return fmt.Errorf("can not connect to %s error:%v", adaptedSqlUrl, err)
  76. }
  77. store.DB.SetMaxIdleConns(maxIdle)
  78. store.DB.SetMaxOpenConns(maxOpen)
  79. store.DB.SetConnMaxLifetime(time.Duration(maxLifetimeSeconds) * time.Second)
  80. if err = store.DB.Ping(); err != nil {
  81. return fmt.Errorf("connect to %s error:%v", sqlUrl, err)
  82. }
  83. if err = store.CreateTable(context.Background(), abstract_sql.DEFAULT_TABLE); err != nil {
  84. return fmt.Errorf("init table %s: %v", abstract_sql.DEFAULT_TABLE, err)
  85. }
  86. return nil
  87. }