populateFacetVars walks the facetsMatrix to compute the value of a facet variable. It sums up the value for float/int type facets so that there is only variable corresponding to each uid in the uidMatrix.
(doneVars map[string]varValue, sgPath []*SubGraph)
| 1670 | // It sums up the value for float/int type facets so that there is only variable corresponding |
| 1671 | // to each uid in the uidMatrix. |
| 1672 | func (sg *SubGraph) populateFacetVars(doneVars map[string]varValue, sgPath []*SubGraph) error { |
| 1673 | if len(sg.Params.FacetVar) == 0 || sg.Params.Facet == nil { |
| 1674 | return nil |
| 1675 | } |
| 1676 | |
| 1677 | sgPath = append(sgPath, sg) |
| 1678 | for _, it := range sg.Params.Facet.Param { |
| 1679 | fvar, ok := sg.Params.FacetVar[it.Key] |
| 1680 | if !ok { |
| 1681 | continue |
| 1682 | } |
| 1683 | // Assign an empty value for every facet that was assigned to a variable and hence is part |
| 1684 | // of FacetVar. |
| 1685 | doneVars[fvar] = varValue{ |
| 1686 | Vals: types.NewShardedMap(), |
| 1687 | path: sgPath, |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | if len(sg.facetsMatrix) == 0 { |
| 1692 | return nil |
| 1693 | } |
| 1694 | |
| 1695 | // Note: We ignore the facets if its a value edge as we can't |
| 1696 | // attach the value to any node. |
| 1697 | for i, uids := range sg.uidMatrix { |
| 1698 | for j, uid := range uids.Uids { |
| 1699 | facet := sg.facetsMatrix[i].FacetsList[j] |
| 1700 | for _, f := range facet.Facets { |
| 1701 | fvar, ok := sg.Params.FacetVar[f.Key] |
| 1702 | if !ok { |
| 1703 | continue |
| 1704 | } |
| 1705 | if pVal, ok := doneVars[fvar].Vals.Get(uid); !ok { |
| 1706 | fVal, err := facets.ValFor(f) |
| 1707 | if err != nil { |
| 1708 | return err |
| 1709 | } |
| 1710 | |
| 1711 | doneVars[fvar].Vals.Set(uid, fVal) |
| 1712 | } else { |
| 1713 | // If the value is int/float we add them up. Else we throw an error as |
| 1714 | // many to one maps are not allowed for other types. |
| 1715 | nVal, err := facets.ValFor(f) |
| 1716 | if err != nil { |
| 1717 | return err |
| 1718 | } |
| 1719 | |
| 1720 | if nVal.Tid != types.IntID && nVal.Tid != types.FloatID { |
| 1721 | return errors.Errorf("Repeated id with non int/float value for " + |
| 1722 | "facet var encountered.") |
| 1723 | } |
| 1724 | ag := aggregator{name: "sum"} |
| 1725 | if err := ag.Apply(pVal); err != nil { |
| 1726 | return err |
| 1727 | } |
| 1728 | if err := ag.Apply(nVal); err != nil { |
| 1729 | return err |
no test coverage detected