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.

206 lines
6.0 KiB

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