Skip to content

Commit cf87d27

Browse files
committed
vecstore: encoding cleanup
No functional change here, just some code cleanup. Epic: None
1 parent c40c636 commit cf87d27

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

pkg/sql/vecindex/vecencoding/encoding.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Vector KV Value:
5757
func EncodeMetadataKey(
5858
indexPrefix []byte, encodedPrefixCols []byte, partitionKey cspann.PartitionKey,
5959
) roachpb.Key {
60-
capacity := len(indexPrefix) + len(encodedPrefixCols) + EncodedPartitionKeyLen(partitionKey) + 1
60+
capacity := len(indexPrefix) + len(encodedPrefixCols) + EncodedPartitionKeyLen(partitionKey) + EncodedPartitionLevelLen(cspann.InvalidLevel)
6161
keyBuffer := make([]byte, 0, capacity)
6262
keyBuffer = append(keyBuffer, indexPrefix...)
6363
keyBuffer = append(keyBuffer, encodedPrefixCols...)
@@ -132,13 +132,16 @@ func DecodeVectorKey(
132132
keyBytes = keyBytes[prefixLen:]
133133
}
134134

135-
partitionKey, keyBytes, err := DecodePartitionKey(keyBytes)
135+
// Decode the partition key and level.
136+
var partitionKey cspann.PartitionKey
137+
partitionKey, keyBytes, err = DecodePartitionKey(keyBytes)
136138
if err != nil {
137139
return vecIndexKey, err
138140
}
139141
vecIndexKey.PartitionKey = partitionKey
140142

141-
level, keyBytes, err := DecodePartitionLevel(keyBytes)
143+
var level cspann.Level
144+
level, keyBytes, err = DecodePartitionLevel(keyBytes)
142145
if err != nil {
143146
return vecIndexKey, err
144147
}

pkg/sql/vecindex/vecencoding/encoding_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func TestDecodeVectorKey(t *testing.T) {
273273
require.Equal(t, expectedSuffix, indexKey.Suffix)
274274
}
275275

276-
func TestDecodeKeyPrefixColumns(t *testing.T) {
276+
func TestDecodeKeyWithPrefix(t *testing.T) {
277277
var buf []byte
278278

279279
// Encode a prefix column.
@@ -301,7 +301,7 @@ func TestDecodeKeyPrefixColumns(t *testing.T) {
301301
expectedSuffix := []byte("suffixData")
302302
buf = append(buf, expectedSuffix...)
303303

304-
// Extract the key with one prefix column.
304+
// Extract the key with two prefix columns.
305305
key, err := vecencoding.DecodeVectorKey(buf, 2)
306306
require.NoError(t, err)
307307

@@ -313,3 +313,23 @@ func TestDecodeKeyPrefixColumns(t *testing.T) {
313313
// Verify that the remaining bytes form the suffix.
314314
require.Equal(t, expectedSuffix, key.Suffix)
315315
}
316+
317+
func TestDecodeVectorKeyNoPrefix(t *testing.T) {
318+
var buf []byte
319+
// Encode a partition key.
320+
partitionKey := cspann.PartitionKey(789)
321+
buf = vecencoding.EncodePartitionKey(buf, partitionKey)
322+
// Encode a partition level.
323+
level := cspann.Level(3)
324+
buf = vecencoding.EncodePartitionLevel(buf, level)
325+
// Add some suffix bytes.
326+
expectedSuffix := []byte("vectorSuffix")
327+
buf = append(buf, expectedSuffix...)
328+
329+
indexKey, err := vecencoding.DecodeVectorKey(buf, 0)
330+
require.NoError(t, err)
331+
require.Empty(t, indexKey.Prefix)
332+
require.Equal(t, partitionKey, indexKey.PartitionKey)
333+
require.Equal(t, level, indexKey.Level)
334+
require.Equal(t, expectedSuffix, indexKey.Suffix)
335+
}

pkg/sql/vecindex/vecstore/store_txn.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,17 +274,15 @@ func (tx *Txn) SearchPartitions(
274274
for i := range toSearch {
275275
metadataKey := vecencoding.EncodeMetadataKey(tx.store.prefix, treeKey, toSearch[i].Key)
276276
b.Get(metadataKey)
277-
var startKey, endKey roachpb.Key
277+
var startKey roachpb.Key
278278
if toSearch[i].ExcludeLeafVectors {
279279
// Skip past vectors at the leaf level.
280280
startKey = vecencoding.EncodePrefixVectorKey(metadataKey, cspann.SecondLevel)
281-
endKey = vecencoding.EncodeEndVectorKey(metadataKey)
282-
b.Scan(startKey, endKey)
283281
} else {
284282
startKey = vecencoding.EncodeStartVectorKey(metadataKey)
285-
endKey = vecencoding.EncodeEndVectorKey(metadataKey)
286-
b.Scan(startKey, endKey)
287283
}
284+
endKey := vecencoding.EncodeEndVectorKey(metadataKey)
285+
b.Scan(startKey, endKey)
288286

289287
if log.ExpensiveLogEnabled(ctx, 2) {
290288
log.VEventf(ctx, 2, "Scan %s", roachpb.Span{Key: startKey, EndKey: endKey}.String())

0 commit comments

Comments
 (0)