Search in sources :

Example 1 with SyncSession

use of com.yahoo.documentapi.SyncSession in project vespa by vespa-engine.

the class OperationHandlerImpl method put.

@Override
public void put(RestUri restUri, VespaXMLFeedReader.Operation data, Optional<String> route) throws RestApiException {
    SyncSession syncSession = syncSessions.alloc();
    Response response;
    try {
        Instant startTime = Instant.now();
        DocumentPut put = new DocumentPut(data.getDocument());
        put.setCondition(data.getCondition());
        setRoute(syncSession, route);
        syncSession.put(put);
        metricsHelper.reportSuccessful(DocumentOperationType.PUT, startTime);
        return;
    } catch (DocumentAccessException documentException) {
        response = createErrorResponse(documentException, restUri);
    } catch (Exception e) {
        response = Response.createErrorResponse(500, ExceptionUtils.getStackTrace(e), restUri, RestUri.apiErrorCodes.INTERNAL_EXCEPTION);
    } finally {
        syncSessions.free(syncSession);
    }
    metricsHelper.reportFailure(DocumentOperationType.PUT, DocumentOperationStatus.fromHttpStatusCode(response.getStatus()));
    throw new RestApiException(response);
}
Also used : Instant(java.time.Instant) DocumentPut(com.yahoo.document.DocumentPut) SyncSession(com.yahoo.documentapi.SyncSession) MessageBusSyncSession(com.yahoo.documentapi.messagebus.MessageBusSyncSession) DocumentAccessException(com.yahoo.documentapi.DocumentAccessException) DocumentAccessException(com.yahoo.documentapi.DocumentAccessException)

Example 2 with SyncSession

use of com.yahoo.documentapi.SyncSession in project vespa by vespa-engine.

the class OperationHandlerImpl method update.

@Override
public void update(RestUri restUri, VespaXMLFeedReader.Operation data, Optional<String> route) throws RestApiException {
    SyncSession syncSession = syncSessions.alloc();
    Response response;
    try {
        Instant startTime = Instant.now();
        setRoute(syncSession, route);
        syncSession.update(data.getDocumentUpdate());
        metricsHelper.reportSuccessful(DocumentOperationType.UPDATE, startTime);
        return;
    } catch (DocumentAccessException documentException) {
        response = createErrorResponse(documentException, restUri);
    } catch (Exception e) {
        response = Response.createErrorResponse(500, ExceptionUtils.getStackTrace(e), restUri, RestUri.apiErrorCodes.INTERNAL_EXCEPTION);
    } finally {
        syncSessions.free(syncSession);
    }
    metricsHelper.reportFailure(DocumentOperationType.UPDATE, DocumentOperationStatus.fromHttpStatusCode(response.getStatus()));
    throw new RestApiException(response);
}
Also used : Instant(java.time.Instant) SyncSession(com.yahoo.documentapi.SyncSession) MessageBusSyncSession(com.yahoo.documentapi.messagebus.MessageBusSyncSession) DocumentAccessException(com.yahoo.documentapi.DocumentAccessException) DocumentAccessException(com.yahoo.documentapi.DocumentAccessException)

Example 3 with SyncSession

use of com.yahoo.documentapi.SyncSession in project vespa by vespa-engine.

the class OperationHandlerImpl method get.

@Override
public Optional<String> get(RestUri restUri, Optional<String> fieldSet) throws RestApiException {
    SyncSession syncSession = syncSessions.alloc();
    setRoute(syncSession, Optional.empty());
    try {
        DocumentId id = new DocumentId(restUri.generateFullId());
        final Document document = syncSession.get(id, fieldSet.orElse(restUri.getDocumentType() + ":[document]"), DocumentProtocol.Priority.NORMAL_1);
        if (document == null) {
            return Optional.empty();
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        JsonWriter jsonWriter = new JsonWriter(outputStream);
        jsonWriter.write(document);
        return Optional.of(outputStream.toString(StandardCharsets.UTF_8.name()));
    } catch (Exception e) {
        throw new RestApiException(Response.createErrorResponse(500, ExceptionUtils.getStackTrace(e), restUri, RestUri.apiErrorCodes.UNSPECIFIED));
    } finally {
        syncSessions.free(syncSession);
    }
}
Also used : DocumentId(com.yahoo.document.DocumentId) SyncSession(com.yahoo.documentapi.SyncSession) MessageBusSyncSession(com.yahoo.documentapi.messagebus.MessageBusSyncSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.yahoo.document.Document) JsonWriter(com.yahoo.document.json.JsonWriter) DocumentAccessException(com.yahoo.documentapi.DocumentAccessException)

Example 4 with SyncSession

use of com.yahoo.documentapi.SyncSession 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

DocumentAccessException (com.yahoo.documentapi.DocumentAccessException)4 SyncSession (com.yahoo.documentapi.SyncSession)4 MessageBusSyncSession (com.yahoo.documentapi.messagebus.MessageBusSyncSession)4 Instant (java.time.Instant)3 DocumentId (com.yahoo.document.DocumentId)2 Document (com.yahoo.document.Document)1 DocumentPut (com.yahoo.document.DocumentPut)1 DocumentRemove (com.yahoo.document.DocumentRemove)1 TestAndSetCondition (com.yahoo.document.TestAndSetCondition)1 JsonWriter (com.yahoo.document.json.JsonWriter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1