Skip to content

Commit

Permalink
WIP - Implement branchable collections
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Oct 30, 2024
1 parent 7c7c030 commit 5035948
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
69 changes: 69 additions & 0 deletions internal/core/crdt/collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2024 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package crdt

import (
"context"

"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/internal/core"
)

type Collection struct {
store datastore.DSReaderWriter
// schemaVersionKey is the schema version datastore key at the time of commit.
//
// It can be used to identify the collection datastructure state at the time of commit.
schemaVersionKey core.CollectionSchemaVersionKey

Check failure on line 25 in internal/core/crdt/collection.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

undefined: core.CollectionSchemaVersionKey

Check failure on line 25 in internal/core/crdt/collection.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

undefined: core.CollectionSchemaVersionKey

Check failure on line 25 in internal/core/crdt/collection.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

undefined: core.CollectionSchemaVersionKey

Check failure on line 25 in internal/core/crdt/collection.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

undefined: core.CollectionSchemaVersionKey
}

var _ core.ReplicatedData = (*Collection)(nil)

func NewCollection(store datastore.DSReaderWriter, schemaVersionKey core.CollectionSchemaVersionKey) *Collection {

Check failure on line 30 in internal/core/crdt/collection.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

undefined: core.CollectionSchemaVersionKey) (typecheck)

Check failure on line 30 in internal/core/crdt/collection.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

undefined: core.CollectionSchemaVersionKey (typecheck)

Check failure on line 30 in internal/core/crdt/collection.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

undefined: core.CollectionSchemaVersionKey) (typecheck)
return &Collection{
store: store,
schemaVersionKey: schemaVersionKey,
}
}

func (c *Collection) Merge(ctx context.Context, other core.Delta) error {
panic("todo")
}

func (c *Collection) Append() *CollectionDelta {
return &CollectionDelta{
SchemaVersionID: c.schemaVersionKey.SchemaVersionID,
}
}

type CollectionDelta struct {
Priority uint64
// todo: This is temporary pending a global collection id (link to ticket)
SchemaVersionID string
}

var _ core.Delta = (*CollectionDelta)(nil)

func (delta *CollectionDelta) IPLDSchemaBytes() []byte {
return []byte(`
type CollectionDelta struct {
priority Int
schemaVersionID String
}`)
}

func (d *CollectionDelta) GetPriority() uint64 {
panic("todo")
}

func (d *CollectionDelta) SetPriority(uint64) {
panic("todo")
}
60 changes: 60 additions & 0 deletions internal/merkle/crdt/collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2024 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package merklecrdt

import (
"context"

cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/internal/core"
coreblock "github.com/sourcenetwork/defradb/internal/core/block"
"github.com/sourcenetwork/defradb/internal/core/crdt"
"github.com/sourcenetwork/defradb/internal/merkle/clock"
)

type MerkleCollection struct {
clock *clock.MerkleClock
reg *crdt.Collection
}

var _ MerkleCRDT = (*MerkleCollection)(nil)

func NewMerkleCollection(
store Stores,
schemaVersionKey core.CollectionSchemaVersionKey,
) *MerkleCollection {
register := crdt.NewCollection(store.Datastore(), schemaVersionKey)

// todo - you can interface HeadStoreKey so that it has a `WithCid` func then it should hopefuly all be good
//clk := clock.NewMerkleClock(store.Headstore(), store.Blockstore(), store.Encstore(), key.ToHeadStoreKey(), register)

return &MerkleCollection{
//clock: clk,
reg: register,
}
}

func (m *MerkleCollection) Clock() *clock.MerkleClock {
return m.clock
}

// Save the value of the Counter to the DAG.
func (m *MerkleCollection) Save(ctx context.Context, data any) (cidlink.Link, []byte, error) {
links, ok := data.([]coreblock.DAGLink)
if !ok {
return cidlink.Link{}, nil, NewErrUnexpectedValueType(client.COMPOSITE, []coreblock.DAGLink{}, data)
}

delta := m.reg.Append()

return m.clock.AddDelta(ctx, delta, links...)
}

0 comments on commit 5035948

Please sign in to comment.