@VisibleForTesting static Document buildQuery(TupleDomain<ColumnHandle> tupleDomain) { Document query = new Document(); if (tupleDomain.getDomains().isPresent()) { for (Map.Entry<ColumnHandle, Domain> entry : tupleDomain.getDomains().get().entrySet()) { MongoColumnHandle column = (MongoColumnHandle) entry.getKey(); query.putAll(buildPredicate(column, entry.getValue())); } } return query; }
@Override public Query addCriteria(CriteriaDefinition criteria) { this.queryObject.putAll(criteria.getCriteriaObject()); return this; }
private static void addAllToMap(Bson bson, Map<String, ?> value) { if (bson instanceof Document) { ((Document) bson).putAll(value); return; } if (bson instanceof DBObject) { ((DBObject) bson).putAll(value); return; } throw new IllegalArgumentException( String.format("Cannot add all to %s. Given Bson must be a Document or DBObject.", bson.getClass())); }
@Override public Document toDocumentExpression(AggregationOperationContext context) { Document document = new Document(); for (ReplacementContributor replacement : replacements) { document.putAll(replacement.toDocument(context)); } return document; }
@Override public Document toDocument(AggregationOperationContext context) { Document fieldObject = new Document(); for (Projection projection : projections) { fieldObject.putAll(projection.toDocument(context)); } return new Document("$project", fieldObject); }
private Document mergeConjunctions(List<Document> conjunctions) { final Multimap<String, Document> merged = LinkedHashMultimap.create(); for (Document doc : conjunctions) { Preconditions.checkState(doc.keySet().size() == 1, "Invalid constraint %s", doc); final String key = doc.keySet().iterator().next(); merged.put(key, doc); } final Document result = new Document(); for (Map.Entry<String, Collection<Document>> entry : merged.asMap().entrySet()) { Preconditions.checkState(!entry.getValue().isEmpty(), "empty constraint: %s", entry); if (entry.getValue().size() == 1) { result.putAll(entry.getValue().iterator().next()); } else { result.putAll(new Document(QueryOperators.AND, entry.getValue())); } } return result; }
/** * @return the query {@link Document}. */ public Document getQueryObject() { Document document = new Document(); for (CriteriaDefinition definition : criteria.values()) { document.putAll(definition.getCriteriaObject()); } if (!restrictedTypes.isEmpty()) { document.put(RESTRICTED_TYPES_KEY, getRestrictedTypes()); } return document; }
@Override public Document toDocument(AggregationOperationContext context) { Document options = new Document(); options.put("buckets", buckets); if (granularity != null) { options.put("granularity", granularity); } options.putAll(super.toDocument(context)); return new Document("$bucketAuto", options); }
/** * Create the JSON schema complying {@link Document} representation. This includes {@literal type}, * {@literal description} and the fields of {@link Restrictions#toDocument()} if set. */ @Override public Document toDocument() { Document document = new Document(); getOrCreateDescription().ifPresent(val -> document.append("description", val)); document.putAll(restrictions.toDocument()); return document; }
private Document toFilter(ExposedFields exposedFields, AggregationOperationContext context) { Document filterExpression = new Document(); InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext( exposedFields, context); filterExpression.putAll(context.getMappedObject(new Document("input", getMappedInput(context)))); filterExpression.put("as", as.getTarget()); filterExpression.putAll(context.getMappedObject(new Document("cond", getMappedCondition(operationContext)))); return new Document("$filter", filterExpression); }
@Override public Document getSortObject() { Document sort = new Document(); if (this.sortByScore) { sort.put(getScoreFieldName(), META_TEXT_SCORE); } sort.putAll(super.getSortObject()); return sort; } }
private Entry<String, Object> getMappedUpdateModifier(Field field, Object rawValue) { Object value = null; if (rawValue instanceof Modifier) { value = getMappedValue(field, (Modifier) rawValue); } else if (rawValue instanceof Modifiers) { Document modificationOperations = new Document(); for (Modifier modifier : ((Modifiers) rawValue).getModifiers()) { modificationOperations.putAll(getMappedValue(field, modifier)); } value = modificationOperations; } else { throw new IllegalArgumentException(String.format("Unable to map value of type '%s'!", rawValue.getClass())); } return createMapEntry(field, value); }
private Document toLet(ExposedFields exposedFields, AggregationOperationContext context) { Document letExpression = new Document(); Document mappedVars = new Document(); InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext( exposedFields, context); for (ExpressionVariable var : this.vars) { mappedVars.putAll(getMappedVariable(var, context)); } letExpression.put("vars", mappedVars); letExpression.put("in", getMappedIn(operationContext)); return new Document("$let", letExpression); }
@Override public Document toDocument(AggregationOperationContext context) { Document options = new Document(); options.put("boundaries", context.getMappedObject(new Document("$set", boundaries)).get("$set")); if (defaultBucket != null) { options.put("default", context.getMappedObject(new Document("$set", defaultBucket)).get("$set")); } options.putAll(super.toDocument(context)); return new Document("$bucket", options); }
@Override public Document toDocument(AggregationOperationContext context) { Document document = new Document(); document.put("input", getMappedValue(input, context)); document.put("initialValue", getMappedValue(initialValue, context)); if (reduceExpressions.iterator().next() instanceof PropertyExpression) { Document properties = new Document(); for (AggregationExpression e : reduceExpressions) { properties.putAll(e.toDocument(context)); } document.put("in", properties); } else { document.put("in", (reduceExpressions.iterator().next()).toDocument(context)); } return new Document("$reduce", document); }
private Document updateTypeRestrictions(Document query, Example example) { Document result = new Document(); if (isTypeRestricting(example)) { result.putAll(query); this.converter.getTypeMapper().writeTypeRestrictions(result, getTypesToMatch(example)); return result; } for (Map.Entry<String, Object> entry : query.entrySet()) { if (!this.converter.getTypeMapper().isTypeKey(entry.getKey())) { result.put(entry.getKey(), entry.getValue()); } } return result; }
private void mapMetaAttributes(Document source, @Nullable MongoPersistentEntity<?> entity, MetaMapping metaMapping) { if (entity == null) { return; } if (entity.hasTextScoreProperty() && !MetaMapping.IGNORE.equals(metaMapping)) { MongoPersistentProperty textScoreProperty = entity.getTextScoreProperty(); if (MetaMapping.FORCE.equals(metaMapping) || (MetaMapping.WHEN_PRESENT.equals(metaMapping) && source.containsKey(textScoreProperty.getFieldName()))) { source.putAll(getMappedTextScoreField(textScoreProperty)); } } }