/** * Construct an JobRowFilter object using the same JobField and values as the object on * which this method is called and using the supplied FilterOperation. * * @param operation The operation to perform (eg: eq) * * @return An instance of JobRowFilter created using the supplied FilterOperation */ public JobRowFilter withOperation(FilterOperation operation) { return new JobRowFilter(jobField, operation, values); }
/** * Builds an instance of the interface to the storage system to use to store metadata about asynchronous * requests. * * @return An instance of the interface that allows Bard to talk to an arbitrary key-value store system for storing * job metadata, by default this returns a {@code NoOpApiJobStore}, which does nothing */ protected ApiJobStore buildApiJobStore() { return new NoOpApiJobStore(); }
@Override public Observable<JobRow> getFilteredRows(Set<JobRowFilter> jobRowFilters) throws IllegalArgumentException { return getAllRows().filter(jobRow -> satisfiesFilters(jobRowFilters, jobRow)); }
/** * Parses the URL ApiJobStore filter query and generates the JobRowFilter object. * * @param filterQuery Expects a URL ApiJobStore filter query String in the format: * <p> * <code>(JobField name)-(operation)[?(value or comma separated values)]?</code> * * @throws BadFilterException when filter pattern is not matched or when any of its properties are not * valid. */ public JobRowFilter(@NotNull String filterQuery) throws BadFilterException { LOG.trace("filterQuery: {}", filterQuery); Matcher tokenizedQuery = QUERY_PATTERN.matcher(filterQuery); // if pattern match found, extract values else throw exception if (!tokenizedQuery.matches()) { LOG.debug(FILTER_INVALID.logFormat(filterQuery)); throw new BadFilterException(FILTER_INVALID.format(filterQuery)); } jobField = extractJobField(tokenizedQuery); operation = extractOperation(tokenizedQuery); values = extractValues(tokenizedQuery, filterQuery); }
JobField filterJobField = jobRowFilter.getJobField(); FilterOperation filterOperation = jobRowFilter.getOperation(); Set<String> filterValues = jobRowFilter.getValues();
ApiJobStore apiJobStore = new HashJobStore(); apiJobStore.save(jobRow1); apiJobStore.save(jobRow2); apiJobStore.save(jobRow3);
/** * Return an Observable containing a stream of job views for jobs in the ApiJobStore. If filter String is non null * and non empty, only return results that satisfy the filter. If filter String is null or empty, return all rows. * If, for any JobRow, the mapping from JobRow to job view fails, an Observable over JobRequestFailedException is * returned. If the ApiJobStore is empty, we return an empty Observable. * * @return An Observable containing a stream of Maps representing the job to be returned to the user */ public Observable<Map<String, String>> getJobViews() { if (filters == null || "".equals(filters)) { return apiJobStore.getAllRows().map(this::mapJobRowsToJobViews); } else { return apiJobStore.getFilteredRows(buildJobStoreFilter(filters)).map(this::mapJobRowsToJobViews); } }
/** * This method checks if the given JobRow satisfies all the JobRowFilters and returns true if it does. * If a JobField in any of the filters is not a part of the JobRow, this method throws an IllegalArgumentException. * * @param jobRowFilters A Set of JobRowFilters specifying the different conditions to be satisfied * @param jobRow The JobRow which needs to be inspected * * @return true if the JobRow satisfies all the filters, false otherwise * * @throws IllegalArgumentException if a JobField in any of the filters is not a part the JobRow */ private boolean satisfiesFilters(Set<JobRowFilter> jobRowFilters, JobRow jobRow) throws IllegalArgumentException { return jobRowFilters.stream().allMatch(filter -> satisfiesFilter(jobRow, filter)); }
/** * Returns an Observable over the Map representing the job to be returned to the user. * * @param ticket The ticket that uniquely identifies the job * * @return An Observable over the Map representing the job to be returned to the user or an Observable wrapping * JobNotFoundException if the Job is not available in the ApiJobStore */ public Observable<Map<String, String>> getJobViewObservable(String ticket) { return apiJobStore.get(ticket) .switchIfEmpty( Observable.error(new JobNotFoundException(ErrorMessageFormat.JOB_NOT_FOUND.format(ticket))) ) .map(jobRow -> jobPayloadBuilder.buildPayload(jobRow, uriInfo)); }
ApiJobStore apiJobStore = new HashJobStore(); apiJobStore.save(jobRow1); apiJobStore.save(jobRow2); apiJobStore.save(jobRow3);
/** * Construct an JobRowFilter object using the same FilterOperation and values as the object on * which this method is called and using the supplied JobField. * * @param jobField The JobField to perform the filtering on * * @return An instance of JobRowFilter created using the supplied JobField */ public JobRowFilter withJobField(JobField jobField) { return new JobRowFilter(jobField, operation, values); }
/** * Construct an JobRowFilter object using the same JobField and FilterOperation as the object on * which this method is called and using the supplied values. * * @param values A Set of Strings to compare the JobField's value to * * @return An instance of JobRowFilter created using the supplied values */ public JobRowFilter withValues(Set<String> values) { return new JobRowFilter(jobField, operation, values); }
/** * Given a filter String, generates a Set of ApiJobStoreFilters. This method will throw a BadApiRequestException if * the filter String cannot be parsed into ApiJobStoreFilters successfully. * * @param filterQuery Expects a URL filterQuery String that may contain multiple filters separated by * comma. The format of a filter String is : * (JobField name)-(operation)[(value or comma separated values)]? * * @return A Set of ApiJobStoreFilters */ public LinkedHashSet<JobRowFilter> buildJobStoreFilter(@NotNull String filterQuery) { // split on '],' to get list of filters return Arrays.stream(filterQuery.split(COMMA_AFTER_BRACKET_PATTERN)) .map( filter -> { try { return new JobRowFilter(filter); } catch (BadFilterException e) { throw new BadApiRequestException(e.getMessage(), e); } } ) .collect(Collectors.toCollection(LinkedHashSet<JobRowFilter>::new)); }