forked from sourcenetwork/defradb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - Implement branchable collections
- Loading branch information
1 parent
0b08d14
commit 5a57c8a
Showing
10 changed files
with
233 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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" | ||
"github.com/sourcenetwork/defradb/internal/keys" | ||
) | ||
|
||
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 keys.CollectionSchemaVersionKey | ||
} | ||
|
||
var _ core.ReplicatedData = (*Collection)(nil) | ||
|
||
func NewCollection(store datastore.DSReaderWriter, schemaVersionKey keys.CollectionSchemaVersionKey) *Collection { | ||
return &Collection{ | ||
store: store, | ||
schemaVersionKey: schemaVersionKey, | ||
} | ||
} | ||
|
||
func (c *Collection) Merge(ctx context.Context, other core.Delta) error { | ||
// Collection merges don't actually need to do anything, as the delta is empty, | ||
// and doc-level merges are handled by the document commits. | ||
|
||
// todo - make sure this is well tested, you might be wrong! | ||
return nil | ||
} | ||
|
||
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 { | ||
return d.Priority | ||
} | ||
|
||
func (d *CollectionDelta) SetPriority(priority uint64) { | ||
d.Priority = priority | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// 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 keys | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/ipfs/go-cid" | ||
ds "github.com/ipfs/go-datastore" | ||
) | ||
|
||
type HeadstoreColKey struct { | ||
CollectionRootID uint32 | ||
Cid cid.Cid | ||
} | ||
|
||
var _ HeadstoreKey = (*HeadstoreColKey)(nil) | ||
|
||
func NewHeadstoreColKey(colRootID uint32) HeadstoreColKey { | ||
return HeadstoreColKey{ | ||
CollectionRootID: colRootID, | ||
} | ||
} | ||
|
||
func (k HeadstoreColKey) WithCid(c cid.Cid) HeadstoreKey { | ||
newKey := k | ||
newKey.Cid = c | ||
return newKey | ||
} | ||
|
||
func (k HeadstoreColKey) ToString() string { | ||
result := HEADSTORE_COL | ||
|
||
if k.CollectionRootID != 0 { | ||
result = result + "/" + strconv.Itoa(int(k.CollectionRootID)) | ||
} | ||
if k.Cid.Defined() { | ||
result = result + "/" + k.Cid.String() | ||
} | ||
|
||
return result | ||
} | ||
|
||
func (k HeadstoreColKey) Bytes() []byte { | ||
return []byte(k.ToString()) | ||
} | ||
|
||
func (k HeadstoreColKey) ToDS() ds.Key { | ||
return ds.NewKey(k.ToString()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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" | ||
coreblock "github.com/sourcenetwork/defradb/internal/core/block" | ||
"github.com/sourcenetwork/defradb/internal/core/crdt" | ||
"github.com/sourcenetwork/defradb/internal/keys" | ||
"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 keys.CollectionSchemaVersionKey, | ||
key keys.HeadstoreColKey, | ||
) *MerkleCollection { | ||
register := crdt.NewCollection(store.Datastore(), schemaVersionKey) | ||
|
||
clk := clock.NewMerkleClock(store.Headstore(), store.Blockstore(), store.Encstore(), key, register) | ||
|
||
return &MerkleCollection{ | ||
clock: clk, | ||
reg: register, | ||
} | ||
} | ||
|
||
func (m *MerkleCollection) Clock() *clock.MerkleClock { | ||
return m.clock | ||
} | ||
|
||
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...) | ||
} |