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.

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