Skip to content

Commit ae7c206

Browse files
craig[bot]bghal
andcommitted
Merge #148296
148296: sql: simplify the sequence cache size helper r=bghal a=bghal Cleaned up the helper and added testing for clarity. Epic: CRDB-42587 Release note: None Co-authored-by: Brendan Gerrity <[email protected]>
2 parents 84e16c0 + 664835a commit ae7c206

File tree

2 files changed

+84
-14
lines changed

2 files changed

+84
-14
lines changed

pkg/sql/catalog/descpb/structured.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -296,24 +296,25 @@ func (opts *TableDescriptor_SequenceOpts) HasOwner() bool {
296296
return !opts.SequenceOwner.Equal(TableDescriptor_SequenceOpts_SequenceOwner{})
297297
}
298298

299-
// EffectiveCacheSize returns the CacheSize or NodeCacheSize field of a sequence option with
300-
// the exception that it will return 1 if both fields are set to 0.
301-
// A cache size of 1 indicates that there is no caching. A node cache size of 0 indicates there is no
302-
// node-level caching. The returned value
303-
// will always be greater than or equal to 1.
299+
// EffectiveCacheSize evaluates the cache size fields of a sequence option.
300+
// A cache size of 1 indicates that the cache is disabled.
301+
// The session cache size takes precedence: If set and enabled, it will be the one used.
304302
//
305-
// Prior to #51259, sequence caching was unimplemented and cache sizes were
306-
// left uninitialized (ie. to have a value of 0). If a sequence has a cache
307-
// size of 0, it should be treated in the same was as sequences with cache
308-
// sizes of 1.
303+
// Note: An unset cache size is considered disabled.
309304
func (opts *TableDescriptor_SequenceOpts) EffectiveCacheSize() int64 {
310-
if opts.CacheSize == 0 && opts.NodeCacheSize == 0 {
311-
return 1
305+
switch sessionCacheSize := opts.CacheSize; sessionCacheSize {
306+
case 0, 1:
307+
default:
308+
return sessionCacheSize
312309
}
313-
if opts.CacheSize == 1 && opts.NodeCacheSize != 0 {
314-
return opts.NodeCacheSize
310+
311+
switch nodeCacheSize := opts.NodeCacheSize; nodeCacheSize {
312+
case 0, 1:
313+
default:
314+
return nodeCacheSize
315315
}
316-
return opts.CacheSize
316+
317+
return 1 // caches disabled
317318
}
318319

319320
// SafeValue implements the redact.SafeValue interface.

pkg/sql/catalog/descpb/structured_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,72 @@ func TestColumnIDsPermutationOf(t *testing.T) {
7474
})
7575
}
7676
}
77+
78+
func TestEffectiveCacheSize(t *testing.T) {
79+
type testCase struct {
80+
name string
81+
session, node int64
82+
expectedResult int64
83+
}
84+
85+
testCases := []testCase{
86+
{
87+
name: "both unset",
88+
session: 0,
89+
node: 0,
90+
expectedResult: 1,
91+
},
92+
{
93+
name: "session unset",
94+
session: 0,
95+
node: 5,
96+
expectedResult: 5,
97+
},
98+
{
99+
name: "node unset",
100+
session: 5,
101+
node: 0,
102+
expectedResult: 5,
103+
},
104+
105+
{
106+
name: "both disabled",
107+
session: 0,
108+
node: 0,
109+
expectedResult: 1,
110+
},
111+
{
112+
name: "session disabled",
113+
session: 1,
114+
node: 5,
115+
expectedResult: 5,
116+
},
117+
{
118+
name: "session disabled",
119+
session: 5,
120+
node: 1,
121+
expectedResult: 5,
122+
},
123+
124+
{
125+
name: "both enabled",
126+
session: 11,
127+
node: 13,
128+
expectedResult: 11,
129+
},
130+
}
131+
132+
for _, tt := range testCases {
133+
t.Run(tt.name, func(t *testing.T) {
134+
opts := TableDescriptor_SequenceOpts{
135+
CacheSize: tt.session,
136+
NodeCacheSize: tt.node,
137+
}
138+
result := opts.EffectiveCacheSize()
139+
if result != tt.expectedResult {
140+
t.Errorf("EffectiveCacheSize() %s: got %v, want %v", tt.name, result, tt.expectedResult)
141+
}
142+
})
143+
}
144+
145+
}

0 commit comments

Comments
 (0)