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.

78 lines
2.5 KiB

7 years ago
6 years ago
5 years ago
5 years ago
  1. package postgres
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer"
  6. "github.com/chrislusf/seaweedfs/weed/filer/abstract_sql"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. _ "github.com/lib/pq"
  9. )
  10. const (
  11. CONNECTION_URL_PATTERN = "host=%s port=%d sslmode=%s connect_timeout=30"
  12. )
  13. func init() {
  14. filer.Stores = append(filer.Stores, &PostgresStore{})
  15. }
  16. type PostgresStore struct {
  17. abstract_sql.AbstractSqlStore
  18. }
  19. func (store *PostgresStore) GetName() string {
  20. return "postgres"
  21. }
  22. func (store *PostgresStore) Initialize(configuration util.Configuration, prefix string) (err error) {
  23. return store.initialize(
  24. configuration.GetString(prefix+"username"),
  25. configuration.GetString(prefix+"password"),
  26. configuration.GetString(prefix+"hostname"),
  27. configuration.GetInt(prefix+"port"),
  28. configuration.GetString(prefix+"database"),
  29. configuration.GetString(prefix+"sslmode"),
  30. configuration.GetInt(prefix+"connection_max_idle"),
  31. configuration.GetInt(prefix+"connection_max_open"),
  32. )
  33. }
  34. func (store *PostgresStore) initialize(user, password, hostname string, port int, database, sslmode string, maxIdle, maxOpen int) (err error) {
  35. store.SqlInsert = "INSERT INTO filemeta (dirhash,name,directory,meta) VALUES($1,$2,$3,$4)"
  36. store.SqlUpdate = "UPDATE filemeta SET meta=$1 WHERE dirhash=$2 AND name=$3 AND directory=$4"
  37. store.SqlFind = "SELECT meta FROM filemeta WHERE dirhash=$1 AND name=$2 AND directory=$3"
  38. store.SqlDelete = "DELETE FROM filemeta WHERE dirhash=$1 AND name=$2 AND directory=$3"
  39. store.SqlDeleteFolderChildren = "DELETE FROM filemeta WHERE dirhash=$1 AND directory=$2"
  40. store.SqlListExclusive = "SELECT NAME, meta FROM filemeta WHERE dirhash=$1 AND name>$2 AND directory=$3 AND name like $4 ORDER BY NAME ASC LIMIT $5"
  41. store.SqlListInclusive = "SELECT NAME, meta FROM filemeta WHERE dirhash=$1 AND name>=$2 AND directory=$3 AND name like $4 ORDER BY NAME ASC LIMIT $5"
  42. sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode)
  43. if user != "" {
  44. sqlUrl += " user=" + user
  45. }
  46. if password != "" {
  47. sqlUrl += " password=" + password
  48. }
  49. if database != "" {
  50. sqlUrl += " dbname=" + database
  51. }
  52. var dbErr error
  53. store.DB, dbErr = sql.Open("postgres", sqlUrl)
  54. if dbErr != nil {
  55. store.DB.Close()
  56. store.DB = nil
  57. return fmt.Errorf("can not connect to %s error:%v", sqlUrl, err)
  58. }
  59. store.DB.SetMaxIdleConns(maxIdle)
  60. store.DB.SetMaxOpenConns(maxOpen)
  61. if err = store.DB.Ping(); err != nil {
  62. return fmt.Errorf("connect to %s error:%v", sqlUrl, err)
  63. }
  64. return nil
  65. }