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.

130 lines
3.7 KiB

  1. package abstract_sql
  2. import (
  3. "fmt"
  4. "database/sql"
  5. "github.com/chrislusf/seaweedfs/weed/filer2"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. )
  8. type AbstractSqlStore struct {
  9. DB *sql.DB
  10. }
  11. func (store *AbstractSqlStore) InsertEntry(entry *filer2.Entry) (err error) {
  12. dir, name := entry.FullPath.DirAndName()
  13. meta, err := entry.EncodeAttributesAndChunks()
  14. if err != nil {
  15. return fmt.Errorf("mysql encode %s: %s", entry.FullPath, err)
  16. }
  17. res, err := store.DB.Exec("INSERT INTO filemeta (dirhash,name,directory,meta) VALUES(?,?,?,?)",
  18. md5hash(dir), name, dir, meta)
  19. if err != nil {
  20. return fmt.Errorf("mysql insert %s: %s", entry.FullPath, err)
  21. }
  22. _, err = res.RowsAffected()
  23. if err != nil {
  24. return fmt.Errorf("mysql insert %s but no rows affected: %s", entry.FullPath, err)
  25. }
  26. return nil
  27. }
  28. func (store *AbstractSqlStore) UpdateEntry(entry *filer2.Entry) (err error) {
  29. dir, name := entry.FullPath.DirAndName()
  30. meta, err := entry.EncodeAttributesAndChunks()
  31. if err != nil {
  32. return fmt.Errorf("mysql encode %s: %s", entry.FullPath, err)
  33. }
  34. res, err := store.DB.Exec("UPDATE filemeta SET meta=? WHERE dirhash=? AND name=? AND directory=?",
  35. meta, md5hash(dir), name, dir)
  36. if err != nil {
  37. return fmt.Errorf("mysql update %s: %s", entry.FullPath, err)
  38. }
  39. _, err = res.RowsAffected()
  40. if err != nil {
  41. return fmt.Errorf("mysql update %s but no rows affected: %s", entry.FullPath, err)
  42. }
  43. return nil
  44. }
  45. func (store *AbstractSqlStore) FindEntry(fullpath filer2.FullPath) (*filer2.Entry, error) {
  46. dir, name := fullpath.DirAndName()
  47. row := store.DB.QueryRow("SELECT meta FROM filemeta WHERE dirhash=? AND name=? AND directory=?",
  48. md5hash(dir), name, dir)
  49. var data []byte
  50. if err := row.Scan(&data); err != nil {
  51. return nil, fmt.Errorf("mysql read entry %s: %v", fullpath, err)
  52. }
  53. entry := &filer2.Entry{
  54. FullPath: fullpath,
  55. }
  56. if err := entry.DecodeAttributesAndChunks(data); err != nil {
  57. return entry, fmt.Errorf("mysql decode %s : %v", entry.FullPath, err)
  58. }
  59. return entry, nil
  60. }
  61. func (store *AbstractSqlStore) DeleteEntry(fullpath filer2.FullPath) (*filer2.Entry, error) {
  62. entry, _ := store.FindEntry(fullpath)
  63. dir, name := fullpath.DirAndName()
  64. res, err := store.DB.Exec("DELETE FROM filemeta WHERE dirhash=? AND name=? AND directory=?",
  65. md5hash(dir), name, dir)
  66. if err != nil {
  67. return nil, fmt.Errorf("mysql delete %s: %s", fullpath, err)
  68. }
  69. _, err = res.RowsAffected()
  70. if err != nil {
  71. return nil, fmt.Errorf("mysql delete %s but no rows affected: %s", fullpath, err)
  72. }
  73. return entry, nil
  74. }
  75. func (store *AbstractSqlStore) ListDirectoryEntries(fullpath filer2.FullPath, startFileName string, inclusive bool, limit int) (entries []*filer2.Entry, err error) {
  76. sqlText := "SELECT NAME, meta FROM filemeta WHERE dirhash=? AND name>? AND directory=? LIMIT ?"
  77. if inclusive {
  78. sqlText = "SELECT NAME, meta FROM filemeta WHERE dirhash=? AND name>=? AND directory=? LIMIT ?"
  79. }
  80. rows, err := store.DB.Query(sqlText, md5hash(string(fullpath)), startFileName, string(fullpath), limit)
  81. if err != nil {
  82. return nil, fmt.Errorf("mysql list %s : %v", fullpath, err)
  83. }
  84. defer rows.Close()
  85. for rows.Next() {
  86. var name string
  87. var data []byte
  88. if err = rows.Scan(&name, &data); err != nil {
  89. glog.V(0).Infof("mysql scan %s : %v", fullpath, err)
  90. return nil, fmt.Errorf("mysql scan %s: %v", fullpath, err)
  91. }
  92. entry := &filer2.Entry{
  93. FullPath: filer2.NewFullPath(string(fullpath), name),
  94. }
  95. if err = entry.DecodeAttributesAndChunks(data); err != nil {
  96. glog.V(0).Infof("mysql scan decode %s : %v", entry.FullPath, err)
  97. return nil, fmt.Errorf("mysql scan decode %s : %v", entry.FullPath, err)
  98. }
  99. entries = append(entries, entry)
  100. }
  101. return entries, nil
  102. }