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.

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