-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringStringMap.go
132 lines (119 loc) · 3.72 KB
/
StringStringMap.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
123
124
125
126
127
128
129
130
131
132
// Copyright 2017 Paul C. Brown. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package core
import (
// "github.com/satori/go.uuid"
"log"
"sync"
)
// StringStringMap is a map from a URI (a string) to the UUID of an Element that represents it
type StringStringMap struct {
sync.Mutex
uriUUIDMap map[string]string
}
// NewStringStringMap creates and initializes a stringElementMap
func NewStringStringMap() *StringStringMap {
var uriUUIDMap StringStringMap
uriUUIDMap.uriUUIDMap = make(map[string]string)
return &uriUUIDMap
}
// Clear clears the map
func (seMap *StringStringMap) Clear() {
seMap.TraceableLock()
defer seMap.TraceableUnlock()
seMap.uriUUIDMap = make(map[string]string)
}
// CopyMap returns a copy of the map
func (seMap *StringStringMap) CopyMap() map[string]string {
seMap.TraceableLock()
defer seMap.TraceableUnlock()
copy := make(map[string]string)
for key, value := range seMap.uriUUIDMap {
copy[key] = value
}
return copy
}
// DeleteEntry removes the map entry for the indicated UUID
func (seMap *StringStringMap) DeleteEntry(key string) {
seMap.TraceableLock()
defer seMap.TraceableUnlock()
delete(seMap.uriUUIDMap, key)
}
// GetEntry returns the Element corresponding to the UUID
func (seMap *StringStringMap) GetEntry(key string) string {
seMap.TraceableLock()
defer seMap.TraceableUnlock()
return seMap.uriUUIDMap[key]
}
// IsEquivalent returns true if the map contains the same number of entries
// and each key same value
func (seMap *StringStringMap) IsEquivalent(sem *StringStringMap, printExceptions ...bool) bool {
var print bool
if len(printExceptions) > 0 {
print = printExceptions[0]
}
seMap.TraceableLock()
defer seMap.TraceableUnlock()
sem.TraceableLock()
defer sem.TraceableUnlock()
if len(seMap.uriUUIDMap) != len(sem.uriUUIDMap) {
if print {
log.Printf("Map1 entries that differ from Map2:")
for k := range seMap.uriUUIDMap {
if seMap.uriUUIDMap[k] != sem.uriUUIDMap[k] {
log.Printf("Map1 key: %s Map1 value %s Map2 value %s", k, seMap.uriUUIDMap[k], sem.uriUUIDMap[k])
}
}
log.Printf("Map2 entries that differ frmo Map1")
for k := range sem.uriUUIDMap {
if sem.uriUUIDMap[k] != seMap.uriUUIDMap[k] {
log.Printf("Map2 key: %s Map2 value %s Map1 value %s", k, sem.uriUUIDMap[k], seMap.uriUUIDMap[k])
}
}
}
return false
}
for k := range seMap.uriUUIDMap {
if seMap.uriUUIDMap[k] != sem.uriUUIDMap[k] {
if print {
log.Printf("Map1 entries that differ from Map2:")
for k := range seMap.uriUUIDMap {
if seMap.uriUUIDMap[k] != sem.uriUUIDMap[k] {
log.Printf("Map1 key: %s Map1 value %s Map2 value %s", k, seMap.uriUUIDMap[k], sem.uriUUIDMap[k])
}
}
}
return false
}
}
return true
}
// Print prints the map. The function is intended for use in debugging
func (seMap *StringStringMap) Print(trans *Transaction) {
seMap.TraceableLock()
defer seMap.TraceableUnlock()
for uri, uuid := range seMap.uriUUIDMap {
log.Printf("Uri: %s UUID: %s\n", uri, uuid)
}
}
// SetEntry sets the UUID corresponding to the given URI
func (seMap *StringStringMap) SetEntry(key string, value string) {
seMap.TraceableLock()
defer seMap.TraceableUnlock()
seMap.uriUUIDMap[key] = value
}
// TraceableLock locks the map. If TraceLocks is true in logs the acquisition of the lock
func (seMap *StringStringMap) TraceableLock() {
// if TraceLocks {
// log.Printf("About to lock stringElementMap %p\n", seMap)
// }
seMap.Lock()
}
// TraceableUnlock unlocks the map. If TraceLocks is true it logs the release of the lock
func (seMap *StringStringMap) TraceableUnlock() {
// if TraceLocks {
// log.Printf("About to unlock stringElementMap %p\n", seMap)
// }
seMap.Unlock()
}