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.

263 lines
7.5 KiB

7 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. const (
  23. DEFAULT = "_main"
  24. )
  25. type TxOrDB interface {
  26. ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
  27. QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
  28. QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
  29. }
  30. func (store *AbstractSqlStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  31. tx, err := store.DB.BeginTx(ctx, &sql.TxOptions{
  32. Isolation: sql.LevelReadCommitted,
  33. ReadOnly: false,
  34. })
  35. if err != nil {
  36. return ctx, err
  37. }
  38. return context.WithValue(ctx, "tx", tx), nil
  39. }
  40. func (store *AbstractSqlStore) CommitTransaction(ctx context.Context) error {
  41. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  42. return tx.Commit()
  43. }
  44. return nil
  45. }
  46. func (store *AbstractSqlStore) RollbackTransaction(ctx context.Context) error {
  47. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  48. return tx.Rollback()
  49. }
  50. return nil
  51. }
  52. func (store *AbstractSqlStore) getTxOrDB(ctx context.Context, fullpath util.FullPath, isForChildren bool) (txOrDB TxOrDB, bucket string, shortPath util.FullPath, err error) {
  53. shortPath = fullpath
  54. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  55. return tx, bucket, shortPath, err
  56. }
  57. return store.DB, bucket, shortPath, err
  58. }
  59. func (store *AbstractSqlStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
  60. db, _, shortPath, err := store.getTxOrDB(ctx, entry.FullPath, false)
  61. if err != nil {
  62. return fmt.Errorf("findDB %s : %v", entry.FullPath, err)
  63. }
  64. dir, name := shortPath.DirAndName()
  65. meta, err := entry.EncodeAttributesAndChunks()
  66. if err != nil {
  67. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  68. }
  69. if len(entry.Chunks) > 50 {
  70. meta = util.MaybeGzipData(meta)
  71. }
  72. res, err := db.ExecContext(ctx, store.SqlInsert, util.HashStringToLong(dir), name, dir, meta)
  73. if err == nil {
  74. return
  75. }
  76. if !strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  77. // return fmt.Errorf("insert: %s", err)
  78. // skip this since the error can be in a different language
  79. }
  80. // now the insert failed possibly due to duplication constraints
  81. glog.V(1).Infof("insert %s falls back to update: %v", entry.FullPath, err)
  82. res, err = db.ExecContext(ctx, store.SqlUpdate, meta, util.HashStringToLong(dir), name, dir)
  83. if err != nil {
  84. return fmt.Errorf("upsert %s: %s", entry.FullPath, err)
  85. }
  86. _, err = res.RowsAffected()
  87. if err != nil {
  88. return fmt.Errorf("upsert %s but no rows affected: %s", entry.FullPath, err)
  89. }
  90. return nil
  91. }
  92. func (store *AbstractSqlStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
  93. db, _, shortPath, err := store.getTxOrDB(ctx, entry.FullPath, false)
  94. if err != nil {
  95. return fmt.Errorf("findDB %s : %v", entry.FullPath, err)
  96. }
  97. dir, name := shortPath.DirAndName()
  98. meta, err := entry.EncodeAttributesAndChunks()
  99. if err != nil {
  100. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  101. }
  102. res, err := db.ExecContext(ctx, store.SqlUpdate, meta, util.HashStringToLong(dir), name, dir)
  103. if err != nil {
  104. return fmt.Errorf("update %s: %s", entry.FullPath, err)
  105. }
  106. _, err = res.RowsAffected()
  107. if err != nil {
  108. return fmt.Errorf("update %s but no rows affected: %s", entry.FullPath, err)
  109. }
  110. return nil
  111. }
  112. func (store *AbstractSqlStore) FindEntry(ctx context.Context, fullpath util.FullPath) (*filer.Entry, error) {
  113. db, _, shortPath, err := store.getTxOrDB(ctx, fullpath, false)
  114. if err != nil {
  115. return nil, fmt.Errorf("findDB %s : %v", fullpath, err)
  116. }
  117. dir, name := shortPath.DirAndName()
  118. row := db.QueryRowContext(ctx, store.SqlFind, util.HashStringToLong(dir), name, dir)
  119. var data []byte
  120. if err := row.Scan(&data); err != nil {
  121. if err == sql.ErrNoRows {
  122. return nil, filer_pb.ErrNotFound
  123. }
  124. return nil, fmt.Errorf("find %s: %v", fullpath, err)
  125. }
  126. entry := &filer.Entry{
  127. FullPath: fullpath,
  128. }
  129. if err := entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data)); err != nil {
  130. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  131. }
  132. return entry, nil
  133. }
  134. func (store *AbstractSqlStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) error {
  135. db, _, shortPath, err := store.getTxOrDB(ctx, fullpath, false)
  136. if err != nil {
  137. return fmt.Errorf("findDB %s : %v", fullpath, err)
  138. }
  139. dir, name := shortPath.DirAndName()
  140. res, err := db.ExecContext(ctx, store.SqlDelete, util.HashStringToLong(dir), name, dir)
  141. if err != nil {
  142. return fmt.Errorf("delete %s: %s", fullpath, err)
  143. }
  144. _, err = res.RowsAffected()
  145. if err != nil {
  146. return fmt.Errorf("delete %s but no rows affected: %s", fullpath, err)
  147. }
  148. return nil
  149. }
  150. func (store *AbstractSqlStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) error {
  151. db, bucket, shortPath, err := store.getTxOrDB(ctx, fullpath, true)
  152. if err != nil {
  153. return fmt.Errorf("findDB %s : %v", fullpath, err)
  154. }
  155. if bucket != DEFAULT && shortPath == "/" {
  156. store.deleteTable(bucket)
  157. return nil
  158. }
  159. res, err := db.ExecContext(ctx, store.SqlDeleteFolderChildren, util.HashStringToLong(string(shortPath)), fullpath)
  160. if err != nil {
  161. return fmt.Errorf("deleteFolderChildren %s: %s", fullpath, err)
  162. }
  163. _, err = res.RowsAffected()
  164. if err != nil {
  165. return fmt.Errorf("deleteFolderChildren %s but no rows affected: %s", fullpath, err)
  166. }
  167. return nil
  168. }
  169. func (store *AbstractSqlStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  170. db, _, shortPath, err := store.getTxOrDB(ctx, dirPath, true)
  171. if err != nil {
  172. return lastFileName, fmt.Errorf("findDB %s : %v", dirPath, err)
  173. }
  174. sqlText := store.SqlListExclusive
  175. if includeStartFile {
  176. sqlText = store.SqlListInclusive
  177. }
  178. rows, err := db.QueryContext(ctx, sqlText, util.HashStringToLong(string(shortPath)), startFileName, string(shortPath), prefix+"%", limit+1)
  179. if err != nil {
  180. return lastFileName, fmt.Errorf("list %s : %v", dirPath, err)
  181. }
  182. defer rows.Close()
  183. for rows.Next() {
  184. var name string
  185. var data []byte
  186. if err = rows.Scan(&name, &data); err != nil {
  187. glog.V(0).Infof("scan %s : %v", dirPath, err)
  188. return lastFileName, fmt.Errorf("scan %s: %v", dirPath, err)
  189. }
  190. lastFileName = name
  191. entry := &filer.Entry{
  192. FullPath: util.NewFullPath(string(dirPath), name),
  193. }
  194. if err = entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data)); err != nil {
  195. glog.V(0).Infof("scan decode %s : %v", entry.FullPath, err)
  196. return lastFileName, fmt.Errorf("scan decode %s : %v", entry.FullPath, err)
  197. }
  198. if !eachEntryFunc(entry) {
  199. break
  200. }
  201. }
  202. return lastFileName, nil
  203. }
  204. func (store *AbstractSqlStore) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  205. return store.ListDirectoryPrefixedEntries(ctx, dirPath, startFileName, includeStartFile, limit, "", nil)
  206. }
  207. func (store *AbstractSqlStore) Shutdown() {
  208. store.DB.Close()
  209. }
  210. func (store *AbstractSqlStore) deleteTable(bucket string) {
  211. }