Esempio n. 1
0
func (this *builder) VisitKeyspaceTerm(node *algebra.KeyspaceTerm) (interface{}, error) {
	node.SetDefaultNamespace(this.namespace)
	keyspace, err := this.getTermKeyspace(node)
	if err != nil {
		return nil, err
	}

	if node.Keys() != nil {
		scan := NewKeyScan(node.Keys())
		this.children = append(this.children, scan)
	} else {
		if this.subquery {
			return nil, errors.NewError(nil, fmt.Sprintf(
				"FROM in subquery must use KEYS clause: FROM %s.",
				node.Keyspace()))
		}

		scan, err := this.selectScan(keyspace, node)
		if err != nil {
			return nil, err
		}

		this.children = append(this.children, scan)
	}

	fetch := NewFetch(keyspace, node)
	this.subChildren = append(this.subChildren, fetch)
	return nil, nil
}
Esempio n. 2
0
func (this *builder) getTermKeyspace(node *algebra.KeyspaceTerm) (datastore.Keyspace, error) {
	node.SetDefaultNamespace(this.namespace)
	ns := node.Namespace()

	datastore := this.datastore
	if strings.ToLower(ns) == "#system" {
		datastore = this.systemstore
	}

	namespace, err := datastore.NamespaceByName(ns)
	if err != nil {
		return nil, err
	}

	keyspace, err := namespace.KeyspaceByName(node.Keyspace())
	if err != nil {
		return nil, err
	}

	return keyspace, nil
}
Esempio n. 3
0
func (this *builder) selectScan(keyspace datastore.Keyspace,
	node *algebra.KeyspaceTerm) (Operator, error) {
	if this.where == nil {
		return this.selectPrimaryScan(keyspace, node)
	}

	nnf := planner.NewNNF()
	where := this.where.Copy()
	where, err := nnf.Map(where)
	if err != nil {
		return nil, err
	}

	formalizer := expression.NewFormalizer()
	formalizer.Keyspace = node.Alias()
	primaryKey := expression.NewField(
		expression.NewMeta(expression.NewConstant(node.Alias())),
		expression.NewFieldName("id"))

	indexers, err := keyspace.Indexers()
	if err != nil {
		return nil, err
	}

	indexes := make([]datastore.Index, 0, len(indexers)*16)
	primaryIndexes := make(map[datastore.Index]bool, len(indexers)*2)

	for _, indexer := range indexers {
		idxs, err := indexer.Indexes()
		if err != nil {
			return nil, err
		}

		indexes = append(indexes, idxs...)

		primaryIdxs, err := indexer.PrimaryIndexes()
		if err != nil {
			return nil, err
		}

		for _, p := range primaryIdxs {
			primaryIndexes[p] = true
		}
	}

	unfiltered := make(map[datastore.Index]expression.Expression, len(indexes))
	filtered := make(map[datastore.Index]expression.Expression, len(indexes))

	for _, index := range indexes {
		state, _, er := index.State()
		if er != nil {
			return nil, er
		}

		if state != datastore.ONLINE {
			continue
		}

		var key expression.Expression

		if primaryIndexes[index] {
			key = primaryKey
		} else {
			rangeKey := index.RangeKey()
			if len(rangeKey) == 0 || rangeKey[0] == nil {
				// Index not rangeable
				continue
			}

			key := rangeKey[0].Copy()

			key, err = formalizer.Map(key)
			if err != nil {
				return nil, err
			}

			key, err = nnf.Map(key)
			if err != nil {
				return nil, err
			}
		}

		if !planner.SargableFor(where, key) {
			// Index not applicable
			continue
		}

		indexCond := index.Condition()
		if indexCond == nil {
			unfiltered[index] = key
			continue
		}

		indexCond = indexCond.Copy()

		indexCond, err = formalizer.Map(indexCond)
		if err != nil {
			return nil, err
		}

		indexCond, err = nnf.Map(indexCond)
		if err != nil {
			return nil, err
		}

		if planner.SubsetOf(where, indexCond) {
			// Index condition satisfies query condition
			filtered[index] = key
			break
		}
	}

	var indexMap map[datastore.Index]expression.Expression
	if len(filtered) > 0 {
		indexMap = filtered
	} else if len(unfiltered) > 0 {
		indexMap = unfiltered
	}

	for index, key := range indexMap {
		spans := planner.SargFor(where, key)
		var scan Operator
		scan = NewIndexScan(index, node, spans, false, math.MaxInt64)
		if len(spans) > 1 {
			// Use UnionScan to de-dup multiple spans
			scan = NewUnionScan(scan)
		}

		return scan, err
	}

	return this.selectPrimaryScan(keyspace, node)
}