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

[BREAKING] Support for fetching facets from value edge list #4267

Merged
merged 21 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
37 changes: 33 additions & 4 deletions posting/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/dgryski/go-farm"

bpb "github.com/dgraph-io/badger/v2/pb"
"github.com/dgraph-io/dgo/v2/protos/api"
"github.com/dgraph-io/dgraph/algo"
"github.com/dgraph-io/dgraph/codec"
"github.com/dgraph-io/dgraph/dgraph/cmd/zero"
Expand Down Expand Up @@ -938,6 +937,21 @@ func (l *List) AllUntaggedValues(readTs uint64) ([]types.Val, error) {
return vals, err
}

// allUntaggedFacets returns facets for all untagged values. Since works well only for
// fetching facets for list predicates as lang tag in not allowed for list predicates.
func (l *List) allUntaggedFacets(readTs uint64) ([]*pb.Facets, error) {
l.AssertRLock()
var facets []*pb.Facets
err := l.iterate(readTs, 0, func(p *pb.Posting) error {
if len(p.LangTag) == 0 {
facets = append(facets, &pb.Facets{Facets: p.Facets})
}
return nil
})

return facets, err
}

// AllValues returns all the values in the posting list.
func (l *List) AllValues(readTs uint64) ([]types.Val, error) {
l.RLock()
Expand Down Expand Up @@ -1117,15 +1131,30 @@ func (l *List) findPosting(readTs uint64, uid uint64) (found bool, pos *pb.Posti
}

// Facets gives facets for the posting representing value.
func (l *List) Facets(readTs uint64, param *pb.FacetParams, langs []string) (fs []*api.Facet,
ferr error) {
func (l *List) Facets(readTs uint64, param *pb.FacetParams, langs []string,
listType bool) ([]*pb.Facets, error) {

l.RLock()
defer l.RUnlock()

var fcs []*pb.Facets
if listType {
fs, err := l.allUntaggedFacets(readTs)
if err != nil {
return nil, err
}

for _, fcts := range fs {
fcs = append(fcs, &pb.Facets{Facets: facets.CopyFacets(fcts.Facets, param)})
}
return fcs, nil
}
p, err := l.postingFor(readTs, langs)
if err != nil {
return nil, err
}
return facets.CopyFacets(p.Facets, param), nil
fcs = append(fcs, &pb.Facets{Facets: facets.CopyFacets(p.Facets, param)})
return fcs, nil
}

func (l *List) readListPart(startUid uint64) (*pb.PostingList, error) {
Expand Down
6 changes: 6 additions & 0 deletions query/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ type Node {
name
}

type Speaker {
name
language
}

name : string @index(term, exact, trigram) @count @lang .
name_lang : string @lang .
lang_type : string @index(exact) .
Expand Down Expand Up @@ -283,6 +288,7 @@ boss : uid .
newfriend : [uid] .
owner : [uid] .
noconflict_pred : string @noconflict .
language : [string] .
`

func populateCluster() {
Expand Down
62 changes: 44 additions & 18 deletions query/outputnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -195,6 +196,28 @@ func (fj *fastJsonNode) writeKey(out *bytes.Buffer) error {
return nil
}

func (fj *fastJsonNode) attachFacets(fieldName string, isList bool,
fList []*api.Facet, facetIdx int) error {

for _, f := range fList {
fName := facetName(fieldName, f)
fVal, err := facets.ValFor(f)
if err != nil {
return err
}

if !isList {
fj.AddValue(fName, fVal)
} else {
facetNode := &fastJsonNode{attr: fName}
facetNode.AddValue(strconv.Itoa(facetIdx), fVal)
fj.AddMapChild(fName, facetNode, false)
}
}

return nil
}

func (fj *fastJsonNode) encode(out *bytes.Buffer) error {
// set relative ordering
for i, a := range fj.attrs {
Expand Down Expand Up @@ -722,6 +745,9 @@ func (sg *SubGraph) preTraverse(uid uint64, dst *fastJsonNode) error {
// We create as many predicate entity children as the length of uids for
// this predicate.
ul := pc.uidMatrix[idx]
// noneEmptyUID will store indexes of non empty UIDs.
// This will be used for indexing facet response
var nonEmptyUID []int
for childIdx, childUID := range ul.Uids {
if fieldName == "" || (invalidUids != nil && invalidUids[childUID]) {
continue
Expand All @@ -741,22 +767,12 @@ func (sg *SubGraph) preTraverse(uid uint64, dst *fastJsonNode) error {
return rerr
}

if pc.Params.Facet != nil && len(fcsList) > childIdx {
fs := fcsList[childIdx]
for _, f := range fs.Facets {
fVal, err := facets.ValFor(f)
if err != nil {
return err
}

uc.AddValue(facetName(fieldName, f), fVal)
}
}

if !uc.IsEmpty() {
if sg.Params.GetUid {
uc.SetUID(childUID, "uid")
}
nonEmptyUID = append(nonEmptyUID, childIdx) // append index to nonEmptyUID.

if pc.Params.Normalize {
// We will normalize at each level instead of
// calling normalize after pretraverse.
Expand Down Expand Up @@ -807,6 +823,19 @@ func (sg *SubGraph) preTraverse(uid uint64, dst *fastJsonNode) error {
}
}

// Now fill facets for non empty UIDs.
facetIdx := 0
for _, uidIdx := range nonEmptyUID {
if pc.Params.Facet != nil && len(fcsList) > uidIdx {
fs := fcsList[uidIdx]
err := dst.attachFacets(fieldName, pc.List, fs.Facets, facetIdx)
if err != nil {
return err
}
facetIdx++
}
}

// add value for count(uid) nodes if any.
_ = handleCountUIDNodes(pc, dst, len(ul.Uids))
default:
Expand All @@ -821,14 +850,11 @@ func (sg *SubGraph) preTraverse(uid uint64, dst *fastJsonNode) error {
}

if len(pc.facetsMatrix) > idx && len(pc.facetsMatrix[idx].FacetsList) > 0 {
// in case of Value we have only one Facets
for _, f := range pc.facetsMatrix[idx].FacetsList[0].Facets {
fVal, err := facets.ValFor(f)
if err != nil {
// In case of Value we have only one Facets.
for i, fcts := range pc.facetsMatrix[idx].FacetsList {
if err := dst.attachFacets(fieldName, pc.List, fcts.Facets, i); err != nil {
return err
}

dst.AddValue(facetName(fieldName, f), fVal)
}
}

Expand Down
Loading