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.

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