postgres2_store.go 2.6 KB

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