-
Notifications
You must be signed in to change notification settings - Fork 4
/
uuid.go
122 lines (116 loc) · 2.45 KB
/
uuid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package dads
import (
"fmt"
"strings"
"sync"
"github.com/LF-Engineering/dev-analytics-libraries/uuid"
)
var (
// uuidsNonEmptyCache caches UUIDNonEmpty calls
uuidsNonEmptyCache = map[string]string{}
uuidsNonEmptyCacheMtx *sync.RWMutex
// uuidsAffsCache caches UUIDAffs calls
uuidsAffsCache = map[string]string{}
uuidsAffsCacheMtx *sync.RWMutex
)
// ResetUUIDCache - resets cache
func ResetUUIDCache() {
uuidsNonEmptyCache = map[string]string{}
uuidsAffsCache = map[string]string{}
}
// UUIDNonEmpty - generate UUID of string args (all must be non-empty)
// uses internal cache
// used to generate document UUID's
func UUIDNonEmpty(ctx *Ctx, args ...string) (h string) {
k := strings.Join(args, ":")
if MT {
uuidsNonEmptyCacheMtx.RLock()
}
h, ok := uuidsNonEmptyCache[k]
if MT {
uuidsNonEmptyCacheMtx.RUnlock()
}
if ok {
return
}
if ctx.Debug > 1 {
defer func() {
Printf("UUIDNonEmpty(%v) --> %s\n", args, h)
}()
}
defer func() {
if MT {
uuidsNonEmptyCacheMtx.Lock()
}
uuidsNonEmptyCache[k] = h
if MT {
uuidsNonEmptyCacheMtx.Unlock()
}
}()
if ctx.LegacyUUID {
var err error
cmdLine := []string{"uuid.py", "a"}
cmdLine = append(cmdLine, args...)
h, _, err = ExecCommand(ctx, cmdLine, "", nil)
FatalOnError(err)
h = h[:len(h)-1]
return
}
var err error
h, err = uuid.Generate(args...)
if err != nil {
Printf("UUIDNonEmpty error for: %+v\n", args)
h = ""
}
return
}
// UUIDAffs - generate UUID of string args
// uses internal cache
// downcases arguments, all but first can be empty
func UUIDAffs(ctx *Ctx, args ...string) (h string) {
k := strings.Join(args, ":")
if MT {
uuidsAffsCacheMtx.RLock()
}
h, ok := uuidsAffsCache[k]
if MT {
uuidsAffsCacheMtx.RUnlock()
}
if ok {
return
}
if ctx.Debug > 1 {
defer func() {
Printf("UUIDAffs(%v) --> %s\n", args, h)
}()
}
defer func() {
if MT {
uuidsAffsCacheMtx.Lock()
}
uuidsAffsCache[k] = h
if MT {
uuidsAffsCacheMtx.Unlock()
}
}()
if ctx.LegacyUUID {
var err error
cmdLine := []string{"uuid.py", "u"}
cmdLine = append(cmdLine, args...)
h, _, err = ExecCommand(ctx, cmdLine, "", nil)
FatalOnError(err)
h = h[:len(h)-1]
return
}
var err error
if len(args) != 4 {
err = fmt.Errorf("GenerateIdentity requires exactly 4 asrguments, got %+v", args)
} else {
h, err = uuid.GenerateIdentity(&args[0], &args[1], &args[2], &args[3])
}
if err != nil {
Printf("UUIDAffs error for: %+v\n", args)
h = ""
}
return
}