private String getCompositeComponentLinkFieldName(Class<? extends ResourceState> stateClass) { if (ComputeState.class.isAssignableFrom(stateClass)) { return QuerySpecification.buildCompositeFieldName( ComputeState.FIELD_NAME_CUSTOM_PROPERTIES, ComputeConstants.FIELD_NAME_COMPOSITE_COMPONENT_LINK_KEY); } else if (ContainerState.class.isAssignableFrom(stateClass)) { return QuerySpecification.buildCompositeFieldName( ComputeState.FIELD_NAME_CUSTOM_PROPERTIES, RequestUtils.FIELD_NAME_CONTEXT_ID_KEY); } return ContainerState.FIELD_NAME_COMPOSITE_COMPONENT_LINK; }
private void addObjectIndexableFieldToDocument(Object v, PropertyDescription pd, String fieldNamePrefix, boolean isCollectionItem) { for (Entry<String, PropertyDescription> e : pd.fieldDescriptions.entrySet()) { PropertyDescription fieldDescription = e.getValue(); Object fieldValue = ReflectionUtils.getPropertyValue(fieldDescription, v); if (v == null) { continue; } if (pd.indexingOptions.contains(PropertyIndexingOption.SORT)) { fieldDescription.indexingOptions.add(PropertyIndexingOption.SORT); } String fieldName = QuerySpecification.buildCompositeFieldName(fieldNamePrefix, e.getKey()); addIndexableFieldToDocument(fieldValue, fieldDescription, fieldName, isCollectionItem, true); } }
public static Query buildQueryForProjectsFromProjectIndex(long projectIndex) { return QueryUtil.addCaseInsensitiveListValueClause(QuerySpecification .buildCompositeFieldName(ProjectState.FIELD_NAME_CUSTOM_PROPERTIES, ProjectService.CUSTOM_PROPERTY_PROJECT_INDEX), Collections.singletonList(Long.toString(projectIndex)), MatchType.TERM); }
@Test public void testIndexingPodoNestedInCollection() { ServiceDocumentDescription sdd = ServiceDocumentDescription.Builder.create() .buildDescription(StateWithPodoNestedInCollection.class); InnerPodo podo1 = new InnerPodo(); podo1.podoField = "value-1"; InnerPodo podo2 = new InnerPodo(); podo2.podoField = "second"; StateWithPodoNestedInCollection state = new StateWithPodoNestedInCollection(); state.collectionOfPodos = Arrays.asList(podo1, podo2); LuceneIndexDocumentHelper helper = new LuceneIndexDocumentHelper(); helper.addIndexableFieldsToDocument(state, sdd); final String nestedFieldName = QuerySpecification.buildCompositeFieldName( QuerySpecification.buildCollectionItemName("collectionOfPodos"), "podoField"); Set<String> indexedInnerValues = helper.getDoc().getFields().stream() .filter(f -> nestedFieldName.equals(f.name())) .map(f -> f.stringValue()) .collect(Collectors.toSet()); assertEquals(new HashSet<>(Arrays.asList(podo1.podoField, podo2.podoField)), indexedInnerValues); } }
private void queryComputes(Operation op, PKSCluster[] pksClusters, String endpointLink) { QueryTask.Query endpointClause = new QueryTask.Query() .setTermPropertyName(QuerySpecification.buildCompositeFieldName( ResourceState.FIELD_NAME_CUSTOM_PROPERTIES, PKS_ENDPOINT_PROP_NAME)) .setTermMatchValue(endpointLink); QueryTask queryTask = QueryUtil.buildQuery(ComputeState.class, true, endpointClause); QueryUtil.addExpandOption(queryTask); Set<String> uuids = new HashSet<>(); new ServiceDocumentQuery<>(getHost(), ComputeState.class) .query(queryTask, (r) -> { if (r.hasException()) { logSevere("Failed to query for compute states with endpoint link [%s]: %s", endpointLink, Utils.toString(r.getException())); op.fail(r.getException()); } else if (r.hasResult()) { String uuid = PropertyUtils.getPropertyString( r.getResult().customProperties, PKS_CLUSTER_UUID_PROP_NAME) .orElse(null); if (uuid != null) { uuids.add(uuid); } } else { processHosts(op, uuids, pksClusters); } }); }
@Test public void testIndexingPodoNestedInCollection() { ServiceDocumentDescription sdd = ServiceDocumentDescription.Builder.create() .buildDescription(StateWithPodoNestedInCollection.class); InnerPodo podo1 = new InnerPodo(); podo1.podoField = "value-1"; InnerPodo podo2 = new InnerPodo(); podo2.podoField = "second"; StateWithPodoNestedInCollection state = new StateWithPodoNestedInCollection(); state.collectionOfPodos = Arrays.asList(podo1, podo2); LuceneIndexDocumentHelper helper = new LuceneIndexDocumentHelper(); helper.addIndexableFieldsToDocument(state, sdd); final String nestedFieldName = QuerySpecification.buildCompositeFieldName( QuerySpecification.buildCollectionItemName("collectionOfPodos"), "podoField"); Set<String> indexedInnerValues = helper.getDoc().getFields().stream() .filter(f -> nestedFieldName.equals(f.name())) .map(f -> f.stringValue()) .collect(Collectors.toSet()); assertEquals(new HashSet<>(Arrays.asList(podo1.podoField, podo2.podoField)), indexedInnerValues); }
@SuppressWarnings({ "rawtypes" }) private void addMapIndexableFieldToDocument(Object v, PropertyDescription pd, String fieldNamePrefix) { final String errorMsg = "Field not supported. Map keys must be of type String."; Map m = (Map) v; if (pd.indexingOptions.contains(PropertyIndexingOption.SORT)) { pd.elementDescription.indexingOptions.add(PropertyIndexingOption.SORT); } for (Object o : m.entrySet()) { Entry entry = (Entry) o; Object mapKey = entry.getKey(); if (!(mapKey instanceof String)) { throw new IllegalArgumentException(errorMsg); } // There is a risk our field thread local maps grow too much from discrete field names // built from map keys. It should be unlikely however: "keys" need to be a set of well known // names for queries to be useful. Even 1M discrete field names should be OK on a loaded // node addIndexableFieldToDocument(entry.getValue(), pd.elementDescription, QuerySpecification.buildCompositeFieldName(fieldNamePrefix, (String) mapKey), false, true); if (pd.indexingOptions.contains(PropertyIndexingOption.FIXED_ITEM_NAME)) { addIndexableFieldToDocument(entry.getKey(), new PropertyDescription(), fieldNamePrefix, true, true); addIndexableFieldToDocument(entry.getValue(), pd.elementDescription, fieldNamePrefix, true, false); } } }
public static DeferredResult<Void> handleDelete(Service service, Operation op) { DeferredResult<Void> dr = new DeferredResult<>(); service.sendRequest(Operation.createPost(service, ServiceUriPaths.CORE_QUERY_TASKS) .setBody(QueryUtil.addCountOption(QueryUtil.buildPropertyQuery(ComputeState.class, QuerySpecification.buildCompositeFieldName( ComputeState.FIELD_NAME_CUSTOM_PROPERTIES, ComputeConstants.HOST_AUTH_CREDENTIALS_PROP_NAME), service.getSelfLink()))) .setCompletion((o, e) -> { if (e != null) { service.getHost().log(Level.WARNING, Utils.toString(e)); dr.fail(e); } ServiceDocumentQueryResult result = o.getBody(QueryTask.class).results; if (result.documentCount != 0) { op.fail(new LocalizableValidationException(CREDENTIALS_IN_USE_MESSAGE, CREDENTIALS_IN_USE_MESSAGE_CODE)); } dr.complete(null); })); return dr; }
@Test public void evaluateWithCompositeField() throws QueryFilterException { String n1 = QueryTask.QuerySpecification.buildCompositeFieldName("nc1", "ns1"); QueryFilter filter = QueryFilter.create(Query.Builder.create() .addFieldClause(n1, "v1") .build()); QueryFilterDocument document; document = new QueryFilterDocument(); document.nc1 = new NestedClass(); document.nc1.ns1 = "v1"; assertTrue(filter.evaluate(document, this.description)); document = new QueryFilterDocument(); document.nc1 = new NestedClass(); document.nc1.ns1 = "v2"; assertFalse(filter.evaluate(document, this.description)); document = new QueryFilterDocument(); assertFalse(filter.evaluate(document, this.description)); }
/** * Builds a query that will select all documents that use any of the specified certificates. */ private static Query buildCertLinkClause(Collection<String> trustCertLinks) { if (trustCertLinks == null || trustCertLinks.isEmpty()) { return null; } Builder builder = Query.Builder.create(Occurance.MUST_OCCUR); TRUST_CERT_CUSTOM_PROPERTY_NAMES.forEach(propName -> { builder.addInClause( QuerySpecification.buildCompositeFieldName( ResourceState.FIELD_NAME_CUSTOM_PROPERTIES, propName), trustCertLinks, Occurance.SHOULD_OCCUR); }); return builder.build(); }
private void queryComputesToAssign(EpzComputeEnumerationTaskState state) { Query mustBeIn = Utils.clone(state.resourcePoolQuery); Query areOut = Query.Builder.create() .addKindFieldClause(ComputeState.class) .addFieldClause( QuerySpecification.buildCompositeFieldName( ResourceState.FIELD_NAME_CUSTOM_PROPERTIES, EPZ_CUSTOM_PROP_NAME_PREFIX + extractRpId(state)), EPZ_CUSTOM_PROP_VALUE, MatchType.TERM, Occurance.MUST_NOT_OCCUR) .build(); Query combinedQuery = Query.Builder.create().addClauses(mustBeIn, areOut).build(); queryComputes(state, combinedQuery, EpzComputeEnumerationTaskState.SubStage.ASSIGN_COMPUTES, EpzComputeEnumerationTaskState.SubStage.COMPLETED); }
@Test public void evaluateWithCompositeField() throws QueryFilterException { String n1 = QueryTask.QuerySpecification.buildCompositeFieldName("nc1", "ns1"); QueryFilter filter = QueryFilter.create(Query.Builder.create() .addFieldClause(n1, "v1") .build()); QueryFilterDocument document; document = new QueryFilterDocument(); document.nc1 = new NestedClass(); document.nc1.ns1 = "v1"; assertTrue(filter.evaluate(document, this.description)); document = new QueryFilterDocument(); document.nc1 = new NestedClass(); document.nc1.ns1 = "v2"; assertFalse(filter.evaluate(document, this.description)); document = new QueryFilterDocument(); assertFalse(filter.evaluate(document, this.description)); }
/** * Add a clause which matches a nested field value. * @param parentFieldName the top level field name. * @param nestedFieldName the nested field name. * @param nestedFieldValue the nested field value to match. * @return a reference to this object. */ public Builder addCompositeFieldClause(String parentFieldName, String nestedFieldName, String nestedFieldValue) { return addFieldClause( QuerySpecification.buildCompositeFieldName(parentFieldName, nestedFieldName), nestedFieldValue); }
/** * Add a clause with the given occurance which matches a nested field value. * @param parentFieldName the top level field name. * @param nestedFieldName the nested field name. * @param nestedFieldValue the nested field value to match. * @param occurance the occurance for this clause. * @return a reference to this object. */ public Builder addCompositeFieldClause(String parentFieldName, String nestedFieldName, String nestedFieldValue, Occurance occurance) { return addFieldClause( QuerySpecification.buildCompositeFieldName(parentFieldName, nestedFieldName), nestedFieldValue, occurance); }
Query createWithMapOfStringToString() { String n1 = QueryTask.QuerySpecification.buildCompositeFieldName("m1", "k1"); String n2 = QueryTask.QuerySpecification.buildCompositeFieldName("m2", "k2"); Query t1 = createTerm(n1, "v2", Occurance.MUST_OCCUR); Query t2 = createTerm(n2, "v3", Occurance.MUST_NOT_OCCUR); Query q = new Query(); q.addBooleanClause(t1); q.addBooleanClause(t2); return q; }
Query createWithMapOfStringToString() { String n1 = QueryTask.QuerySpecification.buildCompositeFieldName("m1", "k1"); String n2 = QueryTask.QuerySpecification.buildCompositeFieldName("m2", "k2"); Query t1 = createTerm(n1, "v2", Occurance.MUST_OCCUR); Query t2 = createTerm(n2, "v3", Occurance.MUST_NOT_OCCUR); Query q = new Query(); q.addBooleanClause(t1); q.addBooleanClause(t2); return q; }
@Test public void numericRangeQueryOnCollection() throws Throwable { doNumericRangeQueries(QuerySpecification.buildCompositeFieldName("mapOfLongs", "long"), QuerySpecification.buildCompositeFieldName("mapOfDoubles", "double")); }
@SuppressWarnings({"rawtypes"}) private void doMapQuery(String mapName, Map map, long documentCount, long expectedResultCount) throws Throwable { for (Object o : map.entrySet()) { Entry e = (Entry) o; this.host.createAndWaitSimpleDirectQuery( QuerySpecification.buildCompositeFieldName(mapName, (String) e.getKey()), e.getValue().toString(), documentCount, expectedResultCount, this.testResults); } }
@SuppressWarnings({"rawtypes"}) private void doMapQuery(String mapName, Map map, long documentCount, long expectedResultCount) throws Throwable { for (Object o : map.entrySet()) { Entry e = (Entry) o; this.host.createAndWaitSimpleDirectQuery( QuerySpecification.buildCompositeFieldName(mapName, (String) e.getKey()), e.getValue().toString(), documentCount, expectedResultCount, this.testResults); } }
@Test public void numericRangeQueryOnCollection() throws Throwable { doNumericRangeQueries(QuerySpecification.buildCompositeFieldName("mapOfLongs", "long"), QuerySpecification.buildCompositeFieldName("mapOfDoubles", "double")); }