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.

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