use of com.bluenimble.platform.db.DatabaseException in project serverless by bluenimble.
the class BulkRecordsSpi method execute.
@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String provider = (String) request.get(CommonSpec.Provider);
final JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
try {
return new JsonApiOutput(space.feature(Database.class, provider, request).bulk(payload));
} catch (DatabaseException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
}
use of com.bluenimble.platform.db.DatabaseException in project serverless by bluenimble.
the class DeleteRecordSpi method execute.
@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String provider = (String) request.get(CommonSpec.Provider);
String sEntity = (String) request.get(CommonSpec.Entity);
String record = (String) request.get(Spec.Record);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
try {
Database db = space.feature(Database.class, provider, request);
DatabaseObject oRecord = db.get(sEntity, record);
if (oRecord == null) {
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Deleted, false));
}
oRecord.delete();
} catch (DatabaseException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Deleted, true));
}
use of com.bluenimble.platform.db.DatabaseException in project serverless by bluenimble.
the class DropEntitySpi method execute.
@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String provider = (String) request.get(CommonSpec.Provider);
String sEntity = (String) request.get(CommonSpec.Entity);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
try {
Database db = space.feature(Database.class, provider, request);
db.drop(sEntity);
} catch (DatabaseException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
} catch (Exception e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
}
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Dropped, true));
}
use of com.bluenimble.platform.db.DatabaseException in project serverless by bluenimble.
the class DatabaseApiRequestTrack method finish.
@Override
public void finish(JsonObject feedback) {
final Date now = new Date();
Json.getObject(track, Fields.Time.class.getSimpleName().toLowerCase()).set(Fields.Time.Finished, now);
int code = Json.getInteger(feedback, ApiResponse.Error.Code, ApiResponse.OK.getCode());
boolean isError = code >= ApiResponse.BAD_REQUEST.getCode();
track.set(Fields.Status, isError ? 0 : 1);
track.set(Fields.Feedback, feedback);
JsonObject metrics = Json.getObject(track, Fields.Metrics.class.getSimpleName().toLowerCase());
if (MX.isCurrentThreadCpuTimeSupported()) {
metrics.set(Fields.Metrics.Cpu, MX.getCurrentThreadUserTime());
}
if (MX.isCurrentThreadCpuTimeSupported()) {
metrics.set(Fields.Metrics.Memory, MX.getThreadAllocatedBytes(Thread.currentThread().getId()));
}
try {
tracker.executor().execute(new Runnable() {
@Override
public void run() {
// shrink
track.shrink();
JsonObject oTracking = api.getTracking();
// put
Database db = null;
try {
db = api.space().feature(Database.class, Json.getString(oTracking, Spec.Feature, ApiSpace.Features.Default), tracker.context());
if (db == null) {
api.tracer().log(Tracer.Level.Error, "CantStore[{0}] due to database declared in the api is not defined at the space level", track.toString());
return;
}
// put
DatabaseObject entity = db.create(Json.getString(oTracking, Spec.Entity, DefaultEntity));
entity.load(track);
entity.save();
} catch (DatabaseException e) {
api.tracer().log(Tracer.Level.Error, "CantStore[{0}] due to {1}", track.toString(), e.getMessage());
api.tracer().log(Tracer.Level.Error, Lang.BLANK, e);
} finally {
if (db != null) {
db.recycle();
}
}
}
});
} catch (Exception ex) {
api.tracer().log(Tracer.Level.Error, Lang.BLANK, ex);
}
}
use of com.bluenimble.platform.db.DatabaseException in project serverless by bluenimble.
the class DatabaseObjectImpl method getSetRelationship.
private Object getSetRelationship(String key, Object v) {
if (!Document.class.isAssignableFrom(v.getClass())) {
return null;
}
Document ref = (Document) v;
Object one2ManyLink = ref.get(One2ManyLinkKey);
if (one2ManyLink != null) {
@SuppressWarnings({ "rawtypes", "unchecked" }) DatabaseObjectList list = new DatabaseObjectList(db, (List<Document>) one2ManyLink);
try {
set(key, list);
} catch (DatabaseException e) {
throw new RuntimeException(e.getMessage(), e);
}
return list;
} else {
String entity = ref.getString(ObjectEntityKey);
Object id = ref.get(ObjectIdKey);
if (entity == null || id == null) {
return null;
}
DatabaseObjectImpl refObject = new DatabaseObjectImpl(db, entity, id, true);
refObject.persistent = true;
try {
set(key, refObject);
} catch (DatabaseException e) {
throw new RuntimeException(e.getMessage(), e);
}
return refObject;
}
}
Aggregations