postgres2_store.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package postgres2
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strconv"
  7. "time"
  8. _ "github.com/lib/pq"
  9. "github.com/seaweedfs/seaweedfs/weed/filer"
  10. "github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
  11. "github.com/seaweedfs/seaweedfs/weed/filer/postgres"
  12. "github.com/seaweedfs/seaweedfs/weed/util"
  13. )
  14. var _ filer.BucketAware = (*PostgresStore2)(nil)
  15. func init() {
  16. filer.Stores = append(filer.Stores, &PostgresStore2{})
  17. }
  18. type PostgresStore2 struct {
  19. abstract_sql.AbstractSqlStore
  20. }
  21. func (store *PostgresStore2) GetName() string {
  22. return "postgres2"
  23. }
  24. func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix string) (err error) {
  25. return store.initialize(
  26. configuration.GetString(prefix+"createTable"),
  27. configuration.GetString(prefix+"upsertQuery"),
  28. configuration.GetBool(prefix+"enableUpsert"),
  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, upsertQuery string, enableUpsert bool, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) {
  42. store.SupportBucketTable = true
  43. if !enableUpsert {
  44. upsertQuery = ""
  45. }
  46. store.SqlGenerator = &postgres.SqlGenPostgres{
  47. CreateTableSqlTemplate: createTable,
  48. DropTableSqlTemplate: `drop table "%s"`,
  49. UpsertQueryTemplate: upsertQuery,
  50. }
  51. sqlUrl := "connect_timeout=30"
  52. if hostname != "" {
  53. sqlUrl += " host=" + hostname
  54. }
  55. if port != 0 {
  56. sqlUrl += " port=" + strconv.Itoa(port)
  57. }
  58. if sslmode != "" {
  59. sqlUrl += " sslmode=" + sslmode
  60. }
  61. if user != "" {
  62. sqlUrl += " user=" + user
  63. }
  64. adaptedSqlUrl := sqlUrl
  65. if password != "" {
  66. sqlUrl += " password=" + password
  67. adaptedSqlUrl += " password=ADAPTED"
  68. }
  69. if database != "" {
  70. sqlUrl += " dbname=" + database
  71. adaptedSqlUrl += " dbname=" + database
  72. }
  73. if schema != "" {
  74. sqlUrl += " search_path=" + schema
  75. adaptedSqlUrl += " search_path=" + schema
  76. }
  77. var dbErr error
  78. store.DB, dbErr = sql.Open("postgres", sqlUrl)
  79. if dbErr != nil {
  80. store.DB.Close()
  81. store.DB = nil
  82. return fmt.Errorf("can not connect to %s error:%v", adaptedSqlUrl, err)
  83. }
  84. store.DB.SetMaxIdleConns(maxIdle)
  85. store.DB.SetMaxOpenConns(maxOpen)
  86. store.DB.SetConnMaxLifetime(time.Duration(maxLifetimeSeconds) * time.Second)
  87. if err = store.DB.Ping(); err != nil {
  88. return fmt.Errorf("connect to %s error:%v", sqlUrl, err)
  89. }
  90. if err = store.CreateTable(context.Background(), abstract_sql.DEFAULT_TABLE); err != nil {
  91. return fmt.Errorf("init table %s: %v", abstract_sql.DEFAULT_TABLE, err)
  92. }
  93. return nil
  94. }