Search in sources :

Example 1 with MaxBatchSize

use of com.linkedin.restli.server.annotations.MaxBatchSize in project rest.li by linkedin.

the class BatchGreetingResource method searchGreetings.

@BatchFinder(value = "searchGreetings", batchParam = "criteria")
@MaxBatchSize(value = 2, validate = true)
public BatchFinderResult<GreetingCriteria, Greeting, Empty> searchGreetings(@QueryParam("criteria") GreetingCriteria[] criteria) {
    BatchFinderResult<GreetingCriteria, Greeting, Empty> batchFinderResult = new BatchFinderResult<>();
    for (GreetingCriteria currentCriteria : criteria) {
        if (currentCriteria.getId() == 1l) {
            CollectionResult<Greeting, Empty> c1 = new CollectionResult<>(Arrays.asList(GREETING_ONE), 1);
            batchFinderResult.putResult(currentCriteria, c1);
        } else if (currentCriteria.getId() == 2l) {
            CollectionResult<Greeting, Empty> c2 = new CollectionResult<>(Arrays.asList(GREETING_TWO), 1);
            batchFinderResult.putResult(currentCriteria, c2);
        }
    }
    return batchFinderResult;
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CollectionResult(com.linkedin.restli.server.CollectionResult) Empty(com.linkedin.restli.examples.greetings.api.Empty) GreetingCriteria(com.linkedin.restli.examples.greetings.api.GreetingCriteria) BatchFinderResult(com.linkedin.restli.server.BatchFinderResult) BatchFinder(com.linkedin.restli.server.annotations.BatchFinder) MaxBatchSize(com.linkedin.restli.server.annotations.MaxBatchSize)

Example 2 with MaxBatchSize

use of com.linkedin.restli.server.annotations.MaxBatchSize in project rest.li by linkedin.

the class BatchGreetingResource method batchGet.

@RestMethod.BatchGet
@MaxBatchSize(value = 2, validate = true)
public Map<Long, Greeting> batchGet(Set<Long> ids) {
    Map<Long, Greeting> batch = new HashMap<>();
    Map<Long, RestLiServiceException> errors = new HashMap<>();
    for (Long id : ids) {
        Greeting greeting = DB.get(id);
        if (greeting != null) {
            batch.put(id, greeting);
        } else {
            errors.put(id, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
        }
    }
    return new BatchResult<>(batch, errors);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) BatchResult(com.linkedin.restli.server.BatchResult) MaxBatchSize(com.linkedin.restli.server.annotations.MaxBatchSize)

Example 3 with MaxBatchSize

use of com.linkedin.restli.server.annotations.MaxBatchSize in project rest.li by linkedin.

the class RestLiAnnotationReader method addMaxBatchSize.

/**
 * Reads annotations on a given method in order to get max batch size, which are then added to
 * a given resource method descriptor.
 *
 * @param resourceMethodDescriptor resource method descriptor to add max batch size to
 * @param method method annotated with max batch size
 * @param resourceMethod resource method which is used to validate the method with max batch size annotation
 *                       is a supported method.
 */
private static void addMaxBatchSize(ResourceMethodDescriptor resourceMethodDescriptor, Method method, ResourceMethod resourceMethod) {
    final MaxBatchSize maxBatchSizeAnnotation = method.getAnnotation(MaxBatchSize.class);
    if (maxBatchSizeAnnotation == null) {
        return;
    }
    // Only batch methods are allowed to use MaxBatchSize annotation.
    if (!BATCH_METHODS.contains(resourceMethod)) {
        throw new ResourceConfigException(String.format("The resource method: %s cannot specify MaxBatchSize.", resourceMethod.toString()));
    }
    int maxBatchSizeValue = maxBatchSizeAnnotation.value();
    // Max batch size value should always be greater than 0
    if (maxBatchSizeValue <= 0) {
        throw new ResourceConfigException(String.format("The resource method: %s max batch size value is %s, " + "it should be greater than 0.", resourceMethod.toString(), maxBatchSizeValue));
    }
    MaxBatchSizeSchema maxBatchSizeSchema = new MaxBatchSizeSchema();
    maxBatchSizeSchema.setValue(maxBatchSizeAnnotation.value());
    maxBatchSizeSchema.setValidate(maxBatchSizeAnnotation.validate());
    resourceMethodDescriptor.setMaxBatchSize(maxBatchSizeSchema);
}
Also used : MaxBatchSizeSchema(com.linkedin.restli.restspec.MaxBatchSizeSchema) MaxBatchSize(com.linkedin.restli.server.annotations.MaxBatchSize) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Aggregations

MaxBatchSize (com.linkedin.restli.server.annotations.MaxBatchSize)3 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)2 Empty (com.linkedin.restli.examples.greetings.api.Empty)1 GreetingCriteria (com.linkedin.restli.examples.greetings.api.GreetingCriteria)1 MaxBatchSizeSchema (com.linkedin.restli.restspec.MaxBatchSizeSchema)1 BatchFinderResult (com.linkedin.restli.server.BatchFinderResult)1 BatchResult (com.linkedin.restli.server.BatchResult)1 CollectionResult (com.linkedin.restli.server.CollectionResult)1 ResourceConfigException (com.linkedin.restli.server.ResourceConfigException)1 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)1 BatchFinder (com.linkedin.restli.server.annotations.BatchFinder)1 HashMap (java.util.HashMap)1