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.

210 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. if err == sql.ErrNoRows {
  102. return nil, filer_pb.ErrNotFound
  103. }
  104. return nil, fmt.Errorf("find %s: %v", fullpath, err)
  105. }
  106. entry := &filer.Entry{
  107. FullPath: fullpath,
  108. }
  109. if err := entry.DecodeAttributesAndChunks(data); err != nil {
  110. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  111. }
  112. return entry, nil
  113. }
  114. func (store *AbstractSqlStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) error {
  115. dir, name := fullpath.DirAndName()
  116. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlDelete, util.HashStringToLong(dir), name, dir)
  117. if err != nil {
  118. return fmt.Errorf("delete %s: %s", fullpath, err)
  119. }
  120. _, err = res.RowsAffected()
  121. if err != nil {
  122. return fmt.Errorf("delete %s but no rows affected: %s", fullpath, err)
  123. }
  124. return nil
  125. }
  126. func (store *AbstractSqlStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) error {
  127. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlDeleteFolderChildren, util.HashStringToLong(string(fullpath)), fullpath)
  128. if err != nil {
  129. return fmt.Errorf("deleteFolderChildren %s: %s", fullpath, err)
  130. }
  131. _, err = res.RowsAffected()
  132. if err != nil {
  133. return fmt.Errorf("deleteFolderChildren %s but no rows affected: %s", fullpath, err)
  134. }
  135. return nil
  136. }
  137. func (store *AbstractSqlStore) ListDirectoryPrefixedEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer.Entry, err error) {
  138. sqlText := store.SqlListExclusive
  139. if inclusive {
  140. sqlText = store.SqlListInclusive
  141. }
  142. rows, err := store.getTxOrDB(ctx).QueryContext(ctx, sqlText, util.HashStringToLong(string(fullpath)), startFileName, string(fullpath), prefix, limit)
  143. if err != nil {
  144. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  145. }
  146. defer rows.Close()
  147. for rows.Next() {
  148. var name string
  149. var data []byte
  150. if err = rows.Scan(&name, &data); err != nil {
  151. glog.V(0).Infof("scan %s : %v", fullpath, err)
  152. return nil, fmt.Errorf("scan %s: %v", fullpath, err)
  153. }
  154. entry := &filer.Entry{
  155. FullPath: util.NewFullPath(string(fullpath), name),
  156. }
  157. if err = entry.DecodeAttributesAndChunks(data); err != nil {
  158. glog.V(0).Infof("scan decode %s : %v", entry.FullPath, err)
  159. return nil, fmt.Errorf("scan decode %s : %v", entry.FullPath, err)
  160. }
  161. entries = append(entries, entry)
  162. }
  163. return entries, nil
  164. }
  165. func (store *AbstractSqlStore) ListDirectoryEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int) (entries []*filer.Entry, err error) {
  166. return store.ListDirectoryPrefixedEntries(ctx, fullpath, startFileName, inclusive, limit, "")
  167. }
  168. func (store *AbstractSqlStore) Shutdown() {
  169. store.DB.Close()
  170. }