MCPcopy Index your code
hub / github.com/dgraph-io/dgraph / sortAndPaginateUsingFacet

Method sortAndPaginateUsingFacet

query/query.go:2611–2695  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

2609}
2610
2611func (sg *SubGraph) sortAndPaginateUsingFacet(ctx context.Context) error {
2612 if len(sg.facetsMatrix) == 0 {
2613 return nil
2614 }
2615 if len(sg.facetsMatrix) != len(sg.uidMatrix) {
2616 return errors.Errorf("Facet matrix and UID matrix mismatch: %d vs %d",
2617 len(sg.facetsMatrix), len(sg.uidMatrix))
2618 }
2619
2620 orderbyKeys := make(map[string]int)
2621 var orderDesc []bool
2622 for i, order := range sg.Params.FacetsOrder {
2623 orderbyKeys[order.Key] = i
2624 orderDesc = append(orderDesc, order.Desc)
2625 }
2626
2627 for i := 0; i < len(sg.uidMatrix); i++ {
2628 ul := sg.uidMatrix[i]
2629 fl := sg.facetsMatrix[i]
2630 uids := ul.Uids[:0]
2631 facetList := fl.FacetsList[:0]
2632
2633 values := make([][]types.Val, len(ul.Uids))
2634 for i := 0; i < len(values); i++ {
2635 values[i] = make([]types.Val, len(sg.Params.FacetsOrder))
2636 }
2637
2638 for j := range ul.Uids {
2639 uid := ul.Uids[j]
2640 f := fl.FacetsList[j]
2641 uids = append(uids, uid)
2642 facetList = append(facetList, f)
2643
2644 // Since any facet can come only once in f.Facets, we can have counter to check if we
2645 // have populated all facets or not. Once we are done populating all facets
2646 // we can break out of below loop.
2647 remainingFacets := len(orderbyKeys)
2648 // TODO: We are searching sequentially, explore if binary search is useful here.
2649 for _, it := range f.Facets {
2650 idx, ok := orderbyKeys[it.Key]
2651 if !ok {
2652 continue
2653 }
2654
2655 fVal, err := facets.ValFor(it)
2656 if err != nil {
2657 return err
2658 }
2659 // If type is not sortable, we are ignoring it.
2660 if types.IsSortable(fVal.Tid) {
2661 values[j][idx] = fVal
2662 }
2663
2664 remainingFacets--
2665 if remainingFacets == 0 {
2666 break
2667 }
2668 }

Callers 1

Calls 6

updateDestUidsMethod · 0.95
ValForFunction · 0.92
IsSortableFunction · 0.92
SortWithFacetFunction · 0.92
PageRangeFunction · 0.92
ErrorfMethod · 0.45

Tested by

no test coverage detected