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.

132 lines
3.4 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. SqlInsert string
  11. SqlUpdate string
  12. SqlFind string
  13. SqlDelete string
  14. SqlListExclusive string
  15. SqlListInclusive string
  16. }
  17. func (store *AbstractSqlStore) InsertEntry(entry *filer2.Entry) (err error) {
  18. dir, name := entry.FullPath.DirAndName()
  19. meta, err := entry.EncodeAttributesAndChunks()
  20. if err != nil {
  21. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  22. }
  23. res, err := store.DB.Exec(store.SqlInsert, hashToLong(dir), name, dir, meta)
  24. if err != nil {
  25. return fmt.Errorf("insert %s: %s", entry.FullPath, err)
  26. }
  27. _, err = res.RowsAffected()
  28. if err != nil {
  29. return fmt.Errorf("insert %s but no rows affected: %s", entry.FullPath, err)
  30. }
  31. return nil
  32. }
  33. func (store *AbstractSqlStore) UpdateEntry(entry *filer2.Entry) (err error) {
  34. dir, name := entry.FullPath.DirAndName()
  35. meta, err := entry.EncodeAttributesAndChunks()
  36. if err != nil {
  37. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  38. }
  39. res, err := store.DB.Exec(store.SqlUpdate, meta, hashToLong(dir), name, dir)
  40. if err != nil {
  41. return fmt.Errorf("update %s: %s", entry.FullPath, err)
  42. }
  43. _, err = res.RowsAffected()
  44. if err != nil {
  45. return fmt.Errorf("update %s but no rows affected: %s", entry.FullPath, err)
  46. }
  47. return nil
  48. }
  49. func (store *AbstractSqlStore) FindEntry(fullpath filer2.FullPath) (*filer2.Entry, error) {
  50. dir, name := fullpath.DirAndName()
  51. row := store.DB.QueryRow(store.SqlFind, hashToLong(dir), name, dir)
  52. var data []byte
  53. if err := row.Scan(&data); err != nil {
  54. return nil, fmt.Errorf("read entry %s: %v", fullpath, err)
  55. }
  56. entry := &filer2.Entry{
  57. FullPath: fullpath,
  58. }
  59. if err := entry.DecodeAttributesAndChunks(data); err != nil {
  60. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  61. }
  62. return entry, nil
  63. }
  64. func (store *AbstractSqlStore) DeleteEntry(fullpath filer2.FullPath) (*filer2.Entry, error) {
  65. entry, _ := store.FindEntry(fullpath)
  66. dir, name := fullpath.DirAndName()
  67. res, err := store.DB.Exec(store.SqlDelete, hashToLong(dir), name, dir)
  68. if err != nil {
  69. return nil, fmt.Errorf("delete %s: %s", fullpath, err)
  70. }
  71. _, err = res.RowsAffected()
  72. if err != nil {
  73. return nil, fmt.Errorf("delete %s but no rows affected: %s", fullpath, err)
  74. }
  75. return entry, nil
  76. }
  77. func (store *AbstractSqlStore) ListDirectoryEntries(fullpath filer2.FullPath, startFileName string, inclusive bool, limit int) (entries []*filer2.Entry, err error) {
  78. sqlText := store.SqlListExclusive
  79. if inclusive {
  80. sqlText = store.SqlListInclusive
  81. }
  82. rows, err := store.DB.Query(sqlText, hashToLong(string(fullpath)), startFileName, string(fullpath), limit)
  83. if err != nil {
  84. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  85. }
  86. defer rows.Close()
  87. for rows.Next() {
  88. var name string
  89. var data []byte
  90. if err = rows.Scan(&name, &data); err != nil {
  91. glog.V(0).Infof("scan %s : %v", fullpath, err)
  92. return nil, fmt.Errorf("scan %s: %v", fullpath, err)
  93. }
  94. entry := &filer2.Entry{
  95. FullPath: filer2.NewFullPath(string(fullpath), name),
  96. }
  97. if err = entry.DecodeAttributesAndChunks(data); err != nil {
  98. glog.V(0).Infof("scan decode %s : %v", entry.FullPath, err)
  99. return nil, fmt.Errorf("scan decode %s : %v", entry.FullPath, err)
  100. }
  101. entries = append(entries, entry)
  102. }
  103. return entries, nil
  104. }