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.

135 lines
3.4 KiB

7 years ago
  1. package abstract_sql
  2. import (
  3. "database/sql"
  4. "fmt"
  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, err := store.FindEntry(fullpath)
  66. if err != nil {
  67. return nil, nil
  68. }
  69. dir, name := fullpath.DirAndName()
  70. res, err := store.DB.Exec(store.SqlDelete, hashToLong(dir), name, dir)
  71. if err != nil {
  72. return nil, fmt.Errorf("delete %s: %s", fullpath, err)
  73. }
  74. _, err = res.RowsAffected()
  75. if err != nil {
  76. return nil, fmt.Errorf("delete %s but no rows affected: %s", fullpath, err)
  77. }
  78. return entry, nil
  79. }
  80. func (store *AbstractSqlStore) ListDirectoryEntries(fullpath filer2.FullPath, startFileName string, inclusive bool, limit int) (entries []*filer2.Entry, err error) {
  81. sqlText := store.SqlListExclusive
  82. if inclusive {
  83. sqlText = store.SqlListInclusive
  84. }
  85. rows, err := store.DB.Query(sqlText, hashToLong(string(fullpath)), startFileName, string(fullpath), limit)
  86. if err != nil {
  87. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  88. }
  89. defer rows.Close()
  90. for rows.Next() {
  91. var name string
  92. var data []byte
  93. if err = rows.Scan(&name, &data); err != nil {
  94. glog.V(0).Infof("scan %s : %v", fullpath, err)
  95. return nil, fmt.Errorf("scan %s: %v", fullpath, err)
  96. }
  97. entry := &filer2.Entry{
  98. FullPath: filer2.NewFullPath(string(fullpath), name),
  99. }
  100. if err = entry.DecodeAttributesAndChunks(data); err != nil {
  101. glog.V(0).Infof("scan decode %s : %v", entry.FullPath, err)
  102. return nil, fmt.Errorf("scan decode %s : %v", entry.FullPath, err)
  103. }
  104. entries = append(entries, entry)
  105. }
  106. return entries, nil
  107. }