Example #1
0
// getRoot gets the root graph query object after parsing the args.
func getRoot(l *lex.Lexer) (gq *GraphQuery, rerr error) {
	gq = &GraphQuery{
		Args: make(map[string]string),
	}
	item := <-l.Items
	if item.Typ != itemName {
		return nil, x.Errorf("Expected some name. Got: %v", item)
	}

	gq.Alias = item.Val
	item = <-l.Items
	if item.Typ != itemLeftRound {
		return nil, x.Errorf("Expected variable start. Got: %v", item)
	}

	item = <-l.Items
	if item.Typ == itemGenerator {
		// Store the generator function.
		gen, err := parseFunction(l)
		if !schema.IsIndexed(gen.Attr) {
			return nil, x.Errorf(
				"Field %s is not indexed and cannot be used in functions",
				gen.Attr)
		}
		if err != nil {
			return nil, err
		}
		gq.Func = gen
	} else if item.Typ == itemArgument {
		args, err := parseArguments(l)
		if err != nil {
			return nil, err
		}
		for _, p := range args {
			if p.Key == "_uid_" {
				gq.UID, rerr = strconv.ParseUint(p.Val, 0, 64)
				if rerr != nil {
					return nil, rerr
				}
			} else if p.Key == "_xid_" {
				gq.XID = p.Val
			} else {
				return nil, x.Errorf("Expecting _uid_ or _xid_. Got: %+v", p)
			}
		}
	} else {
		return nil, x.Errorf("Unexpected root argument.")
	}

	return gq, nil
}
Example #2
0
// AddMutationWithIndex is AddMutation with support for indexing.
func (l *List) AddMutationWithIndex(ctx context.Context, t *task.DirectedEdge, op uint32) error {
	x.AssertTruef(len(t.Attr) > 0,
		"[%s] [%d] [%v] %d %d\n", t.Attr, t.Entity, t.Value, t.ValueId, op)

	var vbytes []byte
	var vtype byte
	var verr error

	doUpdateIndex := pstore != nil && (t.Value != nil) &&
		schema.IsIndexed(t.Attr)
	if doUpdateIndex {
		// Check last posting for original value BEFORE any mutation actually happens.
		vbytes, vtype, verr = l.Value()
	}
	hasMutated, err := l.AddMutation(ctx, t, op)
	if err != nil {
		return err
	}
	if !hasMutated || !doUpdateIndex {
		return nil
	}

	// Exact matches.
	if verr == nil && len(vbytes) > 0 {
		delTerm := vbytes
		delType := vtype
		p := types.ValueForType(types.TypeID(delType))
		if err := p.UnmarshalBinary(delTerm); err != nil {
			return err
		}
		addIndexMutations(ctx, t.Attr, t.Entity, p, true)
	}
	if op == Set {
		p := types.ValueForType(types.TypeID(t.ValueType))
		if err := p.UnmarshalBinary(t.Value); err != nil {
			return err
		}
		addIndexMutations(ctx, t.Attr, t.Entity, p, false)
	}
	return nil
}