postgres2_store.go 2.8 KB

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