hbase_pwd_log.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/binary"
  6. "strconv"
  7. "time"
  8. "go-common/app/job/main/passport/model"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _tPwdLog = "ugc:PwdLog"
  13. _fPwdLog = "pwdlog"
  14. _cPwdLogMid = "mid"
  15. _cPwdLogOldPwd = "old_pwd"
  16. _cPwdLogOldSalt = "old_salt"
  17. _cPwdLogNewPwd = "new_pwd"
  18. _cPwdLogNewSalt = "new_salt"
  19. _cPwdLogIP = "ip"
  20. _cPwdLogTs = "ts"
  21. )
  22. // AddPwdLogHBase add pwd log.
  23. func (d *Dao) AddPwdLogHBase(c context.Context, pwdLog *model.PwdLog) (err error) {
  24. fvs := make(map[string][]byte)
  25. fvs[_cPwdLogMid] = []byte(strconv.FormatInt(pwdLog.Mid, 10))
  26. fvs[_cPwdLogOldPwd] = []byte(pwdLog.OldPwd)
  27. fvs[_cPwdLogOldSalt] = []byte(pwdLog.OldSalt)
  28. fvs[_cPwdLogNewPwd] = []byte(pwdLog.NewPwd)
  29. fvs[_cPwdLogNewSalt] = []byte(pwdLog.NewSalt)
  30. fvs[_cPwdLogTs] = []byte(strconv.FormatInt(pwdLog.Timestamp, 10))
  31. fvs[_cPwdLogIP] = []byte(strconv.FormatInt(pwdLog.IP, 10))
  32. values := map[string]map[string][]byte{_fPwdLog: fvs}
  33. key := rowKeyPwdLog(pwdLog.Mid, pwdLog.Timestamp)
  34. ctx, cancel := context.WithTimeout(c, time.Duration(d.c.HBase.PwdLog.WriteTimeout))
  35. defer cancel()
  36. if _, err = d.pwdLogHBase.PutStr(ctx, _tPwdLog, string(key), values); err != nil {
  37. log.Error("failed to put pwd log to hbase, dao.hbase.Put(%+v) error(%v)", pwdLog, err)
  38. }
  39. log.Info("Add pwdLog to HBase, (%+v)", pwdLog)
  40. return
  41. }
  42. // rowKeyPwdLog generate row key of pwd log.
  43. func rowKeyPwdLog(mid, ts int64) (res []byte) {
  44. buf := bytes.Buffer{}
  45. b := make([]byte, 8)
  46. // reverse mid bytes
  47. binary.BigEndian.PutUint64(b, uint64(mid))
  48. reverse(b)
  49. buf.Write(b)
  50. // (int64_max - ts) bytes
  51. binary.BigEndian.PutUint64(b, uint64(_int64Max-ts))
  52. buf.Write(b)
  53. res = buf.Bytes()
  54. return
  55. }