|
| 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 p2p |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + "strconv" |
| 16 | + "testing" |
| 17 | + |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | + |
| 20 | + "github.com/sourcenetwork/corekv" |
| 21 | + "github.com/sourcenetwork/corekv/memory" |
| 22 | + "github.com/sourcenetwork/immutable" |
| 23 | + |
| 24 | + "github.com/sourcenetwork/defradb/client" |
| 25 | + "github.com/sourcenetwork/defradb/internal/datastore" |
| 26 | + "github.com/sourcenetwork/defradb/internal/db/lock" |
| 27 | + "github.com/sourcenetwork/defradb/internal/keys" |
| 28 | +) |
| 29 | + |
| 30 | +// multistoreDB satisfies DB for the backfill path, which reaches no other method. |
| 31 | +type multistoreDB struct { |
| 32 | + DB |
| 33 | + stores *datastore.Multistore |
| 34 | +} |
| 35 | + |
| 36 | +func (d multistoreDB) Multistore() *datastore.Multistore { return d.stores } |
| 37 | + |
| 38 | +// backfillCollection carries only the identity the backfill reads off a collection. |
| 39 | +type backfillCollection struct { |
| 40 | + client.Collection |
| 41 | + collectionID string |
| 42 | +} |
| 43 | + |
| 44 | +func (c backfillCollection) CollectionID() string { return c.collectionID } |
| 45 | + |
| 46 | +func (c backfillCollection) Version() client.CollectionVersion { |
| 47 | + return client.CollectionVersion{CollectionID: c.collectionID} |
| 48 | +} |
| 49 | + |
| 50 | +// A purge that leaves a document's primary key behind, or any other source of one, must not |
| 51 | +// stop the backfill. The iterator walks the primary prefix in key order, so returning on the |
| 52 | +// first key that will not resolve skips every document sorting after it. |
| 53 | +func TestPushHeadsForAllDocs_SkipsPrimaryKeysWithNoDocument(t *testing.T) { |
| 54 | + ctx := context.Background() |
| 55 | + rootstore := memory.NewDatastore(ctx) |
| 56 | + stores := datastore.NewMultistore(rootstore, lock.NewLockSet(), immutable.None[int]()) |
| 57 | + |
| 58 | + const collectionID = "bafkreicollection" |
| 59 | + const shortID = 1 |
| 60 | + require.NoError(t, stores.Systemstore().Set(ctx, |
| 61 | + keys.NewCollectionID(collectionID).Bytes(), []byte(strconv.Itoa(shortID)))) |
| 62 | + |
| 63 | + // Three primary keys, none of which has a docID mapping behind it. Written through the |
| 64 | + // same unsafe view the backfill iterates, since the locked wrapper wants a txn in context. |
| 65 | + unsafe, ok := stores.Datastore().(interface{ Unsafe() corekv.ReaderWriter }) |
| 66 | + require.True(t, ok, "the backfill reaches the datastore through Unsafe") |
| 67 | + raw := unsafe.Unsafe() |
| 68 | + for _, docShortID := range []uint64{1, 2, 3} { |
| 69 | + key := keys.PrimaryDataStoreKey{CollectionShortID: shortID, DocShortID: docShortID} |
| 70 | + require.NoError(t, raw.Set(ctx, key.Bytes(), []byte{})) |
| 71 | + } |
| 72 | + |
| 73 | + p := &P2P{db: multistoreDB{stores: stores}} |
| 74 | + col := backfillCollection{collectionID: collectionID} |
| 75 | + |
| 76 | + require.NoError(t, p.pushHeadsForAllDocs(ctx, col, "peer"), |
| 77 | + "a primary key with no document must be skipped, not returned as an error") |
| 78 | +} |
0 commit comments