Skip to content

Commit 4da7ca7

Browse files
committed
address comment
1 parent 611f7e7 commit 4da7ca7

File tree

1 file changed

+37
-40
lines changed

1 file changed

+37
-40
lines changed

infoschema/cache.go

+37-40
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,20 @@ type InfoCache struct {
3939
mu sync.RWMutex
4040
// cache is sorted by SchemaVersion in descending order
4141
cache []InfoSchema
42+
// schema versions sorted by tiemstamp
43+
sortedTS []versionAndTimestamp
44+
}
4245

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
4849
}
4950

5051
// NewCache creates a new InfoCache.
5152
func NewCache(capacity int) *InfoCache {
5253
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),
5756
}
5857
}
5958

@@ -62,9 +61,7 @@ func (h *InfoCache) Reset(capacity int) {
6261
h.mu.Lock()
6362
defer h.mu.Unlock()
6463
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)
6865
}
6966

7067
// GetLatest gets the newest information schema.
@@ -87,11 +84,11 @@ func (h *InfoCache) GetVersionByTimestamp(ts uint64) (int64, error) {
8784
}
8885

8986
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
9289
})
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
9592
}
9693

9794
return 0, fmt.Errorf("no schema cached for timestamp %d", ts)
@@ -158,34 +155,34 @@ func (h *InfoCache) Insert(is InfoSchema, snapshotTS uint64) bool {
158155
defer h.mu.Unlock()
159156

160157
version := is.SchemaMetaVersion()
161-
i := sort.Search(len(h.cache), func(i int) bool {
162-
return h.cache[i].SchemaMetaVersion() <= version
163-
})
164158

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)
179166
}
180167
} 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+
}
185182
}
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
189186
})
190187

191188
// cached entry

0 commit comments

Comments
 (0)