@@ -39,21 +39,20 @@ type InfoCache struct {
39
39
mu sync.RWMutex
40
40
// cache is sorted by SchemaVersion in descending order
41
41
cache []InfoSchema
42
+ // schema versions sorted by tiemstamp
43
+ sortedTS []versionAndTimestamp
44
+ }
42
45
43
- // keep ts to version mapping
44
- tsToVersion map [uint64 ]int64
45
- versionToTs map [int64 ]uint64
46
- // maintain the order everytime the cahce is updated
47
- descSortedTs []uint64
46
+ type versionAndTimestamp struct {
47
+ version int64
48
+ timestamp int64
48
49
}
49
50
50
51
// NewCache creates a new InfoCache.
51
52
func NewCache (capacity int ) * InfoCache {
52
53
return & InfoCache {
53
- cache : make ([]InfoSchema , 0 , capacity ),
54
- tsToVersion : make (map [uint64 ]int64 ),
55
- versionToTs : make (map [int64 ]uint64 ),
56
- descSortedTs : make ([]uint64 , 0 , capacity ),
54
+ cache : make ([]InfoSchema , 0 , capacity ),
55
+ sortedTS : make ([]versionAndTimestamp , 0 , capacity ),
57
56
}
58
57
}
59
58
@@ -62,9 +61,7 @@ func (h *InfoCache) Reset(capacity int) {
62
61
h .mu .Lock ()
63
62
defer h .mu .Unlock ()
64
63
h .cache = make ([]InfoSchema , 0 , capacity )
65
- h .tsToVersion = make (map [uint64 ]int64 )
66
- h .versionToTs = make (map [int64 ]uint64 )
67
- h .descSortedTs = make ([]uint64 , 0 , capacity )
64
+ h .sortedTS = make ([]versionAndTimestamp , 0 , capacity )
68
65
}
69
66
70
67
// GetLatest gets the newest information schema.
@@ -87,11 +84,11 @@ func (h *InfoCache) GetVersionByTimestamp(ts uint64) (int64, error) {
87
84
}
88
85
89
86
func (h * InfoCache ) getVersionByTimestampNoLock (ts uint64 ) (int64 , error ) {
90
- i := sort .Search (len (h .descSortedTs ), func (i int ) bool {
91
- return h . descSortedTs [i ] <= ts
87
+ i := sort .Search (len (h .sortedTS ), func (i int ) bool {
88
+ return uint64 ( h . sortedTS [i ]. timestamp ) <= ts
92
89
})
93
- if i < len (h .descSortedTs ) {
94
- return h.tsToVersion [h. descSortedTs [ i ]] , nil
90
+ if i < len (h .sortedTS ) {
91
+ return h .sortedTS [ i ]. version , nil
95
92
}
96
93
97
94
return 0 , fmt .Errorf ("no schema cached for timestamp %d" , ts )
@@ -158,34 +155,34 @@ func (h *InfoCache) Insert(is InfoSchema, snapshotTS uint64) bool {
158
155
defer h .mu .Unlock ()
159
156
160
157
version := is .SchemaMetaVersion ()
161
- i := sort .Search (len (h .cache ), func (i int ) bool {
162
- return h .cache [i ].SchemaMetaVersion () <= version
163
- })
164
158
165
- // maintain ts to version mapping
166
- ts , ok := h .versionToTs [version ]
167
- if ok {
168
- // version exists, only update ts if the new one is smaller
169
- if snapshotTS < ts {
170
- h .versionToTs [version ] = snapshotTS
171
- delete (h .tsToVersion , ts )
172
- h .tsToVersion [snapshotTS ] = version
173
- pos := sort .Search (len (h .descSortedTs ), func (i int ) bool {
174
- return h .descSortedTs [i ] < ts
175
- })
176
- if pos > 0 {
177
- h .descSortedTs [pos - 1 ] = snapshotTS
178
- }
159
+ i := sort .Search (len (h .sortedTS ), func (i int ) bool {
160
+ return h .sortedTS [i ].version <= version
161
+ })
162
+ if i < len (h .sortedTS ) && h .sortedTS [i ].version == version {
163
+ // find the version, update the schema timestamp if the new timestamp is earlier
164
+ if snapshotTS < uint64 (h .sortedTS [i ].timestamp ) {
165
+ h .sortedTS [i ].timestamp = int64 (snapshotTS )
179
166
}
180
167
} else {
181
- // add the version
182
- h .versionToTs [version ] = snapshotTS
183
- h .tsToVersion [snapshotTS ] = version
184
- h .descSortedTs = append (h .descSortedTs , snapshotTS )
168
+ if len (h .sortedTS ) < cap (h .sortedTS ) {
169
+ h .sortedTS = h .sortedTS [:len (h .sortedTS )+ 1 ]
170
+ copy (h .sortedTS [i + 1 :], h .sortedTS [i :])
171
+ h .sortedTS [i ] = versionAndTimestamp {
172
+ version : version ,
173
+ timestamp : int64 (snapshotTS ),
174
+ }
175
+ } else if i < len (h .sortedTS ) {
176
+ copy (h .sortedTS [i + 1 :], h .sortedTS [i :])
177
+ h .sortedTS [i ] = versionAndTimestamp {
178
+ version : version ,
179
+ timestamp : int64 (snapshotTS ),
180
+ }
181
+ }
185
182
}
186
- sort . Slice ( h . descSortedTs , func ( i , j int ) bool {
187
- // reverse the order
188
- return h .descSortedTs [i ] > h . descSortedTs [ j ]
183
+
184
+ i = sort . Search ( len ( h . cache ), func ( i int ) bool {
185
+ return h .cache [i ]. SchemaMetaVersion () <= version
189
186
})
190
187
191
188
// cached entry
0 commit comments