private static <K, RT extends RecordTemplate> Response<RT> unbatchResponse(BatchGetEntityRequest<K, RT> request, Response<BatchKVResponse<K, EntityResponse<RT>>> batchResponse, Object id) throws RemoteInvocationException { final BatchKVResponse<K, EntityResponse<RT>> batchEntity = batchResponse.getEntity(); final ErrorResponse errorResponse = batchEntity.getErrors().get(id); if (errorResponse != null) { throw new RestLiResponseException(errorResponse); } final EntityResponse<RT> entityResponse = batchEntity.getResults().get(id); if (entityResponse != null) { final RT entityResult = entityResponse.getEntity(); if (entityResult != null) { return new ResponseImpl<>(batchResponse, entityResult); } } LOGGER.debug("No result or error for base URI : {}, id: {}. Verify that the batchGet endpoint returns response keys that match batchGet request IDs.", request.getBaseUriTemplate(), id); throw NOT_FOUND_EXCEPTION; }
private void successBatchGet(BatchGetRequest request, Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch, Entry<RestRequestBatchKey, BatchEntry<Response<Object>>> entry, final ProtocolVersion version) { Set<String> ids = (Set<String>) request.getObjectIds().stream() .map(o -> BatchResponse.keyToString(o, version)) .collect(Collectors.toSet()); DataMap dm = filterIdsInBatchResult(responseToBatch.getEntity().data(), ids); BatchResponse br = new BatchResponse<>(dm, request.getResponseDecoder().getEntityClass()); Response rsp = new ResponseImpl(responseToBatch, br); entry.getValue().getPromise().done(rsp); }
@SuppressWarnings({ "deprecation" }) private void successBatchGetKV(BatchGetKVRequest request, Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch, Entry<RestRequestBatchKey, BatchEntry<Response<Object>>> entry, final ProtocolVersion version) { Set<String> ids = (Set<String>) request.getObjectIds().stream() .map(o -> BatchResponse.keyToString(o, version)) .collect(Collectors.toSet()); DataMap dm = filterIdsInBatchResult(responseToBatch.getEntity().data(), ids); BatchKVResponse br = new BatchKVResponse(dm, request.getResourceSpec().getKeyType(), request.getResourceSpec().getValueType(), request.getResourceSpec().getKeyParts(), request.getResourceSpec().getComplexKeyType(), version); Response rsp = new ResponseImpl(responseToBatch, br); entry.getValue().getPromise().done(rsp); }
/** * Get a collection of {@link JobExecutionQueryResult}s for a collection of {@link JobExecutionQuery}s. * * <p> * The order of {@link JobExecutionQueryResult}s may not match the order of {@link JobExecutionQuery}s. * </p> * * @param queries a collection of {@link JobExecutionQuery}s * @return a collection of {@link JobExecutionQueryResult}s * @throws RemoteInvocationException */ public Collection<JobExecutionQueryResult> batchGet(Collection<JobExecutionQuery> queries) throws RemoteInvocationException { Set<ComplexResourceKey<JobExecutionQuery, EmptyRecord>> ids = Sets.newHashSet(); for (JobExecutionQuery query : queries) { ids.add(new ComplexResourceKey<JobExecutionQuery, EmptyRecord>(query, new EmptyRecord())); } BatchGetKVRequest<ComplexResourceKey<JobExecutionQuery, EmptyRecord>, JobExecutionQueryResult> batchGetRequest = new JobExecutionsBuilders().batchGet().ids(ids).buildKV(); BatchKVResponse<ComplexResourceKey<JobExecutionQuery, EmptyRecord>, JobExecutionQueryResult> response = this.restClient.sendRequest(batchGetRequest, ErrorHandlingBehavior.TREAT_SERVER_ERROR_AS_SUCCESS) .getResponseEntity(); return response.getResults().values(); }
if (resultsRaw == null) _results = new ParamlessKeyHashMap<V>(complexKeyType); _results = new ParamlessKeyHashMap<V>( CollectionUtils.getMapInitialCapacity(resultsRaw.size(), 0.75f), 0.75f, complexKeyType); for (Map.Entry<String, Object> entry : resultsRaw.entrySet()) final V value = deserializeValue(entry.getValue()); _results.put(key, value); if (errorsRaw == null) _errors = new ParamlessKeyHashMap<ErrorResponse>(complexKeyType); _errors = new ParamlessKeyHashMap<ErrorResponse>( CollectionUtils.getMapInitialCapacity(errorsRaw.size(), 0.75f), 0.75f, complexKeyType); for (Map.Entry<String, Object> entry : errorsRaw.entrySet())
@Override public BatchKVResponse<K, V> wrapResponse(DataMap dataMap, Map<String, String> headers, ProtocolVersion version) { return dataMap == null ? null : new BatchKVResponse<K, V>(dataMap, _keyType, _elementType, _keyParts, _complexKeyType, version); } }
/** * Constructor resource responses. * * @param data provides the batch response data. * @param keyType provides the class identifying the key type. * <ul> * <li>For collection resources must be a primitive or a typeref to a primitive.</li> * <li>For an association resources must be {@link CompoundKey} and keyParts must contain an entry for each association key field.</li> * <li>For complex resources must be {@link ComplexResourceKey}, keyKeyClass must contain the * key's record template class and if the resource has a key params their record template type keyParamsClass must be provided.</li> * @param valueType provides the entity type of the collection. * @param keyParts provides a map for association keys of each key name to {@link CompoundKey.TypeInfo}, for non-association resources must be an empty map. * @param complexKeyType provides the type of the key for complex key resources, otherwise null. */ public BatchKVResponse(DataMap data, TypeSpec<K> keyType, TypeSpec<V> valueType, Map<String, CompoundKey.TypeInfo> keyParts, ComplexKeySpec<?, ?> complexKeyType, ProtocolVersion version) { super(data, null); _valueClass = valueType.getType(); deserializeData(keyType, keyParts, complexKeyType, version); }
@Override @SuppressWarnings("unchecked") public CreateResponse<K> wrapResponse(DataMap dataMap, Map<String, String> headers, ProtocolVersion version) { String id = HeaderUtil.getIdHeaderValue(headers); K key; if (id == null) { key = null; } else { key = (K) ResponseUtils.convertKey(id, _keyType, _keyParts, _complexKeyType, version); } return new CreateResponse<K>(key); } }
id = createResponse.getId(); castMessage = "CreateResponse";
/** * Extract the get response for this resource out of an auto-batched batch response. * This is pure rest.li logic, and it complements the auto-batching logic in BatchGetRequestBuilder. * @throws com.linkedin.r2.RemoteInvocationException if the server returned an error response for this resource, * or if it returned neither a result nor an error. */ public static <K, V extends RecordTemplate> Response<V> unbatchKVResponse(Request<BatchKVResponse<K, V>> request, Response<BatchKVResponse<K, V>> batchResponse, K id) throws RemoteInvocationException { final BatchKVResponse<K, V> batchEntity = batchResponse.getEntity(); final ErrorResponse errorResponse = batchEntity.getErrors().get(id); if (errorResponse != null) { throw new RestLiResponseException(errorResponse); } final V entityResult = batchEntity.getResults().get(id); if (entityResult == null) { throw new RestLiDecodingException("No result or error for base URI " + request.getBaseUriTemplate() + ", id " + id + ". Verify that the batchGet endpoint returns response keys that match batchGet request IDs.", null); } return new ResponseImpl<V>(batchResponse, entityResult); }
/** * Get a collection of {@link JobExecutionQueryResult}s for a collection of {@link JobExecutionQuery}s. * * <p> * The order of {@link JobExecutionQueryResult}s may not match the order of {@link JobExecutionQuery}s. * </p> * * @param queries a collection of {@link JobExecutionQuery}s * @return a collection of {@link JobExecutionQueryResult}s * @throws RemoteInvocationException */ public Collection<JobExecutionQueryResult> batchGet(Collection<JobExecutionQuery> queries) throws RemoteInvocationException { Set<ComplexResourceKey<JobExecutionQuery, EmptyRecord>> ids = Sets.newHashSet(); for (JobExecutionQuery query : queries) { ids.add(new ComplexResourceKey<JobExecutionQuery, EmptyRecord>(query, new EmptyRecord())); } BatchGetKVRequest<ComplexResourceKey<JobExecutionQuery, EmptyRecord>, JobExecutionQueryResult> batchGetRequest = new JobExecutionsBuilders().batchGet().ids(ids).buildKV(); BatchKVResponse<ComplexResourceKey<JobExecutionQuery, EmptyRecord>, JobExecutionQueryResult> response = this.restClient.sendRequest(batchGetRequest, ErrorHandlingBehavior.TREAT_SERVER_ERROR_AS_SUCCESS) .getResponseEntity(); return response.getResults().values(); }
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"}) private <T> T constructResponseFromDataMap(BatchRequest<T> request, ProtocolVersion protocolVersion, DataMap data) { if (request instanceof BatchGetRequest) { return (T) new BatchResponse(data, request.getResponseDecoder().getEntityClass()); } else { return (T) new BatchKVResponse(data, request.getResourceSpec().getKeyType(), request.getResourceSpec().getValueType(), request.getResourceSpec().getKeyParts(), request.getResourceSpec().getComplexKeyType(), protocolVersion); } }
@SuppressWarnings({ "deprecation" }) private void successBatchGetEntity(BatchGetEntityRequest request, Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch, Entry<RestRequestBatchKey, BatchEntry<Response<Object>>> entry, final ProtocolVersion version) { Set<String> ids = (Set<String>) request.getObjectIds().stream() .map(o -> BatchResponse.keyToString(o, version)) .collect(Collectors.toSet()); DataMap dm = filterIdsInBatchResult(responseToBatch.getEntity().data(), ids); BatchKVResponse br = new BatchEntityResponse<>(dm, request.getResourceSpec().getKeyType(), request.getResourceSpec().getValueType(), request.getResourceSpec().getKeyParts(), request.getResourceSpec().getComplexKeyType(), version); Response rsp = new ResponseImpl(responseToBatch, br); entry.getValue().getPromise().done(rsp); }
CheckedUtil.putWithoutChecking(convertedData, ERRORS, inputErrors); super.deserializeData(convertedData, keyType, keyParts, complexKeyType, version);
responseData.put(BatchKVResponse.ERRORS, inputErrors); return new BatchKVResponse<K, UpdateStatus>(responseData, _keyType, new TypeSpec<UpdateStatus>(UpdateStatus.class), _keyParts, _complexKeyType, version);