history.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package judge
  2. import (
  3. "container/list"
  4. "sync"
  5. "github.com/710leo/urlooker/dataobj"
  6. )
  7. type HistoryData struct {
  8. Timestamp int64 `json:"timestamp"`
  9. Value int64 `json:"value"`
  10. }
  11. var HistoryBigMap = make(map[string]*SafeItemMap)
  12. func InitHistoryBigMap() {
  13. arr := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}
  14. for i := 0; i < 16; i++ {
  15. for j := 0; j < 16; j++ {
  16. HistoryBigMap[arr[i]+arr[j]] = NewSafeItemMap()
  17. }
  18. }
  19. }
  20. type SafeItemMap struct {
  21. sync.RWMutex
  22. M map[string]*SafeLinkedList
  23. }
  24. func NewSafeItemMap() *SafeItemMap {
  25. return &SafeItemMap{M: make(map[string]*SafeLinkedList)}
  26. }
  27. func (this *SafeItemMap) Get(key string) (*SafeLinkedList, bool) {
  28. this.RLock()
  29. defer this.RUnlock()
  30. val, ok := this.M[key]
  31. return val, ok
  32. }
  33. func (this *SafeItemMap) Set(key string, val *SafeLinkedList) {
  34. this.Lock()
  35. defer this.Unlock()
  36. this.M[key] = val
  37. }
  38. func (this *SafeItemMap) Len() int {
  39. this.RLock()
  40. defer this.RUnlock()
  41. return len(this.M)
  42. }
  43. func (this *SafeItemMap) Delete(key string) {
  44. this.Lock()
  45. defer this.Unlock()
  46. delete(this.M, key)
  47. }
  48. func (this *SafeItemMap) BatchDelete(keys []string) {
  49. count := len(keys)
  50. if count == 0 {
  51. return
  52. }
  53. this.Lock()
  54. defer this.Unlock()
  55. for i := 0; i < count; i++ {
  56. delete(this.M, keys[i])
  57. }
  58. }
  59. func (this *SafeItemMap) CleanStale(before int64) {
  60. keys := []string{}
  61. this.RLock()
  62. for key, L := range this.M {
  63. front := L.Front()
  64. if front == nil {
  65. continue
  66. }
  67. if front.Value.(*dataobj.ItemStatus).PushTime < before {
  68. keys = append(keys, key)
  69. }
  70. }
  71. this.RUnlock()
  72. this.BatchDelete(keys)
  73. }
  74. func (this *SafeItemMap) PushFrontAndMaintain(key string, val *dataobj.ItemStatus, maxCount int, now int64) {
  75. if linkedList, exists := this.Get(key); exists {
  76. needJudge := linkedList.PushFrontAndMaintain(val, maxCount)
  77. if needJudge {
  78. Judge(linkedList, val, now)
  79. }
  80. } else {
  81. linkedList := list.New()
  82. linkedList.PushFront(val)
  83. safeList := &SafeLinkedList{L: linkedList}
  84. this.Set(key, safeList)
  85. Judge(safeList, val, now)
  86. }
  87. }