Skip to content

Commit

Permalink
apply suggestion for merkle CRDT specific store
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Dec 8, 2023
1 parent 1d6d672 commit 4f69bc3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
1 change: 0 additions & 1 deletion db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,6 @@ func (c *collection) saveFieldToMerkleCRDT(

return merkleCRDT.Set(ctx, bytes)
default:
return nil, 0, client.ErrUnknownCRDT
return nil, 0, client.NewErrUnknownCRDT(val.Type())
}
}
Expand Down
7 changes: 3 additions & 4 deletions merkle/crdt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/core"
corecrdt "github.com/sourcenetwork/defradb/core/crdt"
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/merkle/clock"
)

Expand All @@ -32,19 +31,19 @@ type MerkleCompositeDAG struct {
// NewMerkleCompositeDAG creates a new instance (or loaded from DB) of a MerkleCRDT
// backed by a CompositeDAG CRDT.
func NewMerkleCompositeDAG(
txn datastore.Txn,
store Stores,
schemaVersionKey core.CollectionSchemaVersionKey,
key core.DataStoreKey,
fieldName string,
) *MerkleCompositeDAG {
compositeDag := corecrdt.NewCompositeDAG(
txn.Datastore(),
store.Datastore(),
schemaVersionKey,
key,
fieldName,
)

clock := clock.NewMerkleClock(txn.Headstore(), txn.DAGstore(), key.ToHeadStoreKey(), compositeDag)
clock := clock.NewMerkleClock(store.Headstore(), store.DAGstore(), key.ToHeadStoreKey(), compositeDag)
base := &baseMerkleCRDT{clock: clock, crdt: compositeDag}

return &MerkleCompositeDAG{
Expand Down
7 changes: 3 additions & 4 deletions merkle/crdt/lwwreg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (

"github.com/sourcenetwork/defradb/core"
corecrdt "github.com/sourcenetwork/defradb/core/crdt"
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/merkle/clock"
)

Expand All @@ -31,13 +30,13 @@ type MerkleLWWRegister struct {
// NewMerkleLWWRegister creates a new instance (or loaded from DB) of a MerkleCRDT
// backed by a LWWRegister CRDT.
func NewMerkleLWWRegister(
txn datastore.Txn,
store Stores,
schemaVersionKey core.CollectionSchemaVersionKey,
key core.DataStoreKey,
fieldName string,
) *MerkleLWWRegister {
register := corecrdt.NewLWWRegister(txn.Datastore(), schemaVersionKey, key, fieldName)
clk := clock.NewMerkleClock(txn.Headstore(), txn.DAGstore(), key.ToHeadStoreKey(), register)
register := corecrdt.NewLWWRegister(store.Datastore(), schemaVersionKey, key, fieldName)
clk := clock.NewMerkleClock(store.Headstore(), store.DAGstore(), key.ToHeadStoreKey(), register)
base := &baseMerkleCRDT{clock: clk, crdt: register}
return &MerkleLWWRegister{
baseMerkleCRDT: base,
Expand Down
14 changes: 10 additions & 4 deletions merkle/crdt/merklecrdt.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ var (
log = logging.MustNewLogger("merklecrdt")
)

type Stores interface {
Datastore() datastore.DSReaderWriter
DAGstore() datastore.DAGStore
Headstore() datastore.DSReaderWriter
}

// MerkleCRDT is the implementation of a Merkle Clock along with a
// CRDT payload. It implements the ReplicatedData interface
// so it can be merged with any given semantics.
Expand Down Expand Up @@ -62,7 +68,7 @@ func (base *baseMerkleCRDT) Value(ctx context.Context) ([]byte, error) {
}

func InstanceWithStore(
txn datastore.Txn,
store Stores,
schemaVersionKey core.CollectionSchemaVersionKey,
ctype client.CType,
key core.DataStoreKey,
Expand All @@ -71,18 +77,18 @@ func InstanceWithStore(
switch ctype {
case client.LWW_REGISTER:
return NewMerkleLWWRegister(
txn,
store,
schemaVersionKey,
key,
fieldName,
), nil
case client.COMPOSITE:
return NewMerkleCompositeDAG(
txn,
store,
schemaVersionKey,
key,
fieldName,
), nil
}
return nil, client.ErrUnknownCRDT
return nil, client.NewErrUnknownCRDT(ctype)
}

0 comments on commit 4f69bc3

Please sign in to comment.