Skip to content

Commit

Permalink
builtinconstants: define constants for unique int bit segments
Browse files Browse the repository at this point in the history
This patch defines constants for the sizes and bitmasks for each
bit segment in a unique int generated for row IDs.

Release note: None
  • Loading branch information
andyyang890 committed Aug 3, 2023
1 parent a477482 commit 56b8c1c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/row/row_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func NewDatumRowConverter(
return c, nil
}

const rowIDBits = 64 - builtinconstants.NodeIDBits
const rowIDBits = 64 - builtinconstants.UniqueIntNodeIDBits

// Row inserts kv operations into the current kv batch, and triggers a SendBatch
// if necessary.
Expand Down
28 changes: 25 additions & 3 deletions pkg/sql/sem/builtins/builtinconstants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ const (
CreateSchemaTelemetryJobBuiltinName = "crdb_internal.create_sql_schema_telemetry_job"
)

// NodeIDBits is the number of bits stored in the lower portion of
// GenerateUniqueInt.
const NodeIDBits = 15
// A unique int generated by GenerateUniqueInt is a 64-bit integer with
// the following format:
//
// [1 leading zero bit][48 bits for timestamp][15 bits for nodeID]
const (
// UniqueIntLeadingZeroBits is the number of leading zero bits in a unique
// int generated by GenerateUniqueInt.
UniqueIntLeadingZeroBits = 1

// UniqueIntTimestampBits is the number of bits in the timestamp segment
// in a unique int generated by GenerateUniqueInt.
UniqueIntTimestampBits = 48

// UniqueIntNodeIDBits is the number of bits in the node ID segment
// in a unique int generated by GenerateUniqueInt.
UniqueIntNodeIDBits = 15

// UniqueIntNodeIDMask is a bitmask for the node ID in a unique int
// generated by GenerateUniqueInt.
UniqueIntNodeIDMask = 1<<UniqueIntNodeIDBits - 1

// UniqueIntTimestampMask is a bitmask for the timestamp in a unique int
// generated by GenerateUniqueInt.
UniqueIntTimestampMask = (1<<UniqueIntTimestampBits - 1) << UniqueIntNodeIDBits
)
2 changes: 1 addition & 1 deletion pkg/sql/sem/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -9859,7 +9859,7 @@ func GenerateUniqueInt(instanceID ProcessUniqueID) tree.DInt {
func GenerateUniqueID(instanceID int32, timestamp uint64) tree.DInt {
// We xor in the instanceID so that instanceIDs larger than 32K will flip bits
// in the timestamp portion of the final value instead of always setting them.
id := (timestamp << builtinconstants.NodeIDBits) ^ uint64(instanceID)
id := (timestamp << builtinconstants.UniqueIntNodeIDBits) ^ uint64(instanceID)
return tree.DInt(id)
}

Expand Down

0 comments on commit 56b8c1c

Please sign in to comment.