github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/lib/stringutil/dedup.go (about) 1 package stringutil 2 3 func (d *StringDeduplicator) clear() { 4 if d.lock { 5 d.mutex.Lock() 6 defer d.mutex.Unlock() 7 } 8 d.mapping = make(map[string]string) 9 d.statistics = StringDuplicationStatistics{} 10 } 11 12 func (d *StringDeduplicator) deDuplicate(str string) string { 13 if str == "" { 14 return "" 15 } 16 if d.lock { 17 d.mutex.Lock() 18 defer d.mutex.Unlock() 19 } 20 if cached, ok := d.mapping[str]; ok { 21 d.statistics.DuplicateBytes += uint64(len(str)) 22 d.statistics.DuplicateStrings++ 23 return cached 24 } else { 25 d.mapping[str] = str 26 d.statistics.UniqueBytes += uint64(len(str)) 27 d.statistics.UniqueStrings++ 28 return str 29 } 30 } 31 32 func (d *StringDeduplicator) getStatistics() StringDuplicationStatistics { 33 if d.lock { 34 d.mutex.Lock() 35 defer d.mutex.Unlock() 36 } 37 return d.statistics 38 }