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.

184 lines
5.1 KiB

7 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. )
  9. type AbstractSqlStore struct {
  10. DB *sql.DB
  11. SqlInsert string
  12. SqlUpdate string
  13. SqlFind string
  14. SqlDelete string
  15. SqlDeleteFolderChildren string
  16. SqlListExclusive string
  17. SqlListInclusive string
  18. }
  19. type TxOrDB interface {
  20. ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
  21. QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
  22. QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
  23. }
  24. func (store *AbstractSqlStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  25. tx, err := store.DB.BeginTx(ctx, &sql.TxOptions{
  26. Isolation: sql.LevelReadCommitted,
  27. ReadOnly: false,
  28. })
  29. if err != nil {
  30. return ctx, err
  31. }
  32. return context.WithValue(ctx, "tx", tx), nil
  33. }
  34. func (store *AbstractSqlStore) CommitTransaction(ctx context.Context) error {
  35. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  36. return tx.Commit()
  37. }
  38. return nil
  39. }
  40. func (store *AbstractSqlStore) RollbackTransaction(ctx context.Context) error {
  41. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  42. return tx.Rollback()
  43. }
  44. return nil
  45. }
  46. func (store *AbstractSqlStore) getTxOrDB(ctx context.Context) TxOrDB {
  47. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  48. return tx
  49. }
  50. return store.DB
  51. }
  52. func (store *AbstractSqlStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  53. dir, name := entry.FullPath.DirAndName()
  54. meta, err := entry.EncodeAttributesAndChunks()
  55. if err != nil {
  56. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  57. }
  58. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlInsert, hashToLong(dir), name, dir, meta)
  59. if err != nil {
  60. return fmt.Errorf("insert %s: %s", entry.FullPath, err)
  61. }
  62. _, err = res.RowsAffected()
  63. if err != nil {
  64. return fmt.Errorf("insert %s but no rows affected: %s", entry.FullPath, err)
  65. }
  66. return nil
  67. }
  68. func (store *AbstractSqlStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  69. dir, name := entry.FullPath.DirAndName()
  70. meta, err := entry.EncodeAttributesAndChunks()
  71. if err != nil {
  72. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  73. }
  74. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlUpdate, meta, hashToLong(dir), name, dir)
  75. if err != nil {
  76. return fmt.Errorf("update %s: %s", entry.FullPath, err)
  77. }
  78. _, err = res.RowsAffected()
  79. if err != nil {
  80. return fmt.Errorf("update %s but no rows affected: %s", entry.FullPath, err)
  81. }
  82. return nil
  83. }
  84. func (store *AbstractSqlStore) FindEntry(ctx context.Context, fullpath filer2.FullPath) (*filer2.Entry, error) {
  85. dir, name := fullpath.DirAndName()
  86. row := store.getTxOrDB(ctx).QueryRowContext(ctx, store.SqlFind, hashToLong(dir), name, dir)
  87. var data []byte
  88. if err := row.Scan(&data); err != nil {
  89. return nil, filer2.ErrNotFound
  90. }
  91. entry := &filer2.Entry{
  92. FullPath: fullpath,
  93. }
  94. if err := entry.DecodeAttributesAndChunks(data); err != nil {
  95. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  96. }
  97. return entry, nil
  98. }
  99. func (store *AbstractSqlStore) DeleteEntry(ctx context.Context, fullpath filer2.FullPath) error {
  100. dir, name := fullpath.DirAndName()
  101. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlDelete, hashToLong(dir), name, dir)
  102. if err != nil {
  103. return fmt.Errorf("delete %s: %s", fullpath, err)
  104. }
  105. _, err = res.RowsAffected()
  106. if err != nil {
  107. return fmt.Errorf("delete %s but no rows affected: %s", fullpath, err)
  108. }
  109. return nil
  110. }
  111. func (store *AbstractSqlStore) DeleteFolderChildren(ctx context.Context, fullpath filer2.FullPath) error {
  112. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlDeleteFolderChildren, hashToLong(string(fullpath)), fullpath)
  113. if err != nil {
  114. return fmt.Errorf("deleteFolderChildren %s: %s", fullpath, err)
  115. }
  116. _, err = res.RowsAffected()
  117. if err != nil {
  118. return fmt.Errorf("deleteFolderChildren %s but no rows affected: %s", fullpath, err)
  119. }
  120. return nil
  121. }
  122. func (store *AbstractSqlStore) ListDirectoryEntries(ctx context.Context, fullpath filer2.FullPath, startFileName string, inclusive bool, limit int) (entries []*filer2.Entry, err error) {
  123. sqlText := store.SqlListExclusive
  124. if inclusive {
  125. sqlText = store.SqlListInclusive
  126. }
  127. rows, err := store.getTxOrDB(ctx).QueryContext(ctx, sqlText, hashToLong(string(fullpath)), startFileName, string(fullpath), limit)
  128. if err != nil {
  129. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  130. }
  131. defer rows.Close()
  132. for rows.Next() {
  133. var name string
  134. var data []byte
  135. if err = rows.Scan(&name, &data); err != nil {
  136. glog.V(0).Infof("scan %s : %v", fullpath, err)
  137. return nil, fmt.Errorf("scan %s: %v", fullpath, err)
  138. }
  139. entry := &filer2.Entry{
  140. FullPath: filer2.NewFullPath(string(fullpath), name),
  141. }
  142. if err = entry.DecodeAttributesAndChunks(data); err != nil {
  143. glog.V(0).Infof("scan decode %s : %v", entry.FullPath, err)
  144. return nil, fmt.Errorf("scan decode %s : %v", entry.FullPath, err)
  145. }
  146. entries = append(entries, entry)
  147. }
  148. return entries, nil
  149. }