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.

131 lines
3.4 KiB

7 years ago
  1. package abstract_sql
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/filer2"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. )
  9. type AbstractSqlStore struct {
  10. DB *sql.DB
  11. SqlInsert string
  12. SqlUpdate string
  13. SqlFind string
  14. SqlDelete string
  15. SqlListExclusive string
  16. SqlListInclusive string
  17. }
  18. func (store *AbstractSqlStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  19. dir, name := entry.FullPath.DirAndName()
  20. meta, err := entry.EncodeAttributesAndChunks()
  21. if err != nil {
  22. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  23. }
  24. res, err := store.DB.Exec(store.SqlInsert, hashToLong(dir), name, dir, meta)
  25. if err != nil {
  26. return fmt.Errorf("insert %s: %s", entry.FullPath, err)
  27. }
  28. _, err = res.RowsAffected()
  29. if err != nil {
  30. return fmt.Errorf("insert %s but no rows affected: %s", entry.FullPath, err)
  31. }
  32. return nil
  33. }
  34. func (store *AbstractSqlStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  35. dir, name := entry.FullPath.DirAndName()
  36. meta, err := entry.EncodeAttributesAndChunks()
  37. if err != nil {
  38. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  39. }
  40. res, err := store.DB.Exec(store.SqlUpdate, meta, hashToLong(dir), name, dir)
  41. if err != nil {
  42. return fmt.Errorf("update %s: %s", entry.FullPath, err)
  43. }
  44. _, err = res.RowsAffected()
  45. if err != nil {
  46. return fmt.Errorf("update %s but no rows affected: %s", entry.FullPath, err)
  47. }
  48. return nil
  49. }
  50. func (store *AbstractSqlStore) FindEntry(ctx context.Context, fullpath filer2.FullPath) (*filer2.Entry, error) {
  51. dir, name := fullpath.DirAndName()
  52. row := store.DB.QueryRow(store.SqlFind, hashToLong(dir), name, dir)
  53. var data []byte
  54. if err := row.Scan(&data); err != nil {
  55. return nil, filer2.ErrNotFound
  56. }
  57. entry := &filer2.Entry{
  58. FullPath: fullpath,
  59. }
  60. if err := entry.DecodeAttributesAndChunks(data); err != nil {
  61. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  62. }
  63. return entry, nil
  64. }
  65. func (store *AbstractSqlStore) DeleteEntry(ctx context.Context, fullpath filer2.FullPath) error {
  66. dir, name := fullpath.DirAndName()
  67. res, err := store.DB.Exec(store.SqlDelete, hashToLong(dir), name, dir)
  68. if err != nil {
  69. return fmt.Errorf("delete %s: %s", fullpath, err)
  70. }
  71. _, err = res.RowsAffected()
  72. if err != nil {
  73. return fmt.Errorf("delete %s but no rows affected: %s", fullpath, err)
  74. }
  75. return nil
  76. }
  77. func (store *AbstractSqlStore) ListDirectoryEntries(ctx context.Context, 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. }