|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "sync" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | +) |
| 9 | + |
| 10 | +type ingester struct { |
| 11 | + currentStore currentStore |
| 12 | + valueEmitter valueEmitter |
| 13 | + |
| 14 | + // Mutable |
| 15 | + // Stores a list of subject names that have been subscribed to. |
| 16 | + topics map[string]map[uuid.UUID]bool |
| 17 | + mux *sync.RWMutex |
| 18 | +} |
| 19 | + |
| 20 | +func newIngester( |
| 21 | + currentStore currentStore, |
| 22 | + valueEmitter valueEmitter, |
| 23 | +) ingester { |
| 24 | + return ingester{ |
| 25 | + currentStore: currentStore, |
| 26 | + valueEmitter: valueEmitter, |
| 27 | + |
| 28 | + topics: map[string]map[uuid.UUID]bool{}, |
| 29 | + mux: &sync.RWMutex{}, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (i *ingester) refreshSubscriptions(recs []rec) { |
| 34 | + // Use read-write lock to avoid modifying topics while onMessage is processing. |
| 35 | + // TODO: Consider breaking out a separate data structure type for topics. |
| 36 | + i.mux.Lock() |
| 37 | + defer i.mux.Unlock() |
| 38 | + |
| 39 | + recIDs := map[uuid.UUID]bool{} |
| 40 | + for _, record := range recs { |
| 41 | + recIDs[record.ID] = true |
| 42 | + } |
| 43 | + |
| 44 | + toSubscribe := []topicAndId{} |
| 45 | + for _, record := range recs { |
| 46 | + topic, ok := record.Tags["mqttSubject"].(string) |
| 47 | + if !ok { |
| 48 | + log.Printf("Error asserting type for mqttSubject") |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + _, present := i.topics[topic] |
| 53 | + if !present { |
| 54 | + toSubscribe = append(toSubscribe, topicAndId{topic: topic, recID: record.ID}) |
| 55 | + continue |
| 56 | + } |
| 57 | + _, present = i.topics[topic][record.ID] |
| 58 | + if !present { |
| 59 | + toSubscribe = append(toSubscribe, topicAndId{topic: topic, recID: record.ID}) |
| 60 | + continue |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + toUnsubscribe := []topicAndId{} |
| 65 | + for topic, subscribedRecIDs := range i.topics { |
| 66 | + for subscribedRecID, _ := range subscribedRecIDs { |
| 67 | + _, present := recIDs[subscribedRecID] |
| 68 | + if !present { |
| 69 | + toUnsubscribe = append(toUnsubscribe, topicAndId{topic: topic, recID: subscribedRecID}) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + for _, topicAndId := range toSubscribe { |
| 75 | + i.subscribe(topicAndId.topic, topicAndId.recID) |
| 76 | + } |
| 77 | + for _, topicAndId := range toUnsubscribe { |
| 78 | + i.unsubscribe(topicAndId.topic, topicAndId.recID) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// Helper methods |
| 83 | + |
| 84 | +// Subscribe to a topic, and associate the rec with the topic |
| 85 | +func (i *ingester) subscribe(topic string, recID uuid.UUID) { |
| 86 | + i.valueEmitter.subscribe( |
| 87 | + topic, |
| 88 | + func(source string, value float64) { |
| 89 | + // Ensure that we are not modifying topics while onMessage is processing. |
| 90 | + i.mux.RLock() |
| 91 | + defer i.mux.RUnlock() |
| 92 | + |
| 93 | + recIDs := i.topics[source] |
| 94 | + for recID, _ := range recIDs { |
| 95 | + i.currentStore.setCurrent(recID, currentInput{Value: &value}) |
| 96 | + } |
| 97 | + }, |
| 98 | + ) |
| 99 | + |
| 100 | + _, present := i.topics[topic] |
| 101 | + if present { |
| 102 | + i.topics[topic][recID] = true |
| 103 | + } else { |
| 104 | + i.topics[topic] = map[uuid.UUID]bool{recID: true} |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func (i *ingester) unsubscribe(topic string, recID uuid.UUID) { |
| 109 | + if i.topics[topic] == nil { |
| 110 | + return |
| 111 | + } else { |
| 112 | + delete(i.topics[topic], recID) |
| 113 | + } |
| 114 | + |
| 115 | + if len(i.topics[topic]) == 0 { |
| 116 | + i.valueEmitter.unsubscribe(topic) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +type topicAndId struct { |
| 121 | + topic string |
| 122 | + recID uuid.UUID |
| 123 | +} |
0 commit comments