Ejemplo n.º 1
0
		ValueArray Alias::selfTemplateArgs() const {
			ValueArray templateArgs;
			templateArgs.reserve(templateVariables().size());
			
			for (const auto templateVar: templateVariables()) {
				// Refer to the template variables of this type alias.
				templateArgs.push_back(templateVar->selfRefValue());
			}
			
			return templateArgs;
		}
Ejemplo n.º 2
0
			void add(std::shared_ptr<ValueImpl>& value) override
			{
				values.push_back(value);
			}
Ejemplo n.º 3
0
		Value
		Value::substitute(const TemplateVarMap& templateVarMap,
		                  const Predicate& selfconst) const {
			switch (kind()) {
				case CONSTANT:
					return copy();
				case ALIAS: {
					ValueArray arguments;
					arguments.reserve(aliasTemplateArguments().size());
					for (const auto& argument: aliasTemplateArguments()) {
						arguments.push_back(argument.substitute(templateVarMap,
						                                        selfconst));
					}
					return Value::Alias(alias(), std::move(arguments),
					                    type()->substitute(templateVarMap,
							                       selfconst));
				}
				case TERNARY: {
					return Value::Ternary(ternaryCondition().substitute(templateVarMap,
					                                                    selfconst),
					                      ternaryIfTrue().substitute(templateVarMap,
							                                 selfconst),
					                      ternaryIfFalse().substitute(templateVarMap,
							                                  selfconst));
				}
				case TYPEREF:
					return Value::TypeRef(typeRefType()->substitute(templateVarMap,
					                                                selfconst),
					                      type()->substitute(templateVarMap,
							                         selfconst));
				case TEMPLATEVARREF: {
					const auto iterator = templateVarMap.find(templateVar());
					if (iterator != templateVarMap.end()) {
						return iterator->second.copy();
					} else {
						return copy();
					}
				}
				case CALL: {
					auto value = callValue().substitute(templateVarMap,
					                                    selfconst);
					ValueArray parameters;
					parameters.reserve(callParameters().size());
					
					for (const auto& parameter: callParameters()) {
						parameters.push_back(parameter.substitute(templateVarMap,
						                                          selfconst));
					}
					
					return Call(std::move(value), std::move(parameters),
					            type()->substitute(templateVarMap, selfconst));
				}
				case FUNCTIONREF: {
					ValueArray templateArguments;
					templateArguments.reserve(functionRefTemplateArguments().size());
					
					for (const auto& templateArgument: functionRefTemplateArguments()) {
						templateArguments.push_back(templateArgument.substitute(templateVarMap,
						                                                        selfconst));
					}
					
					return FunctionRef(functionRefParentType()->substitute(templateVarMap,
					                                                       selfconst),
					                   functionRefFunction(),
					                   std::move(templateArguments),
					                   type()->substitute(templateVarMap,
							                      selfconst));
				}
				case CAPABILITYTEST: {
					return CapabilityTest(capabilityTestCheckType()->substitute(templateVarMap,
					                                                            selfconst),
					                      capabilityTestCapabilityType()->substitute(templateVarMap,
							                                                 selfconst),
					                      type());
				}
				case PREDICATE: {
					return PredicateExpr(predicate().substitute(templateVarMap,
					                                            selfconst), type());
				}
				default:
					locic_unreachable("Invalid value kind for substitute().");
			}
		}
Ejemplo n.º 4
0
	int Ardb::Sort(const DBID& db, const Slice& key, const StringArray& args,
	        ValueArray& values)
	{
		SortOptions options;
		if (parse_sort_options(options, args) < 0)
		{
			DEBUG_LOG("Failed to parse sort options.");
			return ERR_INVALID_ARGS;
		}
		int type = Type(db, key);
		ValueArray sortvals;
		switch (type)
		{
			case LIST_META:
			{
				LRange(db, key, 0, -1, sortvals);
				break;
			}
			case SET_ELEMENT:
			{
				SMembers(db, key, sortvals);
				break;
			}
			case ZSET_ELEMENT_SCORE:
			{
				QueryOptions tmp;
				ZRange(db, key, 0, -1, sortvals, tmp);
				if(NULL == options.by)
				{
					options.nosort = true;
				}
				break;
			}
			default:
			{
				return ERR_INVALID_TYPE;
			}
		}

		if (sortvals.empty())
		{
			return 0;
		}
		if (options.with_limit)
		{
			if (options.limit_offset < 0)
			{
				options.limit_offset = 0;
			}
			if ((uint32) options.limit_offset > sortvals.size())
			{
				values.clear();
				return 0;
			}
			if (options.limit_count < 0)
			{
				options.limit_count = sortvals.size();
			}
		}

		std::vector<SortValue> sortvec;
		if (!options.nosort)
		{
			if (NULL != options.by)
			{
				sortvec.reserve(sortvals.size());
			}
			for (uint32 i = 0; i < sortvals.size(); i++)
			{
				if (NULL != options.by)
				{
					sortvec.push_back(SortValue(&sortvals[i]));
					if (GetValueByPattern(db, options.by, sortvals[i],
					        sortvec[i].cmp) < 0)
					{
						DEBUG_LOG("Failed to get value by pattern:%s", options.by);
						sortvec[i].cmp.Clear();
						continue;
					}
				}
				if (options.with_alpha)
				{
					if (NULL != options.by)
					{
						value_convert_to_raw(sortvec[i].cmp);
					}
					else
					{
						value_convert_to_raw(sortvals[i]);
					}
				}
				else
				{
					if (NULL != options.by)
					{
						value_convert_to_number(sortvec[i].cmp);
					}
					else
					{
						value_convert_to_number(sortvals[i]);
					}
				}
			}
			if (NULL != options.by)
			{
				if (!options.is_desc)
				{
					std::sort(sortvec.begin(), sortvec.end(),
					        less_value<SortValue>);
				}
				else
				{
					std::sort(sortvec.begin(), sortvec.end(),
					        greater_value<SortValue>);
				}

			}
			else
			{
				if (!options.is_desc)
				{
					std::sort(sortvals.begin(), sortvals.end(),
					        less_value<ValueObject>);
				}
				else
				{
					std::sort(sortvals.begin(), sortvals.end(),
					        greater_value<ValueObject>);
				}
			}
		}

		if (!options.with_limit)
		{
			options.limit_offset = 0;
			options.limit_count = sortvals.size();
		}

		uint32 count = 0;
		for (uint32 i = options.limit_offset;
		        i < sortvals.size() && count < (uint32) options.limit_count;
		        i++, count++)
		{
			ValueObject* patternObj = NULL;
			if (NULL != options.by)
			{
				patternObj = sortvec[i].value;
			}
			else
			{
				patternObj = &(sortvals[i]);
			}
			if (options.get_patterns.empty())
			{
				values.push_back(*patternObj);
			}
			else
			{
				for (uint32 j = 0; j < options.get_patterns.size(); j++)
				{
					ValueObject vo;
					if (GetValueByPattern(db, options.get_patterns[j],
					        *patternObj, vo) < 0)
					{
						DEBUG_LOG("Failed to get value by pattern for:%s", options.get_patterns[j]);
						vo.Clear();
					}
					values.push_back(vo);
				}
			}
		}

		if (options.store_dst != NULL && !values.empty())
		{
			BatchWriteGuard guard(GetEngine());
			LClear(db, options.store_dst);
			ValueArray::iterator it = values.begin();
			uint64 score = 0;

			while (it != values.end())
			{
				if (it->type != EMPTY)
				{
					ListKeyObject lk(options.store_dst, score, db);
					SetValue(lk, *it);
					score++;
				}
				it++;
			}
			ListMetaValue meta;
			meta.min_score = 0;
			meta.max_score = (score - 1);
			meta.size = score;
			SetListMetaValue(db, options.store_dst, meta);
		}
		return 0;
	}