-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vecstore: implement partition manipulation functions
Add persistent storage to vecstore. Implement the methods for partition creation, retrieval and deletion. Add a simple test to ensure these methods are functioning.
- Loading branch information
Showing
9 changed files
with
430 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2024 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the CockroachDB Software License | ||
// included in the /LICENSE file. | ||
|
||
package vecstore | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/kv" | ||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/sql/vecindex/quantize" | ||
) | ||
|
||
type PersistentStore struct { | ||
db *kv.DB // Needed for index maintenance functions | ||
quantizer quantize.Quantizer | ||
rootQuantizer quantize.Quantizer | ||
prefix roachpb.Key | ||
} | ||
|
||
var _ Store = (*PersistentStore)(nil) | ||
|
||
// NewPersistentStore creates a vecstore.Store interface backed by the KV for a | ||
// single vector index. | ||
func NewPersistentStore( | ||
db *kv.DB, quantizer quantize.Quantizer, prefix roachpb.Key, | ||
) *PersistentStore { | ||
ps := PersistentStore{ | ||
db: db, | ||
quantizer: quantizer, | ||
rootQuantizer: quantize.NewUnQuantizer(quantizer.GetOriginalDims()), | ||
prefix: prefix, | ||
} | ||
|
||
return &ps | ||
} | ||
|
||
// Begin is part of the vecstore.Store interface. Begin creates a new KV | ||
// transaction on behalf of the user and prepares it to operate on the persistent | ||
// vector store. | ||
func (s *PersistentStore) Begin(ctx context.Context) (Txn, error) { | ||
return NewPersistentStoreTxn(s, s.db.NewTxn(ctx, "vecstore.PersistentStore begin transaction")), nil | ||
} | ||
|
||
// Commit is part of the vecstore.Store interface. Commit commits the | ||
// underlying KV transaction wrapped by the vecstore.Txn passed in. | ||
func (s *PersistentStore) Commit(ctx context.Context, txn Txn) error { | ||
return txn.(*PersistentStoreTxn).kv.Commit(ctx) | ||
} | ||
|
||
// Abort is part of the vecstore.Store interface. Abort causes the underlying | ||
// KV transaction wrapped by the passed vecstore.Txn to roll back. | ||
func (s *PersistentStore) Abort(ctx context.Context, txn Txn) error { | ||
return txn.(*PersistentStoreTxn).kv.Rollback(ctx) | ||
} | ||
|
||
// MergeStats is part of the vecstore.Store interface. | ||
func (s *PersistentStore) MergeStats(ctx context.Context, stats *IndexStats, skipMerge bool) error { | ||
panic("MergeStats() unimplemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2024 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the CockroachDB Software License | ||
// included in the /LICENSE file. | ||
|
||
package vecstore | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/keys" | ||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/sql/rowenc" | ||
"github.com/cockroachdb/cockroach/pkg/sql/vecindex/internal" | ||
"github.com/cockroachdb/cockroach/pkg/sql/vecindex/quantize" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/vector" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPersistentStore(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
ctx := internal.WithWorkspace(context.Background(), &internal.Workspace{}) | ||
s, _, kvDB := serverutils.StartServer(t, base.TestServerArgs{DefaultTestTenant: base.TestIsForStuffThatShouldWorkWithSecondaryTenantsButDoesntYet(42)}) | ||
defer s.Stopper().Stop(ctx) | ||
|
||
childKey2 := ChildKey{PartitionKey: 2} | ||
childKey10 := ChildKey{PartitionKey: 10} | ||
childKey20 := ChildKey{PartitionKey: 20} | ||
primaryKey200 := ChildKey{PrimaryKey: PrimaryKey{2, 00}} | ||
primaryKey300 := ChildKey{PrimaryKey: PrimaryKey{3, 00}} | ||
primaryKey400 := ChildKey{PrimaryKey: PrimaryKey{4, 00}} | ||
|
||
ten5Codec := keys.MakeSQLCodec(roachpb.MustMakeTenantID(5)) | ||
prefix := rowenc.MakeIndexKeyPrefix(ten5Codec, 500, 42) | ||
quantizer := quantize.NewUnQuantizer(2) | ||
store := NewPersistentStore(kvDB, quantizer, prefix) | ||
|
||
t.Run("insert a root partition into the store and read it back", func(t *testing.T) { | ||
txn := beginTransaction(ctx, t, store) | ||
defer commitTransaction(ctx, t, store, txn) | ||
|
||
vectors := vector.T{4, 3}.AsSet() | ||
quantizedSet := quantizer.Quantize(ctx, &vectors) | ||
root := NewPartition(quantizer, quantizedSet, []ChildKey{childKey2}, Level(2)) | ||
require.NoError(t, txn.SetRootPartition(ctx, root)) | ||
readRoot, err := txn.GetPartition(ctx, RootKey) | ||
require.NoError(t, err) | ||
testingAssertPartitionsEqual(t, root, readRoot) | ||
|
||
vectors = vector.T{4, 3}.AsSet() | ||
vectors.Add(vector.T{2, 1}) | ||
quantizedSet = quantizer.Quantize(ctx, &vectors) | ||
root = NewPartition(quantizer, quantizedSet, []ChildKey{childKey10, childKey20}, Level(2)) | ||
require.NoError(t, txn.SetRootPartition(ctx, root)) | ||
readRoot, err = txn.GetPartition(ctx, RootKey) | ||
require.NoError(t, err) | ||
testingAssertPartitionsEqual(t, root, readRoot) | ||
|
||
vectors = vector.T{4, 3}.AsSet() | ||
vectors.Add(vector.T{2, 1}) | ||
vectors.Add(vector.T{5, 6}) | ||
quantizedSet = quantizer.Quantize(ctx, &vectors) | ||
root = NewPartition(quantizer, quantizedSet, []ChildKey{primaryKey200, primaryKey300, primaryKey400}, LeafLevel) | ||
require.NoError(t, txn.SetRootPartition(ctx, root)) | ||
readRoot, err = txn.GetPartition(ctx, RootKey) | ||
require.NoError(t, err) | ||
testingAssertPartitionsEqual(t, root, readRoot) | ||
}) | ||
|
||
t.Run("insert a partition and then delete it", func(t *testing.T) { | ||
txn := beginTransaction(ctx, t, store) | ||
defer commitTransaction(ctx, t, store, txn) | ||
|
||
vectors := vector.T{4, 3}.AsSet() | ||
quantizedSet := quantizer.Quantize(ctx, &vectors) | ||
testPartition := NewPartition(quantizer, quantizedSet, []ChildKey{childKey2}, Level(2)) | ||
partitionKey, err := txn.InsertPartition(ctx, testPartition) | ||
require.NoError(t, err) | ||
newPartition, err := txn.GetPartition(ctx, partitionKey) | ||
require.NoError(t, err) | ||
testingAssertPartitionsEqual(t, testPartition, newPartition) | ||
|
||
err = txn.DeletePartition(ctx, partitionKey) | ||
require.NoError(t, err) | ||
_, err = txn.GetPartition(ctx, partitionKey) | ||
require.Error(t, err) | ||
}) | ||
} |
Oops, something went wrong.