Skip to content

Commit

Permalink
WIP - Simplify VersionedFetcher
Browse files Browse the repository at this point in the history
This simplifies InstanceWithStore, which in turn will allow future simplifications of crdt stuff
  • Loading branch information
AndrewSisley committed Oct 31, 2024
1 parent e628540 commit 4510ceb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 41 deletions.
17 changes: 0 additions & 17 deletions internal/db/base/collection_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
package base

import (
"fmt"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/internal/core"
"github.com/sourcenetwork/defradb/internal/keys"
)

Expand Down Expand Up @@ -43,21 +40,7 @@ func MakePrimaryIndexKeyForCRDT(
fieldName string,
) (keys.DataStoreKey, error) {
switch ctype {
case client.COMPOSITE:
return MakeDataStoreKeyWithCollectionDescription(c.Description).
WithInstanceInfo(key).
WithFieldID(core.COMPOSITE_NAMESPACE),
nil
case client.LWW_REGISTER, client.PN_COUNTER, client.P_COUNTER:
field, ok := c.GetFieldByName(fieldName)
if !ok {
return keys.DataStoreKey{}, client.NewErrFieldNotExist(fieldName)
}

return MakeDataStoreKeyWithCollectionDescription(c.Description).
WithInstanceInfo(key).
WithFieldID(fmt.Sprint(field.ID)),
nil
}
return keys.DataStoreKey{}, ErrInvalidCrdtType
}
39 changes: 21 additions & 18 deletions internal/db/fetcher/versioned.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package fetcher
import (
"container/list"
"context"
"fmt"

"github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
Expand All @@ -27,7 +28,6 @@ import (
"github.com/sourcenetwork/defradb/datastore/memory"
"github.com/sourcenetwork/defradb/internal/core"
coreblock "github.com/sourcenetwork/defradb/internal/core/block"
"github.com/sourcenetwork/defradb/internal/db/base"
"github.com/sourcenetwork/defradb/internal/keys"
merklecrdt "github.com/sourcenetwork/defradb/internal/merkle/crdt"
"github.com/sourcenetwork/defradb/internal/planner/mapper"
Expand Down Expand Up @@ -99,7 +99,7 @@ type VersionedFetcher struct {

col client.Collection
// @todo index *client.IndexDescription
mCRDTs map[uint32]merklecrdt.MerkleCRDT
mCRDTs map[client.FieldID]merklecrdt.MerkleCRDT
}

// Init initializes the VersionedFetcher.
Expand All @@ -118,7 +118,7 @@ func (vf *VersionedFetcher) Init(
vf.acp = acp
vf.col = col
vf.queuedCids = list.New()
vf.mCRDTs = make(map[uint32]merklecrdt.MerkleCRDT)
vf.mCRDTs = make(map[client.FieldID]merklecrdt.MerkleCRDT)
vf.txn = txn

// create store
Expand Down Expand Up @@ -352,7 +352,17 @@ func (vf *VersionedFetcher) merge(c cid.Cid) error {
}

// first arg 0 is the index for the composite DAG in the mCRDTs cache
if err := vf.processBlock(0, block, link, client.COMPOSITE, client.FieldKind_None, ""); err != nil {
mcrdt, exists := vf.mCRDTs[0]
if !exists {
mcrdt = merklecrdt.NewMerkleCompositeDAG(
vf.store,
keys.CollectionSchemaVersionKey{},
vf.dsKey.WithFieldID(core.COMPOSITE_NAMESPACE),
)
vf.mCRDTs[0] = mcrdt
}
err = mcrdt.Clock().ProcessBlock(vf.ctx, block, link)
if err != nil {
return err
}

Expand All @@ -368,7 +378,7 @@ func (vf *VersionedFetcher) merge(c cid.Cid) error {
if !ok {
return client.NewErrFieldNotExist(l.Name)
}
if err := vf.processBlock(uint32(field.ID), subBlock, l.Link, field.Typ, field.Kind, l.Name); err != nil {
if err := vf.processBlock(subBlock, l.Link, field, l.Name); err != nil {
return err
}
}
Expand All @@ -377,32 +387,25 @@ func (vf *VersionedFetcher) merge(c cid.Cid) error {
}

func (vf *VersionedFetcher) processBlock(
crdtIndex uint32,
block *coreblock.Block,
blockLink cidlink.Link,
ctype client.CType,
kind client.FieldKind,
field client.FieldDefinition,
fieldName string,
) (err error) {
// handle CompositeDAG
mcrdt, exists := vf.mCRDTs[crdtIndex]
mcrdt, exists := vf.mCRDTs[field.ID]
if !exists {
dsKey, err := base.MakePrimaryIndexKeyForCRDT(vf.col.Definition(), ctype, vf.dsKey, fieldName)
if err != nil {
return err
}
mcrdt, err = merklecrdt.InstanceWithStore(
vf.store,
keys.CollectionSchemaVersionKey{},
ctype,
kind,
dsKey,
field.Typ,
field.Kind,
vf.dsKey.WithFieldID(fmt.Sprint(field.ID)),
fieldName,
)
if err != nil {
return err
}
vf.mCRDTs[crdtIndex] = mcrdt
vf.mCRDTs[field.ID] = mcrdt
}

return mcrdt.Clock().ProcessBlock(vf.ctx, block, blockLink)
Expand Down
6 changes: 0 additions & 6 deletions internal/merkle/crdt/merklecrdt.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ func InstanceWithStore(
cType == client.PN_COUNTER,
kind.(client.ScalarKind),
), nil
case client.COMPOSITE:
return NewMerkleCompositeDAG(
store,
schemaVersionKey,
key,
), nil
}
return nil, client.NewErrUnknownCRDT(cType)
}

0 comments on commit 4510ceb

Please sign in to comment.