updates.go 942 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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(id string, timestamp int64) *Update {
  37. db := <-dbPool
  38. pid := getpid(db, id)
  39. dbPool <- db
  40. if pid == -1 {
  41. return nil
  42. }
  43. pup, ok := headUpdates[pid]
  44. if !ok {
  45. return nil
  46. }
  47. for pup != nil {
  48. if pup.Timestamp > timestamp {
  49. return pup
  50. }
  51. pup = pup.Next
  52. }
  53. return nil
  54. }