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);
}
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);
}
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);
}
}
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);
}
Aggregations