Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Chunker): don't delete node with empty facet in mutation #7737

Merged
merged 3 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions chunker/json_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ type mapResponse struct {
uid string // uid retrieved or allocated for the node.
namespace uint64 // namespace to which the node belongs.
fcts []*api.Facet // facets on the edge connecting this node to the source if any.
rawFacets map[string]interface{}
}

func handleBasicType(k string, v interface{}, op int, nq *api.NQuad) error {
Expand Down Expand Up @@ -264,7 +265,7 @@ func handleBasicType(k string, v interface{}, op int, nq *api.NQuad) error {

func (buf *NQuadBuffer) checkForDeletion(mr mapResponse, m map[string]interface{}, op int) {
// Since uid is the only key, this must be S * * deletion.
if op == DeleteNquads && len(mr.uid) > 0 && len(m) == 1 {
if op == DeleteNquads && len(mr.uid) > 0 && len(m) == 1 && len(mr.rawFacets) == 0 {
buf.Push(&api.NQuad{
Subject: mr.uid,
Predicate: x.Star,
Expand Down Expand Up @@ -397,11 +398,11 @@ func (buf *NQuadBuffer) mapToNquads(m map[string]interface{}, op int, parentPred
mapResponse, error) {
var mr mapResponse

// move all facets from global map to smaller mf map
mf := make(map[string]interface{})
// move all facets from global map to smaller mr.rawFacets map
mr.rawFacets = make(map[string]interface{})
for k, v := range m {
if strings.Contains(k, x.FacetDelimeter) {
mf[k] = v
mr.rawFacets[k] = v
delete(m, k)
}
}
Expand Down Expand Up @@ -512,7 +513,7 @@ func (buf *NQuadBuffer) mapToNquads(m map[string]interface{}, op int, parentPred

prefix := pred + x.FacetDelimeter
if _, ok := v.([]interface{}); !ok {
fts, err := parseScalarFacets(mf, prefix)
fts, err := parseScalarFacets(mr.rawFacets, prefix)
if err != nil {
return mr, err
}
Expand Down Expand Up @@ -569,14 +570,16 @@ func (buf *NQuadBuffer) mapToNquads(m map[string]interface{}, op int, parentPred
var facetsMapSlice []map[int]*api.Facet
for idx, item := range v {
if idx == 0 {
// determine if this is a scalar list
switch item.(type) {
case string, float64, json.Number, int64:
var err error
facetsMapSlice, err = parseMapFacets(mf, prefix)
facetsMapSlice, err = parseMapFacets(mr.rawFacets, prefix)
if err != nil {
return mr, err
}
default:
// not a scalar list, continue
}
}

Expand Down Expand Up @@ -648,7 +651,7 @@ func (buf *NQuadBuffer) mapToNquads(m map[string]interface{}, op int, parentPred
}
}

fts, err := parseScalarFacets(mf, parentPred+x.FacetDelimeter)
fts, err := parseScalarFacets(mr.rawFacets, parentPred+x.FacetDelimeter)
mr.fcts = fts

return mr, err
Expand Down
18 changes: 18 additions & 0 deletions chunker/json_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,24 @@ func TestSetNquadNilValue(t *testing.T) {
require.Equal(t, 0, len(fastNQ))
}

func TestNquadsFromJsonEmptyFacet(t *testing.T) {
json := `{"uid":1000,"doesnt|exist":null}`

// fast
buf := NewNQuadBuffer(-1)
require.Nil(t, buf.FastParseJSON([]byte(json), DeleteNquads))
buf.Flush()
// needs to be empty, otherwise node gets deleted
require.Equal(t, 0, len(<-buf.Ch()))

// old
buf = NewNQuadBuffer(-1)
require.Nil(t, buf.ParseJSON([]byte(json), DeleteNquads))
buf.Flush()
// needs to be empty, otherwise node gets deleted
require.Equal(t, 0, len(<-buf.Ch()))
}

func BenchmarkNoFacets(b *testing.B) {
json := []byte(`[
{
Expand Down