Search in sources :

Example 1 with TestAndSetCondition

use of com.yahoo.document.TestAndSetCondition in project vespa by vespa-engine.

the class RestApi method createUpdateOperation.

private VespaXMLFeedReader.Operation createUpdateOperation(HttpRequest request, String id, String condition, Optional<Boolean> create) {
    final VespaXMLFeedReader.Operation operationUpdate = singleDocumentParser.parseUpdate(request.getData(), id);
    if (condition != null && !condition.isEmpty()) {
        operationUpdate.getDocumentUpdate().setCondition(new TestAndSetCondition(condition));
    }
    create.ifPresent(c -> operationUpdate.getDocumentUpdate().setCreateIfNonExistent(c));
    return operationUpdate;
}
Also used : VespaXMLFeedReader(com.yahoo.vespaxmlparser.VespaXMLFeedReader) TestAndSetCondition(com.yahoo.document.TestAndSetCondition)

Example 2 with TestAndSetCondition

use of com.yahoo.document.TestAndSetCondition in project vespa by vespa-engine.

the class OperationHandlerImpl method delete.

@Override
public void delete(RestUri restUri, String condition, Optional<String> route) throws RestApiException {
    SyncSession syncSession = syncSessions.alloc();
    Response response;
    try {
        Instant startTime = Instant.now();
        DocumentId id = new DocumentId(restUri.generateFullId());
        DocumentRemove documentRemove = new DocumentRemove(id);
        setRoute(syncSession, route);
        if (condition != null && !condition.isEmpty()) {
            documentRemove.setCondition(new TestAndSetCondition(condition));
        }
        syncSession.remove(documentRemove);
        metricsHelper.reportSuccessful(DocumentOperationType.REMOVE, startTime);
        return;
    } catch (DocumentAccessException documentException) {
        if (documentException.hasConditionNotMetError()) {
            response = Response.createErrorResponse(412, "Condition not met: " + documentException.getMessage(), restUri, RestUri.apiErrorCodes.DOCUMENT_CONDITION_NOT_MET);
        } else {
            response = Response.createErrorResponse(400, documentException.getMessage(), restUri, RestUri.apiErrorCodes.DOCUMENT_EXCPETION);
        }
    } catch (Exception e) {
        response = Response.createErrorResponse(500, ExceptionUtils.getStackTrace(e), restUri, RestUri.apiErrorCodes.UNSPECIFIED);
    } finally {
        syncSessions.free(syncSession);
    }
    metricsHelper.reportFailure(DocumentOperationType.REMOVE, DocumentOperationStatus.fromHttpStatusCode(response.getStatus()));
    throw new RestApiException(response);
}
Also used : DocumentRemove(com.yahoo.document.DocumentRemove) Instant(java.time.Instant) DocumentId(com.yahoo.document.DocumentId) SyncSession(com.yahoo.documentapi.SyncSession) MessageBusSyncSession(com.yahoo.documentapi.messagebus.MessageBusSyncSession) TestAndSetCondition(com.yahoo.document.TestAndSetCondition) DocumentAccessException(com.yahoo.documentapi.DocumentAccessException) DocumentAccessException(com.yahoo.documentapi.DocumentAccessException)

Aggregations

TestAndSetCondition (com.yahoo.document.TestAndSetCondition)2 DocumentId (com.yahoo.document.DocumentId)1 DocumentRemove (com.yahoo.document.DocumentRemove)1 DocumentAccessException (com.yahoo.documentapi.DocumentAccessException)1 SyncSession (com.yahoo.documentapi.SyncSession)1 MessageBusSyncSession (com.yahoo.documentapi.messagebus.MessageBusSyncSession)1 VespaXMLFeedReader (com.yahoo.vespaxmlparser.VespaXMLFeedReader)1 Instant (java.time.Instant)1