use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class Elide method patch.
/**
* Handle PATCH.
*
* @param baseUrlEndPoint base URL with prefix endpoint
* @param contentType the content type
* @param accept the accept
* @param path the path
* @param jsonApiDocument the json api document
* @param queryParams the query params
* @param requestHeaders the request headers
* @param opaqueUser the opaque user
* @param apiVersion the API version
* @param requestId the request ID
* @return Elide response object
*/
public ElideResponse patch(String baseUrlEndPoint, String contentType, String accept, String path, String jsonApiDocument, MultivaluedMap<String, String> queryParams, Map<String, List<String>> requestHeaders, User opaqueUser, String apiVersion, UUID requestId) {
Handler<DataStoreTransaction, User, HandlerResult> handler;
if (JsonApiPatch.isPatchExtension(contentType) && JsonApiPatch.isPatchExtension(accept)) {
handler = (tx, user) -> {
PatchRequestScope requestScope = new PatchRequestScope(baseUrlEndPoint, path, apiVersion, tx, user, requestId, queryParams, requestHeaders, elideSettings);
try {
Supplier<Pair<Integer, JsonNode>> responder = JsonApiPatch.processJsonPatch(dataStore, path, jsonApiDocument, requestScope);
return new HandlerResult(requestScope, responder);
} catch (RuntimeException e) {
return new HandlerResult(requestScope, e);
}
};
} else {
handler = (tx, user) -> {
JsonApiDocument jsonApiDoc = mapper.readJsonApiDocument(jsonApiDocument);
RequestScope requestScope = new RequestScope(baseUrlEndPoint, path, apiVersion, jsonApiDoc, tx, user, queryParams, requestHeaders, requestId, elideSettings);
requestScope.setEntityProjection(new EntityProjectionMaker(elideSettings.getDictionary(), requestScope).parsePath(path));
BaseVisitor visitor = new PatchVisitor(requestScope);
return visit(path, requestScope, visitor);
};
}
return handleRequest(false, opaqueUser, dataStore::beginTransaction, requestId, handler);
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class Elide method post.
/**
* Handle POST.
*
* @param baseUrlEndPoint base URL with prefix endpoint
* @param path the path
* @param jsonApiDocument the json api document
* @param queryParams the query params
* @param requestHeaders the request headers
* @param opaqueUser the opaque user
* @param apiVersion the API version
* @param requestId the request ID
* @return Elide response object
*/
public ElideResponse post(String baseUrlEndPoint, String path, String jsonApiDocument, MultivaluedMap<String, String> queryParams, Map<String, List<String>> requestHeaders, User opaqueUser, String apiVersion, UUID requestId) {
return handleRequest(false, opaqueUser, dataStore::beginTransaction, requestId, (tx, user) -> {
JsonApiDocument jsonApiDoc = mapper.readJsonApiDocument(jsonApiDocument);
RequestScope requestScope = new RequestScope(baseUrlEndPoint, path, apiVersion, jsonApiDoc, tx, user, queryParams, requestHeaders, requestId, elideSettings);
requestScope.setEntityProjection(new EntityProjectionMaker(elideSettings.getDictionary(), requestScope).parsePath(path));
BaseVisitor visitor = new PostVisitor(requestScope);
return visit(path, requestScope, visitor);
});
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class Elide method delete.
/**
* Handle DELETE.
*
* @param baseUrlEndPoint base URL with prefix endpoint
* @param path the path
* @param jsonApiDocument the json api document
* @param queryParams the query params
* @param requestHeaders the request headers
* @param opaqueUser the opaque user
* @param apiVersion the API version
* @param requestId the request ID
* @return Elide response object
*/
public ElideResponse delete(String baseUrlEndPoint, String path, String jsonApiDocument, MultivaluedMap<String, String> queryParams, Map<String, List<String>> requestHeaders, User opaqueUser, String apiVersion, UUID requestId) {
return handleRequest(false, opaqueUser, dataStore::beginTransaction, requestId, (tx, user) -> {
JsonApiDocument jsonApiDoc = StringUtils.isEmpty(jsonApiDocument) ? new JsonApiDocument() : mapper.readJsonApiDocument(jsonApiDocument);
RequestScope requestScope = new RequestScope(baseUrlEndPoint, path, apiVersion, jsonApiDoc, tx, user, queryParams, requestHeaders, requestId, elideSettings);
requestScope.setEntityProjection(new EntityProjectionMaker(elideSettings.getDictionary(), requestScope).parsePath(path));
BaseVisitor visitor = new DeleteVisitor(requestScope);
return visit(path, requestScope, visitor);
});
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class AsyncAPICancelRunnable method cancelAsyncAPI.
/**
* This method cancels queries based on threshold.
* @param type AsyncAPI Type Implementation.
*/
protected <T extends AsyncAPI> void cancelAsyncAPI(Class<T> type) {
try {
TransactionRegistry transactionRegistry = elide.getTransactionRegistry();
Map<UUID, DataStoreTransaction> runningTransactionMap = transactionRegistry.getRunningTransactions();
// Running transaction UUIDs
Set<UUID> runningTransactionUUIDs = runningTransactionMap.keySet();
// Construct filter expression
PathElement statusPathElement = new PathElement(type, QueryStatus.class, "status");
FilterExpression fltStatusExpression = new InPredicate(statusPathElement, QueryStatus.CANCELLED, QueryStatus.PROCESSING, QueryStatus.QUEUED);
Iterable<T> asyncAPIIterable = asyncAPIDao.loadAsyncAPIByFilter(fltStatusExpression, type);
// Active AsyncAPI UUIDs
Set<UUID> asyncTransactionUUIDs = StreamSupport.stream(asyncAPIIterable.spliterator(), false).filter(query -> query.getStatus() == QueryStatus.CANCELLED || TimeUnit.SECONDS.convert(Math.abs(new Date(System.currentTimeMillis()).getTime() - query.getCreatedOn().getTime()), TimeUnit.MILLISECONDS) > maxRunTimeSeconds).map(query -> UUID.fromString(query.getRequestId())).collect(Collectors.toSet());
// AsyncAPI UUIDs that have active transactions
Set<UUID> queryUUIDsToCancel = Sets.intersection(runningTransactionUUIDs, asyncTransactionUUIDs);
// AsyncAPI IDs that need to be cancelled
Set<String> queryIDsToCancel = queryUUIDsToCancel.stream().map(uuid -> StreamSupport.stream(asyncAPIIterable.spliterator(), false).filter(query -> query.getRequestId().equals(uuid.toString())).map(T::getId).findFirst().orElseThrow(IllegalStateException::new)).collect(Collectors.toSet());
// Cancel Transactions
queryUUIDsToCancel.stream().forEach((uuid) -> {
DataStoreTransaction runningTransaction = transactionRegistry.getRunningTransaction(uuid);
if (runningTransaction != null) {
JsonApiDocument jsonApiDoc = new JsonApiDocument();
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
RequestScope scope = new RequestScope("", "query", NO_VERSION, jsonApiDoc, runningTransaction, null, queryParams, Collections.emptyMap(), uuid, elide.getElideSettings());
runningTransaction.cancel(scope);
}
});
// Change queryStatus for cancelled queries
if (!queryIDsToCancel.isEmpty()) {
PathElement idPathElement = new PathElement(type, String.class, "id");
FilterExpression fltIdExpression = new InPredicate(idPathElement, queryIDsToCancel);
asyncAPIDao.updateStatusAsyncAPIByFilter(fltIdExpression, QueryStatus.CANCEL_COMPLETE, type);
}
} catch (Exception e) {
log.error("Exception in scheduled cancellation: {}", e.toString());
}
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class DefaultAsyncAPIDAO method executeInTransaction.
/**
* This method creates a transaction from the datastore, performs the DB action using
* a generic functional interface and closes the transaction.
* @param dataStore Elide datastore retrieved from Elide object
* @param action Functional interface to perform DB action
* @return Object Returns Entity Object (AsyncAPIResult or AsyncResult)
*/
protected Object executeInTransaction(DataStore dataStore, Transactional action) {
log.debug("executeInTransaction");
Object result = null;
try (DataStoreTransaction tx = dataStore.beginTransaction()) {
JsonApiDocument jsonApiDoc = new JsonApiDocument();
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
RequestScope scope = new RequestScope("", "query", NO_VERSION, jsonApiDoc, tx, null, queryParams, Collections.emptyMap(), UUID.randomUUID(), elideSettings);
result = action.execute(tx, scope);
tx.flush(scope);
tx.commit(scope);
} catch (IOException e) {
log.error("IOException: {}", e.toString());
throw new IllegalStateException(e);
}
return result;
}
Aggregations