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.

215 lines
6.3 KiB

7 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. "strings"
  11. )
  12. type AbstractSqlStore struct {
  13. DB *sql.DB
  14. SqlInsert string
  15. SqlUpdate string
  16. SqlFind string
  17. SqlDelete string
  18. SqlDeleteFolderChildren string
  19. SqlListExclusive string
  20. SqlListInclusive string
  21. }
  22. type TxOrDB interface {
  23. ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
  24. QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
  25. QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
  26. }
  27. func (store *AbstractSqlStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  28. tx, err := store.DB.BeginTx(ctx, &sql.TxOptions{
  29. Isolation: sql.LevelReadCommitted,
  30. ReadOnly: false,
  31. })
  32. if err != nil {
  33. return ctx, err
  34. }
  35. return context.WithValue(ctx, "tx", tx), nil
  36. }
  37. func (store *AbstractSqlStore) CommitTransaction(ctx context.Context) error {
  38. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  39. return tx.Commit()
  40. }
  41. return nil
  42. }
  43. func (store *AbstractSqlStore) RollbackTransaction(ctx context.Context) error {
  44. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  45. return tx.Rollback()
  46. }
  47. return nil
  48. }
  49. func (store *AbstractSqlStore) getTxOrDB(ctx context.Context) TxOrDB {
  50. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  51. return tx
  52. }
  53. return store.DB
  54. }
  55. func (store *AbstractSqlStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
  56. dir, name := entry.FullPath.DirAndName()
  57. meta, err := entry.EncodeAttributesAndChunks()
  58. if err != nil {
  59. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  60. }
  61. if len(entry.Chunks) > 50 {
  62. meta = util.MaybeGzipData(meta)
  63. }
  64. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlInsert, util.HashStringToLong(dir), name, dir, meta)
  65. if err == nil {
  66. return
  67. }
  68. if !strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  69. // return fmt.Errorf("insert: %s", err)
  70. // skip this since the error can be in a different language
  71. }
  72. // now the insert failed possibly due to duplication constraints
  73. glog.V(1).Infof("insert %s falls back to update: %v", entry.FullPath, err)
  74. res, err = store.getTxOrDB(ctx).ExecContext(ctx, store.SqlUpdate, meta, util.HashStringToLong(dir), name, dir)
  75. if err != nil {
  76. return fmt.Errorf("upsert %s: %s", entry.FullPath, err)
  77. }
  78. _, err = res.RowsAffected()
  79. if err != nil {
  80. return fmt.Errorf("upsert %s but no rows affected: %s", entry.FullPath, err)
  81. }
  82. return nil
  83. }
  84. func (store *AbstractSqlStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
  85. dir, name := entry.FullPath.DirAndName()
  86. meta, err := entry.EncodeAttributesAndChunks()
  87. if err != nil {
  88. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  89. }
  90. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlUpdate, meta, util.HashStringToLong(dir), name, dir)
  91. if err != nil {
  92. return fmt.Errorf("update %s: %s", entry.FullPath, err)
  93. }
  94. _, err = res.RowsAffected()
  95. if err != nil {
  96. return fmt.Errorf("update %s but no rows affected: %s", entry.FullPath, err)
  97. }
  98. return nil
  99. }
  100. func (store *AbstractSqlStore) FindEntry(ctx context.Context, fullpath util.FullPath) (*filer.Entry, error) {
  101. dir, name := fullpath.DirAndName()
  102. row := store.getTxOrDB(ctx).QueryRowContext(ctx, store.SqlFind, util.HashStringToLong(dir), name, dir)
  103. var data []byte
  104. if err := row.Scan(&data); err != nil {
  105. if err == sql.ErrNoRows {
  106. return nil, filer_pb.ErrNotFound
  107. }
  108. return nil, fmt.Errorf("find %s: %v", fullpath, err)
  109. }
  110. entry := &filer.Entry{
  111. FullPath: fullpath,
  112. }
  113. if err := entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data)); err != nil {
  114. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  115. }
  116. return entry, nil
  117. }
  118. func (store *AbstractSqlStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) error {
  119. dir, name := fullpath.DirAndName()
  120. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlDelete, util.HashStringToLong(dir), name, dir)
  121. if err != nil {
  122. return fmt.Errorf("delete %s: %s", fullpath, err)
  123. }
  124. _, err = res.RowsAffected()
  125. if err != nil {
  126. return fmt.Errorf("delete %s but no rows affected: %s", fullpath, err)
  127. }
  128. return nil
  129. }
  130. func (store *AbstractSqlStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) error {
  131. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlDeleteFolderChildren, util.HashStringToLong(string(fullpath)), fullpath)
  132. if err != nil {
  133. return fmt.Errorf("deleteFolderChildren %s: %s", fullpath, err)
  134. }
  135. _, err = res.RowsAffected()
  136. if err != nil {
  137. return fmt.Errorf("deleteFolderChildren %s but no rows affected: %s", fullpath, err)
  138. }
  139. return nil
  140. }
  141. func (store *AbstractSqlStore) ListDirectoryPrefixedEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer.Entry, err error) {
  142. sqlText := store.SqlListExclusive
  143. if inclusive {
  144. sqlText = store.SqlListInclusive
  145. }
  146. rows, err := store.getTxOrDB(ctx).QueryContext(ctx, sqlText, util.HashStringToLong(string(fullpath)), startFileName, string(fullpath), prefix+"%", limit)
  147. if err != nil {
  148. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  149. }
  150. defer rows.Close()
  151. for rows.Next() {
  152. var name string
  153. var data []byte
  154. if err = rows.Scan(&name, &data); err != nil {
  155. glog.V(0).Infof("scan %s : %v", fullpath, err)
  156. return nil, fmt.Errorf("scan %s: %v", fullpath, err)
  157. }
  158. entry := &filer.Entry{
  159. FullPath: util.NewFullPath(string(fullpath), name),
  160. }
  161. if err = entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data)); err != nil {
  162. glog.V(0).Infof("scan decode %s : %v", entry.FullPath, err)
  163. return nil, fmt.Errorf("scan decode %s : %v", entry.FullPath, err)
  164. }
  165. entries = append(entries, entry)
  166. }
  167. return entries, nil
  168. }
  169. func (store *AbstractSqlStore) ListDirectoryEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int) (entries []*filer.Entry, err error) {
  170. return store.ListDirectoryPrefixedEntries(ctx, fullpath, startFileName, inclusive, limit, "")
  171. }
  172. func (store *AbstractSqlStore) Shutdown() {
  173. store.DB.Close()
  174. }