abstract_sql_store.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. type AbstractSqlStore struct {
  12. DB *sql.DB
  13. SqlInsert string
  14. SqlUpdate string
  15. SqlFind string
  16. SqlDelete string
  17. SqlDeleteFolderChildren string
  18. SqlListExclusive string
  19. SqlListInclusive string
  20. }
  21. type TxOrDB interface {
  22. ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
  23. QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
  24. QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
  25. }
  26. func (store *AbstractSqlStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  27. tx, err := store.DB.BeginTx(ctx, &sql.TxOptions{
  28. Isolation: sql.LevelReadCommitted,
  29. ReadOnly: false,
  30. })
  31. if err != nil {
  32. return ctx, err
  33. }
  34. return context.WithValue(ctx, "tx", tx), nil
  35. }
  36. func (store *AbstractSqlStore) CommitTransaction(ctx context.Context) error {
  37. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  38. return tx.Commit()
  39. }
  40. return nil
  41. }
  42. func (store *AbstractSqlStore) RollbackTransaction(ctx context.Context) error {
  43. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  44. return tx.Rollback()
  45. }
  46. return nil
  47. }
  48. func (store *AbstractSqlStore) getTxOrDB(ctx context.Context) TxOrDB {
  49. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  50. return tx
  51. }
  52. return store.DB
  53. }
  54. func (store *AbstractSqlStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  55. dir, name := entry.FullPath.DirAndName()
  56. meta, err := entry.EncodeAttributesAndChunks()
  57. if err != nil {
  58. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  59. }
  60. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlInsert, util.HashStringToLong(dir), name, dir, meta)
  61. if err != nil {
  62. return fmt.Errorf("insert %s: %s", entry.FullPath, err)
  63. }
  64. _, err = res.RowsAffected()
  65. if err != nil {
  66. return fmt.Errorf("insert %s but no rows affected: %s", entry.FullPath, err)
  67. }
  68. return nil
  69. }
  70. func (store *AbstractSqlStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  71. dir, name := entry.FullPath.DirAndName()
  72. meta, err := entry.EncodeAttributesAndChunks()
  73. if err != nil {
  74. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  75. }
  76. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlUpdate, meta, util.HashStringToLong(dir), name, dir)
  77. if err != nil {
  78. return fmt.Errorf("update %s: %s", entry.FullPath, err)
  79. }
  80. _, err = res.RowsAffected()
  81. if err != nil {
  82. return fmt.Errorf("update %s but no rows affected: %s", entry.FullPath, err)
  83. }
  84. return nil
  85. }
  86. func (store *AbstractSqlStore) FindEntry(ctx context.Context, fullpath filer2.FullPath) (*filer2.Entry, error) {
  87. dir, name := fullpath.DirAndName()
  88. row := store.getTxOrDB(ctx).QueryRowContext(ctx, store.SqlFind, util.HashStringToLong(dir), name, dir)
  89. var data []byte
  90. if err := row.Scan(&data); err != nil {
  91. return nil, filer_pb.ErrNotFound
  92. }
  93. entry := &filer2.Entry{
  94. FullPath: fullpath,
  95. }
  96. if err := entry.DecodeAttributesAndChunks(data); err != nil {
  97. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  98. }
  99. return entry, nil
  100. }
  101. func (store *AbstractSqlStore) DeleteEntry(ctx context.Context, fullpath filer2.FullPath) error {
  102. dir, name := fullpath.DirAndName()
  103. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlDelete, util.HashStringToLong(dir), name, dir)
  104. if err != nil {
  105. return fmt.Errorf("delete %s: %s", fullpath, err)
  106. }
  107. _, err = res.RowsAffected()
  108. if err != nil {
  109. return fmt.Errorf("delete %s but no rows affected: %s", fullpath, err)
  110. }
  111. return nil
  112. }
  113. func (store *AbstractSqlStore) DeleteFolderChildren(ctx context.Context, fullpath filer2.FullPath) error {
  114. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlDeleteFolderChildren, util.HashStringToLong(string(fullpath)), fullpath)
  115. if err != nil {
  116. return fmt.Errorf("deleteFolderChildren %s: %s", fullpath, err)
  117. }
  118. _, err = res.RowsAffected()
  119. if err != nil {
  120. return fmt.Errorf("deleteFolderChildren %s but no rows affected: %s", fullpath, err)
  121. }
  122. return nil
  123. }
  124. func (store *AbstractSqlStore) ListDirectoryEntries(ctx context.Context, fullpath filer2.FullPath, startFileName string, inclusive bool, limit int) (entries []*filer2.Entry, err error) {
  125. sqlText := store.SqlListExclusive
  126. if inclusive {
  127. sqlText = store.SqlListInclusive
  128. }
  129. rows, err := store.getTxOrDB(ctx).QueryContext(ctx, sqlText, util.HashStringToLong(string(fullpath)), startFileName, string(fullpath), limit)
  130. if err != nil {
  131. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  132. }
  133. defer rows.Close()
  134. for rows.Next() {
  135. var name string
  136. var data []byte
  137. if err = rows.Scan(&name, &data); err != nil {
  138. glog.V(0).Infof("scan %s : %v", fullpath, err)
  139. return nil, fmt.Errorf("scan %s: %v", fullpath, err)
  140. }
  141. entry := &filer2.Entry{
  142. FullPath: filer2.NewFullPath(string(fullpath), name),
  143. }
  144. if err = entry.DecodeAttributesAndChunks(data); err != nil {
  145. glog.V(0).Infof("scan decode %s : %v", entry.FullPath, err)
  146. return nil, fmt.Errorf("scan decode %s : %v", entry.FullPath, err)
  147. }
  148. entries = append(entries, entry)
  149. }
  150. return entries, nil
  151. }