You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cache.go 3.7 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package db
  2. import (
  3. "time"
  4. "github.com/jmoiron/sqlx"
  5. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  6. "gitlink.org.cn/cloudream/storage/common/pkgs/db/model"
  7. )
  8. type CacheDB struct {
  9. *DB
  10. }
  11. func (db *DB) Cache() *CacheDB {
  12. return &CacheDB{DB: db}
  13. }
  14. func (*CacheDB) Get(ctx SQLContext, fileHash string, nodeID cdssdk.NodeID) (model.Cache, error) {
  15. var ret model.Cache
  16. err := sqlx.Get(ctx, &ret, "select * from Cache where FileHash = ? and NodeID = ?", fileHash, nodeID)
  17. return ret, err
  18. }
  19. func (*CacheDB) BatchGetAllFileHashes(ctx SQLContext, start int, count int) ([]string, error) {
  20. var ret []string
  21. err := sqlx.Select(ctx, &ret, "select distinct FileHash from Cache limit ?, ?", start, count)
  22. return ret, err
  23. }
  24. func (*CacheDB) GetByNodeID(ctx SQLContext, nodeID cdssdk.NodeID) ([]model.Cache, error) {
  25. var ret []model.Cache
  26. err := sqlx.Select(ctx, &ret, "select * from Cache where NodeID = ?", nodeID)
  27. return ret, err
  28. }
  29. // Create 创建一条的缓存记录,如果已有则不进行操作
  30. func (*CacheDB) Create(ctx SQLContext, fileHash string, nodeID cdssdk.NodeID, priority int) error {
  31. _, err := ctx.Exec("insert ignore into Cache values(?,?,?,?)", fileHash, nodeID, time.Now(), priority)
  32. if err != nil {
  33. return err
  34. }
  35. return nil
  36. }
  37. // 批量创建缓存记录
  38. func (*CacheDB) BatchCreate(ctx SQLContext, caches []model.Cache) error {
  39. if len(caches) == 0 {
  40. return nil
  41. }
  42. return BatchNamedExec(
  43. ctx,
  44. "insert into Cache(FileHash,NodeID,CreateTime,Priority) values(:FileHash,:NodeID,:CreateTime,:Priority)"+
  45. " on duplicate key update CreateTime=values(CreateTime), Priority=values(Priority)",
  46. 4,
  47. caches,
  48. nil,
  49. )
  50. }
  51. func (*CacheDB) BatchCreateOnSameNode(ctx SQLContext, fileHashes []string, nodeID cdssdk.NodeID, priority int) error {
  52. if len(fileHashes) == 0 {
  53. return nil
  54. }
  55. var caches []model.Cache
  56. var nowTime = time.Now()
  57. for _, hash := range fileHashes {
  58. caches = append(caches, model.Cache{
  59. FileHash: hash,
  60. NodeID: nodeID,
  61. CreateTime: nowTime,
  62. Priority: priority,
  63. })
  64. }
  65. return BatchNamedExec(ctx,
  66. "insert into Cache(FileHash,NodeID,CreateTime,Priority) values(:FileHash,:NodeID,:CreateTime,:Priority)"+
  67. " on duplicate key update CreateTime=values(CreateTime), Priority=values(Priority)",
  68. 4,
  69. caches,
  70. nil,
  71. )
  72. }
  73. func (*CacheDB) NodeBatchDelete(ctx SQLContext, nodeID cdssdk.NodeID, fileHashes []string) error {
  74. if len(fileHashes) == 0 {
  75. return nil
  76. }
  77. // TODO in语句有长度限制
  78. query, args, err := sqlx.In("delete from Cache where NodeID = ? and FileHash in (?)", nodeID, fileHashes)
  79. if err != nil {
  80. return err
  81. }
  82. _, err = ctx.Exec(query, args...)
  83. return err
  84. }
  85. // GetCachingFileNodes 查找缓存了指定文件的节点
  86. func (*CacheDB) GetCachingFileNodes(ctx SQLContext, fileHash string) ([]cdssdk.Node, error) {
  87. var x []cdssdk.Node
  88. err := sqlx.Select(ctx, &x,
  89. "select Node.* from Cache, Node where Cache.FileHash=? and Cache.NodeID = Node.NodeID", fileHash)
  90. return x, err
  91. }
  92. // DeleteNodeAll 删除一个节点所有的记录
  93. func (*CacheDB) DeleteNodeAll(ctx SQLContext, nodeID cdssdk.NodeID) error {
  94. _, err := ctx.Exec("delete from Cache where NodeID = ?", nodeID)
  95. return err
  96. }
  97. // FindCachingFileUserNodes 在缓存表中查询指定数据所在的节点
  98. func (*CacheDB) FindCachingFileUserNodes(ctx SQLContext, userID cdssdk.NodeID, fileHash string) ([]cdssdk.Node, error) {
  99. var x []cdssdk.Node
  100. err := sqlx.Select(ctx, &x,
  101. "select Node.* from Cache, UserNode, Node where"+
  102. " Cache.FileHash=? and Cache.NodeID = UserNode.NodeID and"+
  103. " UserNode.UserID = ? and UserNode.NodeID = Node.NodeID", fileHash, userID)
  104. return x, err
  105. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。