From 9b15f243a16f765fee0784a8c51df473b29fcf81 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Thu, 31 Oct 2024 14:50:39 -0400 Subject: [PATCH] WIP - Simplify saving of doc composites --- internal/db/collection.go | 38 +++++-------------------------- internal/db/collection_delete.go | 15 ++++++------ internal/merkle/crdt/composite.go | 3 +-- 3 files changed, 15 insertions(+), 41 deletions(-) diff --git a/internal/db/collection.go b/internal/db/collection.go index 5d988bdb58..3461fefd80 100644 --- a/internal/db/collection.go +++ b/internal/db/collection.go @@ -19,7 +19,6 @@ import ( ds "github.com/ipfs/go-datastore" "github.com/ipfs/go-datastore/query" - cidlink "github.com/ipld/go-ipld-prime/linking/cid" "github.com/sourcenetwork/immutable" "github.com/sourcenetwork/defradb/acp" @@ -686,12 +685,13 @@ func (c *collection) save( } } - link, headNode, err := c.saveCompositeToMerkleCRDT( - ctx, - primaryKey.ToDataStoreKey(), - links, - client.Active, + merkleCRDT := merklecrdt.NewMerkleCompositeDAG( + txn, + keys.NewCollectionSchemaVersionKey(c.Schema().VersionID, c.ID()), + primaryKey.ToDataStoreKey().WithFieldID(core.COMPOSITE_NAMESPACE), ) + + link, headNode, err := merkleCRDT.Save(ctx, links) if err != nil { return err } @@ -887,32 +887,6 @@ func (c *collection) exists( return true, false, nil } -// saveCompositeToMerkleCRDT saves the composite to the merkle CRDT. -// It returns the CID of the block and the encoded block. -// saveCompositeToMerkleCRDT MUST not be called outside the `c.save` -// and `c.applyDelete` methods as we wrap the acp logic around those methods. -// Calling it elsewhere could cause the omission of acp checks. -func (c *collection) saveCompositeToMerkleCRDT( - ctx context.Context, - dsKey keys.DataStoreKey, - links []coreblock.DAGLink, - status client.DocumentStatus, -) (cidlink.Link, []byte, error) { - txn := mustGetContextTxn(ctx) - dsKey = dsKey.WithFieldID(core.COMPOSITE_NAMESPACE) - merkleCRDT := merklecrdt.NewMerkleCompositeDAG( - txn, - keys.NewCollectionSchemaVersionKey(c.Schema().VersionID, c.ID()), - dsKey, - ) - - if status.IsDeleted() { - return merkleCRDT.Delete(ctx, links) - } - - return merkleCRDT.Save(ctx, links) -} - func (c *collection) getPrimaryKeyFromDocID(docID client.DocID) keys.PrimaryDataStoreKey { return keys.PrimaryDataStoreKey{ CollectionRootID: c.Description().RootID, diff --git a/internal/db/collection_delete.go b/internal/db/collection_delete.go index 468095b54c..b0bf933dda 100644 --- a/internal/db/collection_delete.go +++ b/internal/db/collection_delete.go @@ -16,8 +16,9 @@ import ( "github.com/sourcenetwork/defradb/acp" "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/event" - coreblock "github.com/sourcenetwork/defradb/internal/core/block" + "github.com/sourcenetwork/defradb/internal/core" "github.com/sourcenetwork/defradb/internal/keys" + merklecrdt "github.com/sourcenetwork/defradb/internal/merkle/crdt" ) // DeleteWithFilter deletes using a filter to target documents for delete. @@ -138,14 +139,14 @@ func (c *collection) applyDelete( } txn := mustGetContextTxn(ctx) - dsKey := primaryKey.ToDataStoreKey() - link, b, err := c.saveCompositeToMerkleCRDT( - ctx, - dsKey, - []coreblock.DAGLink{}, - client.Deleted, + merkleCRDT := merklecrdt.NewMerkleCompositeDAG( + txn, + keys.NewCollectionSchemaVersionKey(c.Schema().VersionID, c.ID()), + primaryKey.ToDataStoreKey().WithFieldID(core.COMPOSITE_NAMESPACE), ) + + link, b, err := merkleCRDT.Delete(ctx) if err != nil { return err } diff --git a/internal/merkle/crdt/composite.go b/internal/merkle/crdt/composite.go index 44df12f83f..e7b689a2a0 100644 --- a/internal/merkle/crdt/composite.go +++ b/internal/merkle/crdt/composite.go @@ -60,10 +60,9 @@ func (m *MerkleCompositeDAG) Clock() *clock.MerkleClock { // Delete sets the values of CompositeDAG for a delete. func (m *MerkleCompositeDAG) Delete( ctx context.Context, - links []coreblock.DAGLink, ) (cidlink.Link, []byte, error) { delta := m.reg.Set(client.Deleted) - link, b, err := m.clock.AddDelta(ctx, delta, links...) + link, b, err := m.clock.AddDelta(ctx, delta) if err != nil { return cidlink.Link{}, nil, err }