MCPcopy
hub / github.com/dgraph-io/dgraph / verifyUnique

Function verifyUnique

edgraph/server.go:1776–1861  ·  view source on GitHub ↗

verifyUnique verifies uniqueness of mutation

(qc *queryContext, qr query.Request)

Source from the content-addressed store, hash-verified

1774
1775// verifyUnique verifies uniqueness of mutation
1776func verifyUnique(qc *queryContext, qr query.Request) error {
1777 if len(qc.uniqueVars) == 0 {
1778 return nil
1779 }
1780
1781 for i, queryVar := range qc.uniqueVars {
1782 gmuIndex, rdfIndex := decodeIndex(i)
1783 pred := qc.gmuList[gmuIndex].Set[rdfIndex]
1784 queryResult := qr.Vars[queryVar.queryVar]
1785 if !isUpsertCondTrue(qc, int(gmuIndex)) {
1786 continue
1787 }
1788 isEmpty := func(l *pb.List) bool {
1789 return l == nil || len(l.Uids) == 0
1790 }
1791
1792 var subjectUid uint64
1793 if strings.HasPrefix(pred.Subject, "uid(") {
1794 varName := qr.Vars[pred.Subject[4:len(pred.Subject)-1]]
1795 if isEmpty(varName.Uids) {
1796 subjectUid = 0 // blank node
1797 } else if len(varName.Uids.Uids) == 1 {
1798 subjectUid = varName.Uids.Uids[0]
1799 } else {
1800 return errors.Errorf("unique constraint violated for predicate [%v]", pred.Predicate)
1801 }
1802 } else {
1803 var err error
1804 subjectUid, err = parseSubject(pred.Subject)
1805 if err != nil {
1806 return errors.Wrapf(err, "error while parsing [%v]", pred.Subject)
1807 }
1808 }
1809
1810 var predValue interface{}
1811 if strings.HasPrefix(pred.ObjectId, "val(") {
1812 varName := qr.Vars[pred.ObjectId[4:len(pred.ObjectId)-1]]
1813 val, ok := varName.Vals.Get(0)
1814 if !ok {
1815 _, isValueGoingtoSet := varName.Vals.Get(subjectUid)
1816 if !isValueGoingtoSet {
1817 continue
1818 }
1819
1820 results := qr.Vars[queryVar.valVar]
1821 err := results.Vals.Iterate(func(uidOfv uint64, v types.Val) error {
1822 varNameVal, _ := varName.Vals.Get(subjectUid)
1823 if v.Value == varNameVal.Value && uidOfv != subjectUid {
1824 return errors.Errorf("could not insert duplicate value [%v] for predicate [%v]",
1825 v.Value, pred.Predicate)
1826 }
1827 return nil
1828 })
1829 if err != nil {
1830 return err
1831 }
1832 continue
1833 } else {

Callers 1

processQueryFunction · 0.85

Calls 9

TypeValFromFunction · 0.92
decodeIndexFunction · 0.85
isUpsertCondTrueFunction · 0.85
isEmptyFunction · 0.85
parseSubjectFunction · 0.85
isSwapFunction · 0.85
GetMethod · 0.65
ErrorfMethod · 0.45
IterateMethod · 0.45

Tested by

no test coverage detected