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.5 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. return BatchNamedExec(
  40. ctx,
  41. "insert into Cache(FileHash,NodeID,CreateTime,Priority) values(:FileHash,:NodeID,:CreateTime,:Priority)"+
  42. " on duplicate key update CreateTime=values(CreateTime), Priority=values(Priority)",
  43. 4,
  44. caches,
  45. nil,
  46. )
  47. }
  48. func (*CacheDB) BatchCreateOnSameNode(ctx SQLContext, fileHashes []string, nodeID cdssdk.NodeID, priority int) error {
  49. var caches []model.Cache
  50. var nowTime = time.Now()
  51. for _, hash := range fileHashes {
  52. caches = append(caches, model.Cache{
  53. FileHash: hash,
  54. NodeID: nodeID,
  55. CreateTime: nowTime,
  56. Priority: priority,
  57. })
  58. }
  59. return BatchNamedExec(ctx,
  60. "insert into Cache(FileHash,NodeID,CreateTime,Priority) values(:FileHash,:NodeID,:CreateTime,:Priority)"+
  61. " on duplicate key update CreateTime=values(CreateTime), Priority=values(Priority)",
  62. 4,
  63. caches,
  64. nil,
  65. )
  66. }
  67. func (*CacheDB) NodeBatchDelete(ctx SQLContext, nodeID cdssdk.NodeID, fileHashes []string) error {
  68. // TODO in语句有长度限制
  69. query, args, err := sqlx.In("delete from Cache where NodeID = ? and FileHash in (?)", nodeID, fileHashes)
  70. if err != nil {
  71. return err
  72. }
  73. _, err = ctx.Exec(query, args...)
  74. return err
  75. }
  76. // GetCachingFileNodes 查找缓存了指定文件的节点
  77. func (*CacheDB) GetCachingFileNodes(ctx SQLContext, fileHash string) ([]cdssdk.Node, error) {
  78. var x []cdssdk.Node
  79. err := sqlx.Select(ctx, &x,
  80. "select Node.* from Cache, Node where Cache.FileHash=? and Cache.NodeID = Node.NodeID", fileHash)
  81. return x, err
  82. }
  83. // DeleteNodeAll 删除一个节点所有的记录
  84. func (*CacheDB) DeleteNodeAll(ctx SQLContext, nodeID cdssdk.NodeID) error {
  85. _, err := ctx.Exec("delete from Cache where NodeID = ?", nodeID)
  86. return err
  87. }
  88. // FindCachingFileUserNodes 在缓存表中查询指定数据所在的节点
  89. func (*CacheDB) FindCachingFileUserNodes(ctx SQLContext, userID cdssdk.NodeID, fileHash string) ([]cdssdk.Node, error) {
  90. var x []cdssdk.Node
  91. err := sqlx.Select(ctx, &x,
  92. "select Node.* from Cache, UserNode, Node where"+
  93. " Cache.FileHash=? and Cache.NodeID = UserNode.NodeID and"+
  94. " UserNode.UserID = ? and UserNode.NodeID = Node.NodeID", fileHash, userID)
  95. return x, err
  96. }

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