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.

75 lines
2.4 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 user=%s 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, user, sslmode)
  43. if password != "" {
  44. sqlUrl += " password=" + password
  45. }
  46. if database != "" {
  47. sqlUrl += " dbname=" + database
  48. }
  49. var dbErr error
  50. store.DB, dbErr = sql.Open("postgres", sqlUrl)
  51. if dbErr != nil {
  52. store.DB.Close()
  53. store.DB = nil
  54. return fmt.Errorf("can not connect to %s error:%v", sqlUrl, err)
  55. }
  56. store.DB.SetMaxIdleConns(maxIdle)
  57. store.DB.SetMaxOpenConns(maxOpen)
  58. if err = store.DB.Ping(); err != nil {
  59. return fmt.Errorf("connect to %s error:%v", sqlUrl, err)
  60. }
  61. return nil
  62. }