github.com/Cloud-Foundations/Dominator@v0.3.4/lib/stringutil/api.go (about) 1 package stringutil 2 3 import "sync" 4 5 type StringDeduplicator struct { 6 lock bool 7 mutex sync.Mutex 8 mapping map[string]string 9 registered map[string]struct{} 10 statistics StringDuplicationStatistics 11 } 12 13 type StringDuplicationStatistics struct { 14 DuplicateBytes uint64 15 DuplicateStrings uint64 16 UniqueBytes uint64 17 UniqueStrings uint64 18 } 19 20 // ConvertListToMap will convert list entries to map keys. If makeIfEmpty is 21 // true then a map is made for an empty list, otherwise nil is returned. 22 func ConvertListToMap(list []string, makeIfEmpty bool) map[string]struct{} { 23 return convertListToMap(list, makeIfEmpty) 24 } 25 26 // ConvertMapKeysToList will return a list of map keys. 27 func ConvertMapKeysToList(mapData map[string]struct{}, doSort bool) []string { 28 return convertMapKeysToList(mapData, doSort) 29 } 30 31 // DeduplicateList will return a list of strings with duplicates removed, 32 // preserving the (initial) ordering of the input list. If the input list has 33 // no duplicates, it is simply returned rather than returning a copy of the 34 // list. The unique map keys are also returned. If makeIfEmpty, a non-nil map 35 // is always returned. 36 func DeduplicateList(list []string, makeIfEmpty bool) ( 37 []string, map[string]struct{}) { 38 return deduplicateList(list, makeIfEmpty) 39 } 40 41 // NewStringDeduplicator will create a StringDeduplicator which may be used to 42 // eliminate duplicate string contents. It maintains an internal map of unique 43 // strings. If lock is true then each method call will take an exclusive lock. 44 func NewStringDeduplicator(lock bool) *StringDeduplicator { 45 return &StringDeduplicator{lock: lock, mapping: make(map[string]string)} 46 } 47 48 // Clear will clear the internal map and statistics. 49 func (d *StringDeduplicator) Clear() { 50 d.clear() 51 } 52 53 // DeDuplicate will return a string which has the same contents as str. This 54 // method should be called for every string in the application. 55 func (d *StringDeduplicator) DeDuplicate(str string) string { 56 return d.deDuplicate(str) 57 } 58 59 // DeleteUnregistered will delete all strings not previously registered with 60 // Register. This will also delete the previously registered strings. This may 61 // be used for garbage collection. 62 func (d *StringDeduplicator) DeleteUnregistered() { 63 d.deleteUnregistered() 64 } 65 66 // GetStatistics will return de-duplication statistics. 67 func (d *StringDeduplicator) GetStatistics() StringDuplicationStatistics { 68 return d.getStatistics() 69 } 70 71 // Register will register the specified string to indicate it is used, so that 72 // a later call to DeleteUnregistered will not delete it. This method should be 73 // called for every string in the application. 74 func (d *StringDeduplicator) Register(str string) { 75 d.register(str) 76 }