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.

264 lines
7.6 KiB

7 years ago
5 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. "strings"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  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 *filer2.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. return fmt.Errorf("insert %s: %s", entry.FullPath, err)
  64. }
  65. _, err = res.RowsAffected()
  66. if err != nil {
  67. return fmt.Errorf("insert %s but no rows affected: %s", entry.FullPath, err)
  68. }
  69. return nil
  70. }
  71. func (store *AbstractSqlStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  72. dir, name := entry.FullPath.DirAndName()
  73. meta, err := entry.EncodeAttributesAndChunks()
  74. if err != nil {
  75. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  76. }
  77. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlUpdate, meta, util.HashStringToLong(dir), name, dir)
  78. if err != nil {
  79. return fmt.Errorf("update %s: %s", entry.FullPath, err)
  80. }
  81. _, err = res.RowsAffected()
  82. if err != nil {
  83. return fmt.Errorf("update %s but no rows affected: %s", entry.FullPath, err)
  84. }
  85. return nil
  86. }
  87. func (store *AbstractSqlStore) FindEntry(ctx context.Context, fullpath util.FullPath) (*filer2.Entry, error) {
  88. dir, name := fullpath.DirAndName()
  89. row := store.getTxOrDB(ctx).QueryRowContext(ctx, store.SqlFind, util.HashStringToLong(dir), name, dir)
  90. var data []byte
  91. if err := row.Scan(&data); err != nil {
  92. return nil, filer_pb.ErrNotFound
  93. }
  94. entry := &filer2.Entry{
  95. FullPath: fullpath,
  96. }
  97. if err := entry.DecodeAttributesAndChunks(data); err != nil {
  98. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  99. }
  100. return entry, nil
  101. }
  102. func (store *AbstractSqlStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) error {
  103. dir, name := fullpath.DirAndName()
  104. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlDelete, util.HashStringToLong(dir), name, dir)
  105. if err != nil {
  106. return fmt.Errorf("delete %s: %s", fullpath, err)
  107. }
  108. _, err = res.RowsAffected()
  109. if err != nil {
  110. return fmt.Errorf("delete %s but no rows affected: %s", fullpath, err)
  111. }
  112. return nil
  113. }
  114. func (store *AbstractSqlStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) error {
  115. res, err := store.getTxOrDB(ctx).ExecContext(ctx, store.SqlDeleteFolderChildren, util.HashStringToLong(string(fullpath)), fullpath)
  116. if err != nil {
  117. return fmt.Errorf("deleteFolderChildren %s: %s", fullpath, err)
  118. }
  119. _, err = res.RowsAffected()
  120. if err != nil {
  121. return fmt.Errorf("deleteFolderChildren %s but no rows affected: %s", fullpath, err)
  122. }
  123. return nil
  124. }
  125. //func (store *AbstractSqlStore) ListDirectoryPrefixedEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer2.Entry, err error) {
  126. // sqlText := store.SqlListExclusive
  127. // if inclusive {
  128. // sqlText = store.SqlListInclusive
  129. // }
  130. //
  131. // rows, err := store.getTxOrDB(ctx).QueryContext(ctx, sqlText, util.HashStringToLong(string(fullpath)), startFileName, string(fullpath), prefix, limit)
  132. // if err != nil {
  133. // return nil, fmt.Errorf("list %s : %v", fullpath, err)
  134. // }
  135. // defer rows.Close()
  136. //
  137. // for rows.Next() {
  138. // var name string
  139. // var data []byte
  140. // if err = rows.Scan(&name, &data); err != nil {
  141. // glog.V(0).Infof("scan %s : %v", fullpath, err)
  142. // return nil, fmt.Errorf("scan %s: %v", fullpath, err)
  143. // }
  144. //
  145. // entry := &filer2.Entry{
  146. // FullPath: util.NewFullPath(string(fullpath), name),
  147. // }
  148. // if err = entry.DecodeAttributesAndChunks(data); err != nil {
  149. // glog.V(0).Infof("scan decode %s : %v", entry.FullPath, err)
  150. // return nil, fmt.Errorf("scan decode %s : %v", entry.FullPath, err)
  151. // }
  152. //
  153. // entries = append(entries, entry)
  154. // }
  155. //
  156. // return entries, nil
  157. //}
  158. //func (store *AbstractSqlStore) ListDirectoryEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer2.Entry, err error) {
  159. // return nil, fmt.Errorf("not implemented")
  160. //
  161. //}
  162. func (store *AbstractSqlStore) ListDirectoryPrefixedEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer2.Entry, err error) {
  163. count := 0
  164. notPrefixed, err := store.ListDirectoryEntries(ctx, fullpath, startFileName, inclusive, limit)
  165. if err != nil {
  166. return nil, err
  167. }
  168. if prefix == "" {
  169. return notPrefixed, nil
  170. }
  171. var lastFileName string
  172. for count < limit {
  173. for _, entry := range notPrefixed {
  174. lastFileName = entry.Name()
  175. if strings.HasPrefix(entry.Name(), prefix) {
  176. count++
  177. entries = append(entries, entry)
  178. }
  179. }
  180. if count >= limit {
  181. break
  182. }
  183. notPrefixed, err = store.ListDirectoryEntries(ctx, fullpath, lastFileName, inclusive, limit)
  184. if err != nil {
  185. return nil, err
  186. }
  187. if len(notPrefixed) == 0 {
  188. break
  189. }
  190. }
  191. return entries, nil
  192. }
  193. func (store *AbstractSqlStore) ListDirectoryEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int) (entries []*filer2.Entry, err error) {
  194. sqlText := store.SqlListExclusive
  195. if inclusive {
  196. sqlText = store.SqlListInclusive
  197. }
  198. rows, err := store.getTxOrDB(ctx).QueryContext(ctx, sqlText, util.HashStringToLong(string(fullpath)), startFileName, string(fullpath), limit)
  199. if err != nil {
  200. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  201. }
  202. defer rows.Close()
  203. for rows.Next() {
  204. var name string
  205. var data []byte
  206. if err = rows.Scan(&name, &data); err != nil {
  207. glog.V(0).Infof("scan %s : %v", fullpath, err)
  208. return nil, fmt.Errorf("scan %s: %v", fullpath, err)
  209. }
  210. entry := &filer2.Entry{
  211. FullPath: util.NewFullPath(string(fullpath), name),
  212. }
  213. if err = entry.DecodeAttributesAndChunks(data); err != nil {
  214. glog.V(0).Infof("scan decode %s : %v", entry.FullPath, err)
  215. return nil, fmt.Errorf("scan decode %s : %v", entry.FullPath, err)
  216. }
  217. entries = append(entries, entry)
  218. }
  219. return entries, nil
  220. }
  221. func (store *AbstractSqlStore) Shutdown() {
  222. store.DB.Close()
  223. }