Skip to content

Commit

Permalink
WIP - Rename HeadstoreKey
Browse files Browse the repository at this point in the history
In order to accomodate new headstore key types
  • Loading branch information
AndrewSisley committed Nov 5, 2024
1 parent 4dfeab2 commit b9abd21
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions internal/db/fetcher/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type HeadFetcher struct {
func (hf *HeadFetcher) Start(
ctx context.Context,
txn datastore.Txn,
prefix keys.HeadStoreKey,
prefix keys.HeadstoreDocKey,
fieldId immutable.Option[string],
) error {
hf.fieldId = fieldId
Expand Down Expand Up @@ -64,7 +64,7 @@ func (hf *HeadFetcher) FetchNext() (*cid.Cid, error) {
return nil, nil
}

headStoreKey, err := keys.NewHeadStoreKey(res.Key)
headStoreKey, err := keys.NewHeadstoreDocKey(res.Key)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/db/fetcher/versioned.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (vf *VersionedFetcher) Start(ctx context.Context, spans ...core.Span) error

// VersionedFetcher only ever recieves a headstore key
//nolint:forcetypeassert
prefix := spans[0].Start.(keys.HeadStoreKey)
prefix := spans[0].Start.(keys.HeadstoreDocKey)
dk := prefix.DocID
cid := prefix.Cid
if dk == "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/db/p2p_replicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ func (db *db) retryDoc(ctx context.Context, docID string) error {
return err
}
defer txn.Discard(ctx)
headStoreKey := keys.HeadStoreKey{
headStoreKey := keys.HeadstoreDocKey{
DocID: docID,
FieldID: core.COMPOSITE_NAMESPACE,
}
Expand Down
4 changes: 2 additions & 2 deletions internal/keys/datastore_doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ func (k DataStoreKey) WithFieldID(fieldID string) DataStoreKey {
return newKey
}

func (k DataStoreKey) ToHeadStoreKey() HeadStoreKey {
return HeadStoreKey{
func (k DataStoreKey) ToHeadStoreKey() HeadstoreDocKey {
return HeadstoreDocKey{
DocID: k.DocID,
FieldID: k.FieldID,
}
Expand Down
28 changes: 14 additions & 14 deletions internal/keys/headstore_doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,59 @@ import (
ds "github.com/ipfs/go-datastore"
)

type HeadStoreKey struct {
type HeadstoreDocKey struct {
DocID string
FieldID string //can be 'C'
Cid cid.Cid
}

var _ Walkable = (*HeadStoreKey)(nil)
var _ Walkable = (*HeadstoreDocKey)(nil)

// Creates a new HeadStoreKey from a string as best as it can,
// Creates a new HeadstoreDocKey from a string as best as it can,
// splitting the input using '/' as a field deliminator. It assumes
// that the input string is in the following format:
//
// /[DocID]/[FieldId]/[Cid]
//
// Any properties before the above are ignored
func NewHeadStoreKey(key string) (HeadStoreKey, error) {
func NewHeadstoreDocKey(key string) (HeadstoreDocKey, error) {
elements := strings.Split(key, "/")
if len(elements) != 5 {
return HeadStoreKey{}, ErrInvalidKey
return HeadstoreDocKey{}, ErrInvalidKey
}

cid, err := cid.Decode(elements[4])
if err != nil {
return HeadStoreKey{}, err
return HeadstoreDocKey{}, err
}

return HeadStoreKey{
return HeadstoreDocKey{
// elements[0] is empty (key has leading '/')
DocID: elements[2],
FieldID: elements[3],
Cid: cid,
}, nil
}

func (k HeadStoreKey) WithDocID(docID string) HeadStoreKey {
func (k HeadstoreDocKey) WithDocID(docID string) HeadstoreDocKey {
newKey := k
newKey.DocID = docID
return newKey
}

func (k HeadStoreKey) WithCid(c cid.Cid) HeadStoreKey {
func (k HeadstoreDocKey) WithCid(c cid.Cid) HeadstoreDocKey {
newKey := k
newKey.Cid = c
return newKey
}

func (k HeadStoreKey) WithFieldID(fieldID string) HeadStoreKey {
func (k HeadstoreDocKey) WithFieldID(fieldID string) HeadstoreDocKey {
newKey := k
newKey.FieldID = fieldID
return newKey
}

func (k HeadStoreKey) ToString() string {
func (k HeadstoreDocKey) ToString() string {
result := HEADSTORE_DOC

if k.DocID != "" {
Expand All @@ -85,15 +85,15 @@ func (k HeadStoreKey) ToString() string {
return result
}

func (k HeadStoreKey) Bytes() []byte {
func (k HeadstoreDocKey) Bytes() []byte {
return []byte(k.ToString())
}

func (k HeadStoreKey) ToDS() ds.Key {
func (k HeadstoreDocKey) ToDS() ds.Key {
return ds.NewKey(k.ToString())
}

func (k HeadStoreKey) PrefixEnd() Walkable {
func (k HeadstoreDocKey) PrefixEnd() Walkable {
newKey := k

if k.FieldID != "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/merkle/clock/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewMerkleClock(
headstore datastore.DSReaderWriter,
blockstore datastore.Blockstore,
encstore datastore.Blockstore,
namespace keys.HeadStoreKey,
namespace keys.HeadstoreDocKey,
crdt core.ReplicatedData,
) *MerkleClock {
return &MerkleClock{
Expand Down
4 changes: 2 additions & 2 deletions internal/merkle/clock/clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func newTestMerkleClock() *MerkleClock {
multistore.Headstore(),
multistore.Blockstore(),
multistore.Encstore(),
keys.HeadStoreKey{DocID: request.DocIDArgName, FieldID: "1"},
keys.HeadstoreDocKey{DocID: request.DocIDArgName, FieldID: "1"},
reg,
)
}
Expand All @@ -47,7 +47,7 @@ func TestNewMerkleClock(t *testing.T) {
s := newDS()
multistore := datastore.MultiStoreFrom(s)
reg := crdt.NewLWWRegister(multistore.Rootstore(), keys.CollectionSchemaVersionKey{}, keys.DataStoreKey{}, "")
clk := NewMerkleClock(multistore.Headstore(), multistore.Blockstore(), multistore.Encstore(), keys.HeadStoreKey{}, reg)
clk := NewMerkleClock(multistore.Headstore(), multistore.Blockstore(), multistore.Encstore(), keys.HeadstoreDocKey{}, reg)

if clk.headstore != multistore.Headstore() {
t.Error("MerkleClock store not correctly set")
Expand Down
8 changes: 4 additions & 4 deletions internal/merkle/clock/heads.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ import (
// heads manages the current Merkle-CRDT heads.
type heads struct {
store datastore.DSReaderWriter
namespace keys.HeadStoreKey
namespace keys.HeadstoreDocKey
}

func NewHeadSet(store datastore.DSReaderWriter, namespace keys.HeadStoreKey) *heads {
func NewHeadSet(store datastore.DSReaderWriter, namespace keys.HeadstoreDocKey) *heads {
return &heads{
store: store,
namespace: namespace,
}
}

func (hh *heads) key(c cid.Cid) keys.HeadStoreKey {
func (hh *heads) key(c cid.Cid) keys.HeadstoreDocKey {
return hh.namespace.WithCid(c)
}

Expand Down Expand Up @@ -102,7 +102,7 @@ func (hh *heads) List(ctx context.Context) ([]cid.Cid, uint64, error) {
return nil, 0, NewErrFailedToGetNextQResult(r.Error)
}

headKey, err := keys.NewHeadStoreKey(r.Key)
headKey, err := keys.NewHeadstoreDocKey(r.Key)
if err != nil {
return nil, 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/merkle/clock/heads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func newHeadSet() *heads {

return NewHeadSet(
datastore.AsDSReaderWriter(s),
keys.HeadStoreKey{}.WithDocID("myDocID").WithFieldID("1"),
keys.HeadstoreDocKey{}.WithDocID("myDocID").WithFieldID("1"),
)
}

Expand Down
12 changes: 6 additions & 6 deletions internal/planner/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type dagScanNode struct {
queuedCids []*cid.Cid

fetcher fetcher.HeadFetcher
prefix keys.HeadStoreKey
prefix keys.HeadstoreDocKey
commitSelect *mapper.CommitSelect

execInfo dagScanExecInfo
Expand Down Expand Up @@ -67,10 +67,10 @@ func (n *dagScanNode) Kind() string {
}

func (n *dagScanNode) Init() error {
undefined := keys.HeadStoreKey{}
undefined := keys.HeadstoreDocKey{}
if n.prefix == undefined {
if n.commitSelect.DocID.HasValue() {
key := keys.HeadStoreKey{}.WithDocID(n.commitSelect.DocID.Value())
key := keys.HeadstoreDocKey{}.WithDocID(n.commitSelect.DocID.Value())

if n.commitSelect.FieldID.HasValue() {
field := n.commitSelect.FieldID.Value()
Expand Down Expand Up @@ -106,11 +106,11 @@ func (n *dagScanNode) Spans(spans []core.Span) {
}

for _, span := range spans {
var start keys.HeadStoreKey
var start keys.HeadstoreDocKey
switch s := span.Start.(type) {
case keys.DataStoreKey:
start = s.ToHeadStoreKey()
case keys.HeadStoreKey:
case keys.HeadstoreDocKey:
start = s
}

Expand Down Expand Up @@ -144,7 +144,7 @@ func (n *dagScanNode) simpleExplain() (map[string]any, error) {

// Build the explanation of the spans attribute.
spansExplainer := []map[string]any{}
undefinedHsKey := keys.HeadStoreKey{}
undefinedHsKey := keys.HeadstoreDocKey{}
// Note: n.headset is `nil` for single commit selection query, so must check for it.
if n.prefix != undefinedHsKey {
spansExplainer = append(
Expand Down
4 changes: 2 additions & 2 deletions internal/planner/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ func (n *selectNode) initSource() ([]aggregateNode, error) {
origScan.Spans(
[]core.Span{
core.NewSpan(
keys.HeadStoreKey{
keys.HeadstoreDocKey{
DocID: n.selectReq.DocIDs.Value()[0],
Cid: c,
},
keys.HeadStoreKey{},
keys.HeadstoreDocKey{},
),
},
)
Expand Down
2 changes: 1 addition & 1 deletion net/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func getHead(ctx context.Context, db client.DB, docID client.DocID) (cid.Cid, er
}

if len(entries) > 0 {
hsKey, err := keys.NewHeadStoreKey(entries[0].Key)
hsKey, err := keys.NewHeadstoreDocKey(entries[0].Key)
if err != nil {
return cid.Undef, err
}
Expand Down

0 comments on commit b9abd21

Please sign in to comment.