|
| 1 | +// Copyright 2026 Democratized Data Foundation |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0, included in the file |
| 9 | +// licenses/APL.txt. |
| 10 | + |
| 11 | +package datastore |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + "testing" |
| 16 | + |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | + |
| 19 | + "github.com/sourcenetwork/corekv" |
| 20 | + "github.com/sourcenetwork/corekv/memory" |
| 21 | + "github.com/sourcenetwork/immutable" |
| 22 | + |
| 23 | + "github.com/sourcenetwork/defradb/internal/db/lock" |
| 24 | + "github.com/sourcenetwork/defradb/internal/keys" |
| 25 | +) |
| 26 | + |
| 27 | +func TestKindForKey(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + key string |
| 31 | + want ReadKind |
| 32 | + }{ |
| 33 | + {"datastore", "d/1/v/2/3", ReadDoc}, |
| 34 | + {"headstore", "h/1/2", ReadHead}, |
| 35 | + {"blockstore", "bsomecid", ReadBlock}, |
| 36 | + {"systemstore", "s/collection/name/Foo", ReadSystem}, |
| 37 | + {"sequence", "s/seq/doc", ReadSequence}, |
| 38 | + {"peerstore", "p/replicator/1", ReadPeer}, |
| 39 | + {"encstore", "esomecid", ReadEnc}, |
| 40 | + {"unknown", "?/1", 0}, |
| 41 | + {"empty", "", 0}, |
| 42 | + } |
| 43 | + for _, tt := range tests { |
| 44 | + t.Run(tt.name, func(t *testing.T) { |
| 45 | + require.Equal(t, tt.want, kindForKey([]byte(tt.key))) |
| 46 | + }) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestReadKindString(t *testing.T) { |
| 51 | + require.Equal(t, "none", ReadKind(0).String()) |
| 52 | + require.Equal(t, "doc", ReadDoc.String()) |
| 53 | + require.Equal(t, "doc|head|rangeIter", (ReadDoc | ReadHead | ReadRangeIterator).String()) |
| 54 | +} |
| 55 | + |
| 56 | +// The recorder has to see the store discriminator that namespace.Wrap prepends, so these |
| 57 | +// go through a real transaction rather than calling the recorder directly. |
| 58 | +func TestTxnRecordsReadKinds(t *testing.T) { |
| 59 | + ctx := context.Background() |
| 60 | + txn := NewTxnFrom(memory.NewDatastore(ctx), lock.NewLockSet(), 0, false, immutable.None[int]()) |
| 61 | + defer txn.Discard() |
| 62 | + ctx = CtxSetTxn(ctx, txn) |
| 63 | + |
| 64 | + require.Equal(t, ReadKind(0), txn.ReadKinds().Kinds()) |
| 65 | + |
| 66 | + key := keys.DataStoreKey{CollectionShortID: 1, InstanceType: keys.ValueKey, DocShortID: 2, FieldID: "3"} |
| 67 | + _, err := txn.Datastore().Get(ctx, key) |
| 68 | + require.ErrorIs(t, err, corekv.ErrNotFound) |
| 69 | + require.Equal(t, ReadDoc, txn.ReadKinds().Kinds()) |
| 70 | + |
| 71 | + _, err = txn.Headstore().Get(ctx, []byte("/1/2")) |
| 72 | + require.ErrorIs(t, err, corekv.ErrNotFound) |
| 73 | + require.Equal(t, ReadDoc|ReadHead, txn.ReadKinds().Kinds()) |
| 74 | +} |
| 75 | + |
| 76 | +// A start/end iterator is the shape whose bounds check reads one key past the requested |
| 77 | +// range. Distinguishing it from a prefix iterator is the point of the two iterator bits. |
| 78 | +func TestTxnRecordsIteratorShape(t *testing.T) { |
| 79 | + ctx := context.Background() |
| 80 | + |
| 81 | + prefix := keys.DataStoreKey{CollectionShortID: 1, InstanceType: keys.ValueKey, DocShortID: 2} |
| 82 | + |
| 83 | + rangeTxn := NewTxnFrom(memory.NewDatastore(ctx), lock.NewLockSet(), 0, false, immutable.None[int]()) |
| 84 | + defer rangeTxn.Discard() |
| 85 | + iter, err := rangeTxn.Datastore().Iterator( |
| 86 | + CtxSetTxn(ctx, rangeTxn), |
| 87 | + IterOptions{Start: prefix, End: prefix.PrefixEnd()}, |
| 88 | + ) |
| 89 | + require.NoError(t, err) |
| 90 | + require.NoError(t, iter.Close()) |
| 91 | + require.Equal(t, ReadDoc|ReadRangeIterator, rangeTxn.ReadKinds().Kinds()) |
| 92 | + |
| 93 | + prefixTxn := NewTxnFrom(memory.NewDatastore(ctx), lock.NewLockSet(), 0, false, immutable.None[int]()) |
| 94 | + defer prefixTxn.Discard() |
| 95 | + iter, err = prefixTxn.Datastore().Iterator(CtxSetTxn(ctx, prefixTxn), IterOptions{Prefix: prefix}) |
| 96 | + require.NoError(t, err) |
| 97 | + require.NoError(t, iter.Close()) |
| 98 | + require.Equal(t, ReadDoc|ReadPrefixIterator, prefixTxn.ReadKinds().Kinds()) |
| 99 | +} |
0 commit comments