Collect adds a new value to the distinct string collector and returns a boolean indicating whether the value was successfully added or not. To check if the limit has been reached, you must call the Exceeded method separately.
(s string)
| 53 | // and returns a boolean indicating whether the value was successfully added or not. |
| 54 | // To check if the limit has been reached, you must call the Exceeded method separately. |
| 55 | func (d *DistinctString) Collect(s string) (added bool) { |
| 56 | d.mtx.Lock() |
| 57 | defer d.mtx.Unlock() |
| 58 | |
| 59 | if d.limExceeded { |
| 60 | return false |
| 61 | } |
| 62 | valueLen := len(s) |
| 63 | |
| 64 | if d.maxDataSize > 0 && d.currDataSize+valueLen >= d.maxDataSize { |
| 65 | d.stopReason = fmt.Sprintf("Max data exceeded: dataSize %d, maxDataSize %d", d.currDataSize, d.maxDataSize) |
| 66 | d.limExceeded = true |
| 67 | return false |
| 68 | } |
| 69 | |
| 70 | if d.maxValues > 0 && d.currentValuesLen >= d.maxValues { |
| 71 | d.stopReason = fmt.Sprintf("Max values exceeded: values %d, maxValues %d", d.currentValuesLen, d.maxValues) |
| 72 | d.limExceeded = true |
| 73 | return false |
| 74 | } |
| 75 | |
| 76 | if d.maxCacheHits > 0 && d.currentCacheHits >= d.maxCacheHits { |
| 77 | d.stopReason = fmt.Sprintf("Max stale values exceeded: cacheHits %d, maxValues %d", d.currentValuesLen, d.maxCacheHits) |
| 78 | d.limExceeded = true |
| 79 | return false |
| 80 | } |
| 81 | |
| 82 | if _, ok := d.values[s]; ok { |
| 83 | // Already present |
| 84 | d.currentCacheHits++ |
| 85 | return false |
| 86 | } |
| 87 | d.currentCacheHits = 0 // CacheHits reset to 0 when a new value is found |
| 88 | |
| 89 | // Clone instead of referencing original |
| 90 | s = strings.Clone(s) |
| 91 | |
| 92 | if d.diffEnabled { |
| 93 | d.new[s] = struct{}{} |
| 94 | } |
| 95 | d.values[s] = struct{}{} |
| 96 | d.currDataSize += valueLen |
| 97 | d.currentValuesLen++ |
| 98 | |
| 99 | return true |
| 100 | } |
| 101 | |
| 102 | // Strings returns the final list of distinct values collected and sorted. |
| 103 | func (d *DistinctString) Strings() []string { |