Merge branch 'master' into 7490-datamanager-dont-die-return-error
[arvados.git] / services / datamanager / summary / canonical_string.go
1 /* Ensures that we only have one copy of each unique string. This is
2 /* not designed for concurrent access. */
3
4 package summary
5
6 // This code should probably be moved somewhere more universal.
7
8 // CanonicalString struct
9 type CanonicalString struct {
10         m map[string]string
11 }
12
13 // Get a CanonicalString
14 func (cs *CanonicalString) Get(s string) (r string) {
15         if cs.m == nil {
16                 cs.m = make(map[string]string)
17         }
18         value, found := cs.m[s]
19         if found {
20                 return value
21         }
22
23         // s may be a substring of a much larger string.
24         // If we store s, it will prevent that larger string from getting
25         // garbage collected.
26         // If this is something you worry about you should change this code
27         // to make an explict copy of s using a byte array.
28         cs.m[s] = s
29         return s
30 }