Skip to content

Commit

Permalink
refactor: Remove DataStoreKey from (public) dockey struct (sourcenetw…
Browse files Browse the repository at this point in the history
…ork#278)

* Remove dockey.peerId and unused functions

* Remove key from field

Is not used, and of non-public type

* Remove DataStoreKey from dockey

Was weird, circular, and exposed an internal type

* Use constructor for DataStoreKey from dockey
  • Loading branch information
AndrewSisley authored Mar 7, 2022
1 parent 660746d commit 83a652b
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 101 deletions.
7 changes: 7 additions & 0 deletions core/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
"github.com/sourcenetwork/defradb/document/key"
)

var (
Expand Down Expand Up @@ -138,6 +139,12 @@ func NewDataStoreKey(key string) DataStoreKey {
return dataStoreKey
}

func DataStoreKeyFromDocKey(dockey key.DocKey) DataStoreKey {
return DataStoreKey{
DocKey: dockey.String(),
}
}

// Creates a new HeadStoreKey from a string as best as it can,
// splitting the input using '/' as a field deliminater. It assumes
// that the input string is in the following format:
Expand Down
33 changes: 19 additions & 14 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,13 @@ func (c *Collection) create(ctx context.Context, txn core.Txn, doc *document.Doc
}

dockey := key.NewDocKeyV0(doccid)
if !dockey.Key.Equal(doc.Key().Key) {
return fmt.Errorf("Expected %s, got %s : %w", doc.Key().UUID(), dockey.UUID(), ErrDocVerification)
key := core.DataStoreKeyFromDocKey(dockey)
if key.DocKey != doc.Key().String() {
return fmt.Errorf("Expected %s, got %s : %w", doc.Key(), key.DocKey, ErrDocVerification)
}

// check if doc already exists
exists, err := c.exists(ctx, txn, doc.Key())
exists, err := c.exists(ctx, txn, key)
if err != nil {
return err
}
Expand All @@ -476,7 +477,7 @@ func (c *Collection) create(ctx context.Context, txn core.Txn, doc *document.Doc
}

// write object marker
err = writeObjectMarker(ctx, txn.Datastore(), c.getPrimaryIndexDocKey(doc.Key().Key))
err = writeObjectMarker(ctx, txn.Datastore(), c.getPrimaryIndexDocKey(key))
if err != nil {
return err
}
Expand All @@ -496,7 +497,8 @@ func (c *Collection) Update(ctx context.Context, doc *document.Document) error {
}
defer c.discardImplicitTxn(ctx, txn)

exists, err := c.exists(ctx, txn, doc.Key())
dockey := core.DataStoreKeyFromDocKey(doc.Key())
exists, err := c.exists(ctx, txn, dockey)
if err != nil {
return err
}
Expand Down Expand Up @@ -535,7 +537,8 @@ func (c *Collection) Save(ctx context.Context, doc *document.Document) error {
defer c.discardImplicitTxn(ctx, txn)

// Check if document already exists with key
exists, err := c.exists(ctx, txn, doc.Key())
dockey := core.DataStoreKeyFromDocKey(doc.Key())
exists, err := c.exists(ctx, txn, dockey)
if err != nil {
return err
}
Expand All @@ -557,7 +560,7 @@ func (c *Collection) save(ctx context.Context, txn core.Txn, doc *document.Docum
// Loop through doc values
// => instantiate MerkleCRDT objects
// => Set/Publish new CRDT values
dockey := doc.Key().Key
dockey := core.DataStoreKeyFromDocKey(doc.Key())
links := make([]core.DAGLink, 0)
merge := make(map[string]interface{})
for k, v := range doc.Fields() {
Expand Down Expand Up @@ -626,7 +629,8 @@ func (c *Collection) Delete(ctx context.Context, key key.DocKey) (bool, error) {
}
defer c.discardImplicitTxn(ctx, txn)

exists, err := c.exists(ctx, txn, key)
dsKey := core.DataStoreKeyFromDocKey(key)
exists, err := c.exists(ctx, txn, dsKey)
if err != nil {
return false, err
}
Expand All @@ -635,7 +639,7 @@ func (c *Collection) Delete(ctx context.Context, key key.DocKey) (bool, error) {
}

// run delete, commit if successful
deleted, err := c.delete(ctx, txn, key)
deleted, err := c.delete(ctx, txn, dsKey)
if err != nil {
return false, err
}
Expand All @@ -644,9 +648,9 @@ func (c *Collection) Delete(ctx context.Context, key key.DocKey) (bool, error) {

// at the moment, delete only does data storage delete.
// Dag, and head store will soon follow.
func (c *Collection) delete(ctx context.Context, txn core.Txn, key key.DocKey) (bool, error) {
func (c *Collection) delete(ctx context.Context, txn core.Txn, key core.DataStoreKey) (bool, error) {
q := query.Query{
Prefix: c.getPrimaryIndexDocKey(key.Key).ToString(),
Prefix: c.getPrimaryIndexDocKey(key).ToString(),
KeysOnly: true,
}
res, err := txn.Datastore().Query(ctx, q)
Expand All @@ -673,16 +677,17 @@ func (c *Collection) Exists(ctx context.Context, key key.DocKey) (bool, error) {
}
defer c.discardImplicitTxn(ctx, txn)

exists, err := c.exists(ctx, txn, key)
dsKey := core.DataStoreKeyFromDocKey(key)
exists, err := c.exists(ctx, txn, dsKey)
if err != nil && err != ds.ErrNotFound {
return false, err
}
return exists, c.commitImplicitTxn(ctx, txn)
}

// check if a document exists with the given key
func (c *Collection) exists(ctx context.Context, txn core.Txn, key key.DocKey) (bool, error) {
return txn.Datastore().Has(ctx, c.getPrimaryIndexDocKey(key.Key.WithValueFlag()).ToDS())
func (c *Collection) exists(ctx context.Context, txn core.Txn, key core.DataStoreKey) (bool, error) {
return txn.Datastore().Has(ctx, c.getPrimaryIndexDocKey(key.WithValueFlag()).ToDS())
}

func (c *Collection) saveDocValue(ctx context.Context, txn core.Txn, key core.DataStoreKey, val document.Value) (cid.Cid, error) {
Expand Down
27 changes: 13 additions & 14 deletions db/collection_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func (c *Collection) DeleteWithKey(

defer c.discardImplicitTxn(ctx, txn)

res, err := c.deleteWithKey(ctx, txn, key, opts...)
dsKey := core.DataStoreKeyFromDocKey(key)
res, err := c.deleteWithKey(ctx, txn, dsKey, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -138,7 +139,7 @@ func (c *Collection) DeleteWithFilter(
func (c *Collection) deleteWithKey(
ctx context.Context,
txn core.Txn,
key key.DocKey,
key core.DataStoreKey,
opts ...client.DeleteOpt) (*client.DeleteResult, error) {
// Check the docKey we have been given to delete with actually has a corresponding
// document (i.e. document actually exists in the collection).
Expand All @@ -159,7 +160,7 @@ func (c *Collection) deleteWithKey(
// Upon successfull deletion, record a summary.
results := &client.DeleteResult{
Count: 1,
DocKeys: []string{key.String()},
DocKeys: []string{key.DocKey},
}

return results, nil
Expand All @@ -176,9 +177,10 @@ func (c *Collection) deleteWithKeys(
}

for _, key := range keys {
dsKey := core.DataStoreKeyFromDocKey(key)

// Check this docKey actually exists.
found, err := c.exists(ctx, txn, key)
found, err := c.exists(ctx, txn, dsKey)

if err != nil {
return nil, err
Expand All @@ -188,7 +190,7 @@ func (c *Collection) deleteWithKeys(
}

// Apply the function that will perform the full deletion of this document.
err = c.applyFullDelete(ctx, txn, key)
err = c.applyFullDelete(ctx, txn, dsKey)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -245,10 +247,7 @@ func (c *Collection) deleteWithFilter(
docKey := query.Values()[parser.DocKeyFieldName].(string)

// Convert from string to key.DocKey.
key, err := key.NewFromString(docKey)
if err != nil {
return nil, err
}
key := core.DataStoreKey{DocKey: docKey}

// Delete the document that is associated with this key we got from the filter.
err = c.applyFullDelete(ctx, txn, key)
Expand Down Expand Up @@ -287,7 +286,7 @@ func newDagDeleter(bstore core.DAGStore) dagDeleter {
// 3) Deleting headstore state.
func (c *Collection) applyFullDelete(
ctx context.Context,
txn core.Txn, dockey key.DocKey) error {
txn core.Txn, dockey core.DataStoreKey) error {

// Check the docKey we have been given to delete with actually has a corresponding
// document (i.e. document actually exists in the collection).
Expand All @@ -305,7 +304,7 @@ func (c *Collection) applyFullDelete(
// Covert dockey to compositeKey as follows:
// * dockey: bae-kljhLKHJG-lkjhgkldjhlzkdf-kdhflkhjsklgh-kjdhlkghjs
// => compositeKey: bae-kljhLKHJG-lkjhgkldjhlzkdf-kdhflkhjsklgh-kjdhlkghjs/C
compositeKey := dockey.Key.ToHeadStoreKey().WithFieldId(core.COMPOSITE_NAMESPACE)
compositeKey := dockey.ToHeadStoreKey().WithFieldId(core.COMPOSITE_NAMESPACE)
headset := clock.NewHeadSet(txn.Headstore(), compositeKey)

// Get all the heads (cids).
Expand All @@ -324,7 +323,7 @@ func (c *Collection) applyFullDelete(

// 2. =========================== Delete datastore state ============================
dataQuery := query.Query{
Prefix: c.getPrimaryIndexDocKey(dockey.Key).ToString(),
Prefix: c.getPrimaryIndexDocKey(dockey).ToString(),
KeysOnly: true,
}
dataResult, err := txn.Datastore().Query(ctx, dataQuery)
Expand All @@ -340,15 +339,15 @@ func (c *Collection) applyFullDelete(
}
}
// Delete the parent marker key for this document.
err = txn.Datastore().Delete(ctx, c.getPrimaryIndexDocKey(dockey.Key.WithValueFlag()).ToDS())
err = txn.Datastore().Delete(ctx, c.getPrimaryIndexDocKey(dockey.WithValueFlag()).ToDS())
if err != nil {
return err
}
// ======================== Successfully deleted the datastore state of this document

// 3. =========================== Delete headstore state ===========================
headQuery := query.Query{
Prefix: dockey.Key.ToString(),
Prefix: dockey.ToString(),
KeysOnly: true,
}
headResult, err := txn.Headstore().Query(ctx, headQuery)
Expand Down
9 changes: 5 additions & 4 deletions db/collection_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,24 @@ func (c *Collection) Get(ctx context.Context, key key.DocKey) (*document.Documen
return nil, err
}
defer c.discardImplicitTxn(ctx, txn)
dsKey := core.DataStoreKeyFromDocKey(key)

found, err := c.exists(ctx, txn, key)
found, err := c.exists(ctx, txn, dsKey)
if err != nil {
return nil, err
}
if !found {
return nil, ErrDocumentNotFound
}

doc, err := c.get(ctx, txn, key)
doc, err := c.get(ctx, txn, dsKey)
if err != nil {
return nil, err
}
return doc, c.commitImplicitTxn(ctx, txn)
}

func (c *Collection) get(ctx context.Context, txn core.Txn, key key.DocKey) (*document.Document, error) {
func (c *Collection) get(ctx context.Context, txn core.Txn, key core.DataStoreKey) (*document.Document, error) {
// create a new document fetcher
df := new(fetcher.DocumentFetcher)
desc := &c.desc
Expand All @@ -56,7 +57,7 @@ func (c *Collection) get(ctx context.Context, txn core.Txn, key key.DocKey) (*do
}

// construct target key for DocKey
targetKey := base.MakeIndexKey(desc, index, key.Key.DocKey)
targetKey := base.MakeIndexKey(desc, index, key.DocKey)
// run the doc fetcher
err = df.Start(ctx, txn, core.Spans{core.NewSpan(targetKey, targetKey.PrefixEnd())})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion db/collection_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (c *Collection) updateWithKeys(ctx context.Context, txn core.Txn, keys []ke
return nil, err
}

results.DocKeys[i] = key.Key.DocKey
results.DocKeys[i] = key.String()
results.Count++
}
return results, nil
Expand Down
2 changes: 1 addition & 1 deletion document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ func (doc *Document) toMapWithKey() (map[string]interface{}, error) {
}
docMap[k] = value.Value()
}
docMap["_key"] = doc.Key().Key.DocKey
docMap["_key"] = doc.Key().String()

return docMap, nil
}
Expand Down
7 changes: 0 additions & 7 deletions document/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@ import (

// Field is an interface to interact with Fields inside a document
type Field interface {
Key() core.DataStoreKey
Name() string
Type() core.CType //TODO Abstract into a Field Type interface
SchemaType() string
}

type simpleField struct {
name string
key core.DataStoreKey
crdtType core.CType
schemaType string
}

func (doc *Document) newField(t core.CType, name string, schemaType ...string) Field {
f := simpleField{
name: name,
key: doc.Key().Key.WithFieldId(name),
crdtType: t,
}
if len(schemaType) > 0 {
Expand All @@ -49,10 +46,6 @@ func (field simpleField) Type() core.CType {
return field.crdtType
}

func (field simpleField) Key() core.DataStoreKey {
return field.key
}

func (field simpleField) SchemaType() string {
return field.schemaType
}
Loading

0 comments on commit 83a652b

Please sign in to comment.