Skip to content

Commit 56b8c1c

Browse files
committed
builtinconstants: define constants for unique int bit segments
This patch defines constants for the sizes and bitmasks for each bit segment in a unique int generated for row IDs. Release note: None
1 parent a477482 commit 56b8c1c

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

pkg/sql/row/row_converter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ func NewDatumRowConverter(
489489
return c, nil
490490
}
491491

492-
const rowIDBits = 64 - builtinconstants.NodeIDBits
492+
const rowIDBits = 64 - builtinconstants.UniqueIntNodeIDBits
493493

494494
// Row inserts kv operations into the current kv batch, and triggers a SendBatch
495495
// if necessary.

pkg/sql/sem/builtins/builtinconstants/constants.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ const (
7676
CreateSchemaTelemetryJobBuiltinName = "crdb_internal.create_sql_schema_telemetry_job"
7777
)
7878

79-
// NodeIDBits is the number of bits stored in the lower portion of
80-
// GenerateUniqueInt.
81-
const NodeIDBits = 15
79+
// A unique int generated by GenerateUniqueInt is a 64-bit integer with
80+
// the following format:
81+
//
82+
// [1 leading zero bit][48 bits for timestamp][15 bits for nodeID]
83+
const (
84+
// UniqueIntLeadingZeroBits is the number of leading zero bits in a unique
85+
// int generated by GenerateUniqueInt.
86+
UniqueIntLeadingZeroBits = 1
87+
88+
// UniqueIntTimestampBits is the number of bits in the timestamp segment
89+
// in a unique int generated by GenerateUniqueInt.
90+
UniqueIntTimestampBits = 48
91+
92+
// UniqueIntNodeIDBits is the number of bits in the node ID segment
93+
// in a unique int generated by GenerateUniqueInt.
94+
UniqueIntNodeIDBits = 15
95+
96+
// UniqueIntNodeIDMask is a bitmask for the node ID in a unique int
97+
// generated by GenerateUniqueInt.
98+
UniqueIntNodeIDMask = 1<<UniqueIntNodeIDBits - 1
99+
100+
// UniqueIntTimestampMask is a bitmask for the timestamp in a unique int
101+
// generated by GenerateUniqueInt.
102+
UniqueIntTimestampMask = (1<<UniqueIntTimestampBits - 1) << UniqueIntNodeIDBits
103+
)

pkg/sql/sem/builtins/builtins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9859,7 +9859,7 @@ func GenerateUniqueInt(instanceID ProcessUniqueID) tree.DInt {
98599859
func GenerateUniqueID(instanceID int32, timestamp uint64) tree.DInt {
98609860
// We xor in the instanceID so that instanceIDs larger than 32K will flip bits
98619861
// in the timestamp portion of the final value instead of always setting them.
9862-
id := (timestamp << builtinconstants.NodeIDBits) ^ uint64(instanceID)
9862+
id := (timestamp << builtinconstants.UniqueIntNodeIDBits) ^ uint64(instanceID)
98639863
return tree.DInt(id)
98649864
}
98659865

0 commit comments

Comments
 (0)