updates.go 813 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "time"
  4. )
  5. const (
  6. addAction = 0
  7. removeAction = 1
  8. )
  9. type Update struct {
  10. Song *Song
  11. Action uint8
  12. Timestamp int64
  13. Next *Update
  14. }
  15. var headUpdates map[int]*Update
  16. var tailUpdates map[int]*Update
  17. func init() {
  18. headUpdates = make(map[int]*Update)
  19. tailUpdates = make(map[int]*Update)
  20. }
  21. func addUpdate(pid int, action uint8, song *Song) {
  22. update := new(Update)
  23. update.Song = song
  24. update.Action = action
  25. update.Timestamp = time.Nanoseconds()
  26. pup, ok := tailUpdates[pid]
  27. if ok {
  28. pup.Next = update
  29. } else {
  30. headUpdates[pid] = update
  31. }
  32. tailUpdates[pid] = update
  33. }
  34. func getUpdates(pid int, timestamp int64) *Update {
  35. pup, ok := headUpdates[pid]
  36. if !ok {
  37. return nil
  38. }
  39. for pup != nil {
  40. if pup.Timestamp > timestamp {
  41. return pup
  42. }
  43. pup = pup.Next
  44. }
  45. return nil
  46. }