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.

190 lines
5.3 KiB

7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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. "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 util.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 util.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 util.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 util.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: util.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. }
  152. func (store *AbstractSqlStore) Shutdown() {
  153. store.DB.Close()
  154. }