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

Function addQueryIfUnique

edgraph/server.go:1864–2016  ·  view source on GitHub ↗

addQueryIfUnique adds dummy queries in the request for checking whether predicate is unique in the db

(qctx context.Context, qc *queryContext)

Source from the content-addressed store, hash-verified

1862
1863// addQueryIfUnique adds dummy queries in the request for checking whether predicate is unique in the db
1864func addQueryIfUnique(qctx context.Context, qc *queryContext) error {
1865 if len(qc.gmuList) == 0 {
1866 return nil
1867 }
1868
1869 ctx := context.WithValue(qctx, schema.IsWrite, false)
1870 namespace, err := x.ExtractNamespace(ctx)
1871 if err != nil {
1872 // It's okay to ignore this here. If namespace is not set, it could mean either there is no
1873 // authorization or it's trying to be bypassed. So the namespace is either 0 or the mutation would fail.
1874 glog.Errorf("Error while extracting namespace, assuming default %s", err)
1875 namespace = 0
1876 }
1877 isGalaxyQuery := x.IsRootNsOperation(ctx)
1878
1879 missingPreds := make(map[string]struct{})
1880 for _, gmu := range qc.gmuList {
1881 for _, pred := range gmu.Set {
1882 currNs := namespace
1883 if isGalaxyQuery {
1884 currNs = pred.Namespace
1885 }
1886 if pred.Predicate == "dgraph.xid" {
1887 continue
1888 }
1889 fullPred := x.NamespaceAttr(currNs, pred.Predicate)
1890 if _, ok := schema.State().Get(ctx, fullPred); !ok {
1891 missingPreds[fullPred] = struct{}{}
1892 }
1893 }
1894 }
1895
1896 repaired := make(map[string]bool)
1897 if len(missingPreds) > 0 {
1898 predList := make([]string, 0, len(missingPreds))
1899 for p := range missingPreds {
1900 predList = append(predList, p)
1901 }
1902
1903 schReq := &pb.SchemaRequest{Predicates: predList}
1904 remoteNodes, err := worker.GetSchemaOverNetwork(ctx, schReq)
1905 if err != nil {
1906 return errors.Wrapf(err, "unique validation failed to fetch schema for predicates %v", predList)
1907 }
1908
1909 for _, node := range remoteNodes {
1910 repaired[node.Predicate] = node.Unique
1911 }
1912 }
1913
1914 qc.uniqueVars = map[uint64]uniquePredMeta{}
1915 for gmuIndex, gmu := range qc.gmuList {
1916 var buildQuery strings.Builder
1917 for rdfIndex, pred := range gmu.Set {
1918 if isGalaxyQuery {
1919 // The caller should make sure that the directed edges contain the namespace we want
1920 // to insert into.
1921 namespace = pred.Namespace

Callers 1

parseRequestFunction · 0.85

Calls 13

ExtractNamespaceFunction · 0.92
IsRootNsOperationFunction · 0.92
NamespaceAttrFunction · 0.92
StateFunction · 0.92
GetSchemaOverNetworkFunction · 0.92
TypeValFromFunction · 0.92
validateLangTagFunction · 0.85
encodeIndexFunction · 0.85
validateValObjectIdFunction · 0.85
GetMethod · 0.65
LenMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected