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.

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