updates.go 855 B

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