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.

142 lines
3.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package postgres_store
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "sync"
  7. "github.com/chrislusf/seaweedfs/weed/filer"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. _ "github.com/lib/pq"
  10. _ "path/filepath"
  11. )
  12. const (
  13. default_maxIdleConnections = 100
  14. default_maxOpenConnections = 50
  15. filesTableName = "files"
  16. directoriesTableName = "directories"
  17. )
  18. var (
  19. _init_db sync.Once
  20. _db_connection *sql.DB
  21. )
  22. type PostgresConf struct {
  23. User string
  24. Password string
  25. HostName string
  26. Port int
  27. DataBase string
  28. SslMode string
  29. MaxIdleConnections int
  30. MaxOpenConnections int
  31. }
  32. type PostgresStore struct {
  33. db *sql.DB
  34. server string
  35. user string
  36. password string
  37. }
  38. func (s *PostgresStore) CreateFile(fullFilePath string, fid string) (err error) {
  39. var old_fid string
  40. if old_fid, err = s.query(fullFilePath); err != nil && err != sql.ErrNoRows {
  41. return fmt.Errorf("PostgresStore Put operation failed when querying path %s: err is %v", fullFilePath, err)
  42. } else {
  43. if len(old_fid) == 0 {
  44. err = s.insert(fullFilePath, fid)
  45. if err != nil {
  46. return fmt.Errorf("PostgresStore Put operation failed when inserting path %s with fid %s : err is %v", fullFilePath, fid, err)
  47. }
  48. } else {
  49. err = s.update(fullFilePath, fid)
  50. if err != nil {
  51. return fmt.Errorf("PostgresStore Put operation failed when updating path %s with fid %s : err is %v", fullFilePath, fid, err)
  52. }
  53. }
  54. }
  55. return
  56. }
  57. func (s *PostgresStore) FindFile(fullFilePath string) (fid string, err error) {
  58. if err != nil {
  59. return "", fmt.Errorf("PostgresStore Get operation can not parse file path %s: err is %v", fullFilePath, err)
  60. }
  61. fid, err = s.query(fullFilePath)
  62. return fid, err
  63. }
  64. func (s *PostgresStore) DeleteFile(fullFilePath string) (fid string, err error) {
  65. if err != nil {
  66. return "", fmt.Errorf("PostgresStore Delete operation can not parse file path %s: err is %v", fullFilePath, err)
  67. }
  68. if fid, err = s.query(fullFilePath); err != nil {
  69. return "", fmt.Errorf("PostgresStore Delete operation failed when querying path %s: err is %v", fullFilePath, err)
  70. } else if fid == "" {
  71. return "", nil
  72. }
  73. if err = s.delete(fullFilePath); err != nil {
  74. return "", fmt.Errorf("PostgresStore Delete operation failed when deleting path %s: err is %v", fullFilePath, err)
  75. } else {
  76. return "", nil
  77. }
  78. }
  79. func (s *PostgresStore) FindDirectory(dirPath string) (dirId filer.DirectoryId, err error) {
  80. dirId, _, err = s.lookupDirectory(dirPath)
  81. return dirId, err
  82. }
  83. func (s *PostgresStore) ListDirectories(dirPath string) (dirs []filer.DirectoryEntry, err error) {
  84. dirs, err = s.findDirectories(dirPath, 20)
  85. glog.V(3).Infof("Postgres ListDirs = found %d directories under %s", len(dirs), dirPath)
  86. return dirs, err
  87. }
  88. func (s *PostgresStore) ListFiles(dirPath string, lastFileName string, limit int) (files []filer.FileEntry, err error) {
  89. files, err = s.findFiles(dirPath, lastFileName, limit)
  90. return files, err
  91. }
  92. func (s *PostgresStore) DeleteDirectory(dirPath string, recursive bool) (err error) {
  93. err = s.deleteDirectory(dirPath, recursive)
  94. if err != nil {
  95. glog.V(0).Infof("Error in Postgres DeleteDir '%s' (recursive = '%t'): %s", err)
  96. }
  97. return err
  98. }
  99. func (s *PostgresStore) Move(fromPath string, toPath string) (err error) {
  100. glog.V(3).Infoln("Calling posgres_store Move")
  101. return errors.New("Move is not yet implemented for the PostgreSQL store.")
  102. }
  103. //func NewPostgresStore(master string, confs []PostgresConf, isSharding bool, shardCount int) *PostgresStore {
  104. func NewPostgresStore(master string, conf PostgresConf) *PostgresStore {
  105. pg := &PostgresStore{
  106. db: getDbConnection(conf),
  107. }
  108. pg.createDirectoriesTable()
  109. if err := pg.createFilesTable(); err != nil {
  110. fmt.Printf("create table failed %v", err)
  111. }
  112. return pg
  113. }
  114. func (s *PostgresStore) Close() {
  115. s.db.Close()
  116. }