Skip to content

Commit

Permalink
I495: Handle version select within create
Browse files Browse the repository at this point in the history
Turned out the bug wasnt really related to this PR (unless there was another bug hiding this one maybe), but I fixed it anyway.
  • Loading branch information
AndrewSisley committed Jun 17, 2022
1 parent 757262b commit 2f2ebc8
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 26 deletions.
14 changes: 14 additions & 0 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,20 @@ func (c *collection) create(ctx context.Context, txn datastore.Txn, doc *client.
}
// write data to DB via MerkleClock/CRDT
_, err = c.save(ctx, txn, doc)

// If this a Batch masked as a Transaction
// commit our writes so we can see them.
// Batches don't maintain serializability, or
// linearization, or any other transaction
// semantics, which the user already knows
// otherwise they wouldn't use a datastore
// that doesn't support proper transactions.
// So let's just commit, and keep going.
if txn.IsBatch() {
if err := txn.Commit(ctx); err != nil {
return err
}
}
return err
}

Expand Down
45 changes: 39 additions & 6 deletions query/graphql/planner/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/core"
"github.com/sourcenetwork/defradb/db/base"
"github.com/sourcenetwork/defradb/query/graphql/mapper"
)

Expand All @@ -42,8 +43,8 @@ type createNode struct {

err error

returned bool
selection *mapper.Select
returned bool
results planNode
}

func (n *createNode) Kind() string { return "createNode" }
Expand Down Expand Up @@ -92,14 +93,41 @@ func (n *createNode) Next() (bool, error) {

n.returned = true
n.currentValue = currentValue

desc := n.collection.Description()
docKey := base.MakeDocKey(desc, currentValue.GetKey())
n.results.Spans(core.Spans{core.NewSpan(docKey, docKey.PrefixEnd())})

err := n.results.Init()
if err != nil {
return false, err
}

err = n.results.Start()
if err != nil {
return false, err
}

// get the next result based on our point lookup
next, err := n.results.Next()
if err != nil {
return false, err
}
if !next {
return false, nil
}

n.currentValue = n.results.Value()
return true, nil
}

func (n *createNode) Spans(spans core.Spans) { /* no-op */ }

func (n *createNode) Close() error { return nil }
func (n *createNode) Close() error {
return n.results.Close()
}

func (n *createNode) Source() planNode { return nil }
func (n *createNode) Source() planNode { return n.results }

// Explain method returns a map containing all attributes of this node that
// are to be explained, subscribes / opts-in this node to be an explainablePlanNode.
Expand All @@ -117,11 +145,16 @@ func (n *createNode) Explain() (map[string]interface{}, error) {
}

func (p *Planner) CreateDoc(parsed *mapper.Mutation) (planNode, error) {
results, err := p.Select(&parsed.Select)
if err != nil {
return nil, err
}

// create a mutation createNode.
create := &createNode{
p: p,
newDocStr: parsed.Data,
selection: &parsed.Select,
results: results,
docMapper: docMapper{&parsed.DocumentMapping},
}

Expand All @@ -131,5 +164,5 @@ func (p *Planner) CreateDoc(parsed *mapper.Mutation) (planNode, error) {
return nil, err
}
create.collection = col
return p.SelectFromSource(&parsed.Select, create, true, nil)
return create, nil
}
8 changes: 5 additions & 3 deletions query/graphql/planner/dagscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,13 @@ func (n *dagScanNode) Spans(spans core.Spans) {
// otherwise, try to parse as a CID
if n.headset != nil {
// make sure we have the correct field suffix
span := spans[0].Start()
headSetSpans := make(core.Spans, len(spans))
copy(headSetSpans, spans)
span := headSetSpans[0].Start()
if !strings.HasSuffix(span.ToString(), n.field) {
spans[0] = core.NewSpan(span.WithFieldId(n.field), core.DataStoreKey{})
headSetSpans[0] = core.NewSpan(span.WithFieldId(n.field), core.DataStoreKey{})
}
n.headset.Spans(spans)
n.headset.Spans(headSetSpans)
} else {
data := spans[0].Start().ToString()
c, err := cid.Decode(data)
Expand Down
3 changes: 3 additions & 0 deletions query/graphql/planner/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ func (p *Planner) expandPlan(plan planNode, parentPlan *selectTopNode) error {
case *updateNode:
return p.expandPlan(n.results, parentPlan)

case *createNode:
return p.expandPlan(n.results, parentPlan)

default:
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,23 @@ func TestExplainMutationCreateSimple(t *testing.T) {
Results: []dataMap{
{
"explain": dataMap{
"selectTopNode": dataMap{
"selectNode": dataMap{
"createNode": dataMap{
"data": dataMap{
"age": float64(27),
"name": "John",
"points": float64(42.1),
"verified": true,
"createNode": dataMap{
"data": dataMap{
"age": float64(27),
"name": "John",
"points": 42.1,
"verified": true,
},
"selectTopNode": dataMap{
"selectNode": dataMap{
"filter": nil,
"scanNode": dataMap{
"collectionID": "1",
"collectionName": "user",
"filter": nil,
"spans": []map[string]interface{}{},
},
},
"filter": nil,
},
},
},
Expand Down Expand Up @@ -80,15 +86,21 @@ func TestExplainMutationCreateSimpleDoesNotCreateDocGivenDuplicate(t *testing.T)
Results: []dataMap{
{
"explain": dataMap{
"selectTopNode": dataMap{
"selectNode": dataMap{
"createNode": dataMap{
"data": dataMap{
"age": float64(27),
"name": "John",
"createNode": dataMap{
"data": dataMap{
"age": float64(27),
"name": "John",
},
"selectTopNode": dataMap{
"selectNode": dataMap{
"filter": nil,
"scanNode": dataMap{
"collectionID": "1",
"collectionName": "user",
"filter": nil,
"spans": []map[string]interface{}{},
},
},
"filter": nil,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/mutation/simple/create/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestMutationCreateSimple(t *testing.T) {
Results: []map[string]interface{}{
{
"_key": "bae-0a24cf29-b2c2-5861-9d00-abd6250c475d",
"age": int64(27),
"age": uint64(27),
"name": "John",
},
},
Expand Down
42 changes: 42 additions & 0 deletions tests/integration/mutation/simple/create/with_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022 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 create

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
simpleTests "github.com/sourcenetwork/defradb/tests/integration/mutation/simple"
)

func TestMutationCreateSimpleReturnVersionCID(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Simple create mutation",
Query: `mutation {
create_user(data: "{\"name\": \"John\",\"age\": 27,\"points\": 42.1,\"verified\": true}") {
_version {
cid
}
}
}`,
Results: []map[string]interface{}{
{
"_version": []map[string]interface{}{
{
"cid": "bafybeihsaeu7o2kep75fadotbqurrvqnamkjqr6cnpyvxxb3iolzxvzxve",
},
},
},
},
}

simpleTests.ExecuteTestCase(t, test)
}

0 comments on commit 2f2ebc8

Please sign in to comment.