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.

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