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.

207 lines
6.1 KiB

7 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/filer"
  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 *filer.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. affectedRows, err := res.RowsAffected()
  65. if err == nil && affectedRows > 0 {
  66. return nil
  67. }
  68. // now the insert failed possibly due to duplication constraints
  69. glog.V(1).Infof("insert %s falls back to update: %s", entry.FullPath, err)
  70. res, err = store.getTxOrDB(ctx).ExecContext(ctx, store.SqlUpdate, meta, util.HashStringToLong(dir), name, dir)
  71. if err != nil {
  72. return fmt.Errorf("upsert %s: %s", entry.FullPath, err)
  73. }
  74. _, err = res.RowsAffected()
  75. if err != nil {
  76. return fmt.Errorf("upsert %s but no rows affected: %s", entry.FullPath, err)
  77. }
  78. return nil
  79. }
  80. func (store *AbstractSqlStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
  81. dir, name := entry.FullPath.DirAndName()
  82. meta, err := entry.EncodeAttributesAndChunks()
  83. if err != nil {
  84. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  85. }
  86. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlUpdate, meta, util.HashStringToLong(dir), name, dir)
  87. if err != nil {
  88. return fmt.Errorf("update %s: %s", entry.FullPath, err)
  89. }
  90. _, err = res.RowsAffected()
  91. if err != nil {
  92. return fmt.Errorf("update %s but no rows affected: %s", entry.FullPath, err)
  93. }
  94. return nil
  95. }
  96. func (store *AbstractSqlStore) FindEntry(ctx context.Context, fullpath util.FullPath) (*filer.Entry, error) {
  97. dir, name := fullpath.DirAndName()
  98. row := store.getTxOrDB(ctx).QueryRowContext(ctx, store.SqlFind, util.HashStringToLong(dir), name, dir)
  99. var data []byte
  100. if err := row.Scan(&data); err != nil {
  101. glog.Errorf("find %s: %v", fullpath, err)
  102. return nil, filer_pb.ErrNotFound
  103. }
  104. entry := &filer.Entry{
  105. FullPath: fullpath,
  106. }
  107. if err := entry.DecodeAttributesAndChunks(data); err != nil {
  108. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  109. }
  110. return entry, nil
  111. }
  112. func (store *AbstractSqlStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) error {
  113. dir, name := fullpath.DirAndName()
  114. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlDelete, util.HashStringToLong(dir), name, dir)
  115. if err != nil {
  116. return fmt.Errorf("delete %s: %s", fullpath, err)
  117. }
  118. _, err = res.RowsAffected()
  119. if err != nil {
  120. return fmt.Errorf("delete %s but no rows affected: %s", fullpath, err)
  121. }
  122. return nil
  123. }
  124. func (store *AbstractSqlStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) error {
  125. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlDeleteFolderChildren, util.HashStringToLong(string(fullpath)), fullpath)
  126. if err != nil {
  127. return fmt.Errorf("deleteFolderChildren %s: %s", fullpath, err)
  128. }
  129. _, err = res.RowsAffected()
  130. if err != nil {
  131. return fmt.Errorf("deleteFolderChildren %s but no rows affected: %s", fullpath, err)
  132. }
  133. return nil
  134. }
  135. func (store *AbstractSqlStore) ListDirectoryPrefixedEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer.Entry, err error) {
  136. sqlText := store.SqlListExclusive
  137. if inclusive {
  138. sqlText = store.SqlListInclusive
  139. }
  140. rows, err := store.getTxOrDB(ctx).QueryContext(ctx, sqlText, util.HashStringToLong(string(fullpath)), startFileName, string(fullpath), prefix, limit)
  141. if err != nil {
  142. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  143. }
  144. defer rows.Close()
  145. for rows.Next() {
  146. var name string
  147. var data []byte
  148. if err = rows.Scan(&name, &data); err != nil {
  149. glog.V(0).Infof("scan %s : %v", fullpath, err)
  150. return nil, fmt.Errorf("scan %s: %v", fullpath, err)
  151. }
  152. entry := &filer.Entry{
  153. FullPath: util.NewFullPath(string(fullpath), name),
  154. }
  155. if err = entry.DecodeAttributesAndChunks(data); err != nil {
  156. glog.V(0).Infof("scan decode %s : %v", entry.FullPath, err)
  157. return nil, fmt.Errorf("scan decode %s : %v", entry.FullPath, err)
  158. }
  159. entries = append(entries, entry)
  160. }
  161. return entries, nil
  162. }
  163. func (store *AbstractSqlStore) ListDirectoryEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int) (entries []*filer.Entry, err error) {
  164. return store.ListDirectoryPrefixedEntries(ctx, fullpath, startFileName, inclusive, limit, "")
  165. }
  166. func (store *AbstractSqlStore) Shutdown() {
  167. store.DB.Close()
  168. }