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