hbase_pwd.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/binary"
  6. "io"
  7. "time"
  8. "go-common/app/service/main/passport/model"
  9. "go-common/library/log"
  10. "github.com/tsuna/gohbase/hrpc"
  11. )
  12. const (
  13. _tPwdLog = "ugc:PwdLog"
  14. _fPwdLog = "pwdlog"
  15. _cPwdLogOldPwd = "old_pwd"
  16. _cPwdLogOldSalt = "old_salt"
  17. )
  18. var (
  19. _tPwdLogB = []byte(_tPwdLog)
  20. _fPwdLogB = []byte(_fPwdLog)
  21. _cPwdLogOldPwdB = []byte(_cPwdLogOldPwd)
  22. _cPwdLogOldSaltB = []byte(_cPwdLogOldSalt)
  23. )
  24. // HistoryPwds get history pwd
  25. func (d *Dao) HistoryPwds(c context.Context, mid int64) (res []*model.HistoryPwd, err error) {
  26. ctx, cancel := context.WithTimeout(c, time.Duration(d.c.HBase.LoginLog.ReadTimeout))
  27. defer cancel()
  28. st := RowKeyPwdLog(mid, _int64Max)
  29. ed := RowKeyPwdLog(mid, 0)
  30. var scaner hrpc.Scanner
  31. scaner, err = d.pwdLogHBase.ScanRange(ctx, _tPwdLogB, st, ed)
  32. if err != nil {
  33. log.Error("hbase.ScanRange(%s, %s, %s) error(%v)", _tPwdLogB, st, ed, err)
  34. return
  35. }
  36. res = make([]*model.HistoryPwd, 0)
  37. for {
  38. var pwd *model.HistoryPwd
  39. var r *hrpc.Result
  40. r, err = scaner.Next()
  41. if err != nil {
  42. if err == io.EOF {
  43. err = nil
  44. break
  45. }
  46. return
  47. }
  48. if pwd, err = scanPwdLog(r.Cells); err != nil {
  49. scaner.Close()
  50. return
  51. }
  52. if pwd != nil {
  53. res = append(res, pwd)
  54. }
  55. }
  56. if err := scaner.Close(); err != nil {
  57. log.Error("hbase.Scanner.Close error(%v)", err)
  58. }
  59. return
  60. }
  61. // RowKeyPwdLog generate row key of pwd log.
  62. func RowKeyPwdLog(mid, ts int64) (res []byte) {
  63. buf := bytes.Buffer{}
  64. b := make([]byte, 8)
  65. // reverse mid bytes
  66. binary.BigEndian.PutUint64(b, uint64(mid))
  67. reverse(b)
  68. buf.Write(b)
  69. // (int64_max - ts) bytes
  70. binary.BigEndian.PutUint64(b, uint64(_int64Max-ts))
  71. buf.Write(b)
  72. res = buf.Bytes()
  73. return
  74. }
  75. func scanPwdLog(cells []*hrpc.Cell) (res *model.HistoryPwd, err error) {
  76. if len(cells) == 0 {
  77. return
  78. }
  79. res = new(model.HistoryPwd)
  80. for _, cell := range cells {
  81. if bytes.Equal(cell.Family, _fPwdLogB) {
  82. switch {
  83. case bytes.Equal(cell.Qualifier, _cPwdLogOldPwdB):
  84. res.OldPwd = string(cell.Value)
  85. case bytes.Equal(cell.Qualifier, _cPwdLogOldSaltB):
  86. res.OldSalt = string(cell.Value)
  87. }
  88. }
  89. }
  90. return
  91. }